Solved Checking item in player's hand

Discussion in 'Plugin Development' started by MaxFireIce, Feb 13, 2017.

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

    MaxFireIce

    PHP:
        @EventHandler
        
    public void onClick(PlayerInteractEvent event) {
            
    Player player event.getPlayer();
            
    ItemStack dartCard Main.common;
            
    ItemMeta dartCardMeta dartCard.getItemMeta();
            
    dartCardMeta.setLore(Arrays.asList("Shoots a dart""at your foe"));
            
    dartCardMeta.setDisplayName(ChatColor.AQUA "Dart");
            
    dartCard.setItemMeta(dartCardMeta);
            
    ItemStack dart dartCard;

            if (
    player.getInventory().getItemInMainHand().getItemMeta().equals(dartCardMeta)
                    && 
    player.getInventory().getItemInMainHand().getType() == Material.PAPER
                    
    && event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
                
    player.launchProjectile(Arrow.class);
                
    player.getInventory().getItemInMainHand().setType(Material.AIR);
                return;
            }
        }
    I have that except I can shoot an arrow with any item I want AND the item in my hand is never removed. Why is all of the checking not working?
     
  2. Offline

    Zombie_Striker

    This line may be the problem. Always remember to check your encapsulation!
    You can do this, or instead use Player'sInventory#remove(ItemInHand)
     
    MaxFireIce and Jamesthatguy like this.
  3. Offline

    MaxFireIce

    It works! Thanks!

    New issue though, if I have multiple of the paper, it won't remove any of them if they are in a stack.

    Edit: Lol, jk. Fixed it with this code:
    Code:
            HashMap<Integer, ItemStack> cardRemover = new HashMap<Integer, ItemStack>();
            cardRemover.put(1, dart);
        player.getInventory().removeItem(cardRemover.get(1));
     
    Last edited: Feb 15, 2017
Thread Status:
Not open for further replies.

Share This Page