Unhandled exception executing command

Discussion in 'Plugin Development' started by HearIt_, Oct 21, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    HearIt_

    I want to make a /give command that gives any item to anyone, like essentials. But, I can't seem to fix the error "Unhandled exception executing command 'give' in plugin ItemGiver"

    Here is the code :


    Code:
    package me.HearIt_;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ItemGiver extends JavaPlugin {
       
        public Permission playerPermission = new Permission("ItemGiver.give");
       
        @Override
        public void onEnable() {
           
            new ItemListener(this);
           
            PluginManager pm = getServer().getPluginManager();
           
            pm.addPermission(playerPermission);
        }
           
       
        @Override
        public void onDisable() {
           
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (cmd.getName().equalsIgnoreCase("give") && sender instanceof Player) {
               
                Player player = (Player) sender;
               
                if(player.hasPermission("ItemGiver.give")) {
               
                if (args.length == 0) {
                   
                    if (player.getName().equalsIgnoreCase(args[0])) {
                       
                        try {
                           
                            ItemStack item = new ItemStack(Material.matchMaterial(args[1]));
                           
                            player.getInventory().addItem(item);
                           
                            player.sendMessage(ChatColor.GREEN + "You have been given " + CraftItemStack.asNMSCopy(item).getName() + ".");
                }
                   
                    catch(Exception event) {
                       
                        player.sendMessage(ChatColor.RED + "Item not found");
               
                return true;
               
                }}}}}
               
        return false;       
    
        }
    }
    I have another code that works but it only gives 1 item..

    Here's the another code :


    Code:
    package me.HearIt_;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ItemGiver extends JavaPlugin {
       
        public Permission playerPermission = new Permission("ItemGiver.give");
       
        @Override
        public void onEnable() {
            new ItemListener(this);
            PluginManager pm = getServer().getPluginManager();
            pm.addPermission(playerPermission);
        }
           
       
        @Override
        public void onDisable() {
           
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (cmd.getName().equalsIgnoreCase("give") && sender instanceof Player) {
               
                Player player = (Player) sender;
               
                if(player.hasPermission("ItemGiver.give")) {
                   
                    if (args.length == 0) {
                       
                        player.sendMessage(ChatColor.RED + "Incorrect item.");
                       
                        return true;
                    }
                   
                    try {
                       
                        ItemStack item = new ItemStack(Material.matchMaterial(args[0]));
                       
                        player.setItemInHand(item);
                       
                        player.sendMessage(ChatColor.GREEN + "You have been given " + CraftItemStack.asNMSCopy(item).getName() + ".");
                    }
                   
                    catch(Exception e) {
                       
                        player.sendMessage(ChatColor.RED + "Item not found.");
                    }
                }
               
                return true;
            }
       
        return false;
       
        }
    
    }
     
  2. @HearIt_ Why do you have CraftItemStack imported and why are you doing a try catch just to add to an inventory? That's not a very good way to do it. Just make a check to see if Material is null.
     
    mine-care likes this.
  3. Offline

    mine-care

    What is the purpose of this? Why catching ALL exceptions?

    Also follow naming conventions:
     
  4. Offline

    HearIt_

    I don't even know .-. , I followed someone's code. I think it is for telling the player that the arguments are incorrect ?
     
  5. Offline

    mcdorli

    Learn java, and don't copy paste code.
     
    Mrs. bwfctower and mine-care like this.
  6. Offline

    FabeGabeMC

    @HearIt_
    If it is who I think it is....
    Who I think it is (open)
    TheBCBroz

    God help us all...
    You should read this if you are starting Bukkit.
     
    WHQ likes this.
  7. Offline

    Jayyy

    Both classes extend JavaPlugin, unless they are two completely projects?
     
  8. Offline

    Scimiguy

    @Jayyy
    They're both the same class, in the same package
     
  9. Offline

    Jayyy

    Oh lol I looked at it wrong... my bad.
     
Thread Status:
Not open for further replies.

Share This Page