Solved Strange problem with PlayerInteractEvent?

Discussion in 'Plugin Development' started by Explodncheese, Dec 26, 2014.

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

    Explodncheese

    Edit: Fixed :p

    I have a strange issue with PlayerInteractEvents...
    I'm trying to get it to allow "eating" of cocoa beans named "Chocolate," and that bit works, but when you try to eat a piece of normal food, the food just spazzes out in your hand and you eat without the eating animation.
    You are also unable to fire a bow, because it spazzes out too.

    I'm thinking this can be fixed by not having it override the vanilla interact events, but I have no clue how to do this... Can anyone offer a solution? :/


    Code:
    @EventHandler(priority = EventPriority.LOWEST)
        public void eatChocolate(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if (player.getItemInHand().getType() == Material.AIR || !Items.holdingChocolate(player))
                return;
            if (Items.holdingChocolate(player)) {
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(player.getFoodLevel() == 20) {
                    event.setCancelled(true);
                    } else if(player.getFoodLevel() < 20) {
                        if (player.getInventory().getItemInHand().getAmount() > 1) player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount()-1);
                        else player.setItemInHand(null);
                        player.setFoodLevel(player.getFoodLevel() + 1);
                        player.playSound(player.getLocation(), Sound.EAT, 10, 1);
                    }
                }
            }
        }
     
    Last edited: Dec 26, 2014
  2. Offline

    EgyptianKing

  3. Offline

    Explodncheese

    @EgyptianKing
    Nevermind, I fixed it... I added a check for the held item:
    Code:
            if (!Items.holdingChocolate(player))
                return;
    and that worked! I feel dumb now... Still new to this lol

    Thanks for the response!
     
  4. Offline

    XgXXSnipz

Thread Status:
Not open for further replies.

Share This Page