Solved Soup Plugin Using Material.AIR Failing

Discussion in 'Plugin Development' started by IZeusI, Jan 2, 2015.

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

    IZeusI

    What I want this part of my custom PvP plugin to do is drink soup on click, to heal a certain amount of health, and to delete or remove the bowl/mushroom soup from the inventory. I can make the soup turn in to a bowl, however can't seem to set it to 'Material.AIR', 'null', or simply use the remove item function. I really don't understand.

    Here's my listener code:
    Code:
        @EventHandler
        public void onItemRightClick(PlayerInteractEvent e){
            Player player = e.getPlayer();
            if(e.getAction()==Action.RIGHT_CLICK_AIR|| e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(player.getItemInHand()!= null){
                    if(player.getItemInHand().getType()==Material.MUSHROOM_SOUP){
                        if(player.getHealth()>13){
                            player.setHealth(20);
                            player.getItemInHand().setType(Material.AIR);
                            player.sendMessage("This Works!");
                        }else{
                            player.setHealth(player.getHealth()+ 7);
                            player.getItemInHand().setType(Material.AIR);
                            player.sendMessage("This Works! (Less Than Health)");
                        }
                    }
                }
            }
        }
    On top of this, my console doesn't generate any errors at all. All that happens is my health is restored but the mushroom soup stays in the inventory as mushroom soup. I also know that my plugin works due to the fact that I receive the player messages when souping.
     
  2. Offline

    Fuzzybear04

    You're checking if their health is more than 13, surely it should be less than 13? :p Instead of setting it to air, set it as null, or change the amount in their hand by 1 less
     
  3. Offline

    IZeusI

    I don't think so, that just fills the health bar if they have less than 3.5 hearts missing. This stops the person from having more than maximum health, I believe.
    EDIT
    setting it to null gives my console an error:
    Code:
                    if(player.getItemInHand().getType()==Material.MUSHROOM_SOUP){
                        if(player.getHealth()>13){
                            player.setHealth(20);
                            player.getItemInHand().setType(null);
                            player.sendMessage("This Works!");
                        }else{
                            player.setHealth(player.getHealth()+ 7);
                            player.getItemInHand().setType(null);
                            player.sendMessage("This Works! (Less Than Health)");
                        }
                    }
    
    Error:
    http://pastebin.com/DnafxpmT

    Taking one away from the held item gives the same result as using Material.AIR...
     
    Last edited: Jan 2, 2015
  4. Offline

    IZeusI

    [Bump]
     
  5. Offline

    Ivan

    You're doing it wrong. The correct code is:
    Code:java
    1. getPlayer().setItemInHand(null);

    ;)
     
    tomudding likes this.
  6. Offline

    IZeusI

    When using this line, there is a script error, telling me that there is no method. I did try using the event too,
    (e.getPlayer().setItemInHand(null); )but this meant that the soup just didn't disappear when souping, it stayed as a full mushroom soup bowl.
     
    Last edited: Jan 3, 2015
  7. Offline

    Fuzzybear04

    p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1); ?
     
  8. Offline

    Evaluations

    Try updating the player's inventory after you set it to air.@IZeusI
     
  9. Offline

    567legodude

    Just check to see if the item in their hand is the soup, then call:
    player.getInventory().remove(player.getItemInHand());
    No errors and it will remove the item from their hand.
    Also no need to update.
     
  10. Offline

    IZeusI

    @Fuzzybear04 - Tried this method, doesn't delete the soup.
    @Evaluations - This method is deprecated, however I used it anyway, and came to the same result.
    @567legodude - Tried this:
    Code:
        @EventHandler
        public void onItemRightClick(PlayerInteractEvent e){
            Player player = e.getPlayer();
            if(e.getAction()==Action.RIGHT_CLICK_AIR|| e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(player.getItemInHand()!= null){
                    if(player.getItemInHand().getType()==Material.MUSHROOM_SOUP){
                        if(player.getHealth()>13){
                            player.setHealth(20);
                            player.getInventory().remove(player.getItemInHand());
                            player.sendMessage("This Works!");
                        }else{
                            player.setHealth(player.getHealth()+ 7);
                            player.getInventory().remove(player.getItemInHand());
                            player.sendMessage("This Works! (Less Than Health)");
                        }
                    }
                }
            }
        }
    
    
    Is this what you meant? If so, still nothing. The soup remains in the hand.

    Although this seems very strange, I fixed this problem by, firstly, changing the 'MATERIAL.AIR' into 'MATERIAL.STONE' and then removing the player hand item. After this, I realised that when right clicking the soup, it would disappear but would place a stone block down. To fix /this/, I simply changed the material that the soup turns in to from stone to a non-placeable item (I used a stick). Here's the code:
    Code:
        @EventHandler
        public void onItemRightClick(PlayerInteractEvent e){
            Player player = e.getPlayer();
            if(e.getAction()==Action.RIGHT_CLICK_AIR|| e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(player.getItemInHand()!= null){
                    if(player.getItemInHand().getType()==Material.MUSHROOM_SOUP){
                        if(player.getHealth()<20){
                            if(player.getHealth()>13){
                                player.setHealth(20);
                                player.getItemInHand().setType(Material.STICK);
                                player.getInventory().remove(player.getItemInHand());
                            }else{
                                player.setHealth(player.getHealth()+ 7);
                                player.getItemInHand().setType(Material.STICK);
                                player.getInventory().remove(player.getItemInHand());
                            }
                        }
                    }
                }
            }
        }
    }
    

    P.s: Thanks to everybody who helped on this post! I'm sure your ideas and fixes will sort out somebody else's problems.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  11. Offline

    nj2miami

    EDIT: I apparently misread the issue, I thought your event was not firing at all. The below applies to react to clicking air though.

    Action.LEFT|RIGHT_CLICK_AIR is set to cancel by default, and cancelled events do not fire, so in order to capture the event you must add 'ignoreCancelled = false' so you react to the cancelled event.

    Use whatever priority is appropriate for your plugin.

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
     
Thread Status:
Not open for further replies.

Share This Page