-4

I am trying to create a plugin that takes the essentials command /item and gives a lore to every one of the items.

The code I am putting does not have the lore part yet, right now I am just focusing on rewriting the /item command so I can edit it.

public class main extends JavaPlugin implements Listener {
    public static Bukkit plugin;

    @Override
    public void onEnable()
    {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);

        Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + "Enabled!");
    }

    @Override
    public void onDisable()
    {
        getLogger();
        Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + "Disabled!");
    }
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("item")) { 

            Player user = (Player)sender;
            if (args.length < 1)
            {
                user.sendMessage("Not enough arguments");
            }
            Essentials essentials = (Essentials)getServer().getPluginManager().getPlugin("Essentials");

              IItemDb itemDB = essentials.getItemDb();
              ItemStack stack = essentials.getItemDb().get(args[0]);


            final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
            try
            {
                if (args.length > 1 && Integer.parseInt(args[1]) > 0)
                {
                    stack.setAmount(Integer.parseInt(args[1]));
                }
                }
            catch (NumberFormatException e)
            {
                user.sendMessage("Not enough arguements");
            }
            if (args.length > 2)
            {
                MetaItemStack metaStack = new MetaItemStack(stack);
                final boolean allowUnsafe = user.hasPermission("essentials.enchantments.allowunsafe");

                stack = metaStack.getItemStack();
            }


            if (stack.getType() == Material.AIR)
            {
                user.sendMessage("Can not spawn air");
            }

            final String displayName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ');
            user.sendMessage("Given " + stack.getAmount() + " " + displayName);
            if (user.hasPermission("essentials.oversizedstacks"))
            {
                InventoryWorkaround.addOversizedItems(user.getInventory(), 111, stack);
            }
            else
            { 
                InventoryWorkaround.addItems(user.getInventory(), stack);
            }
            user.updateInventory();
            return true;
        }
            return false; 
    }
}

On this line:

ItemStack stack = essentials.getItemDb().get(args[0]);

I get the error:

Unhandled exception type Exception.

I can fix this by rearranging the bottom closing braces, but then I get errors in the bottom closing braces. Does anyone know what is going on?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • 4
    Yes, anybody who has the slightest knowledge of Java knows what's going on. Please read through [the official Java tutorial on Exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/). Please also note that SO is not a site for teaching beginners how to code, but a site for professional and enthusiast programmers - we expect you to know the basics, and there are plenty of resources to learn them. Do your own research before asking further questions, and only ask on SO as a last resort once you're certain you've stumbled on a problem that's not heavily documented already. – l4mpi Feb 16 '15 at 14:54

2 Answers2

0

This is because IItemDb.get(String) throws a checked exception, which you did not handle. Checked exceptions must be handled, or a compile-time error will be thrown. From §11.2:

The Java programming language requires that a program contains handlers for checked exceptions which can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception.

(emphasis mine)

Therefore, you must extend your method signature:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) throws Exception
Unihedron
  • 10,902
  • 13
  • 62
  • 72
0

The line ItemStack stack = essentials.getItemDb().get(args[0]); can throw an Exception.

To handle it (and it's mandatory), you have to surround this line with a try/catch block.

try {
    ItemStack stack = essentials.getItemDb().get(args[0]); 
} catch(Exception e) {
    e.printStackTrace();
}

Also, you may want to read this: Why not catch general Exceptions

Community
  • 1
  • 1
lmo
  • 497
  • 5
  • 23