add the mess of the recipe generator code

This commit is contained in:
modmuss50 2019-07-30 12:13:17 +01:00
parent 7e2bf482a2
commit 9a92325da3
4 changed files with 186 additions and 210 deletions

View file

@ -121,7 +121,7 @@ public class TechReborn implements ModInitializer {
CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(CommandManager.literal("recipe").executes(context -> { CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(CommandManager.literal("recipe").executes(context -> {
try { try {
RecipeTemplate.generate(context.getSource().getPlayer()); RecipeTemplate.generateFromInv(context.getSource().getPlayer());
} catch (Exception e){ } catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -63,8 +63,6 @@ public class ModRecipes {
public static void init() { 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(); //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));
}
} }

View file

@ -6,9 +6,18 @@ import com.google.gson.JsonObject;
import com.mojang.datafixers.Dynamic; import com.mojang.datafixers.Dynamic;
import com.mojang.datafixers.types.JsonOps; import com.mojang.datafixers.types.JsonOps;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.container.Container;
import net.minecraft.datafixers.NbtOps; import net.minecraft.datafixers.NbtOps;
import net.minecraft.entity.player.PlayerEntity; 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.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.text.LiteralText;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
@ -17,16 +26,18 @@ import org.apache.commons.io.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.*;
import java.util.List; import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors;
public class RecipeTemplate { 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()){ if(playerEntity.inventory.getInvStack(0).isEmpty()){
playerEntity.sendMessage(new LiteralText("no machine in first slot")); playerEntity.sendMessage(new LiteralText("no machine in first slot"));
auto(playerEntity);
return; 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<Item> items = Registry.ITEM.stream().collect(Collectors.toList());
Map<String, Map<String, Item>> map = new HashMap<>();
List<String> 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<String, Item> 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<CraftingRecipe> 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<ItemStack> inputs, List<ItemStack> outputs) throws IOException {
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
object.addProperty("type", type.toString()); object.addProperty("type", type.toString());
object.addProperty("power", 20); object.addProperty("power", power);
object.addProperty("time", 400); object.addProperty("time", time);
{ {
JsonArray ingredients = new JsonArray(); JsonArray ingredients = new JsonArray();
@ -103,7 +279,9 @@ public class RecipeTemplate {
name = Registry.FLUID.getId(TRContent.CELL.getFluid(outputs.get(0))).getPath(); 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 ++; i ++;
} }
@ -111,9 +289,7 @@ public class RecipeTemplate {
MinecraftClient.getInstance().keyboard.setClipboard(file.getAbsolutePath()); MinecraftClient.getInstance().keyboard.setClipboard(file.getAbsolutePath());
System.out.println(MinecraftClient.getInstance().keyboard.getClipboard()); return file;
playerEntity.sendMessage(new LiteralText("done: " + file.getAbsolutePath()));
} }

View file

@ -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<ItemStack> 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);
}
}
*/
}