I can't change the item a player is holding

Discussion in 'Plugin Development' started by MrZoraman, Jan 1, 2013.

  1. Offline

    MrZoraman

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Someone asked me to make a plugin that fills up the health bar when soup is consumed. Doesn't sound too hard, so why not?

    Code:
    Code:
    public class InstaHealSoup extends JavaPlugin implements Listener
    {
        public static InstaHealSoup plugin;
        public Logger logger;
       
        @Override
        public void onEnable()
        {
            plugin = this;
            logger = this.getLogger();
           
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public void onDisable()
        {
            logger = null;
            plugin = null;
        }
       
        @EventHandler
        public void onUse(PlayerInteractEvent event)
        {
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
            {
                if(event.getPlayer().hasPermission("instaHealSoup.use"))
                {
                    if(event.getPlayer().getItemInHand().getType().equals(Material.MUSHROOM_SOUP))
                    {
                        event.getPlayer().setItemInHand(new ItemStack(Material.BOWL, 1));
                        event.getPlayer().setHealth(20);
                    }
                }
            }
        }
    }
    The player's inventory is not changing, however. The item he is holding remains to be the mushroom soup. It will not change to just a bowl. I can't even remove the item in his hand, and I have tried every single trick I've found on the forums. Setting it to null, the Inventory.clear method, setting it to Material.AIR.., none of them are working. I also threw in updateInventory() method, even though it was depricated, and that didn't change my results. Any help is appreciated. Thanks!
  2. Offline

    tommycake50

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    do something like this,
    Code:
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onPlayerInteractEvent(PlayerInteractEvent e){
         
                final int l = e.getPlayer().getFoodLevel();
                final Player p = e.getPlayer();
             
                ItemStack i = e.getPlayer().getItemInHand();
                if(i.getType().equals(Material.MUSHROOM_STEW)){
                    //schedule a task to see if they have eaten the stew(maybe the time could be a little faster idk)
                   Bukkit.getServer().getPluginManager().getPlugin("pluginname").getServer().getScheduler().scheduleSyncDelayedTaskBukkit.getServer().getPluginManager().getPlugin("pluginname"), new Runnable(){
                        public void run(){
                            if(p.getFoodLevel() > l && p.getItemInHand().getType().equals(Material.BOWL)){
                             
                                p.setHealth(20);
                        }
                    },90L);
                }
            }
        }

    This post has been edited 1 time. It was last edited by tommycake50 Jan 1, 2013.
  3. Offline

    fireblast709

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  4. Offline

    Scizzr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    This code presents the popular bug that MineZ had where you could have an item in one hand, click, then swap to another item and receive benefits of using the item even though you didn't use it. With MineZ, this was you right clicking with a water bottle, opening your inventory, and swapping it for an empty bottle to get your thirst refilled. With this, I could see people abusing it even easier by having a soup in slot 1 and a bowl in slot 2 and right clicking with slot 1 and swapping to slot 2 every time they want full health.

    Bukkit actually has an event that you can use to prevent this from happening (when eating food, but not drinking potions). Here's some code to get you started:
    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onFoodLevelChange(FoodLevelChangeEvent e) {
        Entity ent = e.getEntity();
        if (ent instanceof Player) {
            Player p = (Player)ent;
            ItemStack hand = p.getItemInHand();
            
            int pre = p.getFoodLevel();
            int post = Math.min(e.getFoodLevel(), 20);
            
            if (post-pre > 0) {
                p.sendMessage(String.format("You ate some %s and gained %d hunger.", WordUtils.capitalize(hand.getType().name().replace("_", " ").toLowerCase()), post-pre));
            }
        }
    }
    
  5. Offline

    MrZoraman

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @fireblast709
    Yes the player does get healed

    Thank you for all of your help guys, but everything in the plugin works how I want it to so far. The only problem is that I can't change the item in the player's hand from a bowl of soup to an empty bowl.
  6. Offline

    fireblast709

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    what about setting the type of the item in hand to empty bowl?
  7. Offline

    Scizzr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Just like my code above, this is a way you can get what you want and not have to manually set their held item or make schedulers and is pretty much fool-proof in terms of your players being able to exploit the fact that you're not checking if they swap items around.

    It checks ItemMeta for a special item name (from the Anvil or from you setting another name using ItemMeta) but defaults to the Minecraft name for the item if an ItemMeta name doesn't exist. As a result of this, you can check the name against a string of your liking and do special things if it matches something such as cactus soup. You can even add colors to the items by using the ItemMeta, but make sure you add that to the check. :D

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onFoodLevelChange(FoodLevelChangeEvent e) {
        Entity ent = e.getEntity();
        if (ent instanceof Player) {
            Player p = (Player)ent;
            ItemStack hand = p.getItemInHand();
           
            ItemMeta im = hand.getItemMeta();
           
            String itemName = im.getDisplayName();
            if (itemName == null) { itemName = hand.getType().toString().toLowerCase().replace("_", " "); }
           
            int pre = p.getFoodLevel();
            int post = Math.min(e.getFoodLevel(), 20);
           
            if (post-pre > 0) {
                if (itemName.equalsIgnoreCase("cactus soup")) {
                    p.setHealth(20);
                    p.sendMessage(String.format("You ate some %s and were healed to full health.", itemName));
                } else {
                    p.sendMessage(String.format("You ate some %s and gained %d hunger.", itemName, post-pre));
                }
            }
        }
    }
    

    This post has been edited 1 time. It was last edited by Scizzr Jan 1, 2013.
  8. Offline

    tommycake50

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    mhm but im afraid you missed the part in my code where i checked the food level.
    i do think of things like this you know.
  9. Offline

    Scizzr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hey, I just offered. If you don't want it, you don't have to take it.

    Anyways, good luck. :)
  10. Offline

    tommycake50

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    wasnt me asking the question...
  11. Offline

    Sparta

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Don't put MATERIALNAME, 1. Just do player.setItemInHand().setType(Material.BOWL);

Share This Page