Solved why cant the playermoveevent detect item change?

Discussion in 'Spigot Plugin Development' started by CrossedLivid, Jun 14, 2021.

  1. I'm trying to give a player potion effects when they hold a item with the haste enchant. But let me just say that it doesn't seem to work (and beacon is useless, see my code, but dont comment on that because i dont care about the beacon)
    Code (Java):
    package net.wardensmp.plugins.events;

    import net.wardensmp.plugins.WardenSMP;
    import net.wardensmp.plugins.enchantments.CustomEnchants;
    import net.wardensmp.plugins.recipes.CustomRecipes;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class MoveEvent implements Listener {
        WardenSMP plugin;
        public MoveEvent(WardenSMP plugin) { this.plugin = plugin; }

        CustomRecipes recipes = new CustomRecipes(plugin);

        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            for (ItemStack piece : player.getInventory().getArmorContents()) {
                if (piece != null) {
                    if (piece.hasItemMeta()) {
                        if (piece.getEnchantments().containsKey(CustomEnchants.AGILITY)) {
                            player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999,
                                    piece.getEnchantments().get(CustomEnchants.AGILITY) - 1, true, false, true));
                            player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999,
                                    piece.getEnchantments().get(CustomEnchants.AGILITY) - 1, true, false, true));
                        } else {
                            if (player.hasPotionEffect(PotionEffectType.SPEED) && player.hasPotionEffect(PotionEffectType.JUMP)) {
                                player.removePotionEffect(PotionEffectType.SPEED);
                                player.removePotionEffect(PotionEffectType.JUMP);
                            }
                        }
                    }
                }
            }
            if (player.getInventory().getItemInMainHand() != null) {
                if (player.getInventory().getItemInMainHand().hasItemMeta()) {
                    if (player.getInventory().getItemInMainHand().getItemMeta().hasEnchant(CustomEnchants.HASTE)) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 999999, 0, true, false, true));
                    } else {
                        if (player.hasPotionEffect(PotionEffectType.FAST_DIGGING)) {
                            player.removePotionEffect(PotionEffectType.FAST_DIGGING);
                        }
                    }
                }
            }
        }
    }
     
     
  2. You are setting a 0 amplifier, maybe you want to set it to 1? And PlayerMoveEvent maybe isnt what you want here. Maybe a combination of InvenotyClickEvent and PlayerItemHeldEvent
     
  3. Also, I forgot to mention that there are many ways that it can come into that active slot, for example the item could be picked up right into the active slot.
    So maybe use InventoryClickEvent, PlayerItemHeldEvent, even PlayerSwapHandItemsEvent for if they switch the enchanted item from the off-hand to main hand.
     
  4. oh well isnt 0 amplifier = haste 1? and fyi it does give me the effect, only when i switch to antother it doesnt delete it
    k ima try
     
  5. Those events are definitely better suited and consume less cpu.

    Why your PlayerMoceEvent lisrener doesn't work is because your else is only for your last if.
    You have no else when the item in players hand is null or if the item doesn't have an item meta.