Advertisement
Guest User

SimpleGive TryCatch Fix

a guest
Jun 19th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.77 KB | None | 0 0
  1.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
  2.         if(commandLabel.equalsIgnoreCase("give")){ // If the command is /give, it will do the following:
  3.            
  4.             // Lets catch those stupid terminal errors
  5.             try {
  6.                 if(!(sender instanceof Player)) {
  7.                     String gname = args[0];//Make the first arguments (/give firstargs) a string
  8.                    
  9.                     Player givee = this.getServer().getPlayer(gname);//Get the player from the string, used to specify a player in the command
  10.                    
  11.                     String ggname = givee.getDisplayName();//Gets the player's display name, only because 2 of my plugins have setDisplayName() stuff, so this is just an added bonus
  12.                    
  13.                     PlayerInventory pinv = givee.getInventory();//Gets the player's inventory, necessary if we want to edit it
  14.                    
  15.                     String itemid = args[1];//Sets the second arguments to a string
  16.    
  17.                                    
  18.                     Integer id = Integer.parseInt(Items.getItem(itemid));//This is a little complicated, refer to the below:
  19.                     //This basically uses the second arguments and runs it through getItem from Items.java, and returns an ID, which it then parses into an Integer
  20.    
  21.                    
  22.                     ItemStack is = new ItemStack(0, 0);//Makes a new itemstack, this is how we add things to the inventory
  23.                    
  24.                     is.setTypeId(id);//Sets the ID of the itemstack to whatever was specified earlier
  25.                    
  26.                     String itemName = is.getType().name().toLowerCase().replace('_', ' ');//This is used to send the player a message saying "Enjoy the new log" or whatever
  27.                    
  28.                     //The following is run if there are more than 2 arguments:
  29.                     if(args.length > 2 ) {
  30.                    
  31.                         Integer am = Integer.parseInt(args[2]);//Parses the third arg (string) into an integer
  32.                        
  33.                         is.setAmount(am);//Sets the amount of the item in the itemstack to the third argument
  34.                        
  35.                     } else {
  36.                        
  37.                         is.setAmount(64);//If no Item amount was specified, sets it to 64
  38.                     }
  39.                    
  40.                     //The following is run if there are more than 3 arguments:
  41.                     if(args.length > 3 ) {
  42.                        
  43.                         String dmg = args[3];//Sets the 4 arguments to a string
  44.                        
  45.                         is.setDurability(Short.parseShort(dmg));//Parses the string into a short (the format durability uses), and sets the durability of the itemstack to the 4 arguments
  46.                        
  47.                     }
  48.                    
  49.                     pinv.addItem(is);//Finally adds the itemstack to the player's inventory
  50.                    
  51.                    
  52.                     log.info("[SimpleGive] " + ggname + " has received the " + itemName + "(s)");
  53.                         givee.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName + "(s)");//Sends the player that is receiving items a message, uses the name from earlier
  54.                     return true;
  55.                 } else {
  56.                 if (canUseGive((Player) sender)) {
  57.                     Player player = (Player) sender;
  58.                     try {
  59.                    
  60.                     String gname = args[0];//Make the first arguments (/give firstargs) a string
  61.                    
  62.                     Player givee = this.getServer().getPlayer(gname);//Get the player from the string, used to specify a player in the command
  63.                    
  64.                     String ggname = givee.getDisplayName();//Gets the player's display name, only because 2 of my plugins have setDisplayName() stuff, so this is just an added bonus
  65.                    
  66.                     PlayerInventory pinv = givee.getInventory();//Gets the player's inventory, necessary if we want to edit it
  67.                    
  68.                     String itemid = args[1];//Sets the second arguments to a string
  69.    
  70.                                    
  71.                     Integer id = Integer.parseInt(Items.getItem(itemid));//This is a little complicated, refer to the below:
  72.                     //This basically uses the second arguments and runs it through getItem from Items.java, and returns an ID, which it then parses into an Integer
  73.                    
  74.                     if(!canUseAGive(player, id)) {
  75.                         player.sendMessage(ChatColor.RED + "You aren't allowed to give " + id.toString());
  76.                         return true;
  77.                     }
  78.                    
  79.                     ItemStack is = new ItemStack(0, 0);//Makes a new itemstack, this is how we add things to the inventory
  80.                    
  81.                     is.setTypeId(id);//Sets the ID of the itemstack to whatever was specified earlier
  82.                    
  83.                     String itemName = is.getType().name().toLowerCase().replace('_', ' ');//This is used to send the player a message saying "Enjoy the new log" or whatever
  84.                    
  85.                     //The following is run if there are more than 2 arguments:
  86.                     if(args.length > 2 ) {
  87.                    
  88.                         Integer am = Integer.parseInt(args[2]);//Parses the third arg (string) into an integer
  89.                        
  90.                         is.setAmount(am);//Sets the amount of the item in the itemstack to the third argument
  91.                        
  92.                     } else {
  93.                        
  94.                         is.setAmount(64);//If no Item amount was specified, sets it to 64
  95.                     }
  96.                    
  97.                     //The following is run if there are more than 3 arguments:
  98.                     if(args.length > 3 ) {
  99.                        
  100.                         String dmg = args[3];//Sets the 4 arguments to a string
  101.                        
  102.                         is.setDurability(Short.parseShort(dmg));//Parses the string into a short (the format durability uses), and sets the durability of the itemstack to the 4 arguments
  103.                        
  104.                     }
  105.                    
  106.                     pinv.addItem(is);//Finally adds the itemstack to the player's inventory
  107.                    
  108.    
  109.                         givee.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName);//Sends the player that is receiving items a message, uses the name from earlier
  110.    
  111.                     player.sendMessage(ChatColor.RED + ggname + ChatColor.BLUE + " Has received the " + itemName);//Sends the command sender a message, uses the name from earlier
  112.                     } catch (NullPointerException e) {
  113.                         player.sendMessage(ChatColor.RED + "That Item Doesn't Exist!");
  114.                     }
  115.                     } else {
  116.                    
  117.                     Player s = (Player) sender;//sets the sender to s
  118.                    
  119.                     s.sendMessage(ChatColor.RED + "You don't have permisson to: " + ChatColor.AQUA + "Give Items!");//Shows this if the player doesn't have simpleadmin.give node
  120.                 }
  121.                     return true;//onCommand must return true or false
  122.                 }
  123.             } catch(ArrayIndexOutOfBoundsException e) {
  124.                
  125.                 sender.sendMessage(ChatColor.RED + "[SimpleGive]" + ChatColor.AQUA + " Incorrect Command Useage.");
  126.                 sender.sendMessage(ChatColor.RED + "[SimpleGive]" + ChatColor.AQUA + " /give <playername> <itemid/name> <amount> <durability>");
  127.             }
  128.         }
  129.        
  130.         // All of the below is the same as above but without the player argument. Basically exactly the same :)
  131.         if(commandLabel.equalsIgnoreCase("i")){
  132.             try {
  133.                 if (canUseI((Player) sender)) {
  134.                     Player player = (Player) sender;
  135.                     try {
  136.                         PlayerInventory pinv = player.getInventory();
  137.                        
  138.                         String itemid = args[0].replace("_", "");
  139.                        
  140.                         Integer id = Integer.parseInt(Items.getItem(itemid));
  141.                        
  142.                         if(!canUseAnI(player, id)) {
  143.                             player.sendMessage(ChatColor.RED+ "You aren't allowed to give yourself a " + id.toString());
  144.                             return true;
  145.                         }
  146.                        
  147.                         ItemStack is = new ItemStack(0, 0);
  148.                        
  149.                         is.setTypeId(id);
  150.                        
  151.                         String itemName = is.getType().name().toLowerCase().replace('_', ' ');
  152.                        
  153.                         if(args.length > 1 ) {
  154.                             Integer am = Integer.parseInt(args[1]);
  155.                             is.setAmount(am);
  156.                         } else {
  157.                             is.setAmount(64);
  158.                         }
  159.                         if(args.length > 2 ) {
  160.                             String dmg = args[2];
  161.                            
  162.                             is.setDurability(Short.parseShort(dmg));
  163.                            
  164.                         }
  165.                         pinv.addItem(is);
  166.                        
  167.                         player.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName);
  168.                     } catch(NullPointerException e) {
  169.                         player.sendMessage(ChatColor.RED + "That Item Doesn't Exist!");
  170.                     }
  171.                 } else {
  172.                     Player s = (Player) sender;
  173.                     s.sendMessage(ChatColor.RED + "You don't have permisson to: " + ChatColor.AQUA + "Itemize!");
  174.                 }
  175.             } catch(ArrayIndexOutOfBoundsException e) {
  176.                
  177.                 sender.sendMessage(ChatColor.RED + "[SimpleGive]" + ChatColor.AQUA + " Incorrect Command Useage.");
  178.                 sender.sendMessage(ChatColor.RED + "[SimpleGive]" + ChatColor.AQUA + " /i <itemid/name> <amount> <durability>");
  179.             }
  180.             return true;
  181.         }
  182.         return false;
  183.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement