ModTweaker2 Rework

- Revised base classes for IUndoableAction
- Reordered lots of code, so stuck actions should be less frequent
- Added wildcard support to many "remove" methods
- Started working on exist & duplicate checks
This commit is contained in:
Zixxl committed 2015-06-29 16:36:35 +02:00
1 parent f5034b04d8
commit da33cc6fab
112 files changed
+4596 -3522

No files matched your search

@@ -2,8 +2,6 @@ package modtweaker2.utils;
import java.util.ArrayList;
import net.minecraftforge.common.util.ForgeDirection;
public class ArrayUtils {
@@ -1,56 +1,22 @@
package modtweaker2.utils;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import java.util.List;
import static modtweaker2.helpers.StackHelper.areEqual;
import modtweaker2.helpers.InputHelper;
import net.minecraft.item.crafting.IRecipe;
public abstract class BaseCraftingAddition extends BaseDescriptionAddition {
protected final List<IRecipe> list;
protected final boolean shapeless;
protected final ItemStack output;
protected final Object[] recipe;
public abstract class BaseCraftingAddition extends BaseListAddition<IRecipe> {
public BaseCraftingAddition(String name, boolean shapeless, List list, ItemStack output, Object... recipe) {
super(name);
this.shapeless = shapeless;
this.output = output;
this.recipe = recipe;
this.list = list;
}
@Override
public void apply() {
if (shapeless) applyShapeless();
else applyShaped();
}
public abstract void applyShaped();
public abstract void applyShapeless();
@Override
public boolean canUndo() {
return list != null;
}
@Override
public void undo() {
IRecipe remove = null;
for (IRecipe recipe : list) {
if (recipe.getRecipeOutput() != null && areEqual(recipe.getRecipeOutput(), output)) {
remove = recipe;
break;
}
}
list.remove(remove);
}
@Override
public String getRecipeInfo() {
return output.getDisplayName();
}
protected BaseCraftingAddition(String name, List<IRecipe> list) {
super(name, list);
}
protected BaseCraftingAddition(String name, List<IRecipe> list, List<IRecipe> recipes) {
super(name, list, recipes);
}
@Override
protected String getRecipeInfo(IRecipe recipe) {
return InputHelper.getStackDescription(recipe.getRecipeOutput());
}
}
@@ -1,29 +1,35 @@
package modtweaker2.utils;
import static modtweaker2.helpers.InputHelper.toIItemStack;
import static modtweaker2.helpers.StackHelper.matches;
import java.util.LinkedList;
import java.util.List;
import minetweaker.api.item.IIngredient;
import modtweaker2.helpers.InputHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import java.util.List;
import static modtweaker2.helpers.StackHelper.areEqual;
public class BaseCraftingRemoval extends BaseListRemoval {
public BaseCraftingRemoval(String name, List list, ItemStack stack) {
super(name, list, stack);
public class BaseCraftingRemoval extends BaseListRemoval<IRecipe> {
public BaseCraftingRemoval(String name, List<IRecipe> list, List<IRecipe> recipes) {
super(name, list, recipes);
}
@Override
public void apply() {
for (IRecipe r : (List<IRecipe>) list) {
if (r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && areEqual(r.getRecipeOutput(), stack)) {
recipes.add(r);
}
}
super.apply();
}
@Override
public String getRecipeInfo() {
return stack.getDisplayName();
}
}
@Override
public String getRecipeInfo(IRecipe recipe) {
return InputHelper.getStackDescription(recipe.getRecipeOutput());
}
public static List<IRecipe> getRecipes(List<IRecipe> list, IIngredient ingredient) {
List<IRecipe> recipes = new LinkedList<IRecipe>();
for (IRecipe r : list) {
if (r != null && r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && matches(ingredient, toIItemStack(r.getRecipeOutput()))) {
recipes.add(r);
}
}
return recipes;
}
}
@@ -1,36 +0,0 @@
package modtweaker2.utils;
import net.minecraft.item.ItemStack;
import minetweaker.IUndoableAction;
public abstract class BaseDescriptionAddition implements IUndoableAction {
protected String description;
public BaseDescriptionAddition(String description) {
this.description = description;
}
@Override
public boolean canUndo() {
return true;
}
public String getRecipeInfo() {
return "Unknown Item";
}
@Override
public String describe() {
return "Adding " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public String describeUndo() {
return "Removing " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public Object getOverrideKey() {
return null;
}
}
@@ -1,35 +0,0 @@
package modtweaker2.utils;
import minetweaker.IUndoableAction;
public abstract class BaseDescriptionRemoval implements IUndoableAction {
protected String description;
public BaseDescriptionRemoval(String description) {
this.description = description;
}
@Override
public boolean canUndo() {
return true;
}
public String getRecipeInfo() {
return "Unknown Item";
}
@Override
public String describe() {
return "Removing " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public String describeUndo() {
return "Restoring " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public Object getOverrideKey() {
return null;
}
}
@@ -1,65 +1,65 @@
package modtweaker2.utils;
import minetweaker.IUndoableAction;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import java.util.List;
public abstract class BaseListAddition implements IUndoableAction {
protected final List list;
protected final Object recipe;
protected String description;
import modtweaker2.helpers.LogHelper;
public BaseListAddition(String description, List list, Object recipe) {
this(list, recipe);
this.description = description;
}
public abstract class BaseListAddition<T> extends BaseListModification<T> {
public BaseListAddition(List list, Object recipe) {
this.list = list;
this.recipe = recipe;
}
protected BaseListAddition(String name, List<T> list) {
super(name, list);
}
protected BaseListAddition(String name, List<T> list, List<T> recipies) {
this(name, list);
if(recipes != null) {
recipes.addAll(recipies);
}
}
@Override
public void apply() {
list.add(recipe);
}
@Override
public void apply() {
if(recipes.isEmpty()) {
return;
}
@Override
public boolean canUndo() {
return list != null;
}
for(T recipe : recipes) {
if(recipe != null) {
if(this.list.add(recipe)) {
successful.add(recipe);
} else {
LogHelper.logError(String.format("Error adding %s Recipe for %s", this.name, this.getRecipeInfo(recipe)));
}
} else {
LogHelper.logError(String.format("Error adding %s Recipe: null object", this.name));
}
}
}
@Override
public void undo() {
if(this.successful.isEmpty()) {
return;
}
for(T recipe : successful) {
if(recipe != null) {
if(!this.list.remove(recipe)) {
LogHelper.logError(String.format("Error removing %s Recipe for %s", this.name, this.getRecipeInfo(recipe)));
}
} else {
LogHelper.logError(String.format("Error removing %s Recipe: null object", this.name));
}
}
}
@Override
public void undo() {
list.remove(recipe);
}
@Override
public String describe() {
return String.format("[ModTweaker2] Adding %d %s Recipe(s) for %s", this.recipes.size(), this.name, this.getRecipeInfo());
}
public String getRecipeInfo() {
return "Unknown Item";
}
@Override
public String describe() {
if (recipe instanceof ItemStack)
return "Adding " + description + " Recipe for :" + ((ItemStack) recipe).getDisplayName();
else if (recipe instanceof FluidStack)
return "Adding " + description + " Recipe for :" + ((FluidStack) recipe).getFluid().getLocalizedName();
else return "Adding " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public String describeUndo() {
if (recipe instanceof ItemStack)
return "Removing " + description + " Recipe for :" + ((ItemStack) recipe).getDisplayName();
else if (recipe instanceof FluidStack)
return "Removing " + description + " Recipe for :" + ((FluidStack) recipe).getFluid().getLocalizedName();
else return "Removing " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public Object getOverrideKey() {
return null;
}
}
@Override
public String describeUndo() {
return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %s", this.recipes.size(), this.name, this.getRecipeInfo());
}
}
@@ -0,0 +1,118 @@
package modtweaker2.utils;
import java.util.LinkedList;
import java.util.List;
public abstract class BaseListModification<T> extends BaseUndoable {
protected final List<T> list;
protected final LinkedList<T> recipes;
protected final LinkedList<T> successful;
protected BaseListModification(String name, List<T> list) {
super(name);
this.list = list;
this.recipes = new LinkedList<T>();
this.successful = new LinkedList<T>();
}
@Override
public boolean canUndo() {
if(list == null || recipes.isEmpty() || successful.isEmpty())
return false;
return true;
}
@Override
protected String getRecipeInfo() {
if(!recipes.isEmpty())
{
StringBuilder sb = new StringBuilder();
for(T recipe : recipes) {
if(recipe != null) {
sb.append(getRecipeInfo(recipe)).append(", ");
}
}
sb.setLength(sb.length() - 2);
return sb.toString();
}
return super.getRecipeInfo();
}
/**
* This method must be overwritten by the extending classes. It should return
* the name of the key item, for which the recipe is for. For example for machines
* which produce new items, it should return the name of the ouput. For machines
* which are processing items (like a pulverizer) it should return the name of the
* the input. Another example would be the name of the enchantmant for a thaumcraft
* infusion recipe.
*/
protected abstract String getRecipeInfo(T recipe);
@Override
public int hashCode() {
return 37 * super.hashCode() ^ (list != null ? 43 * list.hashCode() : 0);
}
@Override
public boolean equals(Object obj) {
if(obj == this)
return true;
if (obj == null)
return false;
// Do both actions have the same base?
if(!super.equals(obj))
return false;
if (!(obj instanceof BaseListModification<?>))
return false;
BaseListModification<?> mod = (BaseListModification<?>)obj;
// Do both actions reference the same list?
if(this.list != mod.list) {
return false;
}
// Do both actions contain the same recipes?
if(recipes.size() != mod.recipes.size()) {
return false;
}
for(T recipe : recipes) {
boolean found = false;
for(T otherRecipe : (List<T>)mod.recipes) {
if(equals(recipe, otherRecipe)) {
found = true;
break;
}
}
if(!found) {
return false;
}
}
return true;
}
/**
* Compares two recipes if they are equal. If the mod does not overwrite equals() in
* their recipe object, this method should be overwritten by the extending classes to
* add a custom equals method
*/
protected boolean equals(T recipe, T otherRecipe) {
if(recipe == otherRecipe) {
return true;
}
if(!recipe.equals(otherRecipe)) {
return false;
}
return true;
}
}
@@ -1,133 +1,65 @@
package modtweaker2.utils;
import minetweaker.IUndoableAction;
import minetweaker.MineTweakerAPI;
import modtweaker2.helpers.LogHelper;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import java.util.LinkedList;
import java.util.List;
public abstract class BaseListRemoval implements IUndoableAction {
protected final String description;
protected final List list;
import modtweaker2.helpers.LogHelper;
protected final FluidStack fluid;
protected final ItemStack stack;
protected final LinkedList recipes = new LinkedList();
public BaseListRemoval(String description, List list, ItemStack stack, FluidStack fluid) {
this.list = list;
this.stack = stack;
this.description = description;
this.fluid = fluid;
}
public BaseListRemoval(String description, List list, ItemStack stack) {
this(description, list, stack, null);
}
public BaseListRemoval(String description, List list, FluidStack fluid) {
this(description, list, null, fluid);
}
public BaseListRemoval(List list, ItemStack stack) {
this(null, list, stack);
}
public BaseListRemoval(List list, FluidStack stack) {
this(null, list, stack);
}
public BaseListRemoval(String description, List list) {
this(description, list, null, null);
}
@Override
public void apply() {
if(recipes.size() == 0) {
MineTweakerAPI.logWarning(String.format("No %s Recipes to remove for: %s", this.description, getRecipeInfo()));
return;
}
for(Object recipe : recipes) {
list.remove(recipe);
}
}
@Override
public boolean canUndo() {
return recipes.size() > 0;
}
@Override
public void undo() {
for(Object recipe : recipes) {
list.add(recipe);
}
}
public String getRecipeInfo() {
return "Unknown Item";
}
@Override
public String describe() {
if(recipes.size() > 0) {
return String.format("Removing %d %s Recipe(s) for: %s", recipes.size(), this.description, getRecipeNames(recipes));
} else {
return String.format("Removing %s Recipe(s) for: %s", this.description, getRecipeInfo());
}
}
@Override
public String describeUndo() {
if(recipes.size() > 0) {
return String.format("Restoring %d %s Recipe(s) for: %s", recipes.size(), this.description, getRecipeNames(recipes));
} else {
return String.format("No %s Recipes found to restore for: %s", this.description, getRecipeInfo());
public abstract class BaseListRemoval<T> extends BaseListModification<T> {
protected BaseListRemoval(String name, List<T> list) {
super(name, list);
}
protected BaseListRemoval(String name, List<T> list, List<T> recipies) {
this(name, list);
if(recipes != null) {
recipes.addAll(recipies);
}
}
}
@Override
public void apply() {
if(recipes.isEmpty()) {
return;
}
for(T recipe : this.recipes) {
if(recipe != null) {
if(this.list.remove(recipe)) {
successful.add(recipe);
} else {
LogHelper.logError(String.format("Error removing %s Recipe for %s", name, getRecipeInfo(recipe)));
}
} else {
LogHelper.logError(String.format("Error removing %s Recipe: null object", name));
}
}
}
@Override
public void undo() {
if(successful.isEmpty()) {
return;
}
for(T recipe : successful) {
if(recipe != null) {
if(!list.add(recipe)) {
LogHelper.logError(String.format("Error restoring %s Recipe for %s", name, getRecipeInfo(recipe)));
}
} else {
LogHelper.logError(String.format("Error restoring %s Recipe: null object", name));
}
}
}
@Override
public Object getOverrideKey() {
return null;
}
/***
* Returns the names of the recipes inside the list
* @param list which holds the recipes
* @return name or list of names describing the recipe
*/
private String getRecipeNames(LinkedList list) {
if(list.size() == 0)
return getRecipeInfo();
if(list.size() == 1) {
if(recipes.getFirst() instanceof ItemStack)
return ((ItemStack)list.getFirst()).getDisplayName();
else if (recipes.getFirst() instanceof FluidStack)
return ((FluidStack)list.getFirst()).getFluid().getLocalizedName();
else
return getRecipeInfo();
}
StringBuilder sb = new StringBuilder();
if(recipes.getFirst() instanceof ItemStack) {
for(ItemStack itemStack : (List<ItemStack>)list) {
sb.append(itemStack.getDisplayName()).append(", ");
}
} else if (recipes.getFirst() instanceof FluidStack) {
for(FluidStack fluidStack : (List<FluidStack>)list) {
sb.append(fluidStack.getFluid().getLocalizedName()).append(", ");
}
} else {
sb.append(getRecipeInfo()).append(", ");
}
sb.setLength(sb.length() - 2);
return sb.toString();
}
}
@Override
public String describe() {
return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %s", this.recipes.size(), this.name, this.getRecipeInfo());
}
@Override
public String describeUndo() {
return String.format("[ModTweaker2] Restoring %d %s Recipe(s) for %s", this.recipes.size(), this.name, this.getRecipeInfo());
}
}
@@ -1,69 +1,75 @@
package modtweaker2.utils;
import minetweaker.IUndoableAction;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public abstract class BaseMapAddition implements IUndoableAction {
protected final Map map;
protected String description;
protected Object key;
protected Object recipe;
import modtweaker2.helpers.LogHelper;
public BaseMapAddition(Map map, Object key, Object recipe) {
this(null, map, key, recipe);
}
public abstract class BaseMapAddition<K, V> extends BaseMapModification<K, V> {
protected final HashMap<K, V> overwritten;
protected BaseMapAddition(String name, Map<K, V> map) {
super(name, map);
this.overwritten = new HashMap<K, V>();
}
protected BaseMapAddition(String name, Map<K, V> map, Map<K, V> recipes) {
this(name, map);
recipes.putAll(recipes);
}
public BaseMapAddition(String description, Map map, Object key, Object recipe) {
this.map = map;
this.key = key;
this.recipe = recipe;
this.description = description;
}
@Override
public void apply() {
if(recipes.isEmpty())
return;
for(Entry<K, V> entry : recipes.entrySet()) {
V value = map.put(entry.getKey(), entry.getValue());
if(value != null) {
LogHelper.logWarning(String.format("Overwritten %s Recipe for %s", name, getRecipeInfo( new AbstractMap.SimpleEntry<K, V>(entry.getKey(), value))));
overwritten.put(entry.getKey(), value);
}
successful.put(entry.getKey(), entry.getValue());
}
}
@Override
public void apply() {
map.put(key, recipe);
}
@Override
public void undo() {
if(successful.isEmpty() || overwritten.isEmpty())
return;
for(K key : recipes.keySet()) {
map.remove(key);
}
for(Entry<K, V> entry : successful.entrySet()) {
V value = map.remove(entry.getKey());
if(value == null) {
LogHelper.logError(String.format("Error removing %s Recipe: null object", name));
}
}
for(Entry<K, V> entry : overwritten.entrySet()) {
V value = map.put(entry.getKey(), entry.getValue());
if(value != null) {
LogHelper.logWarning(String.format("Overwritten %s Recipe which should not exist for %s", name, getRecipeInfo(new AbstractMap.SimpleEntry<K, V>(entry.getKey(), value))));
}
}
}
@Override
public boolean canUndo() {
return map != null;
}
@Override
public String describe() {
return String.format("Adding %d %s Recipe(s) for %d", recipes.size(), name, getRecipeInfo());
}
@Override
public void undo() {
map.remove(key);
}
public String getRecipeInfo() {
return "Unknown Item";
}
@Override
public String describe() {
if (recipe instanceof ItemStack)
return "Adding " + description + " Recipe for :" + ((ItemStack) recipe).getDisplayName();
else if (recipe instanceof FluidStack)
return "Adding " + description + " Recipe for :" + ((FluidStack) recipe).getFluid().getLocalizedName();
else
return "Adding " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public String describeUndo() {
if (recipe instanceof ItemStack)
return "Removing " + description + " Recipe for :" + ((ItemStack) recipe).getDisplayName();
else if (recipe instanceof FluidStack)
return "Removing " + description + " Recipe for :" + ((FluidStack) recipe).getFluid().getLocalizedName();
else
return "Removing " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public Object getOverrideKey() {
return null;
}
@Override
public String describeUndo() {
return String.format("Removing %d %s Recipe(s) for %d", recipes.size(), name, getRecipeInfo());
}
}
@@ -0,0 +1,50 @@
package modtweaker2.utils;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public abstract class BaseMapModification<K, V> extends BaseUndoable {
protected final Map<K, V> map;
protected final HashMap<K, V> recipes;
protected final HashMap<K, V> successful;
protected BaseMapModification(String name, Map<K, V> map) {
super(name);
this.map = map;
this.recipes = new HashMap<K, V>();
this.successful = new HashMap<K, V>();
}
@Override
public boolean canUndo() {
return !recipes.isEmpty();
}
@Override
protected String getRecipeInfo() {
if(!recipes.isEmpty())
{
StringBuilder sb = new StringBuilder();
for(Entry<K, V> recipe : recipes.entrySet()) {
if(recipe != null) {
sb.append(getRecipeInfo(recipe)).append(", ");
}
}
sb.setLength(sb.length() - 2);
return sb.toString();
}
return super.getRecipeInfo();
}
/**
* This method must be overwritten by the extending classes. It should return
* the name of the key item, for which the recipe is for. For example for machines
* which produce new items, it should return the name of the ouput. For machines
* which are processing items (like a pulverizer) it should return the name of the
* the input. Another example would be the name of the enchantmant for a thaumcraft
* infusion recipe.
*/
protected abstract String getRecipeInfo(Entry<K, V> recipe);
}
@@ -1,73 +1,62 @@
package modtweaker2.utils;
import java.util.Map;
import java.util.Map.Entry;
import minetweaker.IUndoableAction;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import modtweaker2.helpers.LogHelper;
public abstract class BaseMapRemoval implements IUndoableAction {
protected final Map map;
protected Object stack;
protected String description;
protected Object key;
protected Object recipe;
public abstract class BaseMapRemoval<K, V> extends BaseMapModification<K, V> {
protected BaseMapRemoval(String name, Map<K, V> map) {
super(name, map);
}
protected BaseMapRemoval(String name, Map<K, V> map, Map<K, V> recipes) {
this(name, map);
if(recipes != null) {
this.recipes.putAll(recipes);
}
}
public BaseMapRemoval(Object stack, Map map, Object key) {
this(null, map, key, stack);
}
@Override
public void apply() {
if(recipes.isEmpty())
return;
for(K key : recipes.keySet()) {
V value = map.remove(key);
if(value != null) {
successful.put(key, value);
} else {
LogHelper.logError(String.format("Error removing %s Recipe : null object", name));
}
}
}
@Override
public void undo() {
if(successful.isEmpty())
return;
for(Entry<K, V> entry : successful.entrySet()) {
if(entry != null) {
V value = map.put(entry.getKey(), entry.getValue());
if(value != null) {
LogHelper.logWarning(String.format("Overwritten %s Recipe for %s while restoring.", name, getRecipeInfo(entry)));
}
}
}
}
@Override
public String describe() {
return String.format("[ModTweaker2] Removing %d %s Recipe(s) for %d", this.recipes.size(), this.name);
}
public BaseMapRemoval(String description, Map map, Object stack) {
this(description, map, null, stack);
}
public BaseMapRemoval(String description, Map map, Object key, Object stack) {
this.stack = stack;
this.map = map;
this.key = key;
this.description = description;
}
@Override
public void apply() {
recipe = map.get(key);
map.remove(key);
}
@Override
public boolean canUndo() {
return map != null;
}
@Override
public void undo() {
map.put(key, recipe);
}
public String getRecipeInfo() {
return "Unknown Item";
}
@Override
public String describe() {
if (recipe instanceof ItemStack)
return "Removing " + description + " Recipe for : " + ((ItemStack) recipe).getDisplayName();
else if (recipe instanceof FluidStack)
return "Removing " + description + " Recipe for : " + ((FluidStack) recipe).getFluid().getLocalizedName();
else return "Removing " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public String describeUndo() {
if (recipe instanceof ItemStack)
return "Restoring " + description + " Recipe for : " + ((ItemStack) recipe).getDisplayName();
else if (recipe instanceof FluidStack)
return "Restoring " + description + " Recipe for : " + ((FluidStack) recipe).getFluid().getLocalizedName();
else return "Restoring " + description + " Recipe for : " + getRecipeInfo();
}
@Override
public Object getOverrideKey() {
return null;
}
@Override
public String describeUndo() {
return String.format("[ModTweaker2] Restoring %d %s Recipe(s) for %d", this.recipes.size(), this.name);
}
}
@@ -0,0 +1,50 @@
package modtweaker2.utils;
import java.util.LinkedList;
import minetweaker.IUndoableAction;
public class BaseMultiModification extends BaseUndoable {
protected final LinkedList<IUndoableAction> actions;
protected BaseMultiModification(String name) {
super(name);
this.actions = new LinkedList<IUndoableAction>();
}
@Override
public void apply() {
for(IUndoableAction action : actions) {
action.apply();
}
}
@Override
public boolean canUndo() {
for(IUndoableAction action : actions) {
if(!action.canUndo())
return false;
}
return true;
}
@Override
public String describe() {
return String.format("Applying %d actions for %s Recipe change", this.name, this.actions.size());
}
@Override
public String describeUndo() {
return String.format("Reverting %d actions for %s Recipe change", this.name, this.actions.size());
}
@Override
public void undo() {
for(IUndoableAction action : actions) {
action.undo();
}
}
}
@@ -1,83 +0,0 @@
package modtweaker2.utils;
import minetweaker.IUndoableAction;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseMultipleListRemoval implements IUndoableAction {
protected final Position pos;
protected final String description;
protected final List list;
protected final Object search;
protected ArrayList<Object> recipes;
public BaseMultipleListRemoval(String description, List list, Object search, Position pos) {
this.pos = pos;
this.description = description;
this.list = list;
this.search = search;
}
//Return whether the items are equal or not
protected abstract boolean isEqual(Object recipe, Object search);
@Override
public void apply() {
//Create a new list
recipes = new ArrayList();
for (Object o : list) {
if (isEqual(o, search)) {
//If we want the last position, reset the array
if (pos == Position.LAST) {
recipes = new ArrayList();
}
recipes.add(o);
//If we want the first position, exit the loop
if (pos == Position.FIRST) {
break;
}
}
}
//Remove all the recipes that were considered valid for removal
for (Object o : recipes) {
list.remove(o);
}
}
@Override
public boolean canUndo() {
return recipes != null;
}
@Override
public void undo() {
for (Object o : recipes) {
list.add(o);
}
}
public abstract String getRecipeInfo();
@Override
public String describe() {
return "Removing " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public String describeUndo() {
return "Restoring " + description + " Recipe for :" + getRecipeInfo();
}
@Override
public Object getOverrideKey() {
return null;
}
public static enum Position {
ALL, FIRST, LAST;
}
}
@@ -1,57 +0,0 @@
package modtweaker2.utils;
import net.minecraft.item.ItemStack;
import minetweaker.IUndoableAction;
import modtweaker2.helpers.ReflectionHelper;
public abstract class BaseSetVar implements IUndoableAction {
protected final String description;
protected final Class clazz;
protected final String field;
protected final int original;
protected final int newValue;
protected Object object;
public BaseSetVar(String description, Class clazz, Object object, String field, int original, int newValue) {
this(description, clazz, field, original, newValue);
this.object = object;
}
public BaseSetVar(String description, Class clazz, String field, int original, int newValue) {
this.description = description;
this.clazz = clazz;
this.field = field;
this.original = original;
this.newValue = newValue;
}
@Override
public void apply() {
ReflectionHelper.setPrivateValue(clazz, object, field, newValue);
}
@Override
public boolean canUndo() {
return true;
}
@Override
public void undo() {
ReflectionHelper.setPrivateValue(clazz, object, field, original);
}
@Override
public String describe() {
return "Setting " + description + " to " + newValue;
}
@Override
public String describeUndo() {
return "Setting " + description + " to the default value of " + original;
}
@Override
public Object getOverrideKey() {
return null;
}
}
@@ -3,13 +3,60 @@ package modtweaker2.utils;
import minetweaker.IUndoableAction;
public abstract class BaseUndoable implements IUndoableAction {
// Holds the name of the mod / machine this action manipulates
protected final String name;
// Basic indicator, if the action was successful and can be reverted
protected boolean success = false;
protected BaseUndoable(String name) {
this.name = name;
}
protected String getRecipeInfo() {
return "Unknown item";
}
@Override
public boolean canUndo() {
return true;
return success;
}
@Override
public Object getOverrideKey() {
return null;
public String describe() {
return String.format("[ModTweaker2] Altering %s Recipe(s) for %s", this.name, this.getRecipeInfo());
}
}
@Override
public String describeUndo() {
return String.format("[ModTweaker2] Reverting %s Recipe(s) changes for %s", this.name, this.getRecipeInfo());
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if(!(obj instanceof BaseUndoable)) {
return false;
}
BaseUndoable u = (BaseUndoable)obj;
if(!name.equals(u.name))
return false;
return true;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public Object getOverrideKey() {
return null;
}
}
@@ -1,9 +1,7 @@
package modtweaker2.utils;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Loader;
public class TweakerPlugin {