2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Items;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
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;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-05-21 19:55:55 +02:00
|
|
|
|
2011-07-15 21:45:26 +02:00
|
|
|
Material material = getMaterial(itemName);
|
2011-07-23 21:00:47 +02:00
|
|
|
if (material != null) return new ItemStack(material, 1);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-07-15 21:45:26 +02:00
|
|
|
return getItemStackWithDataValue(itemName);
|
|
|
|
}
|
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-07-23 21:00:47 +02:00
|
|
|
private static ItemStack getItemStackWithDataValue(String itemName) {
|
|
|
|
if (!itemName.contains(":")) return getItemStackWithDataValueFromWord(itemName);
|
2011-07-15 21:45:26 +02:00
|
|
|
|
|
|
|
String[] word = itemName.split(":");
|
2011-07-23 21:00:47 +02:00
|
|
|
if (word.length < 2 || !uNumber.isInteger(word[1])) return null;
|
2011-07-15 21:45:26 +02:00
|
|
|
|
|
|
|
Material item = getMaterial(word[0]);
|
2011-07-23 21:00:47 +02:00
|
|
|
return item == null ? null : new ItemStack(item, 1, Short.parseShort(word[1]));
|
2011-07-15 21:45:26 +02:00
|
|
|
}
|
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
private static ItemStack getItemStackWithDataValueFromWord(String itemName) {
|
2011-08-13 12:08:34 +02:00
|
|
|
int indexOfChar = itemName.indexOf(' ');
|
2011-07-15 21:45:26 +02:00
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
if (indexOfChar == -1) return null;
|
2011-08-13 12:08:34 +02:00
|
|
|
Material item = getMaterial(itemName.substring(indexOfChar));
|
|
|
|
return item == null ? null : new ItemStack(item, 1, DataValue.get(itemName.substring(0, indexOfChar), item));
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2011-07-15 21:45:26 +02:00
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|