Created
April 12, 2014 22:59
-
-
Save codebucketdev/10561079 to your computer and use it in GitHub Desktop.
Usefull Enchanting snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum EnchantmentType | |
{ | |
TOOL(Arrays.asList(32, 33, 34, 35)), | |
ARMOR(Arrays.asList(0, 1, 2, 3, 4, 7, 34)), | |
SWORD(Arrays.asList(16, 17, 18, 19, 20, 34)), | |
ARROW(Arrays.asList(48, 49, 50, 51, 34)); | |
private List<Integer> enchantments; | |
EnchantmentType(List<Integer> enchantments) | |
{ | |
this.enchantments = enchantments; | |
} | |
@SuppressWarnings("deprecation") | |
public List<Enchantment> getEnchantments() | |
{ | |
List<Enchantment> list = new ArrayList<>(); | |
for(Integer id : enchantments) | |
{ | |
list.add(Enchantment.getById(id)); | |
} | |
return list; | |
} | |
public Map<Enchantment, Integer> getEnchantments(int level) | |
{ | |
Map<Enchantment, Integer> list = new HashMap<>(); | |
for(Enchantment e : getEnchantments()) | |
{ | |
int lvl = e.getMaxLevel(); | |
if(level <= lvl) lvl = level; | |
list.put(e, lvl); | |
} | |
return list; | |
} | |
public void addEnchantments(ItemStack item) | |
{ | |
ItemMeta meta = item.getItemMeta(); | |
for(Enchantment e : getEnchantments()) | |
{ | |
meta.addEnchant(e, e.getMaxLevel(), true); | |
} | |
item.setItemMeta(meta); | |
} | |
public void addEnchantments(ItemStack item, int level) | |
{ | |
ItemMeta meta = item.getItemMeta(); | |
Map<Enchantment, Integer> list = getEnchantments(level); | |
for(Enchantment e : list.keySet()) | |
{ | |
int lvl = list.get(e); | |
meta.addEnchant(e, lvl, true); | |
} | |
item.setItemMeta(meta); | |
} | |
public void addEnchantment(ItemStack item, Enchantment enchantment, int level) | |
{ | |
ItemMeta meta = item.getItemMeta(); | |
Enchantment e = enchantment; | |
int lvl = e.getMaxLevel(); | |
if(level <= lvl) | |
lvl = level; | |
meta.addEnchant(e, lvl, true); | |
item.setItemMeta(meta); | |
} | |
public boolean canEnchant(ItemStack item) | |
{ | |
return ItemType.valueOf(this.toString()).contains(item); | |
} | |
public static EnchantmentType getByItem(ItemStack item) | |
{ | |
for(EnchantmentType type : EnchantmentType.values()) | |
{ | |
if(type.canEnchant(item)) | |
{ | |
return type; | |
} | |
} | |
return null; | |
} | |
} | |
public enum ItemType | |
{ | |
TOOL(Arrays.asList(256, 257, 258, 269, 270, 271, 273, 274, 275, 277, | |
278, 279, 284, 285, 286, 290, 291, 292, 293, 346, 359, 259)), | |
ARMOR(Arrays.asList(298, 299, 300, 301, 302, 303, 304, 305, 306, 307, | |
308, 309, 310, 311, 312, 313, 314, 315, 316, 317)), | |
SWORD(Arrays.asList(267, 268, 272, 276, 283)), | |
ARROW(Arrays.asList(261)); | |
private List<Integer> items; | |
ItemType(List<Integer> items) | |
{ | |
this.items = items; | |
} | |
@SuppressWarnings("deprecation") | |
public List<ItemStack> getItems() | |
{ | |
List<ItemStack> list = new ArrayList<>(); | |
for(Integer id : items) | |
{ | |
list.add(new ItemStack(id)); | |
} | |
return list; | |
} | |
public List<Material> getMaterials() | |
{ | |
List<Material> list = new ArrayList<>(); | |
for(ItemStack item : getItems()) | |
{ | |
list.add(item.getType()); | |
} | |
return list; | |
} | |
public boolean contains(ItemStack item) | |
{ | |
for(Material m : getMaterials()) | |
{ | |
if(m == item.getType()) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
public static ItemType getByItem(ItemStack item) | |
{ | |
for(ItemType type : ItemType.values()) | |
{ | |
if(type.contains(item)) | |
{ | |
return type; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment