Solved Enchantment Maps

Discussion in 'Spigot Plugin Development' started by Minimigamer, Jan 10, 2020.

?

How to enchant an item with an enchantment from your item in your hand?

Poll closed Jan 22, 2020.
  1. 1

    1 vote(s)
    50.0%
  2. 2

    1 vote(s)
    50.0%
  1. Hey guys, Im trying to write a command which opens a inventory with enchanted books each book is enchanted with one enchantment from your item in your hand.

    I tryed this:
    Code (Text):
    :
    import java.util.ArrayList;
    import java.util.Map;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;


    public class Overenchant implements CommandExecutor {

        @SuppressWarnings({ "unchecked", "deprecation" })
        @Override
        public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
            if(s instanceof Player) {
                Player p = (Player) s;
                if(cmd.getName().equalsIgnoreCase("overenchant")) {
                    if(p.getItemInHand() != null) {
                        if(p.getItemInHand().getEnchantments() != null) {
                            Map<Enchantment, Integer> enchants = p.getItemInHand().getEnchantments();
                            enchants = p.getItemInHand().getEnchantments();
                           
                           
                            Inventory inv = Bukkit.createInventory(p, 9);
                           
                            ArrayList<Enchantment> list = new ArrayList<Enchantment>();
                            list.add(Enchantment.ARROW_DAMAGE);
                            list.add(Enchantment.ARROW_FIRE);
                            list.add(Enchantment.ARROW_INFINITE);
                            list.add(Enchantment.ARROW_KNOCKBACK);
                            list.add(Enchantment.CHANNELING);
                            list.add(Enchantment.DAMAGE_ALL);
                            list.add(Enchantment.DEPTH_STRIDER);
                            list.add(Enchantment.DIG_SPEED);
                            list.add(Enchantment.DURABILITY);
                            list.add(Enchantment.FIRE_ASPECT);
                            list.add(Enchantment.FROST_WALKER);
                            list.add(Enchantment.LOOT_BONUS_BLOCKS);
                            list.add(Enchantment.LOOT_BONUS_MOBS);
                            list.add(Enchantment.LUCK);
                            list.add(Enchantment.MENDING);
                            list.add(Enchantment.PROTECTION_ENVIRONMENTAL);
                           
                           
                           
                           
                           
                           
                           
                            for(int i = 0; i < enchants.size(); i++) {
                                p.sendMessage(list.get(i) + "     " + enchants.get(list.get(i)));
                                ItemStack book = new ItemStack(Material.BOOK);
                                ItemMeta meta = book.getItemMeta();
                                meta.setDisplayName("§6Upgrade: ");
                                meta.addEnchant(list.get(i), enchants.get(list.get(i)), true);
                                book.setItemMeta(meta);
                                inv.addItem(book);
                            }
                            p.openInventory(inv);
                        }
                    }
                }
            }
            return false;
        }
    }
    [CODE]
     
  2. What doesn't work? Errors?

    Also, don't use so many nested if statements, instead, check if a condition is not true and return.
     
  3. This should be defined in your class scope.


    Why do you even need the list at all?

    You can just get the Map<Enchantment, Integer> and iterate over it. Then
    map.forEach((ench, lvl) -> {
    create a book with ench and level and add it to your inventory
    });




    PS here is a bit of spoonfeed. Read my comments and try to understand what happens and what the idea behind a line is. If you have questions to anything just ask me.

    Code (Java):
    public class Overenchant implements CommandExecutor {

      @Override
      public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
        if (s instanceof Player) {
          // return if console
          return false;
        }
        Player p = (Player) s;
        if (!cmd.getName().equalsIgnoreCase("overenchant")) {
          // return if false command
          return false;
        }

        ItemStack item = p.getInventory().getItemInMainHand();

        if (item == null || item.getEnchantments().isEmpty()) {
          // return if item null OR item has no enchantments
          // NOTE: The map is NEVER null (notated as @NotNull)
          return false;
        }
     
        // Get map and create inventory
        Map<Enchantment, Integer> enchants = item.getEnchantments();
        Inventory inv = Bukkit.createInventory(p, 9);

        // Iterate over entrys in the map and create books for each
        for (Entry<Enchantment, Integer> mapEntry : enchants.entrySet()) {
          Enchantment enchantment = mapEntry.getKey();
          Integer level = mapEntry.getValue();
          p.sendMessage("Enchantment: " + mapEntry.getKey() + ":" + level);
          ItemStack book = new ItemStack(Material.BOOK);
          ItemMeta meta = book.getItemMeta();
          meta.setDisplayName("§6Upgrade: ");
          meta.addEnchant(enchantment, level, true);
          book.setItemMeta(meta);
          inv.addItem(book);
        }

        p.openInventory(inv);
        return true;
      }

    }
     
    #3 7smile7, Jan 10, 2020
    Last edited: Jan 10, 2020
  4. Danke! das ist die Beste Antword die ich bis jezt je bekommen habe
     
    #4 Minimigamer, Jan 11, 2020
    Last edited: Jan 11, 2020
  5. But this has to be if (!(s instanceof Player)) {}
     
  6. Well, you didn't state the problem you are having - but it stands out to me that you are creating an inventory with 9 slots and trying to put 16 things into it - that's sure to cause problems.