Plugin Error Help- Spawning Items

Discussion in 'Plugin Development' started by MineCoder101, Jan 7, 2015.

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

    MineCoder101

    Hello,
    First time needing help with something so hear it goes,
    This should be spawning the item defined using /item but for some reason it is not generating any error's in console, any error messages in game, or giving the items, How ever, it is registering it as a command. When I type /item it does not send a message saying unknown command instead it replies with nothing.
    Any help I would love and here is the code that is giving me a head ach tonight:



    Code:
                            if(commandLabel.equalsIgnoreCase("item"));
                            if (sender.hasPermission("atomicitems.item")) {
                                Player player = (Player) sender;
                                if (args.length == 0) {
                                    player.sendMessage(ChatColor.DARK_RED + "Not Enough Arguments!" + ChatColor.GOLD + "Correct Usage:" + ChatColor.YELLOW + "/item <itemid> [amount]");
                                } else if (args.length == 1) {
                                    ItemStack itemStack = new ItemStack(Material.getMaterial(Integer.parseInt(args[0])), 64);
                                    player.sendMessage(ChatColor.GRAY + "Giving" + ChatColor.GOLD + "64" + ChatColor.RED + args[0] + ChatColor.GRAY + "to" + ChatColor.GOLD  + player.getName());
                                    player.getInventory().addItem(itemStack);
                                } else if (args.length == 2) {
                                    ItemStack itemStack = new ItemStack(Material.getMaterial(Integer.parseInt(args[0])), Integer.parseInt(args[1]));
                                    player.sendMessage(ChatColor.GRAY + "Giving" + ChatColor.GOLD + args[1] + ChatColor.RED + args[0] + ChatColor.GRAY + "to" + ChatColor.GOLD  + player.getName());
                                    player.getInventory().addItem(itemStack);
                                }
                                else {
                                    player.sendMessage(ChatColor.DARK_RED + "To Many Arguments!" + ChatColor.GOLD + "Correct Usage:" + ChatColor.YELLOW + "/item <itemid> [amount]");
                                }
                            }
                            else {
                                sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "AtomicItems" + ChatColor.GOLD + "]" + ChatColor.DARK_RED + ChatColor.BOLD + " You Do Not Have Permission!");
                                }
     
  2. Offline

    es359

  3. cause you made a ";" semicolon after the if statement i think
    the semicolon stands for a empty statement and that's why it doesn't return anything

    so its like if you do:
    Code:
    if(commandLabel.equalsIgnoreCase("test")){  }
    
    it will return nothing

    if not i am sry i am also quite new here
     
    Last edited: Jan 7, 2015
  4. Offline

    MineCoder101

    Yeah, I noticed that, I just removed it and still nothing :\ Thanks for trying.
     
  5. it should send an error message...
    could you copy it and paste it here ?
     
  6. Offline

    MineCoder101

    I would love to.... I just have one small problem, their is no errors to paste in here, I have checked the console, and this is all I see...
    [​IMG]
     
  7. Offline

    TheOatBaron

    1. Make sure the sender is a player before casting.
    2. Fix the ; on the if statement
    3. Make sure the label is defined in your plugin.yml
    4. Make sure it's not conflicting with other plugins.
    5. Surround parseInts with a try/catch whenever a player gives it.

    This code should work; if not it's either you exported the plugin under two different file names accidentally, improper plugin.yml, or a infliction with another plugin.

    Code:
    if(commandLabel.equalsIgnoreCase("item")){
                if (sender instanceof Player && sender.hasPermission("atomicitems.item")) {
                    Player player = (Player) sender;
                    if (args.length == 0) {
                        player.sendMessage(ChatColor.DARK_RED + "Not Enough Arguments!" + ChatColor.GOLD + "Correct Usage:" + ChatColor.YELLOW + "/item <itemid> [amount]");
                    } else if (args.length == 1) {
                        int id = 0;
                        try{
                            id = Integer.parseInt(args[0]);
                        }catch(NumberFormatException e){
                            player.sendMessage("That has to be an integer!");
                            return false;
                        }
                        ItemStack itemStack = new ItemStack(Material.getMaterial(id), 64);
                        player.sendMessage(ChatColor.GRAY + "Giving" + ChatColor.GOLD + "64" + ChatColor.RED + args[0] + ChatColor.GRAY + "to" + ChatColor.GOLD  + player.getName());
                        player.getInventory().addItem(itemStack);
                    }else if(args.length == 2){
                        int id = 0;
                        int amn = 0;
                        try{
                            id = Integer.parseInt(args[0]);
                        }catch(NumberFormatException e){
                            player.sendMessage("That has to be an integer!");
                            return false;
                        }
                        try{
                            amn = Integer.parseInt(args[1]);
                        }catch(NumberFormatException e){
                            player.sendMessage("That has to be an integer!");
                            return false;
                        }
                        ItemStack itemStack = new ItemStack(Material.getMaterial(id), amn);
                        player.sendMessage(ChatColor.GRAY + "Giving" + ChatColor.GOLD + args[1] + ChatColor.RED + args[0] + ChatColor.GRAY + "to" + ChatColor.GOLD  + player.getName());
                        player.getInventory().addItem(itemStack);
                    }else{
                        player.sendMessage(ChatColor.DARK_RED + "To Many Arguments!" + ChatColor.GOLD + "Correct Usage:" + ChatColor.YELLOW + "/item <itemid> [amount]");
                    }
                }else{
                    sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "AtomicItems" + ChatColor.GOLD + "]" + ChatColor.DARK_RED + ChatColor.BOLD + " You Do Not Have Permission!");
                }
            }
     
  8. Offline

    es359

    Lol the problem is way simpler than that.
    @sn1cko was correct.
     
  9. Offline

    TheOatBaron

    Just because that one error fix will fix most of the problems, it won't fix all. Because it works now, doesn't mean it will always work.

    If the console tried to send this command it would throw a stack trace because they didn't verify the sender was a player.

    If a player tried to execute the command with non-integers it would throw a stack trace and also not notify the player of what they did wrong.
     
    sn1cko and es359 like this.
  10. @es359 he is right
    i just got the command to run
    but you should also define the sender as an player like he said "if(sender instancof Player)"
    and also check if the argument is an integer with the try/catch statement
    cause thats where most of the errors will come i think
     
  11. @sn1cko No reason to add a try catch for arguments.
     
  12. ouh ok i see but he should check if the argument is an Integer cause if not it will throw an Error (internal error)
    @bwfcwalshy
     
  13. @sn1cko Still no need for a try catch, just check if it is an integer.
     
  14. @bwfcwalshy haha yes thats what i just said haha ^^
     
  15. Offline

    MineCoder101


    Ok, I tried your code and it does the exact same as my code, it recignise's the command but again no error.
     
  16. his code works fine
    but the messages are missing the spaces ;) @TheOatBaron

    so either you haven't registered the command in your plugin.yml
    or if it's in a command executor you haven't registered the executor or the command in the onEnable()

    or you have set some wrong {} that will make the command do nothing

    if you have resistered the command/commandexecutor and it still doesn't work
    please share the plugin.yml, the main class and if there is - the command executor class so that we can look for other problems because the code from @TheOatBaron is correct :)

    cheers
    sn1cko ;)
     
  17. Offline

    MineCoder101


    Thanks,
    Here is the link to my GitHub page.

    https://github.com/atomichive/atomicitems
     
Thread Status:
Not open for further replies.

Share This Page