Get enchantments by name

Discussion in 'Plugin Development' started by willeb96, Mar 14, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    willeb96

    I don't know if there's any easier way of getting enchantments by their in-game names, but I just made a switch for all enchantments using this list. Feel free to use if you need it :)

    Code:
    public static Enchantment getEnchantment(String name) {
        switch (name.trim().toLowerCase()) {
            case "protection":
                return Enchantment.PROTECTION_ENVIRONMENTAL;
            case "fireprotection":
                return Enchantment.PROTECTION_FIRE;
            case "featherfalling":
                return Enchantment.PROTECTION_FALL;
            case "blastprotection":
                return Enchantment.PROTECTION_EXPLOSIONS;
            case "projectileprotection":
                return Enchantment.PROTECTION_PROJECTILE;
            case "respiration":
                return Enchantment.OXYGEN;
            case "aquaaffinity":
                return Enchantment.WATER_WORKER;
            case "thorns":
                return Enchantment.THORNS;
            case "unbreaking":
                return Enchantment.DURABILITY;
            case "sharpness":
                return Enchantment.DAMAGE_ALL;
            case "smite":
                return Enchantment.DAMAGE_UNDEAD;
            case "baneofarthropods":
                return Enchantment.DAMAGE_ARTHROPODS;
            case "knockback":
                return Enchantment.KNOCKBACK;
            case "fireaspect":
                return Enchantment.FIRE_ASPECT;
            case "looting":
                return Enchantment.LOOT_BONUS_MOBS;
            case "efficiency":
                return Enchantment.DIG_SPEED;
            case "silktouch":
                return Enchantment.SILK_TOUCH;
            case "fortune":
                return Enchantment.LOOT_BONUS_BLOCKS;
            case "power":
                return Enchantment.ARROW_DAMAGE;
            case "punch":
                return Enchantment.ARROW_KNOCKBACK;
            case "flame":
                return Enchantment.ARROW_FIRE;
            case "infinity":
                return Enchantment.ARROW_INFINITE;
            default:
                return null;
        }
    }
     
  2. Offline

    Zarius

    Better to loop through all enchantment values, then you automatically support all future enchantments too. I'll post my function here when I'm back on the computer.
     
  3. Offline

    Neodork

    Did you guys try Enchantment.getByName(); ?
     
  4. Offline

    Zarius

    Neodork - yes, but it wasn't flexible enough for my needs. I need to make sure my users can type the underscore or not and don't want to worry about case sensitivity. My function below could probably use a little cleaning up but works fine for me :)

    PHP:
        /** Takes a enchantment name by string and matches
        *  to an enchantment value using a little fuzzy
        *  matching (strip any space, underscore or dash
        *  and case doesn't matter)
        * 
        * @param enchString
        * @return
        */
        
    private static Enchantment getEnchantment(String enchString) {
            
    // Clean up string - make lowercase and strip space/dash/underscore
            
    enchString enchString.toLowerCase().replaceAll("[ _-]""");
     
            
    // Set up aliases (this could probably be done outside the function so
            // we only do it once (eg. in a support class init or read from a file)
            
    Map <StringStringaliases = new HashMap<StringString>();
            
    aliases.put("aspectfire""fireaspect");
            
    aliases.put("sharpness""damageall");
            
    aliases.put("smite""damageundead");
            
    aliases.put("punch""arrowknockback");
            
    aliases.put("looting""lootbonusmobs");
            
    aliases.put("fortune""lootbonusblocks");
            
    aliases.put("baneofarthropods""damageundead");
            
    aliases.put("power""arrowdamage");
            
    aliases.put("flame""arrowfire");
            
    aliases.put("infinity""arrowinfinite");
            
    aliases.put("unbreaking""durability");
            
    aliases.put("efficiency""digspeed");
            
    aliases.put("smite""damageundead");
     
            
    // If an alias exists, use it
            
    String alias aliases.get(enchString);
            if (
    alias != null)
                
    enchString alias;
     
            
    // Loop through all enchantments and match (case insensitive and ignoring space,
            // underscore and dashes
            
    for (Enchantment value Enchantment.values()) {
                if (
    enchString.equalsIgnoreCase(value.getName().replaceAll("[ _-]"""))) {
                    return 
    value;
                }
            }
           
            return 
    null// nothing found.
        
    }
    edit: lots of edits because code wouldn't format properly :\
     
    willeb96 likes this.
  5. Offline

    willeb96

    Cool function, might as well use yours instead of mine. It's really nice :)
     
  6. Offline

    TnT

    Moved to the correct forum.
     
Thread Status:
Not open for further replies.

Share This Page