checking boolean pisses me off

Discussion in 'Spigot Plugin Development' started by Kompye, Dec 3, 2019.

  1. Code (Java):
        private void isConflicting(ItemStack item) {
            Map<Enchantment, Integer> enchants = item.getItemMeta().getEnchants();
            String whatToCheck;
            for (Enchantment ench : enchants.keySet()) {
                whatToCheck = ench.getKey().toString().toUpperCase().replace("MINECRAFT:", "");
                if(plugin.getConfig().getBoolean(whatToCheck) == true) {
                    System.out.println("abc");
                }
            }
        }
    I get "abc" even if the config boolean is false wtf
    Code (YAML):
    #Configuration file for Enchanting+

    #Features

    pumpkinNightVision
    : true
    showPowerStats
    : true

    #Messages

    reloadMessage
    : "&aEnchanting+ has been reloaded successfully!"
    noPermissionMessage
    : "&cYou do not have access to this command."
    disabledEnchantment
    : "&6This enchantment is currently disabled."
    nothingToEnchantMessage
    : "&cYou have nothing to enchant in your hand!"

    #Exclusive enchantments

    FIRE_ASPECT
    : true
    FROST_WALKER
    : false
    BANE_OF_ARTHROPODS
    : false
    BLAST_PROTECTION
    : false
    FIRE_PROTECTION
    : false
    PROJECTILE_PROTECTION
    : false
    PROTECTION
    : false
    THORNS
    : false
    DEPTH_STRIDER
    : false
    FEATHER_FALLING
    : false
    FROST_WALKER
    : false
    FLAME
    : false
    INFINITY
    : false
    POWER
    : false
    MULTISHOT
    : false
    PIERCING
    : false
    QUICK_CHARGE
    : false
    LUCK_OF_THE_SEA
    : false
    LURE
    : false
    AQUA_AFFINITY
    : false
    RESPIRATION
    : false
    MENDING
    : false
    DURABILITY
    : false
    EFFICIENCY
    : false
    FORTUNE
    : false
    LOOTING
    : false
    SILK_TOUCH
    : false
    CHANNELING
    : false
    IMPALING
    : false
    LOYALTY
    : false
    RIPTIDE
    : false
    KNOCKBACK
    : false
    SHARPNESS
    : false
    SMITE
    : false
    SWEEPING_EDGE
    : false

    doesTurtleShellBypassExclusivity
    : true
    isRespirationReservedForTurtleShell
    : false
    isAquaAffinityReservedForTurtleShell
    : false

    #Maximum enchantment levels
    #0 means enchantment disabled

    blastProtectionLevels
    : 4
    fireProtectionLevels
    : 4
    projectileProtectionLevels
    : 4
    protectionLevels
    : 4
    thornsLevels
    : 3
    depthStriderLevels
    : 3
    featherFallingLevels
    : 4
    frostWalkerLevels
    : 2
    flameLevels
    : 1
    infinityLevels
    : 1
    powerLevels
    : 5
    punchLevels
    : 2
    multishotLevels
    : 1
    piercingLevels
    : 4
    quickChargeLevels
    : 3
    luckOfTheSeaLevels
    : 3
    lureLevels
    : 3
    aquaAffinityLevels
    : 1
    respirationLevels
    : 3
    mendingLevels
    : 1
    unbreakingLevels
    : 3
    efficiencyLevels
    : 5
    fortuneLevels
    : 3
    lootingLevels
    : 3
    silkTouchLevels
    : 1
    channelingLevels
    : 1
    impalingLevels
    : 5
    loyaltyLevels
    : 3
    riptideLevels
    : 3
    baneOfArthropodsLevels
    : 5
    fireAspectLevels
    : 2
    knockbackLevels
    : 2
    sharpnessLevels
    : 5
    smiteLevels
    : 5
    sweepingEdgeLevels
    : 3

    #Cost

    levelRatio
    : 1
    lapisLazuliRatio
    : 1
     
     
  2. You don't have to put == true since is a bool. You just need
    if(plugin.getConfig().getBoolean(whatToCheck){}

    Can you send other methods ?
    I need it to see which string you are checking
    The first potion effect is set to true, that's probably the cause of the print
     
  3. That's the entire class
    Code (Java):
    package me.kompye.listeners;

    import java.util.Map;

    import org.bukkit.NamespacedKey;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;

    import me.kompye.EnchantingPlus;
    import me.kompye.utils.Utils;

    public class EnchantItemListener implements Listener {

        EnchantingPlus plugin;

        public EnchantItemListener(EnchantingPlus instanceOfMain) {
            this.plugin = instanceOfMain;
        }

        @EventHandler
        public void inventoryClickEvent(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();
            if (e.getCurrentItem().hasItemMeta()) {
                if (e.getCurrentItem().getItemMeta().hasDisplayName()) {
                    if (getFirstWord(e.getCurrentItem().getItemMeta().getDisplayName()).equals("Level")) {
                        e.setCancelled(true);
                        if (player.getInventory().getItemInMainHand() != null) {
                            NamespacedKey key = NamespacedKey.minecraft(e.getView().getTitle().toLowerCase().replace(" ", "_"));
                            Enchantment enchant = Enchantment.getByKey(key);
                            player.getInventory().getItemInMainHand().addUnsafeEnchantment(enchant,levelBook(e.getCurrentItem().getItemMeta().getDisplayName()));
                            isConflicting(player.getInventory().getItemInMainHand());
                            player.playSound(player.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 0.5F, 1);
                        } else {
                            player.sendMessage(Utils.chat(plugin.getConfig().getString("nothingToEnchantMessage")));
                            player.closeInventory();
                        }
                    }
                }
            }
        }

        private String getFirstWord(String text) {
            int index = text.indexOf(' ');
            if (index > -1) {
                return text.substring(0, index).trim();
            } else {
                return text;
            }
        }

        private int levelBook(String s) {
            for (int i = 1; i <= 27; i++) {
                if (s.equals("Level " + String.valueOf(i))) {
                    return i;
                }
            }
            return -1;
        }

        private void isConflicting(ItemStack item) {
            Map<Enchantment, Integer> enchants = item.getItemMeta().getEnchants();
            String whatToCheck;
            for (Enchantment ench : enchants.keySet()) {
                whatToCheck = ench.getKey().toString().toUpperCase().replace("MINECRAFT:", "");
                if(plugin.getConfig().getBoolean(whatToCheck)) {
                    System.out.println("abc");
                }
            }
        }

    }
     
    And yeah that’s set to true but I get true with all the checks
     
    #3 Kompye, Dec 3, 2019
    Last edited: Dec 3, 2019
  4. MrGeneralQ

    MrGeneralQ Previously qintinator

    so your code is always outputting "abc"?
     
  5. Exactly
     
  6. MrGeneralQ

    MrGeneralQ Previously qintinator

    Try moving this part inside the loop
    so it would look like this:

    Code (Java):
    for (Enchantment ench : enchants.keySet()) {
                String whatToCheck = ench.getKey().toString().toUpperCase().replace("MINECRAFT:", "");
     
  7. Debug what your whatToCheck string even holds at that point in time. Chances are the string has a typo.
     
  8. wtf here it works, it is an item with fire aspect, quick charge and bane of arthropods
    Code (Java):

        private void isConflicting(ItemStack item) {
            Map<Enchantment, Integer> enchants = item.getItemMeta().getEnchants();
            for (Enchantment ench : enchants.keySet()) {
                String whatToCheck = ench.getKey().toString().toUpperCase().replace("MINECRAFT:", "");
                System.out.println("whatToCheck is: " + whatToCheck);
                System.out.println("it's " + String.valueOf(plugin.getConfig().getBoolean(whatToCheck)));
            }
        }
     
    Code (Text):
    [13:48:23] [Server thread/INFO]: whatToCheck is: FIRE_ASPECT
    [13:48:23] [Server thread/INFO]: it's true
    [13:48:23] [Server thread/INFO]: whatToCheck is: QUICK_CHARGE
    [13:48:23] [Server thread/INFO]: it's false
    [13:48:23] [Server thread/INFO]: whatToCheck is: BANE_OF_ARTHROPODS
    [13:48:23] [Server thread/INFO]: it's false
     
  9. The configuration you sent above does have true for fire aspect and false for the others... you're sure you changed the config right?
     
    • Agree Agree x 1