use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.
the class ObjectGenerator method recipe.
public Construct recipe(MCRecipe r, Target t) {
if (r == null) {
return CNull.NULL;
}
CArray ret = CArray.GetAssociativeArray(t);
ret.set("type", new CString(r.getRecipeType().name(), t), t);
ret.set("result", item(r.getResult(), t), t);
if (r instanceof MCFurnaceRecipe) {
ret.set("input", item(((MCFurnaceRecipe) r).getInput(), t), t);
} else if (r instanceof MCShapelessRecipe) {
MCShapelessRecipe shapeless = (MCShapelessRecipe) r;
CArray il = new CArray(t);
for (MCItemStack i : shapeless.getIngredients()) {
il.push(item(i, t), t);
}
ret.set("ingredients", il, t);
if (shapeless.getKey() != null) {
ret.set("key", shapeless.getKey(), t);
}
} else if (r instanceof MCShapedRecipe) {
MCShapedRecipe shaped = (MCShapedRecipe) r;
CArray shape = new CArray(t);
for (String line : shaped.getShape()) {
shape.push(new CString(line, t), t);
}
CArray imap = CArray.GetAssociativeArray(t);
for (Map.Entry<Character, MCItemStack> entry : shaped.getIngredientMap().entrySet()) {
imap.set(entry.getKey().toString(), item(entry.getValue(), t), t);
}
ret.set("shape", shape, t);
ret.set("ingredients", imap, t);
if (shaped.getKey() != null) {
ret.set("key", shaped.getKey(), t);
}
}
return ret;
}
use of com.laytonsmith.core.constructs.CArray 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.core.constructs.CArray in project CommandHelper by EngineHub.
the class ObjectGenerator method fireworkEffect.
public CArray fireworkEffect(MCFireworkEffect mcfe, Target t) {
CArray fe = CArray.GetAssociativeArray(t);
fe.set("flicker", CBoolean.get(mcfe.hasFlicker()), t);
fe.set("trail", CBoolean.get(mcfe.hasTrail()), t);
MCFireworkType type = mcfe.getType();
if (type != null) {
fe.set("type", new CString(mcfe.getType().name(), t), t);
} else {
fe.set("type", CNull.NULL, t);
}
CArray colors = new CArray(t);
for (MCColor c : mcfe.getColors()) {
colors.push(ObjectGenerator.GetGenerator().color(c, t), t);
}
fe.set("colors", colors, t);
CArray fadeColors = new CArray(t);
for (MCColor c : mcfe.getFadeColors()) {
fadeColors.push(ObjectGenerator.GetGenerator().color(c, t), t);
}
fe.set("fade", fadeColors, t);
return fe;
}
use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.
the class ObjectGenerator method location.
/**
* Gets a Location Object, optionally with yaw and pitch, given a MCLocation
*
* @param l
* @param includeYawAndPitch
* @return
*/
public CArray location(MCLocation l, boolean includeYawAndPitch) {
CArray ca = CArray.GetAssociativeArray(Target.UNKNOWN);
Construct x = new CDouble(l.getX(), Target.UNKNOWN);
Construct y = new CDouble(l.getY(), Target.UNKNOWN);
Construct z = new CDouble(l.getZ(), Target.UNKNOWN);
Construct world = new CString(l.getWorld().getName(), Target.UNKNOWN);
ca.set("0", x, Target.UNKNOWN);
ca.set("1", y, Target.UNKNOWN);
ca.set("2", z, Target.UNKNOWN);
ca.set("3", world, Target.UNKNOWN);
ca.set("x", x, Target.UNKNOWN);
ca.set("y", y, Target.UNKNOWN);
ca.set("z", z, Target.UNKNOWN);
ca.set("world", world, Target.UNKNOWN);
if (includeYawAndPitch) {
// guarantee yaw in the 0 - 359.9~ range
float yawRaw = l.getYaw() % 360.0f;
if (yawRaw < 0.0f) {
yawRaw += 360.0f;
}
Construct yaw = new CDouble(yawRaw, Target.UNKNOWN);
Construct pitch = new CDouble(l.getPitch(), Target.UNKNOWN);
ca.set("4", yaw, Target.UNKNOWN);
ca.set("5", pitch, Target.UNKNOWN);
ca.set("yaw", yaw, Target.UNKNOWN);
ca.set("pitch", pitch, Target.UNKNOWN);
}
return ca;
}
use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.
the class ObjectGenerator method vector.
/**
* Gets a vector object, given a Vector and a Target.
*
* @param vector the Vector
* @param t the Target
* @return the vector array
*/
public CArray vector(Vector3D vector, Target t) {
CArray ca = CArray.GetAssociativeArray(t);
// Integral keys first
ca.set(0, new CDouble(vector.X(), t), t);
ca.set(1, new CDouble(vector.Y(), t), t);
ca.set(2, new CDouble(vector.Z(), t), t);
// Then string keys
ca.set("x", new CDouble(vector.X(), t), t);
ca.set("y", new CDouble(vector.Y(), t), t);
ca.set("z", new CDouble(vector.Z(), t), t);
return ca;
}
Aggregations