Search in sources :

Example 6 with CInt

use of com.laytonsmith.core.constructs.CInt 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;
}
Also used : MCMaterial(com.laytonsmith.abstraction.blocks.MCMaterial) CInt(com.laytonsmith.core.constructs.CInt) CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString)

Example 7 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class ObjectGenerator method recipe.

public MCRecipe recipe(Construct c, Target t) {
    if (!(c instanceof CArray)) {
        throw new CRECastException("Expected array but recieved " + c.getCType().name(), t);
    }
    CArray recipe = (CArray) c;
    String recipeKey = null;
    if (recipe.containsKey("key")) {
        recipeKey = recipe.get("key", t).val();
    }
    MCRecipeType recipeType;
    try {
        recipeType = MCRecipeType.valueOf(recipe.get("type", t).val());
    } catch (IllegalArgumentException e) {
        throw new CREFormatException("Invalid recipe type.", t);
    }
    MCItemStack result = item(recipe.get("result", t), t);
    MCRecipe ret;
    try {
        ret = StaticLayer.GetNewRecipe(recipeKey, recipeType, result);
    } catch (IllegalArgumentException ex) {
        throw new CREFormatException(ex.getMessage(), t);
    }
    switch(recipeType) {
        case SHAPED:
            CArray shaped = Static.getArray(recipe.get("shape", t), t);
            ;
            String[] shape = new String[(int) shaped.size()];
            if (shaped.size() < 1 || shaped.size() > 3 || shaped.inAssociativeMode()) {
                throw new CREFormatException("Shape array is invalid.", t);
            }
            int i = 0;
            for (Construct row : shaped.asList()) {
                if (row instanceof CString && row.val().length() >= 1 && row.val().length() <= 3) {
                    shape[i] = row.val();
                    i++;
                } else {
                    throw new CREFormatException("Shape array is invalid.", t);
                }
            }
            ((MCShapedRecipe) ret).setShape(shape);
            CArray shapedIngredients = Static.getArray(recipe.get("ingredients", t), t);
            if (!shapedIngredients.inAssociativeMode()) {
                throw new CREFormatException("Ingredients array is invalid.", t);
            }
            for (String key : shapedIngredients.stringKeySet()) {
                MCItemStack is;
                Construct ingredient = shapedIngredients.get(key, t);
                if (ingredient instanceof CString) {
                    CString item = (CString) ingredient;
                    if (item.val().contains(":")) {
                        String[] split = item.val().split(":");
                        is = StaticLayer.GetItemStack(Integer.valueOf(split[0]), Integer.valueOf(split[1]), 1);
                    } else {
                        is = StaticLayer.GetItemStack(Integer.valueOf(item.val()), 1);
                    }
                } else if (ingredient instanceof CInt) {
                    is = StaticLayer.GetItemStack(Static.getInt32(ingredient, t), 1);
                } else if (ingredient instanceof CArray) {
                    is = item(ingredient, t);
                } else if (ingredient instanceof CNull) {
                    is = StaticLayer.GetItemStack(0, 0);
                } else {
                    throw new CREFormatException("Item was not found", t);
                }
                ((MCShapedRecipe) ret).setIngredient(key.charAt(0), is);
            }
            return ret;
        case SHAPELESS:
            CArray ingredients = Static.getArray(recipe.get("ingredients", t), t);
            if (ingredients.inAssociativeMode()) {
                throw new CREFormatException("Ingredients array is invalid.", t);
            }
            for (Construct ingredient : ingredients.asList()) {
                MCItemStack is;
                if (ingredient instanceof CString) {
                    if (ingredient.val().contains(":")) {
                        String[] split = ingredient.val().split(":");
                        is = StaticLayer.GetItemStack(Integer.valueOf(split[0]), Integer.valueOf(split[1]), 1);
                    } else {
                        is = StaticLayer.GetItemStack(Integer.valueOf(ingredient.val()), 1);
                    }
                } else if (ingredient instanceof CArray) {
                    is = item(ingredient, t);
                } else {
                    throw new CREFormatException("Item was not found", t);
                }
                ((MCShapelessRecipe) ret).addIngredient(is);
            }
            return ret;
        case FURNACE:
            CArray is = Static.getArray(recipe.get("input", t), t);
            ((MCFurnaceRecipe) ret).setInput(item(is, t));
            return ret;
        default:
            throw new CREFormatException("Could not find valid recipe type.", t);
    }
}
Also used : MCRecipe(com.laytonsmith.abstraction.MCRecipe) CRECastException(com.laytonsmith.core.exceptions.CRE.CRECastException) MCShapelessRecipe(com.laytonsmith.abstraction.MCShapelessRecipe) CArray(com.laytonsmith.core.constructs.CArray) MCFurnaceRecipe(com.laytonsmith.abstraction.MCFurnaceRecipe) CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString) MCShapedRecipe(com.laytonsmith.abstraction.MCShapedRecipe) CInt(com.laytonsmith.core.constructs.CInt) MCItemStack(com.laytonsmith.abstraction.MCItemStack) MCRecipeType(com.laytonsmith.abstraction.enums.MCRecipeType) Construct(com.laytonsmith.core.constructs.Construct) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) CNull(com.laytonsmith.core.constructs.CNull)

Example 8 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class RandomTests method testJSONEscapeString.

@Test
public void testJSONEscapeString() throws MarshalException {
    CArray ca = new CArray(Target.UNKNOWN);
    final Target t = Target.UNKNOWN;
    ca.push(C.Int(1), t);
    ca.push(C.Double(2.2), t);
    ca.push(C.String("string"), t);
    ca.push(C.String("\"Quote\""), t);
    ca.push(C.Boolean(true), t);
    ca.push(C.Boolean(false), t);
    ca.push(C.Null(), t);
    ca.push(C.Void(), t);
    ca.push(new Command("/Command", Target.UNKNOWN), t);
    ca.push(new CArray(Target.UNKNOWN, new CInt(1, Target.UNKNOWN)), t);
    // [1, 2.2, "string", "\"Quote\"", true, false, null, "", "/Command", [1]]
    assertEquals("[1,2.2,\"string\",\"\\\"Quote\\\"\",true,false,null,\"\",\"\\/Command\",[1]]", Construct.json_encode(ca, Target.UNKNOWN));
}
Also used : Target(com.laytonsmith.core.constructs.Target) CInt(com.laytonsmith.core.constructs.CInt) Command(com.laytonsmith.core.constructs.Command) CArray(com.laytonsmith.core.constructs.CArray) Test(org.junit.Test)

Example 9 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class RandomTests method testClone.

@Test
public void testClone() throws CloneNotSupportedException {
    CArray c1 = C.Array(C.Void(), C.Void()).clone();
    CBoolean c2 = C.Boolean(true).clone();
    CDouble c4 = C.Double(1).clone();
    CFunction c5 = new CFunction("", Target.UNKNOWN).clone();
    CInt c6 = C.Int(1).clone();
    CNull c7 = C.Null().clone();
    CString c8 = C.String("").clone();
    Construct c9 = C.Void().clone();
    Command c10 = new Command("/c", Target.UNKNOWN).clone();
    IVariable c12 = new IVariable(Auto.TYPE, "@name", C.Null(), Target.UNKNOWN).clone();
    Variable c13 = new Variable("$name", "", false, false, Target.UNKNOWN);
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) Command(com.laytonsmith.core.constructs.Command) CBoolean(com.laytonsmith.core.constructs.CBoolean) IVariable(com.laytonsmith.core.constructs.IVariable) CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble) CFunction(com.laytonsmith.core.constructs.CFunction) Construct(com.laytonsmith.core.constructs.Construct) CNull(com.laytonsmith.core.constructs.CNull) CString(com.laytonsmith.core.constructs.CString) Test(org.junit.Test)

Example 10 with CInt

use of com.laytonsmith.core.constructs.CInt in project CommandHelper by EngineHub.

the class RandomTests method testJSONDecodeString.

@Test
public void testJSONDecodeString() throws MarshalException {
    CArray ca = new CArray(Target.UNKNOWN);
    ca.push(C.Int(1), Target.UNKNOWN);
    ca.push(C.Double(2.2), Target.UNKNOWN);
    ca.push(C.String("string"), Target.UNKNOWN);
    ca.push(C.String("\"Quote\""), Target.UNKNOWN);
    ca.push(C.Boolean(true), Target.UNKNOWN);
    ca.push(C.Boolean(false), Target.UNKNOWN);
    ca.push(C.Null(), Target.UNKNOWN);
    ca.push(C.Void(), Target.UNKNOWN);
    ca.push(new Command("/Command", Target.UNKNOWN), Target.UNKNOWN);
    ca.push(new CArray(Target.UNKNOWN, new CInt(1, Target.UNKNOWN)), Target.UNKNOWN);
    StaticTest.assertCEquals(ca, Construct.json_decode("[1, 2.2, \"string\", \"\\\"Quote\\\"\", true, false, null, \"\", \"\\/Command\", [1]]", Target.UNKNOWN));
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) Command(com.laytonsmith.core.constructs.Command) CArray(com.laytonsmith.core.constructs.CArray) Test(org.junit.Test)

Aggregations

CInt (com.laytonsmith.core.constructs.CInt)20 CArray (com.laytonsmith.core.constructs.CArray)17 CString (com.laytonsmith.core.constructs.CString)12 Construct (com.laytonsmith.core.constructs.Construct)11 CDouble (com.laytonsmith.core.constructs.CDouble)8 CNull (com.laytonsmith.core.constructs.CNull)5 CBoolean (com.laytonsmith.core.constructs.CBoolean)4 HashMap (java.util.HashMap)4 CFunction (com.laytonsmith.core.constructs.CFunction)3 Command (com.laytonsmith.core.constructs.Command)3 IVariable (com.laytonsmith.core.constructs.IVariable)3 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)3 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)3 Test (org.junit.Test)3 MCColor (com.laytonsmith.abstraction.MCColor)2 CLabel (com.laytonsmith.core.constructs.CLabel)2 CResource (com.laytonsmith.core.constructs.CResource)2 Target (com.laytonsmith.core.constructs.Target)2 Variable (com.laytonsmith.core.constructs.Variable)2 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)2