Search in sources :

Example 1 with MCShapelessRecipe

use of com.laytonsmith.abstraction.MCShapelessRecipe 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 2 with MCShapelessRecipe

use of com.laytonsmith.abstraction.MCShapelessRecipe 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)

Aggregations

MCFurnaceRecipe (com.laytonsmith.abstraction.MCFurnaceRecipe)2 MCItemStack (com.laytonsmith.abstraction.MCItemStack)2 MCShapedRecipe (com.laytonsmith.abstraction.MCShapedRecipe)2 MCShapelessRecipe (com.laytonsmith.abstraction.MCShapelessRecipe)2 CArray (com.laytonsmith.core.constructs.CArray)2 CString (com.laytonsmith.core.constructs.CString)2 MCRecipe (com.laytonsmith.abstraction.MCRecipe)1 MCRecipeType (com.laytonsmith.abstraction.enums.MCRecipeType)1 CInt (com.laytonsmith.core.constructs.CInt)1 CNull (com.laytonsmith.core.constructs.CNull)1 Construct (com.laytonsmith.core.constructs.Construct)1 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)1 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1