Reputation: 321
And how to add 2 enchantments at once in bukkit with
myItem.addEnchantments( Enchantment.KNOCKBACK, 1 /* TODO Here goes 2nd enchantment */ );
'addEnchantments' accepts 'addEnchantments(Map < Enchantment, int >)'
Upvotes: 0
Views: 1287
Reputation: 101
@John Smith's second question: (How to convert string to hashmap) You can convert hashmap to string but java (as I know) doesn't have string to hashmap. You can make a function that does this (might be impossible) or make a couple functions that convert hashmap to string and string to hashmap. In this case you want a hashmap with Enchantment and an Integer, so you would simply do something like this:
public /*static*/ String hashMapToString(HashMap<Enchantment, Integer> hashMap) {
StringBuilder serializedString = new StringBuilder();
for (Enchantment enchant : hashMap.keySet()) {
serializedString.append(enchant.getName() + "<>" + hashMap.get(enchant) + ";");
}
return serializedString.toString();
}
then you would create a function to convert that back to a hashmap:
public /*static*/ HashMap<Enchantment, Integer> stringToHashMap(String hashMapString) {
HashMap<Enchantment, Integer> hashMap = new HashMap<>();
for (String split : hashMapString.split(";")) {
String[] splited = split.split("<>");
hashMap.put(Enchantment.getByName(splited[0]), Integer.valueOf(splited[1]))
}
return hashMap;
}
You can even make them static (remove the comment marks and if you don't want it at all just remove what is inside the comment marks with the comment marks)
Upvotes: 0
Reputation: 11120
You rather use addEnchantment
twice (or more):
myItem.addEnchantment(Enchantment.KNOCKBACK, 1);
myItem.addEnchantment(Enchantment.THRONS, 2);
If you insist on using addEnchantments
you'll need to create a map, populate it and pass it:
Map<Enhancement, Integer> map = new HashMap<Enhancement, Integer>();
map.put(Enchantment.KNOCKBACK, 1);
map.put(Enchantment.THRONS, 2);
myItem.addEnchantments(map);
In your case, I would go with option 1
Upvotes: 2