EnchantmentUtil

Discussion in 'Resources' started by gomeow, Apr 20, 2013.

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

    gomeow

    So its a bit difficult to work with enchantments and user input because all the enum values are a different name than what is used in game. For example, feather falling is referred to as PROTECTION_FALL.

    Here is a utility class that will make it simpler to get enchantments from a string.

    What the getEnchantment(String) method does is it accepts user input, like "feather-falling" and replaces the hypen with an underscore and capitalizes it. It first tries to see if you entered the enum value, like "protection-fall" and if not, it will check against the hashmap to see if it is one of the ingame names. If so, it will return the enum enchantment.

    In your own code, do a null check against the return value. If its null, then there was user error. If not, then you have the enchantment ready.

    There is also a method, getEnchantments(), which returns a list of strings, with values like "feather-falling" and "fire-protection". You can do what you want with that list, like combine it into a string and send it to the player.

    Code:java
    1. package pack.pack;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7. import org.bukkit.enchantments.Enchantment;
    8.  
    9. public class ItemUtil {
    10.  
    11. private static HashMap<String, Enchantment> enchants = new HashMap<String, Enchantment>();
    12.  
    13. static {
    14. enchants.put("SHARPNESS", Enchantment.DAMAGE_ALL);
    15. enchants.put("POWER", Enchantment.ARROW_DAMAGE);
    16. enchants.put("FIRE_PROTECTION", Enchantment.PROTECTION_FIRE);
    17. enchants.put("FEATHER_FALLING", Enchantment.PROTECTION_FALL);
    18. enchants.put("PROTECTION", Enchantment.PROTECTION_ENVIRONMENTAL);
    19. enchants.put("BLAST_PROTECTION", Enchantment.PROTECTION_EXPLOSIONS);
    20. enchants.put("PROJECTILE_PROTECTION", Enchantment.PROTECTION_PROJECTILE);
    21. enchants.put("RESPIRATION", Enchantment.OXYGEN);
    22. enchants.put("INFINITY", Enchantment.ARROW_INFINITE);
    23. enchants.put("AQUA_AFFINITY", Enchantment.WATER_WORKER);
    24. enchants.put("UNBREAKING", Enchantment.DURABILITY);
    25. enchants.put("SMITE", Enchantment.DAMAGE_UNDEAD);
    26. enchants.put("BANE_OF_ANTHROPODS", Enchantment.DAMAGE_ARTHROPODS);
    27. enchants.put("EFFICIENCY", Enchantment.DIG_SPEED);
    28. enchants.put("FIRE_ASPECT", Enchantment.FIRE_ASPECT);
    29. enchants.put("SILK_TOUCH", Enchantment.SILK_TOUCH);
    30. enchants.put("FORTUNE", Enchantment.LOOT_BONUS_BLOCKS);
    31. enchants.put("LOOTING", Enchantment.LOOT_BONUS_MOBS);
    32. enchants.put("PUNCH", Enchantment.ARROW_KNOCKBACK);
    33. enchants.put("FLAME", Enchantment.ARROW_FIRE);
    34. }
    35.  
    36. public static Enchantment getEnchantment(String ench) {
    37. ench = ench.toUpperCase().replace('-', '_');
    38. Enchantment e = Enchantment.getByName(ench);
    39. if(e == null) {
    40. if(enchants.containsKey(ench)) {
    41. e = enchants.get(ench);
    42. } else {
    43. if(e == null) {
    44. return null;
    45. }
    46. }
    47. }
    48. return e;
    49. }
    50.  
    51. public static List<String> getEnchantments() {
    52. List<String> list = new ArrayList<String>();
    53. for(String key:enchants.keySet()) {
    54. list.add(key.toLowerCase().replace('_', '-'));
    55. }
    56. return list;
    57. }
    58.  
    59. }
     
    chasechocolate likes this.
  2. Offline

    chasechocolate

    Thanks for this! I ended up making something like this a while back in one of my plugins because it got really annoying ._.
     
    gomeow likes this.
  3. Offline

    macguy8

    In the else after you check if enchants.containsKey, is there any reason why you're using == null again, after you already use it higher up in your code?
     
  4. Offline

    gomeow

    macguy8
    I had some more code after that in my own application, but had forgotten to remove it for this, I'll fix that
     
Thread Status:
Not open for further replies.

Share This Page