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-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;
|
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
|
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;
|
2011-12-16 17:20:09 +01:00
|
|
|
|
|
|
|
String[] split = itemName.split(":|-");
|
|
|
|
|
|
|
|
String[] space = split[0].split(" ");
|
|
|
|
Material material = getMaterial(split[0]);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
toReturn = new ItemStack(material, 1);
|
|
|
|
|
|
|
|
for (int i = 1; i < split.length; i++){
|
|
|
|
split[i] = split[i].trim();
|
|
|
|
if (uNumber.isInteger(split[i])) toReturn.setDurability((short) Integer.parseInt(split[i]));
|
|
|
|
else {
|
|
|
|
try{ toReturn.addEnchantments(getEnchantment(split[i]));
|
|
|
|
} catch (Exception ignored){}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Map<Enchantment, Integer> getEnchantment(String itemName){
|
|
|
|
return uEnchantment.decodeEnchantment(itemName);
|
2011-07-15 21:45:26 +02:00
|
|
|
}
|
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
|
|
|
}
|