Compare commits

..
4 Commits
Author SHA1 Message Date
Jason Mitchell 8fff244d95 Make things undoable -- should (mostly) fix /mt reload 2020-11-29 16:37:29 -08:00
bartimaeusnek e4a0cc8f35 Fixed an oversight 2020-11-29 21:13:49 +01:00
bartimaeusnek 7c0eb67215 This is why i should init submodules. 2020-11-29 21:10:20 +01:00
bartimaeusnek 9167cd1f1d Added Overloads 2020-11-29 21:04:11 +01:00
5 changed files with 180 additions and 37 deletions

No files matched your search

@@ -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<ReloadEvent> ONROLLBACK = new EventList<>();
private static final EventList<ReloadEvent> ONRELOAD = new EventList<>();
private static final EventList<ReloadEvent> ONPOSTRELOAD = new EventList<>();
@@ -573,6 +574,9 @@ public class MineTweakerImplementationAPI {
* @param handler
* @return
*/
public static IEventHandle onRollbackEvent(IEventHandler<ReloadEvent> handler) {
return ONROLLBACK.add(handler);
}
public static IEventHandle onReloadEvent(IEventHandler<ReloadEvent> 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() {
@@ -41,7 +41,9 @@ public interface IRecipeManager {
*/
@ZenMethod
int remove(IIngredient output, @Optional boolean nbtMatch);
int remove(IIngredient output);
/**
* Adds a shaped recipe.
*
@@ -51,7 +53,12 @@ public interface IRecipeManager {
*/
@ZenMethod
void addShaped(IItemStack output, IIngredient[][] ingredients, @Optional IRecipeFunction function, @Optional IRecipeAction action);
void addShaped(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function);
void addShaped(IItemStack output, IIngredient[][] ingredients, IRecipeAction action);
void addShaped(IItemStack output, IIngredient[][] ingredients);
/**
* Adds a mirrored shaped recipe.
*
@@ -61,7 +68,12 @@ public interface IRecipeManager {
*/
@ZenMethod
void addShapedMirrored(IItemStack output, IIngredient[][] ingredients, @Optional IRecipeFunction function, @Optional IRecipeAction action);
void addShapedMirrored(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function);
void addShapedMirrored(IItemStack output, IIngredient[][] ingredients, IRecipeAction action);
void addShapedMirrored(IItemStack output, IIngredient[][] ingredients);
/**
* Adds a shapeless recipe.
*
@@ -71,7 +83,13 @@ public interface IRecipeManager {
*/
@ZenMethod
void addShapeless(IItemStack output, IIngredient[] ingredients, @Optional IRecipeFunction function, @Optional IRecipeAction action);
void addShapeless(IItemStack output, IIngredient[] ingredients, IRecipeFunction function);
void addShapeless(IItemStack output, IIngredient[] ingredients, IRecipeAction action);
void addShapeless(IItemStack output, IIngredient[] ingredients);
/**
* Removes shaped recipes.
*
@@ -82,7 +100,8 @@ public interface IRecipeManager {
*/
@ZenMethod
int removeShaped(IIngredient output, @Optional IIngredient[][] ingredients);
int removeShaped(IIngredient output);
/**
* Removes shapeless recipes.
*
@@ -95,7 +114,13 @@ public interface IRecipeManager {
*/
@ZenMethod
int removeShapeless(IIngredient output, @Optional IIngredient[] ingredients, @Optional boolean wildcard);
int removeShapeless(IIngredient output, boolean wildcard);
int removeShapeless(IIngredient output, IIngredient[] ingredients);
int removeShapeless(IIngredient output);
/**
* Performs a crafting with the specified ingredients. Returns null if no
* crafting recipe exists.
@@ -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<IUndoableAction> rollback() {
List<IUndoableAction> stuck = new ArrayList<IUndoableAction>();
// Note: Requires a OnRollback event to be fired. If this is called from MineTweakerImplementationAPI it's handled there.
List<IUndoableAction> stuck = new ArrayList<>();
for (int i = actions.size() - 1; i >= 0; i--) {
IUndoableAction action = actions.get(i);
if (action.canUndo()) {
@@ -48,12 +48,14 @@ public final class MCRecipeManager implements IRecipeManager {
public static final List<ActionBaseAddRecipe> recipesToAdd = new ArrayList<>();
public static final List<ActionBaseRemoveRecipes> recipesToRemove = new ArrayList<>();
public static final ActionRemoveRecipesNoIngredients actionRemoveRecipesNoIngredients = new ActionRemoveRecipesNoIngredients();
public static final List<IRecipe> recipesToUndo = new ArrayList<>();
public static List<IRecipe> recipes;
public static final List<ICraftingRecipe> transformerRecipes = new ArrayList<>();
public MCRecipeManager() {
MineTweakerImplementationAPI.onPostReload(new HandlePostReload());
MineTweakerImplementationAPI.onPostReload(new HandleLateAdditionsAndRemovals());
MineTweakerImplementationAPI.onRollbackEvent(new HandleLateAdditionsAndRemovals());
}
public static boolean hasTransformerRecipes() {
@@ -109,6 +111,11 @@ public final class MCRecipeManager implements IRecipeManager {
return results;
}
@Override
public int remove(IIngredient output) {
return remove(output, false);
}
@Override
public int remove(IIngredient output, @Optional boolean nbtMatch) {
if(output == null) {
@@ -119,11 +126,41 @@ public final class MCRecipeManager implements IRecipeManager {
return 1;
}
@Override
public void addShaped(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function) {
recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, function, null, false));
}
@Override
public void addShaped(IItemStack output, IIngredient[][] ingredients, IRecipeAction action) {
recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, null, action, false));
}
@Override
public void addShaped(IItemStack output, IIngredient[][] ingredients) {
recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, null, null, false));
}
@Override
public void addShaped(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function, IRecipeAction action) {
recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, function, action, false));
}
@Override
public void addShapedMirrored(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function) {
recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, function, null, true));
}
@Override
public void addShapedMirrored(IItemStack output, IIngredient[][] ingredients) {
recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, null, null, true));
}
@Override
public void addShapedMirrored(IItemStack output, IIngredient[][] ingredients, IRecipeAction action) {
recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, null, action, true));
}
@Override
public void addShapedMirrored(IItemStack output, IIngredient[][] ingredients, IRecipeFunction function, IRecipeAction action) {
recipesToAdd.add(new ActionAddShapedRecipe(output, ingredients, function, action, true));
@@ -137,6 +174,21 @@ public final class MCRecipeManager implements IRecipeManager {
recipesToAdd.add(new ActionAddShapelessRecipe(output, ingredients, function, action));
}
@Override
public void addShapeless(IItemStack output, IIngredient[] ingredients, IRecipeFunction function){
addShapeless(output, ingredients, function, null);
}
@Override
public void addShapeless(IItemStack output, IIngredient[] ingredients, IRecipeAction action){
addShapeless(output, ingredients, null, action);
}
@Override
public void addShapeless(IItemStack output, IIngredient[] ingredients){
addShapeless(output, ingredients, null, null);
}
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!");
@@ -155,6 +207,11 @@ public final class MCRecipeManager implements IRecipeManager {
return 1;
}
@Override
public int removeShaped(IIngredient output) {
return removeShaped(output, null);
}
@Override
public int removeShapeless(IIngredient output, IIngredient[] ingredients, boolean wildcard) {
if(output == null) {
@@ -165,6 +222,21 @@ public final class MCRecipeManager implements IRecipeManager {
return 1;
}
@Override
public int removeShapeless(IIngredient output, IIngredient[] ingredients) {
return removeShapeless(output, ingredients, false);
}
@Override
public int removeShapeless(IIngredient output, boolean wildcard) {
return removeShapeless(output, null, wildcard);
}
@Override
public int removeShapeless(IIngredient output) {
return removeShapeless(output, null, true);
}
@Override
public IItemStack craft(IItemStack[][] contents) {
Container container = new ContainerVirtual();
@@ -205,14 +277,19 @@ public final class MCRecipeManager implements IRecipeManager {
public static class ActionRemoveRecipesNoIngredients extends ActionBaseRemoveRecipes implements IUndoableAction {
// pair of output, nbtMatch
private final List<Pair<IIngredient, Boolean>> outputs = new ArrayList<>();
private final Set<IRecipe> 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<IRecipe> toRemove = new HashSet<>();
toRemove.clear();
for (final IRecipe recipe : recipes) {
final ItemStack recipeOutput = recipe.getRecipeOutput();
@@ -228,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
@@ -243,7 +322,7 @@ public final class MCRecipeManager implements IRecipeManager {
@Override
public String describeUndo() {
return null;
return "Trying to restore " + toRemove.size() + " recipes.";
}
@Override
@@ -268,7 +347,8 @@ public final class MCRecipeManager implements IRecipeManager {
final IIngredient output;
final IIngredient[] ingredients;
final boolean wildcard;
Set<IRecipe> toRemove = new HashSet<>();
public ActionRemoveShapelessRecipes(IIngredient output, IIngredient[] ingredients, boolean wildcard) {
this.output = output;
this.ingredients = ingredients;
@@ -277,7 +357,7 @@ public final class MCRecipeManager implements IRecipeManager {
@Override
public void apply() {
Set<IRecipe> toRemove = new HashSet<>();
toRemove.clear();
outer:
for (IRecipe recipe : recipes) {
@@ -342,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
@@ -361,7 +443,7 @@ public final class MCRecipeManager implements IRecipeManager {
@Override
public String describeUndo() {
return null;
return "Trying to restore " + toRemove.size() + " shapeless recipes.";
}
@Override
@@ -373,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<IRecipe> toRemove = new HashSet<>();
public ActionRemoveShapedRecipes(IIngredient output, IIngredient[][] ingredients) {
@@ -384,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;
@@ -393,8 +478,6 @@ public final class MCRecipeManager implements IRecipeManager {
}
}
final Set<IRecipe> toRemove = new HashSet<>();
outer:
for (final IRecipe recipe : recipes) {
final ItemStack output = recipe.getRecipeOutput();
@@ -438,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
@@ -468,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";
}
@@ -508,7 +607,7 @@ public final class MCRecipeManager implements IRecipeManager {
@Override
public String describeUndo() {
return null;
return "Undoing addition of " + output.getDisplayName();
}
@Override
@@ -528,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<MineTweakerImplementationAPI.ReloadEvent>
public static class HandleLateAdditionsAndRemovals implements IEventHandler<MineTweakerImplementationAPI.ReloadEvent>
{
@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();
}
}
+1 -1
View File
@@ -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 {