From 449ce974f40173ff44f268480cbf361115dbe439 Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Sun, 15 Nov 2020 15:04:46 -0800 Subject: [PATCH] Speedup - Inspired by the 1.12 version --- .../MineTweakerImplementationAPI.java | 27 +- .../api/recipes/IRecipeManager.java | 6 +- .../minetweaker/api/recipes/ShapedRecipe.java | 15 +- .../java/minetweaker/runtime/MTTweaker.java | 6 +- .../minetweaker/mc1710/FMLEventHandler.java | 5 +- .../minetweaker/mc1710/MineTweakerMod.java | 8 +- .../minetweaker/mc1710/item/MCItemStack.java | 6 +- .../mc1710/recipes/MCRecipeManager.java | 732 +++++++++++------- .../mc1710/recipes/RecipeConverter.java | 8 +- 9 files changed, 463 insertions(+), 350 deletions(-) diff --git a/MineTweaker3-API/src/main/java/minetweaker/MineTweakerImplementationAPI.java b/MineTweaker3-API/src/main/java/minetweaker/MineTweakerImplementationAPI.java index 89b307f..ddfbc39 100644 --- a/MineTweaker3-API/src/main/java/minetweaker/MineTweakerImplementationAPI.java +++ b/MineTweaker3-API/src/main/java/minetweaker/MineTweakerImplementationAPI.java @@ -58,8 +58,8 @@ public class MineTweakerImplementationAPI { private static final ListenPlayerLoggedIn LISTEN_LOGIN = new ListenPlayerLoggedIn(); private static final ListenPlayerLoggedOut LISTEN_LOGOUT = new ListenPlayerLoggedOut(); private static final ListenBlockInfo LISTEN_BLOCK_INFO = new ListenBlockInfo(); - private static final EventList ONRELOAD = new EventList(); - private static final EventList ONPOSTRELOAD = new EventList(); + private static final EventList ONRELOAD = new EventList<>(); + private static final EventList ONPOSTRELOAD = new EventList<>(); static { minetweakerCommands = new HashMap(); @@ -621,28 +621,7 @@ public class MineTweakerImplementationAPI { events.onPlayerLoggedIn(LISTEN_LOGIN); events.onPlayerLoggedOut(LISTEN_LOGOUT); } -// -// byte[] currentScript = MineTweakerAPI.tweaker.getScriptData(); -// if (currentScript != null) { -// System.out.println("Already loaded a script before"); -// -// // alread loaded a script -// byte[] stagedScript = MineTweakerAPI.tweaker.getStagedScriptData(); -// -// if (Arrays.equals(currentScript, stagedScript)) { -// System.out.println("No reload needed"); -// return; // no reload necessary -// } -// -// if (MineTweakerAPI.game.isLocked()) { -// System.out.println("Reload blocked"); -// MineTweakerAPI.game.signalLockError(); -// return; -// } -// } else { -// System.out.println("First time loading a script, go ahead"); -// } - + MineTweakerAPI.tweaker.rollback(); if (MineTweakerAPI.server != null) { if (!MineTweakerAPI.server.isCommandAdded("minetweaker")) { diff --git a/MineTweaker3-API/src/main/java/minetweaker/api/recipes/IRecipeManager.java b/MineTweaker3-API/src/main/java/minetweaker/api/recipes/IRecipeManager.java index eb23414..c34753c 100644 --- a/MineTweaker3-API/src/main/java/minetweaker/api/recipes/IRecipeManager.java +++ b/MineTweaker3-API/src/main/java/minetweaker/api/recipes/IRecipeManager.java @@ -40,7 +40,7 @@ public interface IRecipeManager { * @return number of removed recipes */ @ZenMethod - int remove(IIngredient output, @Optional boolean nbtMatch); + void remove(IIngredient output, @Optional boolean nbtMatch); /** * Adds a shaped recipe. @@ -81,7 +81,7 @@ public interface IRecipeManager { * @return number of removed recipes */ @ZenMethod - int removeShaped(IIngredient output, @Optional IIngredient[][] ingredients); + void removeShaped(IIngredient output, @Optional IIngredient[][] ingredients); /** * Removes shapeless recipes. @@ -94,7 +94,7 @@ public interface IRecipeManager { * @return number of removed recipes */ @ZenMethod - int removeShapeless(IIngredient output, @Optional IIngredient[] ingredients, @Optional boolean wildcard); + void removeShapeless(IIngredient output, @Optional IIngredient[] ingredients, @Optional boolean wildcard); /** * Performs a crafting with the specified ingredients. Returns null if no diff --git a/MineTweaker3-API/src/main/java/minetweaker/api/recipes/ShapedRecipe.java b/MineTweaker3-API/src/main/java/minetweaker/api/recipes/ShapedRecipe.java index c7049f6..0d08920 100644 --- a/MineTweaker3-API/src/main/java/minetweaker/api/recipes/ShapedRecipe.java +++ b/MineTweaker3-API/src/main/java/minetweaker/api/recipes/ShapedRecipe.java @@ -28,21 +28,14 @@ public class ShapedRecipe implements ICraftingRecipe { public ShapedRecipe(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function, IRecipeAction action, boolean mirrored) { int numIngredients = 0; - for(int i = 0; i < ingredients.length; i++) { - IIngredient[] ingredient = ingredients[i]; - for(int i1 = 0; i1 < ingredient.length; i1++) { - if(ingredient[i1] != null) { + for (IIngredient[] ingredient : ingredients) { + for (IIngredient iIngredient : ingredient) { + if (iIngredient != null) { numIngredients++; } } } - // for(IIngredient[] row : ingredients) { - // for(IIngredient ingredient : row) { - // if(ingredient != null) { - // numIngredients++; - // } - // } - // } + this.posx = new byte[numIngredients]; this.posy = new byte[numIngredients]; this.output = output; diff --git a/MineTweaker3-API/src/main/java/minetweaker/runtime/MTTweaker.java b/MineTweaker3-API/src/main/java/minetweaker/runtime/MTTweaker.java index a4c7233..cfd2044 100644 --- a/MineTweaker3-API/src/main/java/minetweaker/runtime/MTTweaker.java +++ b/MineTweaker3-API/src/main/java/minetweaker/runtime/MTTweaker.java @@ -111,7 +111,7 @@ public class MTTweaker implements ITweaker { System.out.println("Loading scripts"); scriptData = ScriptProviderMemory.collect(scriptProvider); - Set executed = new HashSet(); + Set executed = new HashSet<>(); Iterator scripts = scriptProvider.getScripts(); while (scripts.hasNext()) { @@ -120,10 +120,10 @@ public class MTTweaker implements ITweaker { if (!executed.contains(script.getGroupName())) { executed.add(script.getGroupName()); - Map classes = new HashMap(); + Map classes = new HashMap<>(); IEnvironmentGlobal environmentGlobal = GlobalRegistry.makeGlobalEnvironment(classes); - List files = new ArrayList(); + List files = new ArrayList<>(); while (script.next()) { Reader reader = null; diff --git a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/FMLEventHandler.java b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/FMLEventHandler.java index 7880546..366f08a 100644 --- a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/FMLEventHandler.java +++ b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/FMLEventHandler.java @@ -10,6 +10,7 @@ import minetweaker.api.minecraft.MineTweakerMC; import minetweaker.api.player.IPlayer; import minetweaker.mc1710.network.MineTweakerLoadScriptsPacket; import minetweaker.mc1710.recipes.MCCraftingInventory; +import minetweaker.mc1710.recipes.MCRecipeManager; import net.minecraft.entity.player.EntityPlayerMP; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.PlayerEvent; @@ -32,8 +33,8 @@ public class FMLEventHandler { @SubscribeEvent public void onPlayerItemCrafted(PlayerEvent.ItemCraftedEvent ev) { IPlayer iPlayer = MineTweakerMC.getIPlayer(ev.player); - if (MineTweakerMod.INSTANCE.recipes.hasTransformerRecipes()) { - MineTweakerMod.INSTANCE.recipes.applyTransformations(MCCraftingInventory.get(ev.craftMatrix, ev.player), iPlayer); + if (MCRecipeManager.hasTransformerRecipes()) { + MCRecipeManager.applyTransformations(MCCraftingInventory.get(ev.craftMatrix, ev.player), iPlayer); } if (MineTweakerImplementationAPI.events.hasPlayerCrafted()) { diff --git a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/MineTweakerMod.java b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/MineTweakerMod.java index 641b64b..d88a0e0 100644 --- a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/MineTweakerMod.java +++ b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/MineTweakerMod.java @@ -35,10 +35,13 @@ import minetweaker.runtime.*; import minetweaker.runtime.providers.ScriptProviderCascade; import minetweaker.runtime.providers.ScriptProviderCustom; import minetweaker.runtime.providers.ScriptProviderDirectory; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.IRecipe; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.MinecraftForge; import java.io.File; +import java.util.List; /** * Main mod class. Performs some general logic, initialization of the API and @@ -77,20 +80,21 @@ public class MineTweakerMod { @Mod.Instance(MODID) public static MineTweakerMod INSTANCE; - public final MCRecipeManager recipes; private final IScriptProvider scriptsGlobal; private final ScriptProviderCustom scriptsIMC; public MineTweakerMod() { + MCRecipeManager.recipes = (List) CraftingManager.getInstance().getRecipeList(); MineTweakerImplementationAPI.init( new MCOreDict(), - recipes = new MCRecipeManager(), + new MCRecipeManager(), new MCFurnaceManager(), MCGame.INSTANCE, new MCLoadedMods(), new MCFormatter(), new MCVanilla()); + MineTweakerImplementationAPI.logger.addLogger(new FileLogger(new File("minetweaker.log"))); MineTweakerImplementationAPI.platform = MCPlatformFunctions.INSTANCE; diff --git a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/item/MCItemStack.java b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/item/MCItemStack.java index 369d73f..b7423cb 100644 --- a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/item/MCItemStack.java +++ b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/item/MCItemStack.java @@ -53,7 +53,7 @@ public class MCItemStack implements IItemStack { throw new IllegalArgumentException("stack cannot be null"); stack = itemStack.copy(); - items = Collections.singletonList(this); + items = Collections.singletonList(this); } public MCItemStack(ItemStack itemStack, boolean wildcardSize) { @@ -67,13 +67,13 @@ public class MCItemStack implements IItemStack { throw new IllegalArgumentException("stack cannot be null"); stack = itemStack; - items = Collections.singletonList(this); + items = Collections.singletonList(this); this.tag = tag; } private MCItemStack(ItemStack itemStack, IData tag, boolean wildcardSize) { stack = itemStack; - items = Collections.singletonList(this); + items = Collections.singletonList(this); this.tag = tag; this.wildcardSize = wildcardSize; } diff --git a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/recipes/MCRecipeManager.java b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/recipes/MCRecipeManager.java index 9a14dad..bcb4fd1 100644 --- a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/recipes/MCRecipeManager.java +++ b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/recipes/MCRecipeManager.java @@ -2,12 +2,21 @@ package minetweaker.mc1710.recipes; import minetweaker.IUndoableAction; import minetweaker.MineTweakerAPI; +import minetweaker.MineTweakerImplementationAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.minecraft.MineTweakerMC; import minetweaker.api.player.IPlayer; -import minetweaker.api.recipes.*; +import minetweaker.api.recipes.ICraftingInventory; +import minetweaker.api.recipes.ICraftingRecipe; +import minetweaker.api.recipes.IRecipeAction; +import minetweaker.api.recipes.IRecipeFunction; +import minetweaker.api.recipes.IRecipeManager; +import minetweaker.api.recipes.ShapedRecipe; +import minetweaker.api.recipes.ShapelessRecipe; +import minetweaker.mc1710.item.MCItemStack; import minetweaker.mc1710.util.MineTweakerHacks; +import minetweaker.util.IEventHandler; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.InventoryCrafting; @@ -18,30 +27,40 @@ import net.minecraft.item.crafting.ShapedRecipes; import net.minecraft.item.crafting.ShapelessRecipes; import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; +import org.apache.commons.lang3.tuple.Pair; import stanhebben.zenscript.annotations.Optional; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Objects; +import java.util.Set; -import static minetweaker.api.minecraft.MineTweakerMC.*; +import static minetweaker.api.minecraft.MineTweakerMC.getIItemStack; +import static minetweaker.api.minecraft.MineTweakerMC.getItemStack; +import static minetweaker.api.minecraft.MineTweakerMC.getOreDict; /** * @author Stan */ -public class MCRecipeManager implements IRecipeManager { - private final List recipes; - private final List transformerRecipes; +public final class MCRecipeManager implements IRecipeManager { + public static final List recipesToAdd = new ArrayList<>(); + public static final List recipesToRemove = new ArrayList<>(); + public static final ActionRemoveRecipesNoIngredients actionRemoveRecipesNoIngredients = new ActionRemoveRecipesNoIngredients(); + + public static List recipes; + public static final List transformerRecipes = new ArrayList<>(); public MCRecipeManager() { - recipes = (List) CraftingManager.getInstance().getRecipeList(); - transformerRecipes = new ArrayList(); + MineTweakerImplementationAPI.onPostReload(new HandlePostReload()); } - public boolean hasTransformerRecipes() { + public static boolean hasTransformerRecipes() { return transformerRecipes.size() > 0; } - public void applyTransformations(ICraftingInventory inventory, IPlayer byPlayer) { + public static void applyTransformations(ICraftingInventory inventory, IPlayer byPlayer) { for (ICraftingRecipe recipe : transformerRecipes) { if (recipe.matches(inventory)) { recipe.applyTransformers(inventory, byPlayer); @@ -50,9 +69,35 @@ public class MCRecipeManager implements IRecipeManager { } } + private static boolean matches(Object input, IIngredient ingredient) { + if ((input == null) != (ingredient == null)) { + return false; + } else if (ingredient != null) { + if (input instanceof ItemStack) { + return ingredient.matches(getIItemStack((ItemStack) input)); + } else if (input instanceof String) { + return ingredient.contains(getOreDict((String) input)); + } + } + + return true; + } + + @Override + public List getAll() { + List results = new ArrayList<>(); + + for (IRecipe recipe : recipes) { + ICraftingRecipe converted = RecipeConverter.toCraftingRecipe(recipe); + results.add(converted); + } + + return results; + } + @Override public List getRecipesFor(IIngredient ingredient) { - List results = new ArrayList(); + List results = new ArrayList<>(); for (IRecipe recipe : recipes) { if (ingredient.matches(MineTweakerMC.getIItemStack(recipe.getRecipeOutput()))) { @@ -65,203 +110,56 @@ public class MCRecipeManager implements IRecipeManager { } @Override - public List getAll() { - List results = new ArrayList(); - - for (IRecipe recipe : recipes) { - ICraftingRecipe converted = RecipeConverter.toCraftingRecipe(recipe); - results.add(converted); + public void remove(IIngredient output, @Optional boolean nbtMatch) { + if(output == null) { + MineTweakerAPI.logError("Cannot remove recipes for a null item!"); + return; } - - return results; - } - - @Override - public int remove(IIngredient output, @Optional boolean nbtMatch) { - List toRemove = new ArrayList(); - List removeIndex = new ArrayList(); - for (int i = 0; i < recipes.size(); i++) { - IRecipe recipe = recipes.get(i); - - // certain special recipes have no predefined output. ignore those - // since these cannot be removed with MineTweaker scripts - if (recipe.getRecipeOutput() != null) { - if (nbtMatch ? output.matchesExact(getIItemStack(recipe.getRecipeOutput())) : output.matches(getIItemStack(recipe.getRecipeOutput()))) { - toRemove.add(recipe); - removeIndex.add(i); - } - } - } - - MineTweakerAPI.apply(new ActionRemoveRecipes(toRemove, removeIndex)); - return toRemove.size(); + actionRemoveRecipesNoIngredients.addOutput(output, nbtMatch); } @Override public void addShaped(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function, IRecipeAction action) { - addShaped(output, ingredients, function, action, false); + recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, function, action, false)); } @Override public void addShapedMirrored(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function, IRecipeAction action) { - addShaped(output, ingredients, function, action, true); + recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, function, action, true)); } @Override public void addShapeless(IItemStack output, IIngredient[] ingredients, IRecipeFunction function, IRecipeAction action) { - ShapelessRecipe recipe = new ShapelessRecipe(output, ingredients, function, action); - IRecipe irecipe = RecipeConverter.convert(recipe); - MineTweakerAPI.apply(new ActionAddRecipe(irecipe, recipe)); + if(checkShapelessNulls(output, ingredients)) + return; + + recipesToAdd.add(new ActionAddShapelessRecipe(output, ingredients, function, action)); + } + + private boolean checkShapelessNulls(IItemStack output, IIngredient[] ingredients) { + if(output != null && Arrays.stream(ingredients).allMatch(Objects::isNull)) { + MineTweakerAPI.logError("Null not allowed in shapeless recipes! Recipe for: " + output + " not created!"); + return true; + } + return false; } @Override - public int removeShaped(IIngredient output, IIngredient[][] ingredients) { - int ingredientsWidth = 0; - int ingredientsHeight = 0; - - if (ingredients != null) { - ingredientsHeight = ingredients.length; - - for (int i = 0; i < ingredients.length; i++) { - ingredientsWidth = Math.max(ingredientsWidth, ingredients[i].length); - } + public void removeShaped(IIngredient output, IIngredient[][] ingredients) { + if(output == null) { + MineTweakerAPI.logError("Cannot remove recipes for a null item!"); + return; } - - List toRemove = new ArrayList(); - List removeIndex = new ArrayList(); - outer: - for (int i = 0; i < recipes.size(); i++) { - IRecipe recipe = recipes.get(i); - - if (recipe.getRecipeOutput() == null || !output.matches(getIItemStack(recipe.getRecipeOutput()))) { - continue; - } - - if (ingredients != null) { - if (recipe instanceof ShapedRecipes) { - ShapedRecipes srecipe = (ShapedRecipes) recipe; - if (ingredientsWidth != srecipe.recipeWidth || ingredientsHeight != srecipe.recipeHeight) { - continue; - } - - for (int j = 0; j < ingredientsHeight; j++) { - IIngredient[] row = ingredients[j]; - for (int k = 0; k < ingredientsWidth; k++) { - IIngredient ingredient = k > row.length ? null : row[k]; - ItemStack recipeIngredient = srecipe.recipeItems[j * srecipe.recipeWidth + k]; - - if (!matches(recipeIngredient, ingredient)) { - continue outer; - } - } - } - } else if (recipe instanceof ShapedOreRecipe) { - ShapedOreRecipe srecipe = (ShapedOreRecipe) recipe; - int recipeWidth = MineTweakerHacks.getShapedOreRecipeWidth(srecipe); - int recipeHeight = srecipe.getRecipeSize() / recipeWidth; - if (ingredientsWidth != recipeWidth || ingredientsHeight != recipeHeight) { - continue; - } - - for (int j = 0; j < ingredientsHeight; j++) { - IIngredient[] row = ingredients[j]; - for (int k = 0; k < ingredientsWidth; k++) { - IIngredient ingredient = k > row.length ? null : row[k]; - Object input = srecipe.getInput()[j * recipeWidth + k]; - if (!matches(input, ingredient)) { - continue outer; - } - } - } - } - } else { - if (recipe instanceof ShapedRecipes) { - - } else if (recipe instanceof ShapedOreRecipe) { - - } else { - continue; - } - } - - toRemove.add(recipe); - removeIndex.add(i); - } - - MineTweakerAPI.apply(new ActionRemoveRecipes(toRemove, removeIndex)); - return toRemove.size(); + recipesToRemove.add(new ActionRemoveShapedRecipes(output, ingredients)); } @Override - public int removeShapeless(IIngredient output, IIngredient[] ingredients, boolean wildcard) { - List toRemove = new ArrayList(); - List removeIndex = new ArrayList(); - outer: - for (int i = 0; i < recipes.size(); i++) { - IRecipe recipe = recipes.get(i); - - if (recipe.getRecipeOutput() == null || !output.matches(getIItemStack(recipe.getRecipeOutput()))) { - continue; - } - - if (ingredients != null) { - if (recipe instanceof ShapelessRecipes) { - ShapelessRecipes srecipe = (ShapelessRecipes) recipe; - - if (ingredients.length > srecipe.getRecipeSize()) { - continue; - } else if (!wildcard && ingredients.length < srecipe.getRecipeSize()) { - continue; - } - - checkIngredient: - for (int j = 0; j < ingredients.length; j++) { - for (int k = 0; k < srecipe.getRecipeSize(); k++) { - if (matches(srecipe.recipeItems.get(k), ingredients[j])) { - continue checkIngredient; - } - } - - continue outer; - } - } else if (recipe instanceof ShapelessOreRecipe) { - ShapelessOreRecipe srecipe = (ShapelessOreRecipe) recipe; - ArrayList inputs = srecipe.getInput(); - - if (inputs.size() < ingredients.length) { - continue; - } - if (!wildcard && inputs.size() > ingredients.length) { - continue; - } - - checkIngredient: - for (int j = 0; j < ingredients.length; j++) { - for (int k = 0; k < srecipe.getRecipeSize(); k++) { - if (matches(inputs.get(k), ingredients[j])) { - continue checkIngredient; - } - } - - continue outer; - } - } - } else { - if (recipe instanceof ShapelessRecipes) { - - } else if (recipe instanceof ShapelessOreRecipe) { - - } else { - continue; - } - } - - toRemove.add(recipe); - removeIndex.add(i); + public void removeShapeless(IIngredient output, IIngredient[] ingredients, boolean wildcard) { + if(output == null) { + MineTweakerAPI.logError("Cannot remove recipes for a null item!"); + return; } - - MineTweakerAPI.apply(new ActionRemoveRecipes(toRemove, removeIndex)); - return toRemove.size(); + recipesToRemove.add(new ActionRemoveShapelessRecipes(output, ingredients, wildcard)); } @Override @@ -294,6 +192,352 @@ public class MCRecipeManager implements IRecipeManager { return getIItemStack(result); } } + + public abstract static class ActionBaseRemoveRecipes implements IUndoableAction { + public void removeRecipes(Set toRemove ) { + recipes.removeIf(toRemove::contains); + } + } + + public static class ActionRemoveRecipesNoIngredients extends ActionBaseRemoveRecipes implements IUndoableAction { + // pair of output, nbtMatch + private final List> outputs = new ArrayList<>(); + + public void addOutput(IIngredient output, @Optional boolean nbtMatch) { + outputs.add(Pair.of(output, nbtMatch)); + } + + @Override + public void apply() { + Set toRemove = new HashSet<>(); + + for (final IRecipe recipe : recipes) { + final ItemStack recipeOutput = recipe.getRecipeOutput(); + if (recipeOutput != null) { + final IItemStack stack = getIItemStack(recipeOutput); + if (matches(stack)) { + toRemove.add(recipe); + } + } + } + super.removeRecipes(toRemove); + } + + @Override + public boolean canUndo() { + return false; + } + + @Override + public void undo() { + + } + + @Override + public String describe() { + return "Removing recipes for various outputs"; + } + + @Override + public String describeUndo() { + return null; + } + + @Override + public Object getOverrideKey() { + return null; + } + + private boolean matches(IItemStack stack) { + for(Pair entry : outputs) { + IIngredient output = entry.getKey(); + Boolean nbtMatch = entry.getValue(); + if(nbtMatch ? output.matchesExact(stack) : output.matches(stack)) { + return true; + } + } + return false; + } + + } + + public static class ActionRemoveShapelessRecipes extends ActionBaseRemoveRecipes implements IUndoableAction { + final IIngredient output; + final IIngredient[] ingredients; + final boolean wildcard; + + public ActionRemoveShapelessRecipes(IIngredient output, IIngredient[] ingredients, boolean wildcard) { + this.output = output; + this.ingredients = ingredients; + this.wildcard = wildcard; + } + + @Override + public void apply() { + Set toRemove = new HashSet<>(); + + outer: + for (IRecipe recipe : recipes) { + if (recipe.getRecipeOutput() == null || !output.matches(new MCItemStack(recipe.getRecipeOutput()))) { + continue; + } + if (recipe instanceof ShapedRecipes) { + continue; + } + if (!(recipe instanceof ShapelessRecipes) && !(recipe instanceof ShapelessOreRecipe)) { + continue; + } + if (ingredients != null) { + if (recipe instanceof ShapelessRecipes) { + ShapelessRecipes srecipe = (ShapelessRecipes) recipe; + + if (ingredients.length > srecipe.getRecipeSize()) { + continue; + } else if (!wildcard && ingredients.length < srecipe.getRecipeSize()) { + continue; + } + + checkIngredient: + for (IIngredient ingredient : ingredients) { + for (int k = 0; k < srecipe.getRecipeSize(); k++) { + if (matches(srecipe.recipeItems.get(k), ingredient)) { + continue checkIngredient; + } + } + + continue outer; + } + } else if (recipe instanceof ShapelessOreRecipe) { + ShapelessOreRecipe srecipe = (ShapelessOreRecipe) recipe; + ArrayList inputs = srecipe.getInput(); + + if (inputs.size() < ingredients.length) { + continue; + } + if (!wildcard && inputs.size() > ingredients.length) { + continue; + } + + checkIngredient: + for (IIngredient ingredient : ingredients) { + for (int k = 0; k < srecipe.getRecipeSize(); k++) { + if (matches(inputs.get(k), ingredient)) { + continue checkIngredient; + } + } + + continue outer; + } + } + } + + toRemove.add(recipe); + } + MineTweakerAPI.logInfo("Removing " + toRemove.size() + " Shapeless recipes."); + super.removeRecipes(toRemove); + } + + @Override + public boolean canUndo() { + return false; + } + + @Override + public void undo() { + + } + + @Override + public String describe() { + if(output != null) { + return "Removing Shapeless recipes for " + output.toString(); + } else { + return "Trying to remove recipes for invalid output"; + } + } + + @Override + public String describeUndo() { + return null; + } + + @Override + public Object getOverrideKey() { + return null; + } + } + + public static class ActionRemoveShapedRecipes extends ActionBaseRemoveRecipes implements IUndoableAction { + final IIngredient output; + final IIngredient[][] ingredients; + + + public ActionRemoveShapedRecipes(IIngredient output, IIngredient[][] ingredients) { + this.output = output; + this.ingredients = ingredients; + } + + @Override + public void apply() { + int ingredientsWidth = 0; + int ingredientsHeight = 0; + + if (ingredients != null) { + ingredientsHeight = ingredients.length; + + for (IIngredient[] ingredient : ingredients) { + ingredientsWidth = Math.max(ingredientsWidth, ingredient.length); + } + } + + final Set toRemove = new HashSet<>(); + + outer: + for (final IRecipe recipe : recipes) { + final ItemStack output = recipe.getRecipeOutput(); + if (output == null || !this.output.matches(new MCItemStack(output))) { + continue; + } + if (!(recipe instanceof ShapedRecipes || recipe instanceof ShapedOreRecipe)) + continue; + + if (ingredients != null) { + boolean ore = recipe instanceof ShapedOreRecipe; + final ShapedRecipes shapedRecipe = !ore ? (ShapedRecipes) recipe : null; + final ShapedOreRecipe shapedOreRecipe = ore ? (ShapedOreRecipe) recipe : null; + + final int recipeWidth = ore ? MineTweakerHacks.getShapedOreRecipeWidth(shapedOreRecipe) : shapedRecipe.recipeWidth; + final int recipeHeight = ore ? shapedOreRecipe.getRecipeSize() / recipeWidth : shapedRecipe.recipeHeight; + + if (ingredientsWidth != recipeWidth || ingredientsHeight != recipeHeight) { + continue; + } + + for (int j = 0; j < ingredientsHeight; j++) { + final IIngredient[] row = ingredients[j]; + for (int k = 0; k < ingredientsWidth; k++) { + final IIngredient ingredient = k > row.length ? null : row[k]; + final Object input = (ore ? shapedOreRecipe.getInput() : shapedRecipe.recipeItems)[j * recipeWidth + k]; + + if (!matches(input, ingredient)) { + continue outer; + } + } + } + } + // If null ingredient list given, remove all ShapedRecipes with the given output + toRemove.add(recipe); + } + + MineTweakerAPI.logInfo(toRemove.size() + " removed"); + super.removeRecipes(toRemove); + } + + @Override + public boolean canUndo() { + return false; + } + + @Override + public void undo() { + + } + + @Override + public String describe() { + return null; + } + + @Override + public String describeUndo() { + return null; + } + + @Override + public Object getOverrideKey() { + return null; + } + } + + public static class ActionBaseAddRecipe implements IUndoableAction { + private final IRecipe iRecipe; + private final ICraftingRecipe craftingRecipe; + + protected final IItemStack output; + protected final boolean isShaped; + + private ActionBaseAddRecipe(ICraftingRecipe craftingRecipe, IItemStack output, boolean isShaped) { + this.iRecipe = RecipeConverter.convert(craftingRecipe); + this.craftingRecipe = craftingRecipe; + + this.output = output; + this.isShaped = isShaped; + +// if(craftingRecipe.hasTransformers()) +// transformerRecipes.add(craftingRecipe); + } + + @Override + public void apply() { + recipes.add(iRecipe); + if (craftingRecipe.hasTransformers()) + transformerRecipes.add(craftingRecipe); + } + + @Override + public boolean canUndo() { + return false; + } + + @Override + public void undo() { + + } + + @Override + public String describe() { + if(output != null) { + return "Adding " + (isShaped ? "shaped" : "shapeless") + " recipe for " + output.getDisplayName(); + } else { + return "Trying to add " + (isShaped ? "shaped" : "shapeless") + "recipe without correct output"; + } + } + + @Override + public String describeUndo() { + return null; + } + + @Override + public Object getOverrideKey() { + return null; + } + } + + private static class ActionAddShapedRecipe extends ActionBaseAddRecipe { + public ActionAddShapedRecipe(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function, IRecipeAction action, boolean mirrored) { + super(new ShapedRecipe(output, ingredients, function, action, mirrored), output, true); + } + } + + private static class ActionAddShapelessRecipe extends ActionBaseAddRecipe { + public ActionAddShapelessRecipe(IItemStack output, IIngredient[] ingredients, IRecipeFunction function, IRecipeAction action) { + super(new ShapelessRecipe(output, ingredients, function, action), output, false); + } + } + + public static class HandlePostReload implements IEventHandler + { + @Override + public void handle(MineTweakerImplementationAPI.ReloadEvent event) + { + System.out.println("MineTweaker: Applying additions and removals"); + MineTweakerAPI.apply(MCRecipeManager.actionRemoveRecipesNoIngredients); + MCRecipeManager.recipesToRemove.forEach(MineTweakerAPI::apply); + MCRecipeManager.recipesToAdd.forEach(MineTweakerAPI::apply); + + } + } private class ContainerVirtual extends Container { @Override @@ -301,119 +545,5 @@ public class MCRecipeManager implements IRecipeManager { return false; } } - - private class ActionRemoveRecipes implements IUndoableAction { - private final List removingIndices; - private final List removingRecipes; - - public ActionRemoveRecipes(List recipes, List indices) { - this.removingIndices = indices; - this.removingRecipes = recipes; - } - - @Override - public void apply() { - for (int i = removingIndices.size() - 1; i >= 0; i--) { - recipes.remove((int) removingIndices.get(i)); - } - } - - @Override - public boolean canUndo() { - return true; - } - - @Override - public void undo() { - for (int i = 0; i < removingIndices.size(); i++) { - int index = Math.min(recipes.size(), removingIndices.get(i)); - recipes.add(index, removingRecipes.get(i)); - } - } - - @Override - public String describe() { - return "Removing " + removingIndices.size() + " recipes"; - } - - @Override - public String describeUndo() { - return "Restoring " + removingIndices.size() + " recipes"; - } - - @Override - public Object getOverrideKey() { - return null; - } - } - - private class ActionAddRecipe implements IUndoableAction { - private final IRecipe recipe; - private final ICraftingRecipe craftingRecipe; - - public ActionAddRecipe(IRecipe recipe, ICraftingRecipe craftingRecipe) { - this.recipe = recipe; - this.craftingRecipe = craftingRecipe; - } - - @Override - public void apply() { - recipes.add(recipe); - if (craftingRecipe.hasTransformers()) { - transformerRecipes.add(craftingRecipe); - } - } - - @Override - public boolean canUndo() { - return true; - } - - @Override - public void undo() { - recipes.remove(recipe); - if (craftingRecipe.hasTransformers()) { - transformerRecipes.remove(craftingRecipe); - } - } - - @Override - public String describe() { - return "Adding recipe for " + recipe.getRecipeOutput().getDisplayName(); - } - - @Override - public String describeUndo() { - return "Removing recipe for " + recipe.getRecipeOutput().getDisplayName(); - } - - @Override - public Object getOverrideKey() { - return null; - } - } - - private void addShaped(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function, IRecipeAction action, boolean mirrored) { - ShapedRecipe recipe = new ShapedRecipe(output, ingredients, function, action, mirrored); - IRecipe irecipe = RecipeConverter.convert(recipe); - MineTweakerAPI.apply(new ActionAddRecipe(irecipe, recipe)); - } - - private static boolean matches(Object input, IIngredient ingredient) { - if ((input == null) != (ingredient == null)) { - return false; - } else if (ingredient != null) { - if (input instanceof ItemStack) { - if (!ingredient.matches(getIItemStack((ItemStack) input))) { - return false; - } - } else if (input instanceof String) { - if (!ingredient.contains(getOreDict((String) input))) { - return false; - } - } - } - - return true; - } + } diff --git a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/recipes/RecipeConverter.java b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/recipes/RecipeConverter.java index 56ef857..975e9f5 100644 --- a/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/recipes/RecipeConverter.java +++ b/MineTweaker3-MC1710-Main/src/main/java/minetweaker/mc1710/recipes/RecipeConverter.java @@ -53,7 +53,13 @@ public class RecipeConverter { } return type; } - + public static IRecipe convert(ICraftingRecipe recipe) { + if (recipe instanceof ShapedRecipe) + return RecipeConverter.convert((ShapedRecipe)recipe); + else + return RecipeConverter.convert((ShapelessRecipe) recipe); + } + public static IRecipe convert(ShapelessRecipe recipe) { IIngredient[] ingredients = recipe.getIngredients(); int type = getRecipeType(ingredients);