1.14.4 Repeating Code that Should Be Fired Once

Discussion in 'Spigot Plugin Development' started by AVCK, Jan 12, 2020.

  1. So I have this plugin that has a global map which contains a score which is a credit that decreases every time the player enchants items over a certain enchantment level, and here is my code.
    Code (Java):
    public class Anvil implements Listener {

        @EventHandler
        public void onInventoryClick(PrepareAnvilEvent e) {

            ItemStack firstSlot = e.getInventory().getItem(0);
            HumanEntity player = e.getView().getPlayer();
            ItemStack secondSlot = e.getInventory().getItem(1);

            if (firstSlot.getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                    || firstSlot.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL) > 4
                    || firstSlot.getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                    || firstSlot.getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
            ) {
                if (Bindenchant.globalMap.get(player) == null){
                    Bindenchant.globalMap.put((Player) player, 0);
                    e.setResult(null);
                    player.sendMessage(ChatColor.DARK_RED + "You cannot repair what gods forged!");

                }else if (Bindenchant.globalMap.get(player) > 0){
                    int i = Bindenchant.globalMap.get(player) - 1;
                    Bindenchant.globalMap.put((Player) player, i);
                    player.sendMessage(ChatColor.GREEN + "You have prepared the anvil. Your credit has decreased. Your current credit is " + i + " credits.");
                    ItemStack resultItem = e.getResult();
                    if (!resultItem.getType().equals(Material.AIR) && resultItem.hasItemMeta()) {
                        ItemMeta meta = resultItem.getItemMeta();
                        if (resultItem.getItemMeta().hasDisplayName()) {

                            //Set the display name
                            meta.setDisplayName(firstSlot.getItemMeta().getDisplayName());

                            //Set the resulting item
                            e.setResult(resultItem);

                        }
                        if (secondSlot.getType() == Material.ENCHANTED_BOOK){
                            e.setResult(null);
                            player.sendMessage(ChatColor.DARK_RED + "You cannot enchant what gods forged!");
                        }
                    }

                }else if (Bindenchant.globalMap.get(player) == 0){
                    e.setResult(null);
                    player.sendMessage(ChatColor.DARK_RED + "You cannot repair what gods forged!");
                }
                //Get resulting item of inventory
            }
        }
    }


     
    But this instead decreases the value every tick it is loaded. How can I make it so that it's fired only when the inventory is clicked and the transaction is made? I tried adding a separate event but it didn't work as intended, not even showing the red name in the end. It looks like this:
    Code (Java):
    package me.joseph.bindenchant.bindenchant.enchants;

    import me.joseph.bindenchant.bindenchant.Bindenchant;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.event.inventory.PrepareAnvilEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class Anvil implements Listener {

        @EventHandler
        public void onPrepareAnvil(PrepareAnvilEvent e) {

            ItemStack firstSlot = e.getInventory().getItem(0);

                ItemStack resultItem = e.getResult();
                if (!resultItem.getType().equals(Material.AIR) && resultItem.hasItemMeta()) {
                    ItemMeta meta = resultItem.getItemMeta();
                    if (resultItem.getItemMeta().hasDisplayName()) {

                        //Set the display name
                        meta.setDisplayName(firstSlot.getItemMeta().getDisplayName());

                        //Set the resulting item
                        e.setResult(resultItem);

                    }
                }

        }
        @EventHandler
        public void OnPlayerClick(InventoryClickEvent event){
            Player p = (Player) event.getWhoClicked();

            if (event.getInventory().getType() == InventoryType.ANVIL && event.getCurrentItem() == event.getInventory().getItem(2)){
                ItemStack firstSlot = event.getInventory().getItem(0);
                ItemStack secondSlot = event.getInventory().getItem(1);
                if (firstSlot.getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                        || firstSlot.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL) > 4
                        || firstSlot.getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                        || firstSlot.getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
                ){
                    if (Bindenchant.globalMap.get(p) == 0){
                        event.setCancelled(true);
                        p.sendMessage(ChatColor.DARK_RED + "You cannot repair what gods forge");

                    }else if (Bindenchant.globalMap.get(p) > 0){
                        int a = Bindenchant.globalMap.get(p) - 1;
                        Bindenchant.globalMap.put(p, a);
                        p.sendMessage(ChatColor.GREEN + "Your item has been successfully repaired! Your remaining credit is " + a + ".");
                    }else if (Bindenchant.globalMap.get(p) == null){
                        event.setCancelled(true);
                        p.sendMessage(ChatColor.DARK_RED + "You didn't even buy repair credit? Cringe.");
                    }
                }else if ((firstSlot.getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 5
                        || firstSlot.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL) > 4
                        || firstSlot.getEnchantmentLevel(Enchantment.DIG_SPEED) > 5
                        || firstSlot.getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5) && secondSlot.getType() == Material.ENCHANTED_BOOK
                ){
                    event.setCancelled(true);
                    p.sendMessage(ChatColor.DARK_RED + "You cannot enchant what gods forge");
                   
                }
            }

        }
    }




     

    Can somebody help me fix this?
     
  2. drives_a_ford

    Moderator

    The items get combined (or enchantments added) in the anvil when the player clicks on the item in the result slot. Thus you'll have to listen to the InventoryClickEvent. You'll have to make sure it's the correct inventory and the correct action is made, of course.
     
  3. I know for enchantments you can use EnchantItemEvent
     
  4. When I use InventoryClickEvent, the keeping colored name function does not work.