Trying to make potion effects to player when holding an item.

Discussion in 'Plugin Development' started by Radyngg, Sep 22, 2019.

  1. Offline

    Radyngg

    I want that: when player'll hold poppy in extra slot (for shield) it will give regeneration effect and send specify message.

    When i run plugin on server, console doesnt say about errors or warns. Just nothing: only "plugin enable" and "plugin disable", when server stopped.

    Code:


    Code:
    package main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class mainClass extends JavaPlugin implements Listener
    {
        public void onEnable()
        {
            getLogger().info("FBB is enabled :}");
            getServer().getPluginManager().registerEvents(this, this);
        }
      
        @EventHandler
        public void onHoldPoppy(PlayerItemHeldEvent e)
        {
            Player player = e.getPlayer();
            ItemStack p = player.getInventory().getItem(45);
          
                if(p != null && p.getType().equals(Material.POPPY))
                {
                    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 100000, 1));
                    player.sendMessage(ChatColor.RED + "Poppy is bringing you life!");
                }
                else
                {
                    player.removePotionEffect(PotionEffectType.REGENERATION);
                }
                  
        }
      
        public void onDisable()
        {
            getLogger().info("FBB is disabled :{");
            getServer().getPluginManager().registerEvents(this, this);
        }
    }
     
  2. Offline

    wand555

    Not quite certain because I can't test it right now, but maybe PlayerItemHeldEvent only triggers once a player scrolls through their hotbar.
    In general I believe that this event isn't the best solution for this problem, since it doesn't deal with dropping an item by pressing q for example (I think, anyone can correct me if I'm wrong).
    I'd suggest you use inventoryInteractEvent
     
  3. Offline

    DerDonut

    You could create a runnable which checks the slot every (whatever) ticks. The more efficient way is to think about every possible way the slot can be changed and listen for each of these events. There should be PlayerPickupItemEvent, PlayerItemHeldEvent, PlayerSwapHandItemsEvent, PlayerDeathEvent, PlayerInteractEvent (don't know what happens if you place the poppy), InventoryClickEvent and maybe more. Then depending on the event add or remove the effect
     

Share This Page