2011-12-16 17:20:09 +01:00
|
|
|
package com.Acrobot.ChestShop.Utils;
|
|
|
|
|
|
|
|
import org.bukkit.enchantments.Enchantment;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class uEnchantment {
|
2012-03-17 15:00:25 +01:00
|
|
|
public static String getEnchantment(ItemStack item) {
|
2011-12-16 17:20:09 +01:00
|
|
|
return encodeEnchantment(item.getEnchantments());
|
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
public static String encodeEnchantment(Map<Enchantment, Integer> map) {
|
2011-12-16 17:20:09 +01:00
|
|
|
int integer = 0;
|
2012-03-17 15:00:25 +01:00
|
|
|
for (Map.Entry<Enchantment, Integer> entry : map.entrySet()) {
|
2011-12-16 17:20:09 +01:00
|
|
|
integer = integer * 1000 + (entry.getKey().getId()) * 10 + entry.getValue();
|
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
if (integer == 0) return null;
|
|
|
|
|
|
|
|
return Integer.toString(integer, 32);
|
2011-12-16 17:20:09 +01:00
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
public static Map<Enchantment, Integer> decodeEnchantment(String base32) {
|
2011-12-16 17:20:09 +01:00
|
|
|
if (base32 == null) return new HashMap<Enchantment, Integer>();
|
|
|
|
Map<Enchantment, Integer> map = new HashMap<Enchantment, Integer>();
|
|
|
|
|
|
|
|
String integer = String.valueOf(Integer.parseInt(base32, 32));
|
2012-03-17 15:00:25 +01:00
|
|
|
if (integer.length() < 3) integer = '0' + integer;
|
2011-12-16 17:20:09 +01:00
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
for (int i = 0; i < integer.length() / 3; i++) {
|
2011-12-16 17:20:09 +01:00
|
|
|
String item = integer.substring(i * 3, i * 3 + 3);
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
Enchantment ench = Enchantment.getById(Integer.parseInt(item.substring(0, 2)));
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
if (ench == null) continue;
|
|
|
|
int level = Integer.parseInt(item.substring(2));
|
|
|
|
if (ench.getMaxLevel() < level || level < ench.getStartLevel()) continue;
|
|
|
|
map.put(ench, level);
|
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
}
|