Problems with anvils and books (limiting enchantments)

Discussion in 'Spigot Plugin Development' started by ItsMonkey, Jul 1, 2015.

  1. Hey guys, so I have an enchantment limiter part in my plugin and I would like to not allow users to get higher enchantments through combining items in anvils. What I try doing is getting the result itemstack and checking its enchantments. However, for some reason it detects no enchantments. I tried debugging by printing out the enchantments in chat and none appeared. I also added a few other debugging lines. Check the code for yourself

    NOTE: This is inside a InventoryClickEvent
    PHP:
    if (e.getInventory().getType() == InventoryType.ANVIL) {
                if(e.getSlotType() == InventoryType.SlotType.RESULT){
                    ItemStack item = e.getCurrentItem();

                    Bukkit.broadcastMessage("1");
                    for(Enchantment ench : item.getEnchantments().keySet()){
                        Bukkit.broadcastMessage(ench.toString());
                        Bukkit.broadcastMessage(":)");
                    }
                    Bukkit.broadcastMessage("2");

                    for (String blockedEnchantments : HCFEssentials.getInstance().getConfig().getStringList("BlockedEnchants")) {

                        String[] parse = blockedEnchantments.split(":");
                        Enchantment selectedEnchantment = Enchantment.getByName(parse[0]);
                        int level = Integer.parseInt(parse[1]);

                        if (item.getEnchantments().containsKey(selectedEnchantment)) {
                            Bukkit.broadcastMessage("contains selected enchantment");
                            if (item.getEnchantments().get(selectedEnchantment) > level) {
                                Bukkit.broadcastMessage("greater than level allowed");
                                e.setCancelled(true);
                                p.closeInventory();
                                p.sendMessage(Utils.color(HCFEssentials.getInstance().getConfig().getString("BlockedEnchants.highAnvilEnchant")));
                            }
                        }
                    }
                }
            }
    So yeah, sadly, the only messages that actually get broadcasted are "1" and "2". Help is appreciated :>
     
  2. @ItsMonkey debug more. You should've put broadcasts in your second for loop (to indicate what it is iterating over) and broadcasted item.getEnchantments() as well.
     
  3. Although it is not in the code, I have done that before with no luck. I will post the updated code in a few minutes
     
  4. @ItsMonkey make sure to include your results as well :p
     
  5. @DarkSeraphim
    http://prntscr.com/7nfc30
    ^Those are the results
    NOTE: I had a hotbar full of sharp 1 books. I put in the 2 sharp 1 books and got a sharp 2 book (all the way to the left of the hotbar).

    Here is the relevant part of the config.yml
    PHP:
    BlockedEnchants:
    - DAMAGE_ALL:1
    - PROTECTION_ENVIRONMENTAL:1
    - FIRE_ASPECT:1
    - KNOCKBACK:1
    And here is the code
    PHP:
    if (e.getInventory().getType() == InventoryType.ANVIL) {
                if(e.getSlotType() == InventoryType.SlotType.RESULT){
                    ItemStack item = e.getCurrentItem();

                    Bukkit.broadcastMessage("Starting the FUN!");

                    for (String blockedEnchantments : HCFEssentials.getInstance().getConfig().getStringList("BlockedEnchants")) {

                        String[] parse = blockedEnchantments.split(":");
                        Enchantment selectedEnchantment = Enchantment.getByName(parse[0]);
                        int level = Integer.parseInt(parse[1]);

                        //check the item's enchantments
                        for(Enchantment ench : item.getEnchantments().keySet()){
                            Bukkit.broadcastMessage(ench.toString());
                            Bukkit.broadcastMessage(":)");
                        }

                        //check the config for the blocked enchants
                        Bukkit.broadcastMessage(selectedEnchantment + " " + level);

                        if (item.getEnchantments().containsKey(selectedEnchantment)) {
                            Bukkit.broadcastMessage("contains selected enchantment");
                            if (item.getEnchantments().get(selectedEnchantment) > level) {
                                Bukkit.broadcastMessage("greater than level allowed");
                                e.setCancelled(true);
                                p.closeInventory();
                                p.sendMessage(Utils.color(HCFEssentials.getInstance().getConfig().getString("BlockedEnchants.highAnvilEnchant")));
                            } else {
                                //Enchantment is limited, but the level was allowed
                                Bukkit.broadcastMessage("It is limited but this is an appropriate level");
                            }
                        } else {
                            //The enchantments on the item are not limited
                            Bukkit.broadcastMessage("Enchantments for this item are not limited!");
                        }
                    }
                }
            }
     
  6. @ItsMonkey Enchanted books store their enchantments in their meta.
     
  7. Oh! That makes sense. I will work on it tomorrow. Thanks!
     
  8. Thanks @DarkSeraphim for the help. For anyone wondering here is the final code and it works : >
    PHP:
    if (e.getInventory().getType() == InventoryType.ANVIL) {
                if(e.getSlotType() == InventoryType.SlotType.RESULT){
                    ItemStack item = e.getCurrentItem();

                    if(item.getType() == Material.ENCHANTED_BOOK){
                        for (String blockedEnchantments : HCFEssentials.getInstance().getConfig().getStringList("BlockedEnchants")) {

                            String[] parse = blockedEnchantments.split(":");
                            Enchantment selectedEnchantment = Enchantment.getByName(parse[0]);
                            int level = Integer.parseInt(parse[1]);

                            EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) item.getItemMeta();

                            if (bookMeta.getStoredEnchants().containsKey(selectedEnchantment)) {
                                if (bookMeta.getStoredEnchants().get(selectedEnchantment) > level) {
                                    e.setCancelled(true);
                                    p.closeInventory();
                                    p.sendMessage(Utils.color(HCFEssentials.getInstance().getConfig().getString("BlockedEnchants.highAnvilEnchant")));
                                }
                            }
                        }
                    }
                }
            }
    >