Search in sources :

Example 46 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project Wizardry by TeamWizardry.

the class RecipeShapedFluidFactory method parse.

@Override
public IRecipe parse(JsonContext context, JsonObject json) {
    String group = JsonUtils.getString(json, "group", "");
    Map<Character, Ingredient> ingredientMap = new HashMap<>();
    ingredientMap.put(' ', Ingredient.EMPTY);
    for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, "key").entrySet()) {
        if (entry.getKey().length() != 1)
            throw new JsonSyntaxException("Invalid key entry: '" + entry.getKey() + "' is an invalid symbol (must be 1 character only).");
        if (" ".equals(entry.getKey()))
            throw new JsonSyntaxException("Invalid key entry: ' ' is a reserved symbol.");
        ingredientMap.put(entry.getKey().toCharArray()[0], CraftingHelper.getIngredient(entry.getValue(), context));
    }
    JsonArray jsonPattern = JsonUtils.getJsonArray(json, "pattern");
    if (jsonPattern.size() == 0)
        throw new JsonSyntaxException("Invalid pattern: empty pattern not allowed");
    String[] pattern = new String[jsonPattern.size()];
    for (int x = 0; x < pattern.length; x++) {
        String line = JsonUtils.getString(jsonPattern.get(x), "pattern[" + x + "]");
        if (x > 0 && pattern[0].length() != line.length())
            throw new JsonSyntaxException("Invalid pattern: each row must be the same width");
        pattern[x] = line;
    }
    ShapedPrimer primer = new ShapedPrimer();
    primer.width = pattern[0].length();
    primer.height = pattern.length;
    primer.mirrored = JsonUtils.getBoolean(json, "mirrored", true);
    primer.input = NonNullList.withSize(primer.width * primer.height, Ingredient.EMPTY);
    Set<Character> keys = Sets.newHashSet(ingredientMap.keySet());
    keys.remove(' ');
    int x = 0;
    for (String line : pattern) {
        for (char c : line.toCharArray()) {
            Ingredient ingredient = ingredientMap.get(c);
            if (ingredient == null)
                throw new JsonSyntaxException("Pattern references symbol '" + c + "' but it's not defined in the key");
            primer.input.set(x++, ingredient);
            keys.remove(c);
        }
    }
    if (!keys.isEmpty())
        throw new JsonSyntaxException("Key defineds symbols that aren't used in pattern: " + keys);
    ItemStack result = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
    RecipeShapedFluid recipe = new RecipeShapedFluid(group.isEmpty() ? null : new ResourceLocation(group), result, primer);
    return recipe;
}
Also used : HashMap(java.util.HashMap) JsonArray(com.google.gson.JsonArray) ShapedPrimer(net.minecraftforge.common.crafting.CraftingHelper.ShapedPrimer) JsonSyntaxException(com.google.gson.JsonSyntaxException) Ingredient(net.minecraft.item.crafting.Ingredient) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.util.ResourceLocation) ItemStack(net.minecraft.item.ItemStack)

Example 47 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project Wizardry by TeamWizardry.

the class FluidRecipeLoader method processRecipes.

@SuppressWarnings("deprecation")
public void processRecipes(Map<String, FluidCrafter> recipeRegistry, Multimap<Ingredient, FluidCrafter> recipes) {
    Wizardry.logger.info("<<========================================================================>>");
    Wizardry.logger.info("> Starting fluid recipe loading.");
    JsonContext context = new JsonContext("minecraft");
    LinkedList<File> recipeFiles = new LinkedList<>();
    Stack<File> toProcess = new Stack<>();
    toProcess.push(directory);
    while (!toProcess.isEmpty()) {
        File file = toProcess.pop();
        if (file.isDirectory()) {
            File[] children = file.listFiles();
            if (children != null)
                for (File child : children) toProcess.push(child);
        } else if (file.isFile())
            if (file.getName().endsWith(".json"))
                recipeFiles.add(file);
    }
    fileLoop: for (File file : recipeFiles) {
        try {
            if (!file.exists()) {
                Wizardry.logger.error("  > SOMETHING WENT WRONG! " + file.getPath() + " can NOT be found. Ignoring file...");
                continue;
            }
            JsonElement element;
            try {
                element = new JsonParser().parse(new FileReader(file));
            } catch (FileNotFoundException e) {
                Wizardry.logger.error("  > SOMETHING WENT WRONG! " + file.getPath() + " can NOT be found. Ignoring file...");
                continue;
            }
            if (element == null) {
                Wizardry.logger.error("  > SOMETHING WENT WRONG! Could not parse " + file.getPath() + ". Ignoring file...");
                continue;
            }
            if (!element.isJsonObject()) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT contain a JsonObject. Ignoring file...: " + element.toString());
                continue;
            }
            JsonObject fileObject = element.getAsJsonObject();
            List<Ingredient> extraInputs = new LinkedList<>();
            Fluid fluid = ModFluids.MANA.getActual();
            int duration = 100;
            int required = 1;
            boolean consume = false;
            boolean explode = false;
            boolean bubbling = true;
            boolean harp = true;
            if (recipeRegistry.containsKey(file.getPath())) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " already exists in the recipe map. Ignoring file...: " + element.toString());
                continue;
            }
            if (!fileObject.has("output")) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT specify a recipe output. Ignoring file...: " + element.toString());
                continue;
            }
            if (!fileObject.get("output").isJsonObject()) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide a valid output. Ignoring file...: " + element.toString());
                continue;
            }
            if (!fileObject.has("input")) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide an initial input item. Ignoring file...: " + element.toString());
                continue;
            }
            JsonElement inputObject = fileObject.get("input");
            Ingredient inputItem = CraftingHelper.getIngredient(inputObject, context);
            if (inputItem == Ingredient.EMPTY) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide a valid input item. Ignoring file...: " + element.toString());
                continue;
            }
            if (fileObject.has("extraInputs")) {
                if (!fileObject.get("extraInputs").isJsonArray()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " has extra inputs NOT in a JsonArray format. Ignoring file...: " + element.toString());
                    continue;
                }
                JsonArray extraInputArray = fileObject.get("extraInputs").getAsJsonArray();
                for (JsonElement extraInput : extraInputArray) {
                    Ingredient ingredient = CraftingHelper.getIngredient(extraInput, context);
                    if (ingredient == Ingredient.EMPTY) {
                        Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide a valid extra input item. Ignoring file...: " + element.toString());
                        continue fileLoop;
                    }
                    extraInputs.add(ingredient);
                }
            }
            if (fileObject.has("fluid")) {
                if (!fileObject.get("fluid").isJsonPrimitive() || !fileObject.getAsJsonPrimitive("fluid").isString()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT give rfluid as a string. Ignoring file...: " + element.toString());
                    continue;
                }
                fluid = FluidRegistry.getFluid(fileObject.get("fluid").getAsString());
            }
            if (fileObject.has("duration")) {
                if (!fileObject.get("duration").isJsonPrimitive() || !fileObject.getAsJsonPrimitive("duration").isNumber()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT give duration as a number. Ignoring file...:" + element.toString());
                    continue;
                }
                duration = fileObject.get("duration").getAsInt();
            }
            if (fileObject.has("required")) {
                if (!fileObject.get("required").isJsonPrimitive() || !fileObject.getAsJsonPrimitive("required").isNumber()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT give required as a number. Ignoring file...: " + element.toString());
                    continue;
                }
                required = fileObject.get("required").getAsInt();
            }
            if (fileObject.has("consume")) {
                if (!fileObject.get("consume").isJsonPrimitive()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT give consume as a boolean. Ignoring file...: " + element.toString());
                    continue;
                }
                consume = fileObject.get("consume").getAsBoolean();
            }
            if (fileObject.has("explode")) {
                if (!fileObject.get("explode").isJsonPrimitive()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT give explode as a boolean. Ignoring file...: " + element.toString());
                    continue;
                }
                explode = fileObject.get("explode").getAsBoolean();
            }
            if (fileObject.has("harp")) {
                if (!fileObject.get("harp").isJsonPrimitive()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT give harp as a boolean. Ignoring file...: " + element.toString());
                    continue;
                }
                harp = fileObject.get("harp").getAsBoolean();
            }
            if (fileObject.has("bubbling")) {
                if (!fileObject.get("bubbling").isJsonPrimitive()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT give bubbling as a boolean. Ignoring file...: " + element.toString());
                    continue;
                }
                bubbling = fileObject.get("bubbling").getAsBoolean();
            }
            JsonElement typeElement = fileObject.get("type");
            String type = typeElement == null ? "item" : typeElement.getAsString();
            JsonObject output = fileObject.get("output").getAsJsonObject();
            if (type.equalsIgnoreCase("item")) {
                ItemStack outputItem = CraftingHelper.getItemStack(output, context);
                if (outputItem.isEmpty()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide a valid output item. Ignoring file...: " + element.toString());
                    continue;
                }
                FluidCrafter build = buildFluidCrafter(file.getPath(), outputItem, inputItem, extraInputs, fluid, duration, required, consume, explode, bubbling, harp);
                recipeRegistry.put(file.getPath(), build);
                recipes.put(inputItem, build);
            } else if (type.equalsIgnoreCase("block")) {
                IBlockState outputBlock;
                JsonElement name = output.get("item");
                if (name == null)
                    name = output.get("block");
                if (name == null)
                    name = output.get("name");
                Block block = name != null ? ForgeRegistries.BLOCKS.getValue(new ResourceLocation(name.getAsString())) : null;
                if (block == null) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide a valid output block. Ignoring file...: " + element.toString());
                    continue;
                }
                int meta = 0;
                JsonElement data = output.get("data");
                if (data == null)
                    data = output.get("meta");
                if (data != null && data.isJsonPrimitive() && data.getAsJsonPrimitive().isNumber())
                    meta = data.getAsInt();
                outputBlock = block.getStateFromMeta(meta);
                FluidCrafter build = buildFluidCrafter(file.getPath(), outputBlock, inputItem, extraInputs, fluid, duration, required, consume, explode, bubbling, harp);
                recipeRegistry.put(file.getPath(), build);
                recipes.put(inputItem, build);
            } else
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " specifies an invalid recipe output type. Valid recipe types: \"item\" \"block\". Ignoring file...: " + element.toString());
        } catch (Exception jsonException) {
            Wizardry.logger.error("  > WARNING! Skipping " + file.getPath() + " due to error: ", jsonException);
        }
    }
    Wizardry.logger.info("> Finished mana recipe loading.");
    Wizardry.logger.info("<<========================================================================>>");
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JsonObject(com.google.gson.JsonObject) ResourceLocation(net.minecraft.util.ResourceLocation) FileReader(java.io.FileReader) List(java.util.List) JsonParser(com.google.gson.JsonParser) IBlockState(net.minecraft.block.state.IBlockState) Fluid(net.minecraftforge.fluids.Fluid) FileNotFoundException(java.io.FileNotFoundException) ItemStack(net.minecraft.item.ItemStack) FluidStack(net.minecraftforge.fluids.FluidStack) JsonArray(com.google.gson.JsonArray) JsonContext(net.minecraftforge.common.crafting.JsonContext) Ingredient(net.minecraft.item.crafting.Ingredient) JsonElement(com.google.gson.JsonElement) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) File(java.io.File)

Example 48 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project TechReborn by TechReborn.

the class RollingMachineRecipe method addRecipe.

public void addRecipe(ResourceLocation resourceLocation, ItemStack output, Object... components) {
    String s = "";
    int i = 0;
    int j = 0;
    int k = 0;
    if (components[i] instanceof String[]) {
        String[] as = (String[]) components[i++];
        for (int l = 0; l < as.length; l++) {
            String s2 = as[l];
            k++;
            j = s2.length();
            s = (new StringBuilder()).append(s).append(s2).toString();
        }
    } else {
        while (components[i] instanceof String) {
            String s1 = (String) components[i++];
            k++;
            j = s1.length();
            s = (new StringBuilder()).append(s).append(s1).toString();
        }
    }
    HashMap<Character, ItemStack> hashmap = new HashMap<Character, ItemStack>();
    for (; i < components.length; i += 2) {
        Character character = (Character) components[i];
        ItemStack itemstack1 = null;
        if (components[i + 1] instanceof Item) {
            itemstack1 = new ItemStack((Item) components[i + 1]);
        } else if (components[i + 1] instanceof Block) {
            itemstack1 = new ItemStack((Block) components[i + 1], 1, -1);
        } else if (components[i + 1] instanceof ItemStack) {
            itemstack1 = (ItemStack) components[i + 1];
        }
        hashmap.put(character, itemstack1);
    }
    NonNullList<Ingredient> recipeArray = NonNullList.create();
    for (int i1 = 0; i1 < j * k; i1++) {
        char c = s.charAt(i1);
        if (hashmap.containsKey(c)) {
            recipeArray.set(i1, CraftingHelper.getIngredient(((ItemStack) hashmap.get(c)).copy()));
        } else {
            recipeArray.set(i1, CraftingHelper.getIngredient(ItemStack.EMPTY));
        }
    }
    recipes.put(resourceLocation, new ShapedRecipes("", j, k, recipeArray, output));
}
Also used : ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) HashMap(java.util.HashMap) Item(net.minecraft.item.Item) Ingredient(net.minecraft.item.crafting.Ingredient) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack)

Example 49 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project TechReborn by TechReborn.

the class GuiAutoCrafting method renderRecipe.

// Based of vanilla code
public void renderRecipe(IRecipe recipe, int x, int y) {
    RenderHelper.enableGUIStandardItemLighting();
    GlStateManager.enableAlpha();
    mc.getTextureManager().bindTexture(RECIPE_BOOK_TEXTURE);
    this.drawTexturedModalRect(x, y, 152, 78, 24, 24);
    int recipeWidth = 3;
    int recipeHeight = 3;
    if (recipe instanceof ShapedRecipes) {
        ShapedRecipes shapedrecipes = (ShapedRecipes) recipe;
        recipeWidth = shapedrecipes.getWidth();
        recipeHeight = shapedrecipes.getHeight();
    }
    if (recipe instanceof ShapedOreRecipe) {
        ShapedOreRecipe shapedrecipes = (ShapedOreRecipe) recipe;
        recipeWidth = shapedrecipes.getRecipeWidth();
        recipeHeight = shapedrecipes.getRecipeHeight();
    }
    Iterator<Ingredient> ingredients = recipe.getIngredients().iterator();
    for (int rHeight = 0; rHeight < recipeHeight; ++rHeight) {
        int j1 = 3 + rHeight * 7;
        for (int rWidth = 0; rWidth < recipeWidth; ++rWidth) {
            if (ingredients.hasNext()) {
                ItemStack[] aitemstack = ingredients.next().getMatchingStacks();
                if (aitemstack.length != 0) {
                    int l1 = 3 + rWidth * 7;
                    GlStateManager.pushMatrix();
                    int i2 = (int) ((float) (x + l1) / 0.42F - 3.0F);
                    int j2 = (int) ((float) (y + j1) / 0.42F - 3.0F);
                    GlStateManager.scale(0.42F, 0.42F, 1.0F);
                    GlStateManager.enableLighting();
                    mc.getRenderItem().renderItemAndEffectIntoGUI(aitemstack[0], i2, j2);
                    GlStateManager.disableLighting();
                    GlStateManager.popMatrix();
                }
            }
        }
    }
    GlStateManager.disableAlpha();
    RenderHelper.disableStandardItemLighting();
}
Also used : ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) Ingredient(net.minecraft.item.crafting.Ingredient) ShapedOreRecipe(net.minecraftforge.oredict.ShapedOreRecipe) ItemStack(net.minecraft.item.ItemStack)

Example 50 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project artisan-worktables by codetaylor.

the class CTArtisanRecipe method getStacksShapeless.

/**
 * @return the matrix items re-ordered to match the recipe's ingredient list order
 */
private MatchInfo getStacksShapeless(ICraftingContext context) {
    List<IArtisanIngredient> ingredients = this.getIngredientList();
    ICraftingMatrixStackHandler matrixHandler = context.getCraftingMatrixHandler();
    List<ItemStack> itemList = new ArrayList<>(matrixHandler.getSlots());
    List<Integer> indexList = new ArrayList<>(matrixHandler.getSlots());
    IItemStack[] stacks = new IItemStack[ingredients.size()];
    int[] indices = new int[ingredients.size()];
    for (int i = 0; i < matrixHandler.getSlots(); i++) {
        ItemStack itemStack = matrixHandler.getStackInSlot(i);
        if (!itemStack.isEmpty()) {
            itemList.add(itemStack);
            indexList.add(i);
        }
    }
    List<Ingredient> ingredientList = new ArrayList<>(ingredients.size());
    for (IArtisanIngredient ingredient : ingredients) {
        ingredientList.add(ingredient.toIngredient());
    }
    int[] matches = RecipeMatcher.findMatches(itemList, ingredientList);
    for (int i = 0; i < matches.length; i++) {
        stacks[matches[i]] = CraftTweakerMC.getIItemStack(itemList.get(i));
        indices[matches[i]] = indexList.get(i);
    }
    return new MatchInfo(stacks, indices);
}
Also used : ArrayList(java.util.ArrayList) Ingredient(net.minecraft.item.crafting.Ingredient) IIngredient(crafttweaker.api.item.IIngredient) IItemStack(crafttweaker.api.item.IItemStack) ItemStack(net.minecraft.item.ItemStack) IItemStack(crafttweaker.api.item.IItemStack)

Aggregations

Ingredient (net.minecraft.item.crafting.Ingredient)57 ItemStack (net.minecraft.item.ItemStack)49 ResourceLocation (net.minecraft.util.ResourceLocation)12 ArrayList (java.util.ArrayList)10 JsonElement (com.google.gson.JsonElement)8 JsonObject (com.google.gson.JsonObject)7 List (java.util.List)7 IRecipe (net.minecraft.item.crafting.IRecipe)7 JsonArray (com.google.gson.JsonArray)6 Block (net.minecraft.block.Block)6 Item (net.minecraft.item.Item)6 HashMap (java.util.HashMap)5 IBlockState (net.minecraft.block.state.IBlockState)5 ShapelessRecipes (net.minecraft.item.crafting.ShapelessRecipes)5 FluidStack (net.minecraftforge.fluids.FluidStack)5 JsonParser (com.google.gson.JsonParser)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 FileReader (java.io.FileReader)4 EntityItem (net.minecraft.entity.item.EntityItem)4