ChestShop-3/com/Acrobot/ChestShop/Items/Items.java

65 lines
2.0 KiB
Java
Raw Normal View History

2011-05-15 19:33:03 +02:00
package com.Acrobot.ChestShop.Items;
import com.Acrobot.ChestShop.Utils.Numerical;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
/**
* @author Acrobot
2011-05-29 13:25:25 +02:00
* Manages ItemStack names and ID's
*/
public class Items {
2011-05-29 13:25:25 +02:00
public static String getItemName(ItemStack itemStack) {
return getItemName(itemStack.getType().name());
}
2011-05-29 13:25:25 +02:00
public static String getItemName(String itemName) {
return getMat(itemName).name();
}
2011-05-29 13:25:25 +02:00
public static Material getMat(String itemName) {
int length = 256;
Material finalMat = null;
2011-05-29 13:25:25 +02:00
itemName = itemName.toLowerCase().replace("_", "").replace(" ", "");
for (Material m : Material.values()) {
String matName = m.name().toLowerCase().replace("_", "").replace(" ", "");
if (matName.startsWith(itemName) && (matName.length() < length)) {
length = matName.length();
finalMat = m;
}
}
return finalMat;
}
2011-05-29 13:25:25 +02:00
public static int getItemID(String itemName) {
return getMat(itemName).getId();
}
2011-05-29 13:25:25 +02:00
public static ItemStack getItemStack(String itemName) {
if (Odd.isInitialized()) {
ItemStack odd = Odd.returnItemStack(itemName.replace(":", ";"));
2011-05-29 13:25:25 +02:00
if (odd != null) {
return odd;
}
}
String[] split = itemName.split(":");
itemName = split[0];
short dataValue = (short) (split.length > 1 && Numerical.isInteger(split[1]) ? Integer.parseInt(split[1]) : 0);
2011-05-29 13:25:25 +02:00
if (Numerical.isInteger(itemName)) {
Material mat = Material.getMaterial(Integer.parseInt(itemName));
return mat == null ? null : new ItemStack(mat, 1, dataValue);
}
2011-05-29 13:25:25 +02:00
Material mat = getMat(itemName);
return (mat != null ? new ItemStack(mat, 1, dataValue) : null);
}
2011-05-29 13:25:25 +02:00
public static Material getMaterial(String name) {
ItemStack is = getItemStack(name);
return is != null ? is.getType() : null;
}
}