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

@@ -1,16 +1,22 @@
package modtweaker2.mods.thaumcraft.handlers;
import static modtweaker2.helpers.InputHelper.toIItemStack;
import static modtweaker2.helpers.InputHelper.toStack;
import static modtweaker2.helpers.InputHelper.toStacks;
import static modtweaker2.helpers.StackHelper.areEqual;
import minetweaker.IUndoableAction;
import static modtweaker2.helpers.StackHelper.matches;
import java.util.LinkedList;
import java.util.List;
import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.mods.thaumcraft.ThaumcraftHelper;
import modtweaker2.mods.thaumcraft.recipe.MTInfusionRecipe;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
import modtweaker2.utils.TweakerPlugin;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.ItemStack;
import stanhebben.zenscript.annotations.ZenClass;
@@ -21,6 +27,10 @@ import thaumcraft.api.crafting.InfusionRecipe;
@ZenClass("mods.thaumcraft.Infusion")
public class Infusion {
public static final String name = "Thaumcraft Infusion";
public static final String enchName = name + " Enchantment";
@ZenMethod
public static void addRecipe(String key, IItemStack input, IItemStack[] recipe, String aspects, IItemStack result, int instability) {
@@ -31,131 +41,112 @@ public class Infusion {
// fuzzy or not
@ZenMethod
public static void addRecipe(String key, IItemStack input, IItemStack[] recipe, String aspects, IItemStack result, int instability, boolean fuzzyCentre, boolean[] fuzzyRecipe) {
MineTweakerAPI.apply(new Add(new MTInfusionRecipe(key, toStack(result), instability, ThaumcraftHelper.parseAspects(aspects), toStack(input), toStacks(recipe), fuzzyCentre, fuzzyRecipe)));
MineTweakerAPI.apply(new Add(new MTInfusionRecipe(key, toStack(result), instability, ThaumcraftHelper.parseAspects(aspects), toStack(input), toStacks(recipe), fuzzyCentre, fuzzyRecipe)));
}
@ZenMethod
public static void addEnchantment(String key, int enchantID, int instability, String aspects, IItemStack[] recipe) {
MineTweakerAPI.apply(new AddEnchant(new InfusionEnchantmentRecipe(key, Enchantment.enchantmentsList[enchantID], instability, ThaumcraftHelper.parseAspects(aspects), toStacks(recipe))));
MineTweakerAPI.apply(new AddEnchant(new InfusionEnchantmentRecipe(key, Enchantment.enchantmentsList[enchantID], instability, ThaumcraftHelper.parseAspects(aspects), toStacks(recipe))));
}
private static class Add extends BaseListAddition {
private static class Add extends BaseListAddition<InfusionRecipe> {
public Add(InfusionRecipe recipe) {
super("Thaumcraft Infusion", ThaumcraftApi.getCraftingRecipes(), recipe);
super(Infusion.name, ThaumcraftApi.getCraftingRecipes());
recipes.add(recipe);
}
@Override
public String getRecipeInfo() {
Object out = ((InfusionRecipe) recipe).getRecipeOutput();
if (out instanceof ItemStack) {
return ((ItemStack) out).getDisplayName();
} else
return super.getRecipeInfo();
protected String getRecipeInfo(InfusionRecipe recipe) {
Object out = ((InfusionRecipe) recipe).getRecipeOutput();
if (out instanceof ItemStack) {
return ((ItemStack) out).getDisplayName();
} else
return "Unknown item";
}
}
private static class AddEnchant implements IUndoableAction {
InfusionEnchantmentRecipe recipe;
private static class AddEnchant extends BaseListAddition<InfusionEnchantmentRecipe> {
public AddEnchant(InfusionEnchantmentRecipe inp) {
recipe = inp;
}
@Override
public void apply() {
ThaumcraftApi.getCraftingRecipes().add(recipe);
}
@Override
public String describe() {
return "Adding Infusion Enchantment Recipe: " + recipe.enchantment.getName();
}
@Override
public boolean canUndo() {
return recipe != null;
}
@Override
public void undo() {
ThaumcraftApi.getCraftingRecipes().remove(recipe);
}
@Override
public String describeUndo() {
return "Removing Infusion Enchantment Recipe: " + recipe.enchantment.getName();
}
@Override
public String getOverrideKey() {
return null;
super(Infusion.enchName, ThaumcraftApi.getCraftingRecipes());
recipes.add(inp);
}
@Override
protected String getRecipeInfo(InfusionEnchantmentRecipe recipe) {
return recipe.getEnchantment().getName();
}
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ZenMethod
public static void removeRecipe(IItemStack output) {
MineTweakerAPI.apply(new Remove(toStack(output)));
public static void removeRecipe(IIngredient output) {
List<InfusionRecipe> recipes = new LinkedList<InfusionRecipe>();
for (Object o : ThaumcraftApi.getCraftingRecipes()) {
if (o instanceof InfusionRecipe) {
InfusionRecipe r = (InfusionRecipe) o;
if (r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && matches(output, toIItemStack((ItemStack)r.getRecipeOutput()))) {
recipes.add(r);
}
}
}
if(!recipes.isEmpty()) {
MineTweakerAPI.apply(new Remove(recipes));
} else {
LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Infusion.name, output.toString()));
}
}
@ZenMethod
public static void removeEnchant(int id) {
MineTweakerAPI.apply(new RemoveEnchant(Enchantment.enchantmentsList[id]));
}
private static class Remove extends BaseListRemoval {
public Remove(ItemStack stack) {
super("Thaumcraft Infusion", ThaumcraftApi.getCraftingRecipes(), stack);
}
@Override
public void apply() {
for (Object o : ThaumcraftApi.getCraftingRecipes()) {
if (o instanceof InfusionRecipe) {
InfusionRecipe r = (InfusionRecipe) o;
if (r.getRecipeOutput() != null && r.getRecipeOutput() instanceof ItemStack && areEqual((ItemStack) r.getRecipeOutput(), stack)) {
recipes.add(r);
}
}
}
super.apply();
}
@Override
public String getRecipeInfo() {
return stack.getDisplayName();
}
}
private static class RemoveEnchant extends BaseListRemoval {
Enchantment enchant;
public RemoveEnchant(Enchantment ench) {
super("Thaumcraft Infusion Enchantment", ThaumcraftApi.getCraftingRecipes());
enchant = ench;
}
@Override
public void apply() {
for (Object recipe : list) {
if (recipe instanceof InfusionEnchantmentRecipe) {
InfusionEnchantmentRecipe enchRecipe = (InfusionEnchantmentRecipe) recipe;
if (enchRecipe.getEnchantment() == enchant) {
recipes.add(enchRecipe);
}
}
}
super.apply();
}
@Override
public String getRecipeInfo() {
return enchant.getName();
List<InfusionEnchantmentRecipe> recipes = new LinkedList<InfusionEnchantmentRecipe>();
Enchantment ench = Enchantment.enchantmentsList[id];
for (Object recipe : ThaumcraftApi.getCraftingRecipes()) {
if (recipe instanceof InfusionEnchantmentRecipe) {
InfusionEnchantmentRecipe enchRecipe = (InfusionEnchantmentRecipe) recipe;
if (enchRecipe.getEnchantment() == ench) {
recipes.add(enchRecipe);
}
}
}
if(!recipes.isEmpty()) {
MineTweakerAPI.apply(new RemoveEnchant(recipes));
} else {
LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Infusion.enchName + " Enchantment", ench.getName()));
}
}
private static class Remove extends BaseListRemoval<InfusionRecipe> {
public Remove(List<InfusionRecipe> recipes) {
super(Infusion.name, ThaumcraftApi.getCraftingRecipes(), recipes);
}
@Override
protected String getRecipeInfo(InfusionRecipe recipe) {
Object o = recipe.getRecipeOutput();
if(o instanceof ItemStack) {
return InputHelper.getStackDescription((ItemStack)o);
} else {
return "Unknown Item";
}
}
}
private static class RemoveEnchant extends BaseListRemoval<InfusionEnchantmentRecipe> {
public RemoveEnchant(List<InfusionEnchantmentRecipe> recipes) {
super(Infusion.enchName, ThaumcraftApi.getCraftingRecipes(), recipes);
}
@Override
protected String getRecipeInfo(InfusionEnchantmentRecipe recipe) {
return recipe.getEnchantment().getName();
}
}
}