Reworked BaseListRemoval

- Fixed various crashes if no recipes are removed and the undo method is
called (e.g. on /mt reload), adding a null object.
- Added option to remove more than 1 recipe with BaseListRemoval
- Cleanup of some classes which are extending BaseListRemoval
This commit is contained in:
Zixxl committed 2015-06-16 12:11:20 +02:00
1 parent 8efaa6c92c
commit de099e4798
40 files changed
+197 -265

No files matched your search

@@ -16,12 +16,10 @@ public class BaseCraftingRemoval extends BaseListRemoval {
public void apply() {
for (IRecipe r : (List<IRecipe>) list) {
if (r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && areEqual(r.getRecipeOutput(), stack)) {
recipe = r;
break;
recipes.add(r);
}
}
list.remove(recipe);
super.apply();
}
@Override
@@ -4,6 +4,7 @@ import minetweaker.IUndoableAction;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import java.util.LinkedList;
import java.util.List;
public abstract class BaseListRemoval implements IUndoableAction {
@@ -12,7 +13,7 @@ public abstract class BaseListRemoval implements IUndoableAction {
protected final FluidStack fluid;
protected final ItemStack stack;
protected Object recipe;
protected final LinkedList recipes = new LinkedList();
public BaseListRemoval(String description, List list, ItemStack stack, FluidStack fluid) {
this.list = list;
@@ -40,16 +41,24 @@ public abstract class BaseListRemoval implements IUndoableAction {
public BaseListRemoval(String description, List list) {
this(description, list, null, null);
}
@Override
public void apply() {
for(Object recipe : recipes) {
list.remove(recipe);
}
}
@Override
public boolean canUndo() {
return list != null;
return list.size() > 0;
}
@Override
public void undo() {
list.add(recipe);
for(Object recipe : recipes) {
list.add(recipe);
}
}
public String getRecipeInfo() {
@@ -58,19 +67,19 @@ public abstract class BaseListRemoval implements IUndoableAction {
@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();
if (recipes.getFirst() instanceof ItemStack)
return "Removing " + description + " Recipe(s) for :" + getItemStackNames(recipes);
else if (recipes.getFirst() instanceof FluidStack)
return "Removing " + description + " Recipe(s) for :" + getFluidStackNames(recipes);
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();
if (recipes.getFirst() instanceof ItemStack)
return "Restoring " + description + " Recipe(s) for :" + getItemStackNames(recipes);
else if (recipes.getFirst() instanceof FluidStack)
return "Restoring " + description + " Recipe(s) for :" + getFluidStackNames(recipes);
else return "Restoring " + description + " Recipe for :" + getRecipeInfo();
}
@@ -78,4 +87,34 @@ public abstract class BaseListRemoval implements IUndoableAction {
public Object getOverrideKey() {
return null;
}
private String getItemStackNames(LinkedList list) {
if(list.size() == 1)
return ((ItemStack)list.getFirst()).getDisplayName();
StringBuilder sb = new StringBuilder();
for(Object entry : list) {
sb.append(((ItemStack)entry).getDisplayName()).append(", ");
}
sb.setLength(sb.length() - 2);
return sb.toString();
}
private String getFluidStackNames(LinkedList list) {
if(list.size() == 1)
return ((FluidStack)list.getFirst()).getFluid().getLocalizedName();
StringBuilder sb = new StringBuilder();
for(Object entry : list) {
sb.append(((FluidStack)entry).getFluid().getLocalizedName()).append(", ");
}
sb.setLength(sb.length() - 2);
return sb.toString();
}
}