2011-06-13 00:59:10 +02:00
|
|
|
package com.Acrobot.ChestShop.Items;
|
|
|
|
|
|
|
|
import org.bukkit.CoalType;
|
|
|
|
import org.bukkit.DyeColor;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.TreeSpecies;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.entity.EntityType;
|
2011-12-20 21:39:45 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-06-13 00:59:10 +02:00
|
|
|
import org.bukkit.material.*;
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
public class DataValue {
|
2012-05-10 16:32:25 +02:00
|
|
|
/**
|
|
|
|
* Gets the data value from a string
|
|
|
|
*
|
|
|
|
* @param type Data Value string
|
|
|
|
* @param material Material
|
|
|
|
* @return data value
|
|
|
|
*/
|
2011-08-13 12:08:34 +02:00
|
|
|
public static byte get(String type, Material material) {
|
2012-05-10 16:32:25 +02:00
|
|
|
if (material == null || material.getData() == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-02 20:34:14 +02:00
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
type = type.toUpperCase().replace(" ", "_");
|
2011-06-13 00:59:10 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
MaterialData materialData = material.getNewData((byte) 0);
|
2011-06-13 00:59:10 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (materialData instanceof TexturedMaterial) {
|
|
|
|
TexturedMaterial texturedMaterial = (TexturedMaterial) materialData;
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
for (Material mat : texturedMaterial.getTextures()) {
|
|
|
|
if (mat.name().startsWith(type)) {
|
|
|
|
return (byte) texturedMaterial.getTextures().indexOf(mat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (materialData instanceof Colorable) {
|
|
|
|
DyeColor color;
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
try {
|
|
|
|
color = DyeColor.valueOf(type);
|
|
|
|
} catch (IllegalArgumentException exception) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-20 21:39:45 +01:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (material == Material.INK_SACK) {
|
|
|
|
color = DyeColor.getByData((byte) (15 - color.getData()));
|
|
|
|
}
|
2011-12-20 21:39:45 +01:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
return color.getData();
|
|
|
|
} else if (materialData instanceof Tree) {
|
|
|
|
try {
|
|
|
|
return TreeSpecies.valueOf(type).getData();
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
return 0;
|
2011-12-20 21:39:45 +01:00
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
} else if (materialData instanceof SpawnEgg) {
|
|
|
|
try {
|
|
|
|
EntityType entityType = EntityType.valueOf(type);
|
|
|
|
|
|
|
|
return (byte) entityType.getTypeId();
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (materialData instanceof Coal) {
|
|
|
|
try {
|
|
|
|
return CoalType.valueOf(type).getData();
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string with the DataValue
|
|
|
|
*
|
|
|
|
* @param itemStack ItemStack to describe
|
|
|
|
* @return Data value string
|
|
|
|
*/
|
|
|
|
public static String name(ItemStack itemStack) {
|
|
|
|
MaterialData data = itemStack.getData();
|
|
|
|
|
|
|
|
if (data == null) {
|
2012-03-17 15:00:25 +01:00
|
|
|
return null;
|
|
|
|
}
|
2011-12-20 21:39:45 +01:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (data instanceof TexturedMaterial) {
|
|
|
|
return ((TexturedMaterial) data).getMaterial().name();
|
|
|
|
} else if (data instanceof Colorable) {
|
|
|
|
return ((Colorable) data).getColor().name();
|
|
|
|
} else if (data instanceof Tree) {
|
|
|
|
//TreeSpecies specie = TreeSpecies.getByData((byte) (data.getData() & 3)); //This works, but not as intended
|
|
|
|
TreeSpecies specie = ((Tree) data).getSpecies();
|
|
|
|
return (specie != null && specie != TreeSpecies.GENERIC ? specie.name() : null);
|
|
|
|
} else if (data instanceof SpawnEgg) {
|
|
|
|
EntityType type = ((SpawnEgg) data).getSpawnedType();
|
|
|
|
return (type != null ? type.name() : null);
|
|
|
|
} else if (data instanceof Coal) {
|
|
|
|
CoalType coal = ((Coal) data).getType();
|
|
|
|
return (coal != null && coal != CoalType.COAL ? coal.name() : null);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-20 21:39:45 +01:00
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|