Search in sources :

Example 6 with JsonContext

use of net.minecraftforge.common.crafting.JsonContext 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)

Aggregations

JsonContext (net.minecraftforge.common.crafting.JsonContext)6 JsonObject (com.google.gson.JsonObject)4 ItemStack (net.minecraft.item.ItemStack)4 File (java.io.File)3 Ingredient (net.minecraft.item.crafting.Ingredient)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 JsonParser (com.google.gson.JsonParser)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 AssemblyRecipeBasic (buildcraft.api.recipes.AssemblyRecipeBasic)1 IngredientStack (buildcraft.api.recipes.IngredientStack)1 IntegrationRecipeBasic (buildcraft.lib.recipe.IntegrationRecipeBasic)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 JsonParseException (com.google.gson.JsonParseException)1