use of com.laytonsmith.abstraction.blocks.MCMaterial in project CommandHelper by EngineHub.
the class ObjectGenerator method item.
/**
* Gets an MCItemStack from a given item "object". Supports both the old and new formats currently
*
* @param i
* @param t
* @return An abstract item stack
*/
public MCItemStack item(Construct i, Target t) {
if (i instanceof CNull) {
return EmptyItem();
}
if (!(i instanceof CArray)) {
throw new CREFormatException("Expected an array!", t);
}
CArray item = (CArray) i;
MCMaterial mat;
int data = 0;
int qty = 1;
if (item.containsKey("qty")) {
qty = Static.getInt32(item.get("qty", t), t);
if (qty <= 0) {
return EmptyItem();
}
}
if (item.containsKey("name")) {
mat = StaticLayer.GetConvertor().GetMaterial(item.get("name", t).val());
} else if (item.containsKey("type")) {
Construct type = item.get("type", t);
if (type instanceof CString) {
int seperatorIndex = type.val().indexOf(':');
if (seperatorIndex != -1) {
try {
data = Integer.parseInt(type.val().substring(seperatorIndex + 1));
} catch (NumberFormatException e) {
throw new CRERangeException("The item data \"" + type.val().substring(seperatorIndex + 1) + "\" is not a valid integer.", t);
}
type = new CString(type.val().substring(0, seperatorIndex), t);
}
}
mat = StaticLayer.GetConvertor().getMaterial(Static.getInt32(type, t));
} else {
throw new CREFormatException("Could not find item name!", t);
}
if (mat == null) {
throw new CRENotFoundException("A material could not be found based on the given name.", t);
}
if (mat.getType() == 0) {
return EmptyItem();
}
if (item.containsKey("data")) {
data = Static.getInt32(item.get("data", t), t);
}
MCItemMeta meta = null;
if (item.containsKey("meta")) {
meta = itemMeta(item.get("meta", t), mat, t);
}
// Create itemstack
MCItemStack ret = StaticLayer.GetItemStack(mat, data, qty);
if (meta != null) {
ret.setItemMeta(meta);
}
// Fallback to enchants in item array if not in meta
if (item.containsKey("enchants")) {
try {
Map<MCEnchantment, Integer> enchants = enchants((CArray) item.get("enchants", t), t);
for (Map.Entry<MCEnchantment, Integer> entry : enchants.entrySet()) {
ret.addUnsafeEnchantment(entry.getKey(), entry.getValue());
}
} catch (ClassCastException ex) {
throw new CREFormatException("Enchants must be an array of enchantment arrays.", t);
}
}
return ret;
}
use of com.laytonsmith.abstraction.blocks.MCMaterial in project CommandHelper by EngineHub.
the class ObjectGenerator method item.
/**
* An Item Object consists of data about a particular item stack. Information included is: recipeType, data, qty,
* and an array of enchantment objects (labeled enchants): erecipeType (enchantment recipeType) and elevel
* (enchantment level). For backwards compatibility, this information is also listed in numerical slots as well as
* associative slots. If the MCItemStack is null, or the underlying item is nonexistant (or air) CNull is returned.
*
* @param is
* @return An item array or CNull
*/
public Construct item(MCItemStack is, Target t) {
if (is == null || is.getAmount() == 0 || is.getTypeId() == 0) {
return CNull.NULL;
}
MCMaterial mat = is.getType();
CArray ret = CArray.GetAssociativeArray(t);
ret.set("name", new CString(mat.getName(), t), t);
ret.set("type", new CInt(mat.getType(), t), t);
ret.set("data", new CInt(is.getDurability(), t), t);
ret.set("qty", new CInt(is.getAmount(), t), t);
ret.set("enchants", enchants(is.getEnchantments(), t), t);
ret.set("meta", itemMeta(is, t), t);
return ret;
}
use of com.laytonsmith.abstraction.blocks.MCMaterial in project CommandHelper by EngineHub.
the class BukkitMCItemFactory method isApplicable.
@Override
public boolean isApplicable(MCItemMeta meta, MCMaterial material) {
ItemMeta bmeta = ((BukkitMCItemMeta) meta).asItemMeta();
Material bmat = (Material) material.getHandle();
return f.isApplicable(bmeta, bmat);
}
use of com.laytonsmith.abstraction.blocks.MCMaterial in project CommandHelper by EngineHub.
the class BukkitMCItemFactory method asMetaFor.
@Override
public MCItemMeta asMetaFor(MCItemMeta meta, MCMaterial material) {
ItemMeta bmeta = ((BukkitMCItemMeta) meta).asItemMeta();
Material bmat = (Material) material.getHandle();
return BukkitConvertor.BukkitGetCorrectMeta(f.asMetaFor(bmeta, bmat));
}
Aggregations