Command isn't working

Discussion in 'Plugin Development' started by Ragnarok_, Jan 19, 2017.

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

    Ragnarok_

    Hello, I am making a plugin for my friend's server, and there are gems you get from killing players and such. I have that taken care of, but there's this one part. It just won't register, and I hope you can maybe find my mistake. Here's my code, and before you ask, my other cmds work, and the plugin.yml is correct.

    Code:
     }
                                if(cmd.getName().equalsIgnoreCase("redeem")) {
                                    Material material = Material.GOLD_INGOT;
                                    int ammount = 1;
                                    String name = ChatColor.RED + "Gem";
                           
                                    ItemStack item = new ItemStack(material, ammount);
                                    ItemMeta itemMeta = item.getItemMeta();
                                    itemMeta.setDisplayName(name);
                           
                                    itemMeta.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Take this to the gem master,", ChatColor.DARK_GREEN + "to add them to your account"));
                           
                                    item.setItemMeta(itemMeta);
                                    if(player.getInventory().contains(item)) {
                                        player.getInventory().remove(item);
                                        player.sendMessage(Prefix + ChatColor.WHITE + "+" + ChatColor.GOLD + amount + ChatColor.RED + " Gems");
                                        amount=0;
                                        return true;
                                    }
                                      
                                        else if(!player.getInventory().contains(item)) {
                                            player.sendMessage(Prefix + ChatColor.RED + "Ops! You currently don't have any gems in your inventory!");
                                            return true;
                                        }
                                    }
    I'm using the ItemMeta, but that's not what's wrong. It just repeats the command in chat, and I have return true;. It worked once when I tested it, then it just stopped, don't know where to go from now. Help a guy out. Thanks regardless!
     
  2. Offline

    kameronn

    Code:
    if(player.getInventory().contains(item)) {
    It is most likely not passing this check. Try doing System.out.println after this and see if it prints in console. I would recommend checking the name instead of the itemstack.
     
  3. Offline

    Ragnarok_

    Ya, dude it just repeats it in chat, I tested for the name in general. I type the command, and it just says, "/redeem"(What I typed)

    Also, I did the check thingy:

    Code:
                                }
                                int amount=0;
                                for (ItemStack is : player.getInventory().all(Material.GOLD_INGOT).values()) {
                                amount = amount + is.getAmount();
                                Material material = Material.GOLD_INGOT;
                                int ammount = 1;
                                String name = ChatColor.RED + "Gem";
                                ItemStack item = new ItemStack(material, ammount);
                                ItemMeta itemMeta = item.getItemMeta();
                                itemMeta.setDisplayName(name);
                                itemMeta.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Take this to the gem master,", ChatColor.DARK_GREEN + "to add them to your account"));
                                item.setItemMeta(itemMeta);
                                if(cmd.getName().equalsIgnoreCase("redeem")) {
                                    System.out.println("Done");
                                  if(player.getInventory().contains(Material.GOLD_INGOT)) {
                                    player.getInventory().remove(Material.GOLD_INGOT);
                                    player.sendMessage(Prefix + ChatColor.WHITE + "+" + ChatColor.GOLD + amount + ChatColor.RED + " Gems");
                                    amount=0;
                                    return true;
                                        }
    No luck :(
     
  4. Offline

    kameronn

    Why is the rredeem command in the for loop? Can I see the whole onCommand boolean? Remove return true or set return true to return false
     
  5. Offline

    Ragnarok_

    Oh, It wasn't in the loop before, but I put it there for testing purposes. Also, it's a gigantic line of code, there's 2 commands at the top, like 18 if[args], then there's this.

    Okay so I got it to work, I put it next to the other command at the top, but now they aren't working!!
    Code:
    if(cmd.getName().equalsIgnoreCase("kit-list")) {
                player.sendMessage(ChatColor.GREEN + "    McArtOnline Kit Information");
                player.sendMessage(ChatColor.AQUA + " To select a kit, type /kit <kit name>");
                player.sendMessage(ChatColor.LIGHT_PURPLE + "              Kit Names");
                player.sendMessage(ChatColor.GOLD + " - Warrior");
                player.sendMessage(ChatColor.GOLD + " - Archer");
                player.sendMessage(ChatColor.GOLD + " - Rogue");
                player.sendMessage(ChatColor.GOLD + " - Ranger");
                player.sendMessage(ChatColor.GOLD + " - Tank");
                player.sendMessage(ChatColor.GOLD + " - Cleric");
                player.sendMessage(ChatColor.GOLD + " - Bard");
                return true;
            }
            if(cmd.getName().equalsIgnoreCase("redeem")) {
                int amount=0;
                for (ItemStack is : player.getInventory().all(Material.GOLD_INGOT).values()) {
                amount = amount + is.getAmount();
                Material material = Material.GOLD_INGOT;
                int ammount = 1;
                String name = ChatColor.RED + "Gem";
                ItemStack item = new ItemStack(material, ammount);
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.setDisplayName(name);
                itemMeta.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Take this to the gem master,", ChatColor.DARK_GREEN + "to add them to your account"));
                item.setItemMeta(itemMeta);
                  if(player.getInventory().contains(item)) {
                    player.getInventory().remove(item);
                    player.sendMessage(Prefix + ChatColor.WHITE + "+" + ChatColor.GOLD + amount + ChatColor.RED + " Gems");
                    amount=0;
            }
           
            if(cmd.getName().equalsIgnoreCase("kit")) {
                if(cooldown.contains(player)) {
                    player.sendMessage(Prefix + ChatColor.RED + "Please wait for the cooldown to expire");
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                            cooldown.remove(player);
                           
                        }
                    }, 200);
                    return true;
                }
                if(args.length == 0) {
                player.sendMessage(Prefix + ChatColor.RED + "Ops! Please specify an argument or type /kit help for more info");
                return true;
                }
     
  6. Offline

    Zombie_Striker

    @Ragnarok_
    Are you sure the player will have a slot with only one gold ingot? If you're looking for gold ingots regardless of amount, you will have to do separate checks. If this is the case, check the lore or displayname is correct, and if so, remove -1 from the amount amount and reset the item.
     
  7. Offline

    Ragnarok_

    Even If I change the code to the command, it just keeps repeating what I say in chat. It doesn't even work -its in plugin.yml correctly. Please help
     
  8. Offline

    mehboss

    Make sure all of the cmd.getName brackets are closed at the ends. I can see multiple cmd.getNames that aren't closed. ;)
     
    Last edited: Jan 19, 2017
  9. Offline

    Ragnarok_

    What do you mean closed? If your talking about the "( )" at the end of cmd.getName, they are all closed... If not, can someone help me out with what he's saying?
     
  10. Offline

    Zombie_Striker

    @Ragnarok_
    He's talking about Encapsulation. The brackets { and }
     
  11. Offline

    mehboss

    @Ragnarok_
    Yeah, you have to close the command labels with brackets, make sure you enclose them in order for the command to work. {}

    Here I have fixed the brackets ( {} )... Make sure you close the rest of the code that you didn't show us. ;P..
    Code:
    if(cmd.getName().equalsIgnoreCase("kit-list")) {
                player.sendMessage(ChatColor.GREEN + "    McArtOnline Kit Information");
                player.sendMessage(ChatColor.AQUA + " To select a kit, type /kit <kit name>");
                player.sendMessage(ChatColor.LIGHT_PURPLE + "              Kit Names");
                player.sendMessage(ChatColor.GOLD + " - Warrior");
                player.sendMessage(ChatColor.GOLD + " - Archer");
                player.sendMessage(ChatColor.GOLD + " - Rogue");
                player.sendMessage(ChatColor.GOLD + " - Ranger");
                player.sendMessage(ChatColor.GOLD + " - Tank");
                player.sendMessage(ChatColor.GOLD + " - Cleric");
                player.sendMessage(ChatColor.GOLD + " - Bard");
                return true;
            }
            if(cmd.getName().equalsIgnoreCase("redeem")) {
                int amount=0;
                for (ItemStack is : player.getInventory().all(Material.GOLD_INGOT).values()) {
                amount = amount + is.getAmount();
                Material material = Material.GOLD_INGOT;
                int ammount = 1;
                String name = ChatColor.RED + "Gem";
                ItemStack item = new ItemStack(material, ammount);
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.setDisplayName(name);
                itemMeta.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Take this to the gem master,", ChatColor.DARK_GREEN + "to add them to your account"));
                item.setItemMeta(itemMeta);
                  if(player.getInventory().contains(item)) {
                    player.getInventory().remove(item);
                    player.sendMessage(Prefix + ChatColor.WHITE + "+" + ChatColor.GOLD + amount + ChatColor.RED + " Gems");
                    amount=0;
            }
        }
    }
         
            if(cmd.getName().equalsIgnoreCase("kit")) {
                if(cooldown.contains(player)) {
                    player.sendMessage(Prefix + ChatColor.RED + "Please wait for the cooldown to expire");
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                            cooldown.remove(player);
                         
                        }
                    }, 200);
                    return true;
                }
                if(args.length == 0) {
                player.sendMessage(Prefix + ChatColor.RED + "Ops! Please specify an argument or type /kit help for more info");
                return true;
                }
    }
    
    Like that ^^^
     
  12. Offline

    Ragnarok_

    Thanks for fixing them! But that didn't fix my problem sadly. I'm just really confused on why it just repeats.. Even if I set it to just send a message. It repeats. It's weird because all my other commands work, but this one.
     
  13. Offline

    mehboss

    @Ragnarok_
    So your commands work now, it just repeats? Which commands are repeating, all of them?
    I'll test it out when I get home and hopefully fix the problem. :)
     
    Last edited: Jan 21, 2017
  14. Offline

    Ragnarok_

    No, all my other commands work, its just, whenever I do /redeem, it repeats it in chat, I even added a temporary "player.sendMessage("Test") when the command ran, it just repeated it in chat... I did a arg for it and that worked, but then the counter for the gems didn't work.
     
  15. Offline

    Zombie_Striker

    @Ragnarok_
    Where are you putting that message? Post the updated code.
     
  16. Offline

    mehboss

    @Ragnarok_
    The only thing I think could be doing this is returns.. try this:
    Code:
            if(cmd.getName().equalsIgnoreCase("redeem")) {
                int amount=0;
                for (ItemStack is : player.getInventory().all(Material.GOLD_INGOT).values()) {
                amount = amount + is.getAmount();
                Material material = Material.GOLD_INGOT;
                int ammount = 1;
                String name = ChatColor.RED + "Gem";
                ItemStack item = new ItemStack(material, ammount);
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.setDisplayName(name);
                itemMeta.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Take this to the gem master,", ChatColor.DARK_GREEN + "to add them to your account"));
                item.setItemMeta(itemMeta);
    
                  if(player.getInventory().contains(item)) {
                    player.getInventory().remove(item);
                    player.sendMessage(Prefix + ChatColor.WHITE + "+" + ChatColor.GOLD + amount + ChatColor.RED + " Gems");
                    amount=0;
                    return true;
            }
        }
    }
    

    Also are there any errors in console?
     
Thread Status:
Not open for further replies.

Share This Page