Solved Canceling EventB while EventA gets triggered

Discussion in 'Plugin Development' started by L33m4n123, Apr 7, 2013.

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

    L33m4n123

    Hey!

    I know titel may be a bit confusing.. What I want to do is that on
    PlayerItemConsumEvent
    the event
    FoodLevelChange gets canceled if it should be called. How can I do that?
    since
    Code:java
    1.  
    2. public final void onFoodEat(final PlayerItemConsumeEvent event,
    3. final FoodLevelChangeEvent event2) {
    4. Player p = event.getPlayer();
    5. if (p.getItemInHand().getType() == Material.COOKIE) {
    6. event2.setCancelled(true);
    7. }
    8. }
    9.  

    is not working out of obvious reasons
     
  2. Offline

    MCForger

    L33m4n123
    Why not store what player just ate in a list and for the foodlevelchangeevent if it is the same player cancel it.
     
    L33m4n123 likes this.
  3. Offline

    L33m4n123

    MCForger
    Works perfectly fine, thank you!

    For whoever wants the code here it is

    Code:java
    1. public class EatFoodListener implements Listener {
    2.  
    3.  
    4. public EatFoodListener() {
    5. this.food.add("");
    6. }
    7.  
    8.  
    9. private List<String> food = new ArrayList<String>();
    10.  
    11. @EventHandler
    12. public final void onFoodEat(final PlayerItemConsumeEvent event) {
    13. Player player = event.getPlayer();
    14. if (player.getItemInHand().getType() == Material.COOKIE) {
    15. food.set(0, player.getName() + "cookie");
    16. System.out.println(food.get(0));
    17. }
    18. }
    19.  
    20. @EventHandler
    21. public final void onFoodConsume(final FoodLevelChangeEvent event) {
    22. Entity playerEntity = event.getEntity();
    23. String foodConsumed = food.get(0);
    24. if (playerEntity instanceof Player) {
    25. Player player = (Player) playerEntity;
    26. if (player.getItemInHand().getType() == Material.COOKIE) {
    27. if (foodConsumed.equalsIgnoreCase(player.getName() + "cookie")) {
    28. food.set(0, "");
    29. event.setCancelled(true);
    30. }
    31. }
    32. }
    33. }
    34. }
    35.  


    Edit: Also this works only for cookies now. But well yeah. You would need to add the other food aswell, but can leave the the way the list gets stored. It doesn't matter anymore what kind of food was eaten. As long as a sort of food was eaten and with that the event gets canceled
     
Thread Status:
Not open for further replies.

Share This Page