2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Items;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.uEnchantment;
|
2011-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uNumber;
|
2011-12-20 21:39:45 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.uSign;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.Material;
|
2011-12-16 17:20:09 +01:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
import java.util.HashMap;
|
2011-12-16 17:20:09 +01:00
|
|
|
import java.util.Map;
|
2012-01-09 22:39:38 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2011-12-16 17:20:09 +01:00
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
2011-05-29 13:25:25 +02:00
|
|
|
* Manages ItemStack names and ID's
|
2011-05-15 18:16:25 +02:00
|
|
|
*/
|
2011-05-21 19:55:55 +02:00
|
|
|
public class Items {
|
2012-01-09 22:39:38 +01:00
|
|
|
private static final Pattern Durability = Pattern.compile(":(\\d)*");
|
|
|
|
private static final Pattern Enchant = Pattern.compile("-([0-9a-zA-Z])*");
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-07-15 21:45:26 +02:00
|
|
|
public static Material getMaterial(String itemName) {
|
|
|
|
if (uNumber.isInteger(itemName)) return Material.getMaterial(Integer.parseInt(itemName));
|
2011-08-13 12:08:34 +02:00
|
|
|
itemName = itemName.replace(" ", "_");
|
|
|
|
Material finalMaterial = Material.getMaterial(itemName.toUpperCase());
|
|
|
|
if (finalMaterial != null) return finalMaterial;
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
int length = 256;
|
2011-08-13 12:08:34 +02:00
|
|
|
itemName = itemName.toLowerCase().replace("_", "");
|
|
|
|
for (Material currentMaterial : Material.values()) {
|
|
|
|
String materialName = currentMaterial.name().toLowerCase().replace("_", "");
|
|
|
|
if (materialName.startsWith(itemName) && (materialName.length() < length)) {
|
|
|
|
length = materialName.length();
|
|
|
|
finalMaterial = currentMaterial;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
return finalMaterial;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
public static String getName(ItemStack is) {
|
2011-12-20 21:39:45 +01:00
|
|
|
return getName(is, true);
|
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
public static String getName(ItemStack is, boolean showData) {
|
2011-12-20 21:39:45 +01:00
|
|
|
String name = DataValue.getName(is);
|
|
|
|
return uSign.capitalizeFirst((name != null && showData ? name + '_' : "") + is.getType());
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
public static String getSignName(ItemStack is) {
|
|
|
|
return is.getType().name()
|
2012-04-19 16:13:42 +02:00
|
|
|
+ (is.getDurability() > 0 ? ":" + is.getDurability() : "")
|
2012-03-17 15:00:25 +01:00
|
|
|
+ (!is.getEnchantments().isEmpty() ? '-' + uEnchantment.encodeEnchantment(is.getEnchantments()) : "");
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static ItemStack getItemStack(String itemName) {
|
2011-08-13 12:08:34 +02:00
|
|
|
ItemStack toReturn = getFromOddItem(itemName);
|
|
|
|
if (toReturn != null) return toReturn;
|
2012-02-16 19:09:37 +01:00
|
|
|
|
|
|
|
if (itemName == null) itemName = "";
|
|
|
|
String[] split = itemName.split(":|-");
|
|
|
|
|
|
|
|
if (split.length == 0) return null;
|
|
|
|
String first = split[0];
|
2011-12-16 17:20:09 +01:00
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
String[] space = first.split(" ");
|
2012-03-01 22:03:59 +01:00
|
|
|
if (space.length == 0) return null;
|
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
Material material = getMaterial(first);
|
2011-12-16 17:20:09 +01:00
|
|
|
|
|
|
|
for (int i = (space.length > 1 ? 1 : 0); i >= 0 && material == null; i--) material = getMaterial(space[i]);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
if (material == null) return null;
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
toReturn = new ItemStack(material, 1);
|
2012-01-09 22:39:38 +01:00
|
|
|
toReturn = addEnchantments(toReturn, itemName);
|
|
|
|
toReturn = addDurability(toReturn, itemName);
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
short data = getDataFromWord(space[0], material);
|
|
|
|
if (data != 0) toReturn.setDurability(data);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
return toReturn;
|
|
|
|
}
|
2012-01-09 22:39:38 +01:00
|
|
|
|
|
|
|
private static ItemStack addDurability(ItemStack toReturn, String itemName) {
|
|
|
|
Matcher m = Durability.matcher(itemName);
|
|
|
|
if (!m.find()) return toReturn;
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
String data = m.group();
|
|
|
|
if (data == null || data.isEmpty()) return toReturn;
|
|
|
|
data = data.substring(1);
|
|
|
|
if (uNumber.isInteger(data)) toReturn.setDurability(Short.valueOf(data));
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2012-03-17 15:00:25 +01:00
|
|
|
private static Map<Enchantment, Integer> getEnchantment(String itemName) {
|
2011-12-16 17:20:09 +01:00
|
|
|
return uEnchantment.decodeEnchantment(itemName);
|
2011-07-15 21:45:26 +02:00
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
private static Map<Enchantment, Integer> getEnchant(String original) {
|
2012-01-09 22:39:38 +01:00
|
|
|
Matcher m = Enchant.matcher(original);
|
|
|
|
if (!m.find()) return new HashMap<Enchantment, Integer>();
|
|
|
|
String group = m.group().substring(1);
|
|
|
|
return getEnchantment(group);
|
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
private static ItemStack addEnchantments(ItemStack is, String itemname) {
|
|
|
|
try { is.addEnchantments(getEnchant(itemname));
|
2012-01-09 22:39:38 +01:00
|
|
|
} catch (Exception ignored) {}
|
|
|
|
return is;
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-07-15 21:45:26 +02:00
|
|
|
private static ItemStack getFromOddItem(String itemName) {
|
2011-08-13 12:08:34 +02:00
|
|
|
return !Odd.isInitialized() ? null : Odd.returnItemStack(itemName.replace(":", ";"));
|
2011-07-15 21:45:26 +02:00
|
|
|
}
|
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
private static short getDataFromWord(String name, Material material) {
|
|
|
|
return DataValue.get(name, material);
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2011-07-15 21:45:26 +02:00
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|