Compare arg to enchantment

Discussion in 'Spigot Plugin Development' started by TheDiamondWorm, May 1, 2015.

  1. Hello, I want to check if the first arguement for a command is a valid enchantment from here: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/enchantments/Enchantment.html

    Here's how:
    Code (Java):
                    if (!args[0].equalsIgnoreCase(Enchantment.getByName(args[0])
                            .toString())) {
                        p.sendMessage("invalid");
                        return true;
                    }
    But, this sends the invalid message even if args[0] is DAMAGE_ALL for example. What's the best way to do this, I would rather not make the code messy by checking for every enchantment name manually in the same if statement.
     
  2. gigosaurus

    Supporter

    Code (Java):
    Enchantment.getByName(args[0]).toString()
    will cause a NPE if there is no enchantment with the name args[0], otherwise it will return a string with the format "Enchantment[id, name]".
    Because args[0] will not be of the form "Enchantment[id, name]",
    Code (Java):
    args[0].equalsIgnoreCase(Enchantment.getByName(args[0]).toString())
    will always return false.

    What you want is this:
    Code (Java):
    if (Enchantment.getByName(args[0]) != null) {
        //valid enchantment
    }
     
    #2 gigosaurus, May 1, 2015
    Last edited: May 2, 2015
    • Winner Winner x 1
  3. Thanks, just had to put a toUpperCase() after the arg as case-sensitive is irritating. Works perfectly :)
     
  4. Ummm, what happens with Protection? Enchantment.PROTECTION does not exist, it's DAMAGE_PROTECTION neither does POWER, it's called ARROW_DAMAGE?

    Just saying... :/
     
  5. make them type protection and replace that arg with the protection enum or whatever it is.
     
  6. Why does that matter?
     
  7. People might just wonder why their Power-enchantment doesn't work...
     
    • Informative Informative x 1