Get enchantments on an item

Discussion in 'Spigot Plugin Development' started by cheesyfreezy, Jun 21, 2016.

  1. Hey guys,

    I have got a little problem. So I am making a plugin where you can enchant items using signs. But when there already is a enchantment on the item that is the same as the one that the player wants to buy it should give them an upgraded level. Whenever I try to get the enchantments on an item it gives me nothing, just an empty list of the enchantments, even when there are enchantments on there.


    Code (Text):
    ArrayList<Enchantment> availableEnchants = new ArrayList<Enchantment>();
    for(Enchantment enchants : Enchantment.values()) {
        try {
            player.getItemInHand().addEnchantment(enchants, 1);
        } catch(IllegalArgumentException e) {
    Enchantment ench = EnchantmentTools.getEnchantmentTools().getEnchantment(ChatColor.stripColor(sign.getLine(1)));

    if(availableEnchants.contains(ench)) {
        player.sendMessage(ChatColor.YELLOW + "You purchased the enchantment!");
        player.playSound(player.getEyeLocation(), Sound.LEVEL_UP, 10, 1);
        if(!player.getItemInHand().containsEnchantment(ench)) {
            player.getItemInHand().addEnchantment(ench, 1);
        } else {
            int level = player.getItemInHand().getEnchantmentLevel(ench);
            player.getItemInHand().addEnchantment(ench, level + 1);

        int cost = Integer.parseInt(sign.getLine(2));
        File etFile = new File(Plugin.core.getDataFolder() + "/dat0/", "enchantTokens.yml");
        YamlConfiguration etConfig = YamlConfiguration.loadConfiguration(etFile);
        if(etConfig.getInt(player.getUniqueId().toString()) >= cost) {
            etConfig.set(player.getUniqueId().toString(), etConfig.getInt(player.getUniqueId().toString()) - cost);
            try {
            } catch (IOException e) {
        } else {
            player.sendMessage(ChatColor.RED + "You do not have enough enchant tokens to purchase this enchant!");
    } else {
        player.sendMessage(ChatColor.RED + "You cannot put this enchant on the current item!");
  3. That was to check if the enchantment was able to be added on the item. Scroll down to get the part where I am struggling
  4. @cheesyfreezy it's the part that causes your stuggle though. Because you are doing that, all enchantments of the item are being removed.