Won't Detect Holding Item

Discussion in 'Plugin Development' started by Drewski2222, Jun 18, 2015.

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

    Drewski2222

    I'm making a plugin whenever you are holding a cake, you get 100 XP levels. (Yes, I know it's dumb) My problem is it does not detect when you are holding a cake, but detects when you are not holding a cake.
    Here is my code:
    Code:
    public class Main extends JavaPlugin {
       
        public void onEnable() {
            getLogger().info("Cool Is Starting Up!");
        }
       
        public void onDisable() {
            getLogger().info("Cool Is Shutting Down!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String lablel, String args[]) {
            if(cmd.getName().equalsIgnoreCase("cool"));
            Player player = (Player) sender;
            if(player.getItemInHand().equals(Material.CAKE)) {
                player.sendMessage("Congrats! Here is your 100 XP!");
                player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount()-1);   
                }
            if(!player.getItemInHand().equals(Material.CAKE)) {
                player.sendMessage(ChatColor.DARK_RED + "Error: Please Hold A Cake In Your Hand.");
            }
                 
                 return false;
            }
        }
     
  2. Offline

    SuperOriginal

    an ItemStack is not a Material so it will always be false. Use getType() to get the Material.
     
  3. Offline

    Drewski2222

    Where would I put that?
     
  4. Offline

    tomudding

    Code:
    player.getItemInHand().getType().equals(Material.CAKE)
    should work
     
  5. Offline

    Drewski2222

  6. Offline

    CyranoTrinity

    @Drewski2222
    Instead of doing .equals(""), I think == works.
    So change this:
    Code:
    player.getItemInHand().getType().equals(Material.CAKE)
    to:
    Code:
    player.getItemInHand().getType() == Material.CAKE
     
  7. Offline

    Boomer

    first, rather than dereference the object twice for a IF thing is cake / If thing is not cake, recognize that that is an IF/ELSE structure..
    Second, try a debug statement , send the player the message player.getItemInHand().getType().toString() before doing the checks, to see what it does report, in order to figure out if you're picking up the wrong information or not.

    Actually, what Cyrano just wrote :) You need to compare equivalent values, not equal objects
     
Thread Status:
Not open for further replies.

Share This Page