From 9a92325da37fadad0134909203c1c5799a4e47d4 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Tue, 30 Jul 2019 12:13:17 +0100 Subject: [PATCH] add the mess of the recipe generator code --- src/main/java/techreborn/TechReborn.java | 2 +- src/main/java/techreborn/init/ModRecipes.java | 84 -------- .../java/techreborn/init/RecipeTemplate.java | 194 +++++++++++++++++- .../recipes/IndustrialSawmillRecipes.java | 116 ----------- 4 files changed, 186 insertions(+), 210 deletions(-) delete mode 100644 src/main/java/techreborn/init/recipes/IndustrialSawmillRecipes.java diff --git a/src/main/java/techreborn/TechReborn.java b/src/main/java/techreborn/TechReborn.java index 63e622b21..f91519a2a 100644 --- a/src/main/java/techreborn/TechReborn.java +++ b/src/main/java/techreborn/TechReborn.java @@ -121,7 +121,7 @@ public class TechReborn implements ModInitializer { CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(CommandManager.literal("recipe").executes(context -> { try { - RecipeTemplate.generate(context.getSource().getPlayer()); + RecipeTemplate.generateFromInv(context.getSource().getPlayer()); } catch (Exception e){ e.printStackTrace(); } diff --git a/src/main/java/techreborn/init/ModRecipes.java b/src/main/java/techreborn/init/ModRecipes.java index 5c94feb58..92c29e905 100644 --- a/src/main/java/techreborn/init/ModRecipes.java +++ b/src/main/java/techreborn/init/ModRecipes.java @@ -63,8 +63,6 @@ public class ModRecipes { public static void init() { - //Gonna rescan to make sure we have an uptodate list - //OreUtil.scanForOres(); /* @@ -93,87 +91,5 @@ public class ModRecipes { //IndustrialSawmillRecipes.init(); } - private static void addCompressorRecipes() { - -// ItemStack plate; -// for (String ore : OreUtil.oreNames) { -// if (ore.equals("iridium")) { -// continue; -// } -// if (OreUtil.hasPlate(ore)) { -// try { -// plate = ItemPlates.getPlateByName(ore, 1); -// } catch (InvalidParameterException e) { -// plate = OreUtil.getStackFromName("plate" + OreUtil.capitalizeFirstLetter(ore), 1); -// } -// if (plate.isEmpty()) { -// continue; -// } -// if (OreUtil.hasIngot(ore)) { -// RecipeHandler.addRecipe(Reference.COMPRESSOR_RECIPE, new CompressorRecipe( -// OreUtil.getStackFromName("ingot" + OreUtil.capitalizeFirstLetter(ore), 1), plate, 300, 4)); -// } -// if (OreUtil.hasGem(ore) && OreUtil.hasDust(ore)) { -// RecipeHandler.addRecipe(Reference.COMPRESSOR_RECIPE, new CompressorRecipe( -// OreUtil.getStackFromName("dust" + OreUtil.capitalizeFirstLetter(ore), 1), plate, 300, 4)); -// } -// if (OreUtil.hasBlock(ore)) { -// ItemStack morePlates = plate.copy(); -// morePlates.setCount(9); -// RecipeHandler.addRecipe(Reference.COMPRESSOR_RECIPE, new CompressorRecipe( -// OreUtil.getStackFromName("block" + OreUtil.capitalizeFirstLetter(ore), 1), morePlates, 300, 4)); -// } -// } -// } - } - - static void addGrinderRecipes() { - - //See comments bellow, this allows the ore to go to the product when it sometimes goes straight to dust. - -// for (String oreDictionaryName : OreDictionary.getOreNames()) { -// if (isDictPrefixed(oreDictionaryName, "ore", "gem", "ingot")) { -// ItemStack oreStack = getDictOreOrEmpty(oreDictionaryName, 1); -// String[] data = getDictData(oreDictionaryName); -// -// //High-level ores shouldn't grind here -// if (data[0].equals("ore") && ( -// data[1].equals("tungsten") || -// data[1].equals("titanium") || -// data[1].equals("aluminium") || -// data[1].equals("iridium") || -// data[1].equals("saltpeter")|| -// data[1].equals("coal") || //Done here to skip going to dust so it can go to the output -// data[1].equals("diamond") || //For example diamond ore should go to diamonds not the diamond dust -// data[1].equals("emerald") || //TODO possibly remove this and make it a bit more dyamic? (Check for furnace recipes? and then the block drop?) -// data[1].equals("redstone") || -// data[1].equals("quartz") -// ) || -// oreStack.isEmpty()) -// continue; -// -// boolean ore = data[0].equals("ore"); -// TechReborn.LOGGER.debug("Ore: " + data[1]); -// ItemStack dust = getDictOreOrEmpty(joinDictName("dust", data[1]), ore ? 2 : 1); -// if (dust.isEmpty() || dust.getItem() == null) { -// continue; -// } -// dust = dust.copy(); -// if (ore) { -// dust.setCount(2); -// } -// boolean useOreDict = true; -// //Disables the ore dict for lapis, this is becuase it is oredict with dye. This may cause some other lapis ores to not be grindable, but we can fix that when that arrises. -// if(data[1].equalsIgnoreCase("lapis")){ -// useOreDict = false; -// } -// RecipeHandler.addRecipe(Reference.GRINDER_RECIPE, new GrinderRecipe(oreStack, dust, ore ? 270 : 200, ore ? 31 : 22, useOreDict)); -// } -// } - } - - public static ItemStack getBucketWithFluid(Fluid fluid) { - return FluidUtil.getFilledBucket(new FluidInstance(fluid, 1000)); - } } diff --git a/src/main/java/techreborn/init/RecipeTemplate.java b/src/main/java/techreborn/init/RecipeTemplate.java index 26a5672c7..49afd7210 100644 --- a/src/main/java/techreborn/init/RecipeTemplate.java +++ b/src/main/java/techreborn/init/RecipeTemplate.java @@ -6,9 +6,18 @@ import com.google.gson.JsonObject; import com.mojang.datafixers.Dynamic; import com.mojang.datafixers.types.JsonOps; import net.minecraft.client.MinecraftClient; +import net.minecraft.container.Container; import net.minecraft.datafixers.NbtOps; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.fluid.Fluid; +import net.minecraft.fluid.Fluids; +import net.minecraft.inventory.CraftingInventory; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.recipe.CraftingRecipe; +import net.minecraft.recipe.Recipe; +import net.minecraft.recipe.RecipeType; import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; @@ -17,16 +26,18 @@ import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.function.BiConsumer; import java.util.function.Function; +import java.util.stream.Collectors; public class RecipeTemplate { - public static void generate(PlayerEntity playerEntity) throws IOException { + public static void generateFromInv(PlayerEntity playerEntity) throws IOException { if(playerEntity.inventory.getInvStack(0).isEmpty()){ playerEntity.sendMessage(new LiteralText("no machine in first slot")); + auto(playerEntity); return; } @@ -45,10 +56,175 @@ public class RecipeTemplate { } } + File file = generate(type,false, 20, 400, inputs, outputs); + + playerEntity.sendMessage(new LiteralText("done: " + file.getAbsolutePath())); + + } + + public static void auto(PlayerEntity playerEntity) throws IOException { + List items = Registry.ITEM.stream().collect(Collectors.toList()); + + + Map> map = new HashMap<>(); + List names = new ArrayList<>(); + + + for(Item item : items){ + Identifier identifier = Registry.ITEM.getId(item); + String path = identifier.getPath(); + + if(path.contains("_")){ + String name = path.substring(0, path.lastIndexOf('_')); + String type = path.substring(path.lastIndexOf('_') + 1); + + Map typeMap = map.computeIfAbsent(type, s -> new HashMap<>()); + + typeMap.put(name, item); + + if (!names.contains(name)) { + names.add(name); + } + } + + } + + boolean compressor = false; + boolean grinder = false; + boolean sawmill = true; + + if(compressor){ + //Compressor + for(String name : names){ + ItemStack plateStack = ItemStack.EMPTY; + if(map.get("plate").containsKey(name)){ + Item plate = map.get("plate").get(name); + plateStack = new ItemStack(plate); + } + + if(plateStack.isEmpty()){ + continue; + } + + if(map.get("ingot").containsKey(name)){ + ItemStack ingotStack = new ItemStack(map.get("ingot").get(name)); + generate(new Identifier("techreborn", "compressor"), true, 10, 300, Collections.singletonList(ingotStack), Collections.singletonList(plateStack)); + } + + if(map.get("dust").containsKey(name)){ + ItemStack dust = new ItemStack(map.get("dust").get(name)); + generate(new Identifier("techreborn", "compressor"), true, 10, 250, Collections.singletonList(dust), Collections.singletonList(plateStack)); + } + + if(map.get("block").containsKey(name)){ + ItemStack blockStack = new ItemStack(map.get("block").get(name)); + + ItemStack morePlates = plateStack.copy(); + morePlates.setCount(9); + + generate(new Identifier("techreborn", "compressor"), true, 10, 300, Collections.singletonList(blockStack), Collections.singletonList(morePlates)); + } + } + } + + if(grinder){ + String[] highTeirNames = new String[]{"tungsten", "titanium", "aluminium", "iridium", "saltpeter", "coal", "diamond", "emerald", "redstone", "quartz"}; + + String[] types = new String[]{"ore", "gem", "ingot"}; + + //Grinder + for(String name : names){ + for(String type : types){ + ItemStack inputStack = ItemStack.EMPTY; + if(map.get(type).containsKey(name)){ + Item ore = map.get(type).get(name); + inputStack = new ItemStack(ore); + } + + if(inputStack.isEmpty()){ + continue; + } + + boolean ore = type.equals("ore"); + + if (ore && Arrays.asList(highTeirNames).contains(name)) { + continue; + } + + ItemStack dustStack = ItemStack.EMPTY; + if(map.get("dust").containsKey(name)){ + Item dust = map.get("dust").get(name); + dustStack = new ItemStack(dust); + } + + if(dustStack.isEmpty()){ + continue; + } + + if (ore){ + dustStack.setCount(2); + } + + generate(new Identifier("techreborn", "grinder"), true, ore ? 270 : 200, ore ? 31 : 22, Collections.singletonList(inputStack), Collections.singletonList(dustStack)); + } + + } + } + + if(sawmill){ + + CraftingInventory inventory = new CraftingInventory(new Container(null, 0) { + @Override + public boolean canUse(PlayerEntity var1) { + return true; + } + }, 1, 1); + + for(String name : names){ + ItemStack inputStack = ItemStack.EMPTY; + if(map.get("log").containsKey(name)){ + Item ore = map.get("log").get(name); + inputStack = new ItemStack(ore); + } + + if(inputStack.isEmpty()){ + continue; + } + + inventory.setInvStack(0, inputStack.copy()); + + List recipes = playerEntity.world.getRecipeManager().getAllMatches(RecipeType.CRAFTING, inventory, playerEntity.world); + CraftingRecipe recipe = recipes.get(0); + + ItemStack output = recipe.getOutput(); + + if(output.isEmpty()){ + continue; + } + + + addRecipe(inputStack, output); + + } + } + + } + + public static void addRecipe(ItemStack log, ItemStack plank) throws IOException { + plank.setCount(4); + register(log, Fluids.WATER, 100, 128, plank, TRContent.Dusts.SAW.getStack(3), new ItemStack(Items.PAPER)); + } + + static void register(ItemStack input1, Fluid fluid, int ticks, int euPerTick, ItemStack... outputs) throws IOException { + Identifier sawmill = new Identifier("techreborn:industrial_sawmill"); + generate(sawmill, true, euPerTick, ticks, Collections.singletonList(input1), Arrays.asList(outputs)); + } + + public static File generate(Identifier type, boolean auto, int power, int time, List inputs, List outputs) throws IOException { JsonObject object = new JsonObject(); object.addProperty("type", type.toString()); - object.addProperty("power", 20); - object.addProperty("time", 400); + object.addProperty("power", power); + object.addProperty("time", time); { JsonArray ingredients = new JsonArray(); @@ -103,7 +279,9 @@ public class RecipeTemplate { name = Registry.FLUID.getId(TRContent.CELL.getFluid(outputs.get(0))).getPath(); } - file = new File(dir, "src/main/resources/data/techreborn/recipes/" + type.getPath() + "/" + name + (i == 0 ? "" : "_" + i) + ".json"); + String extraPath = auto ? "/auto/" : "/"; + + file = new File(dir, "src/main/resources/data/techreborn/recipes/" + type.getPath() + extraPath + name + (i == 0 ? "" : "_" + i) + ".json"); i ++; } @@ -111,9 +289,7 @@ public class RecipeTemplate { MinecraftClient.getInstance().keyboard.setClipboard(file.getAbsolutePath()); - System.out.println(MinecraftClient.getInstance().keyboard.getClipboard()); - - playerEntity.sendMessage(new LiteralText("done: " + file.getAbsolutePath())); + return file; } diff --git a/src/main/java/techreborn/init/recipes/IndustrialSawmillRecipes.java b/src/main/java/techreborn/init/recipes/IndustrialSawmillRecipes.java deleted file mode 100644 index 4c69859c9..000000000 --- a/src/main/java/techreborn/init/recipes/IndustrialSawmillRecipes.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * This file is part of TechReborn, licensed under the MIT License (MIT). - * - * Copyright (c) 2018 TechReborn - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package techreborn.init.recipes; - -import reborncore.common.registration.RebornRegister; -import techreborn.TechReborn; - -@RebornRegister(TechReborn.MOD_ID) -public class IndustrialSawmillRecipes extends RecipeMethods { - /* TODO 1.13 :D - - static FluidStack WATER = new FluidStack(FluidRegistry.WATER, 1000); - - @ConfigRegistry(config = "recipes", category = "sawmill", key = "plankCount", comment = "Number of planks the saw mill will ouput") - public static int plankCount = 4; - - @ConfigRegistry(config = "recipes", category = "sawmill", key = "disableRecipes", comment = "Set to true to disable sawmill recipes from loading.") - public static boolean disableRecipes = false; - - public static void init() { - if(disableRecipes){ - return; - } - InventoryCrafting inventoryCrafting = new InventoryCrafting(new Container() { - @Override - public boolean canInteractWith(EntityPlayer playerIn) { - return false; - } - }, 1, 1); - inventoryCrafting.setInventorySlotContents(0, ItemStack.EMPTY); - - List logs = OreDictionary.getOres("logWood").stream() - .filter(Objects::nonNull) - .map(ItemStack::copy) - .collect(Collectors.toList()); - - for (ItemStack logStack : logs) { - if (logStack.getItemDamage() == OreDictionary.WILDCARD_VALUE && logStack.getHasSubtypes()) { - for (int i = 0; i < 16; i++) { - ItemStack newStack = logStack.copy(); - newStack.setItemDamage(i); - inventoryCrafting.setInventorySlotContents(0, newStack); - ItemStack output = findMatchingRecipe(inventoryCrafting); - if (!output.isEmpty()) { - if (ItemUtils.isInputEqual("plankWood", output, false, false, false)) { - addRecipe(newStack.copy(), output.copy()); - } - } - } - } else { - logStack.setItemDamage(0); //Done to remove the wildcard value if the item doesnt have subtypes - inventoryCrafting.setInventorySlotContents(0, logStack.copy()); - ItemStack output = findMatchingRecipe(inventoryCrafting); - if (!output.isEmpty()) { - if (ItemUtils.isInputEqual("plankWood", output, false, false, false)) { - addRecipe(logStack.copy(), output.copy()); - } - } - } - } - - } - - @Nonnull - public static ItemStack findMatchingRecipe(InventoryCrafting inv) { - for (IRecipe recipe : CraftingManager.REGISTRY) { - if (recipe.canFit(1, 1) && recipe.matches(inv, null)) { - return recipe.getCraftingResult(inv); - } - } - return ItemStack.EMPTY; - - } - - public static void addRecipe(ItemStack log, ItemStack plank) { - plank.setCount(plankCount); - register(log, WATER, 100, 128, plank, TRContent.Dusts.SAW.getStack(3), getStack(Items.PAPER, 1)); - } - - static void register(ItemStack input1, FluidStack fluid, int ticks, int euPerTick, ItemStack... outputs) { - if (outputs.length == 3) { - RecipeHandler.addRecipe(Reference.INDUSTRIAL_SAWMILL_RECIPE, new IndustrialSawmillRecipe(input1, fluid, outputs[0], outputs[1], outputs[2], ticks, euPerTick, false)); - } else if (outputs.length == 2) { - RecipeHandler.addRecipe(Reference.INDUSTRIAL_SAWMILL_RECIPE, new IndustrialSawmillRecipe(input1, fluid, outputs[0], outputs[1], null, ticks, euPerTick, false)); - } else if (outputs.length == 1) { - RecipeHandler.addRecipe(Reference.INDUSTRIAL_SAWMILL_RECIPE, new IndustrialSawmillRecipe(input1, fluid, outputs[0], null, null, ticks, euPerTick, false)); - } else { - throw new InvalidParameterException("Invalid industrial sawmill outputs: " + outputs); - } - } - - */ - -}