Solved Test if an Enchant Event value equals something

Discussion in 'Spigot Plugin Development' started by Halflove, Jul 19, 2018.

  1. Hey, so I'm basically trying to check if an item that is being enchanted with the EnchantItemEvent event gets the required enchantments added, but I'm having trouble with it - it's just not working, here is my code:

    Code (Text):
    Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
                    enchants.put(Enchantment.getByName(QManager.getType(QManager.getQuestId(player)).split(";")[1].toUpperCase()), Integer.parseInt(QManager.getType(QManager.getQuestId(player)).split(";")[2]));
                    if(event.getEnchantsToAdd().equals(enchants)) {
                        int current = SettingsManager.getData().getInt(player.getUniqueId().toString()+".selected-quest.amount");
                        SettingsManager.getData().set(player.getUniqueId().toString()+".selected-quest.amount", current+1);
                        SettingsManager.saveData();
                        QManager.testComplete(player, quest);
                    }
    Any help would be much appreciated!
    Thanks.
     
  2. The .equals() method is used for comparing Strings. Try using == to compare objects.
     
    • Funny Funny x 1
  3. Nevermind, I figured it out by doing the following:

    Code (Text):
                    if(event.getEnchantsToAdd().containsKey(Enchantment.getByName(QManager.getType(QManager.getQuestId(player)).split(";")[1].toUpperCase()))) {
                        if(event.getEnchantsToAdd().get(Enchantment.getByName(QManager.getType(QManager.getQuestId(player)).split(";")[1].toUpperCase())).equals(Integer.parseInt(QManager.getType(QManager.getQuestId(player)).split(";")[2]))) {
                            int current = SettingsManager.getData().getInt(player.getUniqueId().toString()+".selected-quest.amount");
                            SettingsManager.getData().set(player.getUniqueId().toString()+".selected-quest.amount", current+1);
                            SettingsManager.saveData();
                            QManager.testComplete(player, quest);
                        }
                    }