.getItemInHand()

Discussion in 'Plugin Development' started by WafflesYumyum, Jan 23, 2013.

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

    WafflesYumyum

    So, I'm having trouble with the getItemInHand and setItemInHand methods. First, I cannot pass through the if statement if I try to see if the player is holding paper. The problem with setItemInHand kind of depends on what the problem is with getItemInHand, so I'll stick with that for now:


    Code:
     @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent event){
            if(event.getEntity() instanceof Player && event.getDamager() instanceof Player){
                Player target = (Player) event.getEntity();
                Player attacker = (Player) event.getDamager();
                plugin.getServer().broadcastMessage("" + attacker.getDisplayName() + " attacked " + target.getDisplayName());
                if(event.getCause() == DamageCause.ENTITY_ATTACK  && attacker.getItemInHand() == new ItemStack(Material.PAPER)){
                    attacker.sendMessage("You hit " + target.getDisplayName());
                    plugin.getServer().broadcastMessage("sdf");
                    if(attacker.getItemInHand().getAmount() == 1){
                        attacker.setItemInHand(null);
                    } else {
                        attacker.setItemInHand(new ItemStack(339, attacker.getItemInHand().getAmount() - 1));
                    }
                    target.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 180, 1));
                    event.setCancelled(true);
                }
            }
        }
    (I'm trying to make it so that when a player hits another with a paper, or "bandage", it does not do damage and gives the target regeneration, and using up a paper)
     
  2. WafflesYumyum
    Code:java
    1.  
    2. attacker.getItemInHand() == new ItemStack(Material.PAPER)
    3.  


    You can't do this, if you compare two objects it will only return true if you are comparing the same instance of the variable. What you need to do is the following.

    Code:java
    1.  
    2. attacker.getItemInHand().getTypeId() == Material.PAPER.getTypeId()
    3.  
     
  3. Offline

    WafflesYumyum

    Thanks! It was actually Material.Paper.getId(), but you helped me out a ton :)
     
  4. WafflesYumyum
    Great :). I was just typing from memory so sorry if there were any minor errors.
     
  5. Offline

    gomeow

    You can just do if(attacker.getItemInHand().getType() == Material.PAPER) and not worry about the id
     
Thread Status:
Not open for further replies.

Share This Page