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

@@ -9,7 +9,5 @@ public class AppliedEnergistics {
public AppliedEnergistics() {
MineTweakerAPI.registerClass(Inscriber.class);
MineTweakerAPI.registerClass(Grind.class);
}
}
@@ -0,0 +1,77 @@
package modtweaker2.mods.appeng;
import static modtweaker2.helpers.StackHelper.areEqualOrNull;
import net.minecraft.item.ItemStack;
import appeng.api.features.IGrinderEntry;
import appeng.api.features.IInscriberRecipe;
public class AppliedEnergisticsHelper {
/**
* Compares two IInscriberRecipe objects, if they are the same or have the same inputs
*/
public static boolean equals(IInscriberRecipe r1, IInscriberRecipe r2) {
if(r1 == r2) {
return true;
}
if (r1 == null || r2 == null) {
return false;
}
if(!areEqualOrNull(r1.getTopOptional().orNull(), r2.getTopOptional().orNull())) {
return false;
}
if(!areEqualOrNull(r1.getBottomOptional().orNull(), r2.getBottomOptional().orNull())) {
return false;
}
if(r1.getInputs() == null && r2.getInputs() != null || r1.getInputs() != null && r2.getInputs() == null) {
return false;
}
if(r1.getInputs() != null && r2.getInputs() != null) {
if(r1.getInputs().size() != r2.getInputs().size()) {
return false;
}
// Check if every item in recipe 1 is in recipe 2
for(ItemStack i1 : r1.getInputs()) {
boolean found = false;
for(ItemStack i2 : r2.getInputs()) {
if(areEqualOrNull(i1, i2)) {
found = true;
}
}
if(!found) {
return false;
}
}
}
return true;
}
/**
* Compares two IGrinderEntry objects, if they are the same or have the same inputs
*/
public static boolean equals(IGrinderEntry r1, IGrinderEntry r2) {
if(r1 == r2) {
return true;
}
if (r1 == null || r2 == null) {
return false;
}
if(!areEqualOrNull(r1.getInput(), r2.getInput())) {
return false;
}
return true;
}
}
@@ -1,14 +1,20 @@
package modtweaker2.mods.appeng.handlers;
import java.util.ArrayList;
import java.util.List;
import static modtweaker2.helpers.InputHelper.getStackDescription;
import static modtweaker2.helpers.InputHelper.toIngredient;
import static modtweaker2.helpers.InputHelper.toStack;
import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.mods.appeng.AppliedEnergisticsHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
import stanhebben.zenscript.annotations.Optional;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
import appeng.api.AEApi;
@@ -17,49 +23,83 @@ import appeng.core.features.registries.entries.AppEngGrinderRecipe;
@ZenClass("mods.appeng.Grinder")
public class Grind {
protected static final String name = "Applied Energistics 2 Grinder";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ZenMethod
public static void addRecipe(IItemStack input, IItemStack output, int energy, @Optional IItemStack output2, @Optional float chance2, @Optional IItemStack output3, @Optional float chance3) {
// Create recipe
IGrinderEntry recipe;
if(output2 != null && output3 != null)
recipe = new AppEngGrinderRecipe(InputHelper.toStack(input), InputHelper.toStack(output), InputHelper.toStack(output2), InputHelper.toStack(output3), chance2, chance3, energy);
else if(output2 != null)
recipe = new AppEngGrinderRecipe(InputHelper.toStack(input), InputHelper.toStack(output), InputHelper.toStack(output2), chance2, energy);
else
recipe = new AppEngGrinderRecipe(InputHelper.toStack(input), InputHelper.toStack(output), energy);
// Check if the recipe is already present, we don't want to add duplicates
for(IGrinderEntry r : AEApi.instance().registries().grinder().getRecipes()) {
if(r != null && AppliedEnergisticsHelper.equals(r, recipe)) {
LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", Grind.name, InputHelper.getStackDescription(toStack(input))));
return;
}
}
MineTweakerAPI.apply(new Add(recipe));
}
private static class Add extends BaseListAddition<IGrinderEntry> {
public Add(IGrinderEntry recipe) {
super(Grind.name, AEApi.instance().registries().grinder().getRecipes());
recipes.add(recipe);
}
@Override
public String getRecipeInfo(IGrinderEntry recipe) {
return getStackDescription(recipe.getInput());
}
@Override
protected boolean equals(IGrinderEntry recipe, IGrinderEntry otherRecipe) {
return AppliedEnergisticsHelper.equals(recipe, otherRecipe);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ZenMethod
public static void addRecipe(IItemStack input, IItemStack output, int energy) {
MineTweakerAPI.apply(new Add(new AppEngGrinderRecipe(InputHelper.toStack(input), InputHelper.toStack(output), energy)));
public static void removeRecipe(IIngredient output) {
// Get list of existing recipes, matching with parameter
LinkedList<IGrinderEntry> result = new LinkedList<IGrinderEntry>();
for(IGrinderEntry entry : AEApi.instance().registries().grinder().getRecipes()) {
if(entry != null && entry.getOutput() != null && output.contains(toIngredient(entry.getOutput()))) {
result.add(entry);
}
}
// Check if we found the recipes and apply the action
if(!result.isEmpty()) {
MineTweakerAPI.apply(new Remove(result));
} else {
LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Grind.name, output.toString()));
}
}
private static class Add extends BaseListAddition {
public Add(AppEngGrinderRecipe recipe) {
super(AEApi.instance().registries().grinder().getRecipes(), recipe);
private static class Remove extends BaseListRemoval<IGrinderEntry> {
public Remove(LinkedList<IGrinderEntry> recipes) {
super(Grind.name, AEApi.instance().registries().grinder().getRecipes(), recipes);
}
@Override
public void apply() {
AEApi.instance().registries().grinder().getRecipes().add((AppEngGrinderRecipe) recipe);
public String getRecipeInfo(IGrinderEntry recipe) {
return getStackDescription(recipe.getOutput());
}
}
@ZenMethod
public static void removeRecipe(IItemStack output) {
MineTweakerAPI.apply(new Remove(InputHelper.toStack(output)));
}
private static class Remove extends BaseListRemoval {
public Remove(ItemStack stack) {
super("Applied Energistics 2 Grinder", AEApi.instance().registries().grinder().getRecipes(), stack);
}
@Override
public void apply() {
for (IGrinderEntry r : AEApi.instance().registries().grinder().getRecipes()) {
if (r.getOutput().isItemEqual(stack)) {
recipes.add(r);
}
}
super.apply();
}
@Override
public String getRecipeInfo() {
return stack.getDisplayName();
}
}
}
@@ -1,77 +1,151 @@
package modtweaker2.mods.appeng.handlers;
import static modtweaker2.helpers.InputHelper.getStackDescription;
import static modtweaker2.helpers.InputHelper.toIngredient;
import static modtweaker2.helpers.InputHelper.toStack;
import static modtweaker2.helpers.InputHelper.toStacks;
import java.util.ArrayList;
import java.util.LinkedList;
import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.mods.appeng.AppliedEnergisticsHelper;
import modtweaker2.utils.ArrayUtils;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
import net.minecraft.item.ItemStack;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
import appeng.api.AEApi;
import appeng.api.features.IInscriberRecipe;
import appeng.api.features.InscriberProcessType;
import appeng.core.features.registries.entries.InscriberRecipe;
import appeng.recipes.handlers.Inscribe;
@ZenClass("mods.appeng.Inscriber")
public class Inscriber {
protected static final String name = "Applied Energistics 2 Inscriber";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ZenMethod
public static void addRecipe(IItemStack[] imprintable, IItemStack plateA, IItemStack plateB, IItemStack out, String type) {
MineTweakerAPI.apply(new Add(new InscriberRecipe(ArrayUtils.toArrayList(toStacks(imprintable)), toStack(out), toStack(plateA), toStack(plateB), InscriberProcessType.valueOf(type))));
for (IItemStack stack : imprintable) {
AEApi.instance().registries().inscriber().getInputs().add(toStack(stack));
}
if (plateA != null)
AEApi.instance().registries().inscriber().getOptionals().add(toStack(plateA));
if (plateB != null)
AEApi.instance().registries().inscriber().getOptionals().add(toStack(plateB));
// Create recipe
IInscriberRecipe recipe = new InscriberRecipe(ArrayUtils.toArrayList(toStacks(imprintable)), toStack(out), toStack(plateA), toStack(plateB), InscriberProcessType.valueOf(type));
// Check if the recipe is already present, we don't want to add duplicates
for(IInscriberRecipe r : AEApi.instance().registries().inscriber().getRecipes()) {
if(r != null && AppliedEnergisticsHelper.equals(r, recipe)) {
LogHelper.logWarning(String.format("Duplicate %s Recipe found for %s. Command ignored!", Inscriber.name, InputHelper.getStackDescription(toStack(out))));
return;
}
}
MineTweakerAPI.apply(new Add(recipe));
}
public static class Add extends BaseListAddition {
public static class Add extends BaseListAddition<IInscriberRecipe> {
public Add(IInscriberRecipe recipe) {
super(recipe.toString(), AEApi.instance().registries().inscriber().getRecipes(), recipe);
super(Inscriber.name, AEApi.instance().registries().inscriber().getRecipes());
recipes.add(recipe);
}
@Override
public void apply() {
AEApi.instance().registries().inscriber().addRecipe((IInscriberRecipe) recipe);
}
if(recipes.isEmpty()) {
return;
}
for(IInscriberRecipe recipe : recipes) {
try {
AEApi.instance().registries().inscriber().addRecipe(recipe);
successful.add(recipe);
} catch (Exception ex) { LogHelper.logError("Error adding inscriber recipe.", ex); }
}
}
@Override
public void undo() {
if(successful.isEmpty()) {
return;
}
for(IInscriberRecipe recipe : successful) {
try {
AEApi.instance().registries().inscriber().removeRecipe(recipe);
} catch (Exception ex) { LogHelper.logError("Error removing inscriber recipe.", ex); }
}
}
@Override
public String getRecipeInfo(IInscriberRecipe recipe) {
return getStackDescription(recipe.getOutput());
}
@Override
protected boolean equals(IInscriberRecipe recipe, IInscriberRecipe otherRecipe) {
return AppliedEnergisticsHelper.equals(recipe, otherRecipe);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ZenMethod
public static void removeRecipe(IItemStack output) {
MineTweakerAPI.apply(new Remove(toStack(output)));
public static void removeRecipe(IIngredient output) {
// Get list of existing recipes, matching with parameter
LinkedList<IInscriberRecipe> result = new LinkedList<IInscriberRecipe>();
for(IInscriberRecipe entry : AEApi.instance().registries().inscriber().getRecipes()) {
if(entry != null && entry.getOutput() != null && output.contains(toIngredient(entry.getOutput()))) {
result.add(entry);
}
}
// Check if we found the recipes and apply the action
if(!result.isEmpty()) {
MineTweakerAPI.apply(new Remove(result));
} else {
LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Inscriber.name, output.toString()));
}
}
public static class Remove extends BaseListRemoval {
public Remove(ItemStack stack) {
super("Applied Energistics 2 Inscriber", AEApi.instance().registries().inscriber().getRecipes(), stack);
}
@Override
public void apply() {
for (IInscriberRecipe recipe : AEApi.instance().registries().inscriber().getRecipes()) {
if (recipe != null && recipe.getOutput() != null && recipe.getOutput().isItemEqual(stack)) {
recipes.add(recipe);
}
}
super.apply();
public static class Remove extends BaseListRemoval<IInscriberRecipe> {
public Remove(LinkedList<IInscriberRecipe> recipes) {
super(Inscriber.name, AEApi.instance().registries().inscriber().getRecipes(), recipes);
}
@Override
public String getRecipeInfo() {
return stack.getDisplayName();
@Override
public void apply() {
if(this.recipes.isEmpty()) {
return;
}
for(IInscriberRecipe recipe : recipes) {
try {
AEApi.instance().registries().inscriber().removeRecipe(recipe);
successful.add(recipe);
} catch (Exception ex) { LogHelper.logError("Error removing inscriber recipe.", ex); }
}
}
@Override
public void undo() {
if(successful.isEmpty()) {
return;
}
for(IInscriberRecipe recipe : successful) {
try {
AEApi.instance().registries().inscriber().addRecipe(recipe);
} catch (Exception ex) { LogHelper.logError("Error restoring inscriber recipe.", ex); }
}
}
@Override
public String getRecipeInfo(IInscriberRecipe recipe) {
return getStackDescription(recipe.getOutput());
}
}
}