From 8fff244d9577f3e4358974175165a821c30b911f Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Sun, 29 Nov 2020 16:37:29 -0800 Subject: [PATCH] Make things undoable -- should (mostly) fix `/mt reload` --- .../MineTweakerImplementationAPI.java | 6 ++ .../java/minetweaker/runtime/MTTweaker.java | 4 +- .../mc1710/recipes/MCRecipeManager.java | 98 +++++++++++++------ configuration.gradle | 2 +- 4 files changed, 79 insertions(+), 31 deletions(-) diff --git a/MineTweaker3-API/src/main/java/minetweaker/MineTweakerImplementationAPI.java b/MineTweaker3-API/src/main/java/minetweaker/MineTweakerImplementationAPI.java index ddfbc39..a2f1382 100644 --- a/MineTweaker3-API/src/main/java/minetweaker/MineTweakerImplementationAPI.java +++ b/MineTweaker3-API/src/main/java/minetweaker/MineTweakerImplementationAPI.java @@ -58,6 +58,7 @@ 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 ONROLLBACK = new EventList<>(); private static final EventList ONRELOAD = new EventList<>(); private static final EventList ONPOSTRELOAD = new EventList<>(); @@ -573,6 +574,9 @@ public class MineTweakerImplementationAPI { * @param handler * @return */ + public static IEventHandle onRollbackEvent(IEventHandler handler) { + return ONROLLBACK.add(handler); + } public static IEventHandle onReloadEvent(IEventHandler handler) { return ONRELOAD.add(handler); } @@ -623,6 +627,8 @@ public class MineTweakerImplementationAPI { } MineTweakerAPI.tweaker.rollback(); + ONROLLBACK.publish(new ReloadEvent()); + if (MineTweakerAPI.server != null) { if (!MineTweakerAPI.server.isCommandAdded("minetweaker")) { server.addCommand("minetweaker", "", new String[]{"mt", "crafttweaker", "ct"}, new ICommandFunction() { diff --git a/MineTweaker3-API/src/main/java/minetweaker/runtime/MTTweaker.java b/MineTweaker3-API/src/main/java/minetweaker/runtime/MTTweaker.java index cfd2044..278ea71 100644 --- a/MineTweaker3-API/src/main/java/minetweaker/runtime/MTTweaker.java +++ b/MineTweaker3-API/src/main/java/minetweaker/runtime/MTTweaker.java @@ -52,6 +52,7 @@ public class MTTweaker implements ITweaker { Object overrideKey = action.getOverrideKey(); if (wereStuck.contains(action)) { + MineTweakerAPI.logInfo("WAS STUCK"); wereStuck.remove(action); if (overrideKey != null) { @@ -76,7 +77,8 @@ public class MTTweaker implements ITweaker { @Override public List rollback() { - List stuck = new ArrayList(); + // Note: Requires a OnRollback event to be fired. If this is called from MineTweakerImplementationAPI it's handled there. + List stuck = new ArrayList<>(); for (int i = actions.size() - 1; i >= 0; i--) { IUndoableAction action = actions.get(i); if (action.canUndo()) { 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 7658b82..b8a2500 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 @@ -48,12 +48,14 @@ 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 final List recipesToUndo = new ArrayList<>(); public static List recipes; public static final List transformerRecipes = new ArrayList<>(); public MCRecipeManager() { - MineTweakerImplementationAPI.onPostReload(new HandlePostReload()); + MineTweakerImplementationAPI.onPostReload(new HandleLateAdditionsAndRemovals()); + MineTweakerImplementationAPI.onRollbackEvent(new HandleLateAdditionsAndRemovals()); } public static boolean hasTransformerRecipes() { @@ -275,14 +277,19 @@ public final class MCRecipeManager implements IRecipeManager { public static class ActionRemoveRecipesNoIngredients extends ActionBaseRemoveRecipes implements IUndoableAction { // pair of output, nbtMatch private final List> outputs = new ArrayList<>(); + private final Set toRemove = new HashSet<>(); public void addOutput(IIngredient output, @Optional boolean nbtMatch) { outputs.add(Pair.of(output, nbtMatch)); } + + public void clearOutputs() { + outputs.clear(); + } @Override public void apply() { - Set toRemove = new HashSet<>(); + toRemove.clear(); for (final IRecipe recipe : recipes) { final ItemStack recipeOutput = recipe.getRecipeOutput(); @@ -298,12 +305,14 @@ public final class MCRecipeManager implements IRecipeManager { @Override public boolean canUndo() { - return false; + return !toRemove.isEmpty(); } @Override public void undo() { - + for(IRecipe recipe: toRemove) { + recipesToAdd.add(new ActionBaseAddRecipe(recipe)); + } } @Override @@ -313,7 +322,7 @@ public final class MCRecipeManager implements IRecipeManager { @Override public String describeUndo() { - return null; + return "Trying to restore " + toRemove.size() + " recipes."; } @Override @@ -338,7 +347,8 @@ public final class MCRecipeManager implements IRecipeManager { final IIngredient output; final IIngredient[] ingredients; final boolean wildcard; - + Set toRemove = new HashSet<>(); + public ActionRemoveShapelessRecipes(IIngredient output, IIngredient[] ingredients, boolean wildcard) { this.output = output; this.ingredients = ingredients; @@ -347,7 +357,7 @@ public final class MCRecipeManager implements IRecipeManager { @Override public void apply() { - Set toRemove = new HashSet<>(); + toRemove.clear(); outer: for (IRecipe recipe : recipes) { @@ -412,12 +422,14 @@ public final class MCRecipeManager implements IRecipeManager { @Override public boolean canUndo() { - return false; + return !toRemove.isEmpty(); } @Override public void undo() { - + for(IRecipe recipe: toRemove) { + recipesToAdd.add(new ActionBaseAddRecipe(recipe)); + } } @Override @@ -431,7 +443,7 @@ public final class MCRecipeManager implements IRecipeManager { @Override public String describeUndo() { - return null; + return "Trying to restore " + toRemove.size() + " shapeless recipes."; } @Override @@ -443,6 +455,8 @@ public final class MCRecipeManager implements IRecipeManager { public static class ActionRemoveShapedRecipes extends ActionBaseRemoveRecipes implements IUndoableAction { final IIngredient output; final IIngredient[][] ingredients; + final Set toRemove = new HashSet<>(); + public ActionRemoveShapedRecipes(IIngredient output, IIngredient[][] ingredients) { @@ -454,6 +468,7 @@ public final class MCRecipeManager implements IRecipeManager { public void apply() { int ingredientsWidth = 0; int ingredientsHeight = 0; + toRemove.clear(); if (ingredients != null) { ingredientsHeight = ingredients.length; @@ -463,8 +478,6 @@ public final class MCRecipeManager implements IRecipeManager { } } - final Set toRemove = new HashSet<>(); - outer: for (final IRecipe recipe : recipes) { final ItemStack output = recipe.getRecipeOutput(); @@ -508,22 +521,28 @@ public final class MCRecipeManager implements IRecipeManager { @Override public boolean canUndo() { - return false; + return !toRemove.isEmpty(); } @Override public void undo() { - + for(IRecipe recipe: toRemove) { + recipesToAdd.add(new ActionBaseAddRecipe(recipe)); + } } @Override public String describe() { - return null; + if(output != null) { + return "Removing Shaped recipes for " + output.toString(); + } else { + return "Trying to remove recipes for invalid output"; + } } @Override public String describeUndo() { - return null; + return "Trying to restore " + toRemove.size() + " shaped recipes."; } @Override @@ -538,39 +557,49 @@ public final class MCRecipeManager implements IRecipeManager { protected final IItemStack output; protected final boolean isShaped; + protected final boolean isRestoring; + + private ActionBaseAddRecipe(IRecipe iRecipe) { + this.iRecipe = iRecipe; + this.craftingRecipe = null; + this.isRestoring = true; + + this.isShaped = iRecipe instanceof ShapedRecipes; + this.output = new MCItemStack(iRecipe.getRecipeOutput()); + } private ActionBaseAddRecipe(ICraftingRecipe craftingRecipe, IItemStack output, boolean isShaped) { this.iRecipe = RecipeConverter.convert(craftingRecipe); this.craftingRecipe = craftingRecipe; + this.isRestoring = false; this.output = output; this.isShaped = isShaped; -// if(craftingRecipe.hasTransformers()) -// transformerRecipes.add(craftingRecipe); } @Override public void apply() { recipes.add(iRecipe); - if (craftingRecipe.hasTransformers()) + if (craftingRecipe != null && craftingRecipe.hasTransformers()) transformerRecipes.add(craftingRecipe); } @Override public boolean canUndo() { - return false; + return iRecipe != null; } @Override public void undo() { - + recipesToUndo.add(iRecipe); } @Override public String describe() { + if(output != null) { - return "Adding " + (isShaped ? "shaped" : "shapeless") + " recipe for " + output.getDisplayName(); + return "" + (this.isRestoring ? "Restoring" : "Adding ") + (isShaped ? "shaped" : "shapeless") + " recipe for " + output.getDisplayName(); } else { return "Trying to add " + (isShaped ? "shaped" : "shapeless") + "recipe without correct output"; } @@ -578,7 +607,7 @@ public final class MCRecipeManager implements IRecipeManager { @Override public String describeUndo() { - return null; + return "Undoing addition of " + output.getDisplayName(); } @Override @@ -598,17 +627,28 @@ public final class MCRecipeManager implements IRecipeManager { super(new ShapelessRecipe(output, ingredients, function, action), output, false); } } + + public static void applyAdditionsAndRemovals() { + System.out.println("MineTweaker: Applying additions and removals"); + MineTweakerAPI.apply(MCRecipeManager.actionRemoveRecipesNoIngredients); + if(recipesToUndo.size() > 0) { + recipes.removeIf(recipesToUndo::contains); + } + MCRecipeManager.recipesToRemove.forEach(MineTweakerAPI::apply); + MCRecipeManager.recipesToAdd.forEach(MineTweakerAPI::apply); + + actionRemoveRecipesNoIngredients.clearOutputs(); + recipesToRemove.clear();; + recipesToAdd.clear(); + recipesToUndo.clear(); + } - public static class HandlePostReload implements IEventHandler + public static class HandleLateAdditionsAndRemovals 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); - + MCRecipeManager.applyAdditionsAndRemovals(); } } diff --git a/configuration.gradle b/configuration.gradle index ae68534..bd5645b 100644 --- a/configuration.gradle +++ b/configuration.gradle @@ -4,7 +4,7 @@ import javax.tools.ToolProvider // ### Global settings ### // ####################### -ext.mineTweakerVersion = "3.2.1-GTNH" +ext.mineTweakerVersion = "3.2.2-GTNH" if (System.getenv('BUILD_NUMBER') != null) project.mineTweakerVersion += "."+System.getenv('BUILD_NUMBER') else {