Get enchant from config

Discussion in 'Spigot Plugin Development' started by jessegeerts, Jun 12, 2015.

  1. Hey guys i am trying to create an backpack plugin but i cant get an enchant from the item to load that out of the config =/

    Here is what i currently have:
    Code (Text):

    import java.util.HashMap;
    import java.util.Map.Entry;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Core extends JavaPlugin implements Listener {
       
        private HashMap<UUID, Inventory> backpacks = new HashMap<UUID, Inventory>();
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Inventory inv = Bukkit.getServer().createInventory(e.getPlayer(), InventoryType.CHEST, "Backpack");
           
            if (getConfig().contains("backpacks." + e.getPlayer().getUniqueId())) {
                for (String item : getConfig().getConfigurationSection("backpacks." + e.getPlayer().getUniqueId()).getKeys(false)) {
                    inv.addItem(loadItem(getConfig().getConfigurationSection("backpacks." + e.getPlayer().getUniqueId() + "." + item)));
                }
            }
           
            backpacks.put(e.getPlayer().getUniqueId(), inv);
        }
       
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
            if (!getConfig().contains("backpacks." + e.getPlayer().getUniqueId())) {
                getConfig().createSection("backpacks." + e.getPlayer().getUniqueId());
            }
           
            char c = 'a';
            for (ItemStack itemStack : backpacks.get(e.getPlayer().getUniqueId())) {
                if (itemStack != null) {
                    saveItem(getConfig().createSection("backpacks." + e.getPlayer().getUniqueId() + "." + c++), itemStack);
                }
            }
           
            saveConfig();
        }
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        public void onDisable() {
            for (Entry<UUID, Inventory> entry : backpacks.entrySet()) {
                if (!getConfig().contains("backpacks." + entry.getKey())) {
                    getConfig().createSection("backpacks." + entry.getKey());
                }
               
                char c = 'a';
                for (ItemStack itemStack : entry.getValue()) {
                    if (itemStack != null) {
                        saveItem(getConfig().createSection("backpacks." + entry.getKey() + "." + c++), itemStack);
                    }
                }
               
                saveConfig();
            }
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "The console cannot have an backpack.");
                return true;
            }
           
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("backpack")) {
                p.openInventory(backpacks.get(p.getUniqueId()));
            }
           
            return true;
        }
       
        private void saveItem(ConfigurationSection section, ItemStack itemStack) {
            section.set("type", itemStack.getType().name());
            section.set("amount", itemStack.getAmount());
            section.set("enchant", itemStack.getEnchantments());
        }
       
        private ItemStack loadItem(ConfigurationSection section) {
            return new ItemStack(Material.valueOf(section.getString("type")), section.getInt("amount"));
        }
    }
     
     
  2. Where are you even adding the enchantment to the item?
     
  3. Code (Text):
    ArrayList<String> ens = new ArrayList<String>();
                    if(item.getItem().getItemMeta().hasEnchants()) {
                        for (Enchantment enchantment : item.getItem().getEnchantments().keySet()) {
                            String s = enchantment.getName();
                            s = s + ":";
                            s = s + item.getItem().getEnchantments().get(enchantment);
                            ens.add(s);
                        }
                    }
                    data.set(path + ".item." + item.getSlot() + ".enchantment", ens);  
    Saves an enchantment like this:
    enchantment:
    - LOOT_BONUS_MOBS:3
    - DURABILITY:3


    Use this to add enchantments to the config.
    Code (Text):
     ArrayList<String> ens = (ArrayList)data.getList(p + ".enchantment");  //Enchantment name
                                    HashMap<Enchantment, Integer> enchantmentIntegerMap = new HashMap<Enchantment, Integer>();
                                    for(String s : ens)
                                    {
                                        String[] ss = s.split(":");
                                        enchantmentIntegerMap.put(Enchantment.getByName(ss[0]), Integer.parseInt(ss[1]));
                                    }
                                    ItemStack pItem = new ItemStack(material, 1, type);
                                    pItem.addEnchantments(enchantmentIntegerMap);
     
  4. Gianluca

    Gianluca Retired Resource Staff
    Retired Patron

    Is this code from PogoStick29?
     
  5. Yes, he is using the same code.
     
  6. Get the ItemMeta of the item and use .addEnchant on the ItemMeta and then apply it to the item.