PotionEffect if ItemStack is equiped

Discussion in 'Spigot Plugin Development' started by ProjectFTW, May 22, 2017.

  1. Code (Text):
    public class EnchantsHandler implements Listener {
        @EventHandler
        public void interact(InventoryClickEvent e) {
            ItemStack AIR = new ItemStack(Material.AIR);
            Player player = (Player) e.getWhoClicked();
            ItemStack EQUIP = e.getCursor();
            if(e.getSlotType() == SlotType.ARMOR) {
                if(e.getSlot() == 100) {
                    if(EQUIP.hasItemMeta()) {
                        ItemMeta meta = EQUIP.getItemMeta();
                        if(meta.hasLore()) {
                            ArrayList<String> Lore = new ArrayList<String>();
                            if(Lore.contains(ChatColor.RED + "Speed I")) {
                                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999, 1));
                                }
                            if(EQUIP.getItemMeta() == AIR) {
                                player.removePotionEffect(PotionEffectType.SPEED);
                            }
                        }
                    }  
                }
            }
        }

    }
    What is the problem?
    If you have a random armor piece with the 'Speed I' as lore it should give you Speed 1 if it has been equipped and remove the potion effect if you unequipped the armor piece.
    I am currently where nothing happens if i equip the armor piece with the lore on it.
     
  2. There's an event called ArmorEquipEvent, use it.
     
    • Optimistic Optimistic x 1
  3. There is no armor equip event :)
     
    • Agree Agree x 2
  4. D E B U G.

    Code (Text):
        public class EnchantsHandler implements Listener {
            @EventHandler
            public void interact(InventoryClickEvent e) {
                ItemStack AIR = new ItemStack(Material.AIR);
                Player player = (Player) e.getWhoClicked();
                ItemStack EQUIP = e.getCursor();
                if(e.getSlotType() == SlotType.ARMOR) {
                    player.sendMessage("YOU HAVE EQUIPED ARMOR FUCK HEAD");
                    if(e.getSlot() == 100) {
                        player.sendMessage("YOU HAVE EQUIPED BOOTS (tbh you could just check to see if their boots now has the item)");
                        if(EQUIP.hasItemMeta()) {
                            player.sendMessage("THIS ITEM HAS META xddd");
                            ItemMeta meta = EQUIP.getItemMeta();
                            if(meta.hasLore()) {
                                player.sendMessage("THIS ITEM MIGHT HAVE A CUSTOM ENCHANT");
                                ArrayList<String> Lore = new ArrayList<String>();
                                if(Lore.contains(ChatColor.RED + "Speed I")) {
                                    player.sendMessage("THE ITEM HAS A CUSTOM ENCHANT");
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999, 1));
                                }
                                if(EQUIP.getItemMeta() == AIR) {
                                    player.sendMessage("YOU HAVE UNEQUIPED ARMOR FUCK HEAD");
                                    player.removePotionEffect(PotionEffectType.SPEED);
                                }
                            }
                        }
                    }
                }
            }
        }
     
    • Optimistic Optimistic x 1
  5. So far I know you can also use a schedule timer, and check every 1 second or the player is wearing a piece of armor and then apply the effects to it.
    If you make it lower than 1 second it will be activated faster but will create more lagg if there are much players on your server.
     
  6. What I do is maintain a cache of what's equipped, and do the update logic on the cache once per 10 ticks. That way you only do the check once, and can hit the cache as many times as you want...no performance hit.
     
  7. there's a couple utility classes that are available for you to use, on github/bukkit/etc. try searching for them, should have been your first option when coming across this problem ;)
     
  8. Several replies yet no one pointed out the flawed logic..

    Code (Text):
                            ArrayList<String> Lore = new ArrayList<String>();
                            if(Lore.contains(ChatColor.RED + "Speed I")) {
    Your creating a new array list, and then checking if the new array list (which would be empty, because its new and empty) will contain it. I wouldn't be surprised if your IDE was giving you a warning about this. Get the lore from the item meta. Also, variables should only all be in upper case if they are a constant.
     
  9. Thank you so much!
    Finally a reply which made sense :)