mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-09-29 15:07:40 +02:00
Improve MaterialData handling
- Adds ability to shorten MaterialData type strings - Removes Banner color string in name
This commit is contained in:
parent
dd44217132
commit
679000de15
@ -9,6 +9,7 @@ import info.somethingodd.OddItem.OddItem;
|
|||||||
import org.bukkit.CoalType;
|
import org.bukkit.CoalType;
|
||||||
import org.bukkit.DyeColor;
|
import org.bukkit.DyeColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.SandstoneType;
|
||||||
import org.bukkit.TreeSpecies;
|
import org.bukkit.TreeSpecies;
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -109,35 +110,10 @@ public class MaterialUtil {
|
|||||||
return material;
|
return material;
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] nameParts = name.toUpperCase().split(" |_");
|
material = new EnumParser<Material>().parse(name, Material.values());
|
||||||
|
if (material != null) {
|
||||||
short length = Short.MAX_VALUE;
|
|
||||||
|
|
||||||
for (Material currentMaterial : Material.values()) {
|
|
||||||
String matName = currentMaterial.toString();
|
|
||||||
|
|
||||||
if (matName.length() < length && matName.startsWith(formatted)) {
|
|
||||||
length = (short) matName.length();
|
|
||||||
material = currentMaterial;
|
|
||||||
} else if (nameParts.length > 1) {
|
|
||||||
String[] matParts = matName.split("_");
|
|
||||||
if (nameParts.length == matParts.length) {
|
|
||||||
boolean matched = true;
|
|
||||||
for (int i = 0; i < matParts.length; i++) {
|
|
||||||
if (!matParts[i].startsWith(nameParts[i])) {
|
|
||||||
matched = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (matched) {
|
|
||||||
material = currentMaterial;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MATERIAL_CACHE.put(formatted, material);
|
MATERIAL_CACHE.put(formatted, material);
|
||||||
|
}
|
||||||
|
|
||||||
return material;
|
return material;
|
||||||
}
|
}
|
||||||
@ -290,7 +266,7 @@ public class MaterialUtil {
|
|||||||
|
|
||||||
Material material = getMaterial(split[0]);
|
Material material = getMaterial(split[0]);
|
||||||
short durability = getDurability(itemName);
|
short durability = getDurability(itemName);
|
||||||
byte data = -1;
|
MaterialData data = null;
|
||||||
|
|
||||||
if (material == null) {
|
if (material == null) {
|
||||||
if (!split[0].contains(" ")) {
|
if (!split[0].contains(" ")) {
|
||||||
@ -301,9 +277,7 @@ public class MaterialUtil {
|
|||||||
material = getMaterial(split[0].substring(index + 1));
|
material = getMaterial(split[0].substring(index + 1));
|
||||||
|
|
||||||
if (material != null) {
|
if (material != null) {
|
||||||
if (durability == 0) {
|
data = DataValue.getData(split[0].substring(0, index), material);
|
||||||
durability = data = DataValue.get(split[0].substring(0, index), material);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -315,10 +289,14 @@ public class MaterialUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
itemStack = new ItemStack(material);
|
itemStack = new ItemStack(material);
|
||||||
itemStack.setDurability(durability);
|
if (data == null && durability > 0 && material.getMaxDurability() == 0) {
|
||||||
if (data > -1) {
|
data = material.getNewData((byte) durability);
|
||||||
itemStack.getData().setData(data);
|
|
||||||
}
|
}
|
||||||
|
if (data != null) {
|
||||||
|
itemStack.setData(data);
|
||||||
|
durability = data.getData();
|
||||||
|
}
|
||||||
|
itemStack.setDurability(durability);
|
||||||
|
|
||||||
ItemMeta meta = getMetadata(itemName);
|
ItemMeta meta = getMetadata(itemName);
|
||||||
|
|
||||||
@ -378,61 +356,64 @@ public class MaterialUtil {
|
|||||||
* @param type Data Value string
|
* @param type Data Value string
|
||||||
* @param material Material
|
* @param material Material
|
||||||
* @return data value
|
* @return data value
|
||||||
|
* @deprecated Use {@link #getData}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static byte get(String type, Material material) {
|
public static byte get(String type, Material material) {
|
||||||
if (material == null || material.getData() == null) {
|
if (material == null || material.getData() == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
MaterialData data = getData(type, material);
|
||||||
|
return data != null ? data.getData() : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the dat from a string
|
||||||
|
*
|
||||||
|
* @param type Data Value string
|
||||||
|
* @param material Material
|
||||||
|
* @return data
|
||||||
|
*/
|
||||||
|
public static MaterialData getData(String type, Material material) {
|
||||||
|
|
||||||
type = type.toUpperCase().replace(" ", "_");
|
type = type.toUpperCase().replace(" ", "_");
|
||||||
|
|
||||||
MaterialData materialData = material.getNewData((byte) 0);
|
MaterialData materialData = new ItemStack(material).getData();
|
||||||
|
|
||||||
if (materialData instanceof TexturedMaterial) {
|
if (materialData instanceof TexturedMaterial) {
|
||||||
TexturedMaterial texturedMaterial = (TexturedMaterial) materialData;
|
TexturedMaterial texturedMaterial = (TexturedMaterial) materialData;
|
||||||
|
Material texture = new EnumParser<Material>().parse(type, texturedMaterial.getTextures().toArray(new Material[0]));
|
||||||
for (Material mat : texturedMaterial.getTextures()) {
|
if (texture != null) {
|
||||||
if (mat.name().startsWith(type) && !mat.equals(material)) {
|
((TexturedMaterial) materialData).setMaterial(texture);
|
||||||
return (byte) texturedMaterial.getTextures().indexOf(mat);
|
|
||||||
}
|
}
|
||||||
|
} else if (materialData instanceof Colorable) {
|
||||||
|
DyeColor color = new EnumParser<DyeColor>().parse(type, DyeColor.values());
|
||||||
|
if (color != null) {
|
||||||
|
((Colorable) materialData).setColor(color);
|
||||||
}
|
}
|
||||||
} else if (materialData instanceof Colorable || material == Material.BANNER) { // Banners might not use the Banner MaterialData...
|
} else if (materialData instanceof Wood) {
|
||||||
DyeColor color;
|
TreeSpecies species = new EnumParser<TreeSpecies>().parse(type, TreeSpecies.values());
|
||||||
|
if (species != null) {
|
||||||
try {
|
((Wood) materialData).setSpecies(species);
|
||||||
color = DyeColor.valueOf(type);
|
|
||||||
} catch (IllegalArgumentException exception) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (material == Material.INK_SACK || material == Material.BANNER) {
|
|
||||||
return color.getDyeData();
|
|
||||||
}
|
|
||||||
|
|
||||||
return color.getWoolData();
|
|
||||||
} else if (materialData instanceof Tree) {
|
|
||||||
try {
|
|
||||||
return TreeSpecies.valueOf(type).getData();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
} else if (materialData instanceof SpawnEgg) {
|
} else if (materialData instanceof SpawnEgg) {
|
||||||
try {
|
EntityType entityType = new EnumParser<EntityType>().parse(type, EntityType.values());
|
||||||
EntityType entityType = EntityType.valueOf(type);
|
if (entityType != null) {
|
||||||
|
((SpawnEgg) materialData).setSpawnedType(entityType);
|
||||||
return (byte) entityType.getTypeId();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
} else if (materialData instanceof Coal) {
|
} else if (materialData instanceof Coal) {
|
||||||
try {
|
CoalType coalType = new EnumParser<CoalType>().parse(type, CoalType.values());
|
||||||
return CoalType.valueOf(type).getData();
|
if (coalType != null) {
|
||||||
} catch (IllegalArgumentException ex) {
|
((Coal) materialData).setType(coalType);
|
||||||
return 0;
|
}
|
||||||
|
} else if (materialData instanceof Sandstone) {
|
||||||
|
SandstoneType sandstoneType = new EnumParser<SandstoneType>().parse(type, SandstoneType.values());
|
||||||
|
if (sandstoneType != null) {
|
||||||
|
((Sandstone) materialData).setType(sandstoneType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return materialData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -448,20 +429,15 @@ public class MaterialUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Banner do not return the Banner MaterialData? Wat?!?
|
|
||||||
if (data.getItemType() == Material.BANNER && data.getData() < 16) {
|
|
||||||
return DyeColor.getByDyeData(data.getData()).name();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data instanceof TexturedMaterial) {
|
if (data instanceof TexturedMaterial) {
|
||||||
return ((TexturedMaterial) data).getMaterial().name();
|
return ((TexturedMaterial) data).getMaterial().name();
|
||||||
} else if (data instanceof Colorable) {
|
} else if (data instanceof Colorable) {
|
||||||
DyeColor color = ((Colorable) data).getColor();
|
DyeColor color = ((Colorable) data).getColor();
|
||||||
|
|
||||||
return (color != null ? color.name() : null);
|
return (color != null ? color.name() : null);
|
||||||
} else if (data instanceof Tree) {
|
} else if (data instanceof Wood) {
|
||||||
//TreeSpecies specie = TreeSpecies.getByData((byte) (data.getData() & 3)); //This works, but not as intended
|
//TreeSpecies specie = TreeSpecies.getByData((byte) (data.getData() & 3)); //This works, but not as intended
|
||||||
TreeSpecies specie = ((Tree) data).getSpecies();
|
TreeSpecies specie = ((Wood) data).getSpecies();
|
||||||
return (specie != null && specie != TreeSpecies.GENERIC ? specie.name() : null);
|
return (specie != null && specie != TreeSpecies.GENERIC ? specie.name() : null);
|
||||||
} else if (data instanceof SpawnEgg) {
|
} else if (data instanceof SpawnEgg) {
|
||||||
EntityType type = ((SpawnEgg) data).getSpawnedType();
|
EntityType type = ((SpawnEgg) data).getSpawnedType();
|
||||||
@ -469,12 +445,52 @@ public class MaterialUtil {
|
|||||||
} else if (data instanceof Coal) {
|
} else if (data instanceof Coal) {
|
||||||
CoalType coal = ((Coal) data).getType();
|
CoalType coal = ((Coal) data).getType();
|
||||||
return (coal != null && coal != CoalType.COAL ? coal.name() : null);
|
return (coal != null && coal != CoalType.COAL ? coal.name() : null);
|
||||||
|
} else if (data instanceof Sandstone) {
|
||||||
|
SandstoneType type = ((Sandstone) data).getType();
|
||||||
|
return (type != null && type != SandstoneType.CRACKED ? type.name() : null);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class EnumParser<E extends Enum<E>> {
|
||||||
|
private E parse(String name, E[] values) {
|
||||||
|
name = name.toUpperCase();
|
||||||
|
|
||||||
|
try {
|
||||||
|
return E.valueOf(values[0].getDeclaringClass(), name);
|
||||||
|
} catch (IllegalArgumentException exception) {
|
||||||
|
E currentEnum = null;
|
||||||
|
String[] typeParts = name.split("[ _]");
|
||||||
|
int length = Short.MAX_VALUE;
|
||||||
|
for (E e : values) {
|
||||||
|
String enumName = e.name();
|
||||||
|
if (enumName.length() < length && enumName.startsWith(name)) {
|
||||||
|
length = (short) enumName.length();
|
||||||
|
currentEnum = e;
|
||||||
|
} else if (typeParts.length > 1) {
|
||||||
|
String[] nameParts = enumName.split("_");
|
||||||
|
if (typeParts.length == nameParts.length) {
|
||||||
|
boolean matched = true;
|
||||||
|
for (int i = 0; i < nameParts.length; i++) {
|
||||||
|
if (!nameParts[i].startsWith(typeParts[i])) {
|
||||||
|
matched = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (matched) {
|
||||||
|
currentEnum = e;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentEnum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class Metadata {
|
public static class Metadata {
|
||||||
/**
|
/**
|
||||||
* Returns the ItemMeta represented by this code
|
* Returns the ItemMeta represented by this code
|
||||||
|
Loading…
Reference in New Issue
Block a user