Check if item is in hand?? Need help O_o

Discussion in 'Plugin Development' started by Bust_Open, Sep 1, 2012.

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

    Bust_Open

    Current Code:

    Code:
    package info.epscraft;
     
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class PlayerInteract implements Listener{
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
    Player p = event.getPlayer();
            ItemStack item = event.getItem();
            if (Material.BLAZE_ROD.equals(item)) {
           event.getAction().equals(Action.RIGHT_CLICK_AIR);
                item.getType().equals(Material.BLAZE_ROD);
               p.sendMessage(ChatColor.YELLOW+"You fired a fireball, need help? type: /bhelp for help!");
               p.launchProjectile(Fireball.class);
               if (p.getGameMode().equals(GameMode.SURVIVAL)) {
               if (item.getAmount() == 1) {
                            event.getPlayer().getInventory().remove(item);
               } else
                             item.setAmount(item.getAmount() - 1);
                     }
            } else if (Material.PAPER.equals(item)) {
           event.getAction().equals(Action.RIGHT_CLICK_AIR);
    item.getType().equals(Material.PAPER);
    if(p.getHealth()>=19)
    {
    p.sendMessage(ChatColor.AQUA+"I feel much better...");
    p.setHealth(20);
    if (p.getGameMode().equals(GameMode.SURVIVAL)) {
           if (item.getAmount() == 1) {
                       event.getPlayer().getInventory().remove(item);
           } else
                        item.setAmount(item.getAmount() - 1);
                }
    }
    else
    {
    p.setHealth(event.getPlayer().getHealth() + 1);
    p.sendMessage(ChatColor.AQUA+"I feel much better...");
    if (p.getGameMode().equals(GameMode.SURVIVAL)) {
           if (item.getAmount() == 1) {
                       event.getPlayer().getInventory().remove(item);
           } else
                        item.setAmount(item.getAmount() - 1);
                }
    }
    }
    }
        }
    
    That doesn't work, is there anyway to check what the material is in hand instead of what I did in the IF statement?
     
  2. Offline

    Seadragon91

    You are comparing here an itemstack with a enum from material, you need to use getType() by the item to get the Material. Example: (I added a null check for the item, because item can be null if you have nothing in the hand.)

    Code:
    if (item != null && item.getType() == Material.BLAZE_ROD) {
    // your code
    }
     
  3. Offline

    Bust_Open

    thanks, bro It works.

    Any ideas on how to make sure people can't spam it?
    So make it like people can only fire the rod once per second or per 2 seconds?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  4. Offline

    Maximvdw

    Maybe add a variable with time format? then poll if the new time >= 2 secconds + prevTime?
    gr,
    Maxim
     
Thread Status:
Not open for further replies.

Share This Page