0

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 >)'

John Smith
  • 321
  • 1
  • 3
  • 10

2 Answers2

2

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

Aviram Segal
  • 10,962
  • 3
  • 39
  • 52
  • Great, thanks! By the way do you know how to convert string to HashMap? ;-) I want to store enchantments in DB – John Smith Dec 22 '12 at 12:59
  • There isn't really anything built in Java to do that – Aviram Segal Dec 22 '12 at 13:00
  • I guess I will do loop to work with exploded string to add it to new HashMap ;-) – John Smith Dec 22 '12 at 13:21
  • Would definitely not advise storing split strings all over the database and constructing a `HashMap` at runtime. Instead have a table for player data (ie `players`) and have another table dedicated to enchantment data, you can then use a foreign key to reference the `id` or `username` field on the `players` table from inside of the table used for enchantments. You could then query what enchantments a player has by using `SELECT enchantment_id, enchantment_level FROM enchantments WHERE player_id = 5`. – Connor Spencer Harries Jan 04 '16 at 17:33
0

@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)

PhantomUnicorns
  • 101
  • 1
  • 2