Search in sources :

Example 11 with CArray

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;
}
Also used : MCShapedRecipe(com.laytonsmith.abstraction.MCShapedRecipe) MCItemStack(com.laytonsmith.abstraction.MCItemStack) MCShapelessRecipe(com.laytonsmith.abstraction.MCShapelessRecipe) CArray(com.laytonsmith.core.constructs.CArray) MCFurnaceRecipe(com.laytonsmith.abstraction.MCFurnaceRecipe) CString(com.laytonsmith.core.constructs.CString) Map(java.util.Map) HashMap(java.util.HashMap) CString(com.laytonsmith.core.constructs.CString)

Example 12 with CArray

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;
}
Also used : CRENotFoundException(com.laytonsmith.core.exceptions.CRE.CRENotFoundException) CArray(com.laytonsmith.core.constructs.CArray) MCItemMeta(com.laytonsmith.abstraction.MCItemMeta) CString(com.laytonsmith.core.constructs.CString) MCMaterial(com.laytonsmith.abstraction.blocks.MCMaterial) MCEnchantment(com.laytonsmith.abstraction.MCEnchantment) MCItemStack(com.laytonsmith.abstraction.MCItemStack) Construct(com.laytonsmith.core.constructs.Construct) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) Map(java.util.Map) HashMap(java.util.HashMap) CRERangeException(com.laytonsmith.core.exceptions.CRE.CRERangeException) CNull(com.laytonsmith.core.constructs.CNull)

Example 13 with CArray

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;
}
Also used : MCColor(com.laytonsmith.abstraction.MCColor) CArray(com.laytonsmith.core.constructs.CArray) MCFireworkType(com.laytonsmith.abstraction.enums.MCFireworkType) CString(com.laytonsmith.core.constructs.CString)

Example 14 with CArray

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;
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) CDouble(com.laytonsmith.core.constructs.CDouble) CString(com.laytonsmith.core.constructs.CString)

Example 15 with CArray

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;
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble)

Aggregations

CArray (com.laytonsmith.core.constructs.CArray)50 CString (com.laytonsmith.core.constructs.CString)27 Construct (com.laytonsmith.core.constructs.Construct)23 CInt (com.laytonsmith.core.constructs.CInt)17 CNull (com.laytonsmith.core.constructs.CNull)12 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)12 HashMap (java.util.HashMap)9 CDouble (com.laytonsmith.core.constructs.CDouble)8 IVariable (com.laytonsmith.core.constructs.IVariable)8 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 CBoolean (com.laytonsmith.core.constructs.CBoolean)5 Target (com.laytonsmith.core.constructs.Target)5 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)5 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)4 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)4 MCItemStack (com.laytonsmith.abstraction.MCItemStack)4