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

@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import mariculture.api.core.FuelInfo;
import mariculture.api.core.MaricultureHandlers;
import mariculture.api.fishery.Fishing;
import mariculture.api.fishery.Loot;
@@ -15,7 +16,7 @@ import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.oredict.OreDictionary;
public class MaricultureHelper {
public static Map fuels = null;
public static Map<Object, FuelInfo> fuels = null;
public static HashMap<Rarity, ArrayList<Loot>> loot = null;
static {
@@ -27,7 +28,7 @@ public class MaricultureHelper {
private MaricultureHelper() {}
//Helper for getting the key that is used when adding/removing fuels
// Helper for getting the key that is used when adding/removing fuels
public static String getKey(Object o) {
if (o instanceof String) return (String) o;
else if (o instanceof ItemStack) {
@@ -1,33 +1,47 @@
package modtweaker2.mods.mariculture.handlers;
import static modtweaker2.helpers.InputHelper.toIItemStack;
import static modtweaker2.helpers.InputHelper.toStack;
import static modtweaker2.helpers.StackHelper.matches;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import mariculture.api.core.IAnvilHandler.RecipeAnvil;
import mariculture.api.core.MaricultureHandlers;
import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.mods.mariculture.MaricultureHelper;
import modtweaker2.utils.BaseMapAddition;
import modtweaker2.utils.BaseMapRemoval;
import net.minecraft.item.ItemStack;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
@ZenClass("mods.mariculture.Anvil")
public class Anvil {
//Adding a Mariculture Anvil recipe
public static final String name = "Mariculture Anvil";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ZenMethod
public static void addRecipe(IItemStack input, IItemStack output, int hits) {
MineTweakerAPI.apply(new Add(new RecipeAnvil(toStack(input), toStack(output), hits)));
}
private static class Add extends BaseMapAddition {
private static class Add extends BaseMapAddition<String, RecipeAnvil> {
public Add(RecipeAnvil recipe) {
super("Mariculture Anvil", MaricultureHandlers.anvil.getRecipes(), MaricultureHelper.getKey(recipe.input), recipe);
super("Mariculture Anvil", MaricultureHandlers.anvil.getRecipes());
recipes.put(MaricultureHelper.getKey(recipe.input), recipe);
}
@Override
public String getRecipeInfo() {
return ((RecipeAnvil) recipe).output.getDisplayName();
public String getRecipeInfo(Entry<String, RecipeAnvil> recipe) {
return InputHelper.getStackDescription(recipe.getValue().output);
}
}
@@ -35,18 +49,30 @@ public class Anvil {
//Removing a Mariculture Anvil recipe
@ZenMethod
public static void removeRecipe(IItemStack input) {
MineTweakerAPI.apply(new Remove(toStack(input)));
public static void removeRecipe(IIngredient input) {
Map<String, RecipeAnvil> recipes = new HashMap<String, RecipeAnvil>();
for(Entry<String, RecipeAnvil> recipe : MaricultureHandlers.anvil.getRecipes().entrySet()) {
if(recipe != null && recipe.getValue() != null && recipe.getValue().input != null && matches(input, toIItemStack(recipe.getValue().input))) {
recipes.put(recipe.getKey(), recipe.getValue());
}
}
if(!recipes.isEmpty()) {
MineTweakerAPI.apply(new Remove(recipes));
} else {
LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored!", Anvil.name, input.toString()));
}
}
private static class Remove extends BaseMapRemoval {
public Remove(ItemStack stack) {
super("Mariculture Anvil", MaricultureHandlers.anvil.getRecipes(), MaricultureHelper.getKey(stack), stack);
private static class Remove extends BaseMapRemoval<String, RecipeAnvil> {
public Remove(Map<String, RecipeAnvil> recipes) {
super("Mariculture Anvil", MaricultureHandlers.anvil.getRecipes(), recipes);
}
@Override
public String getRecipeInfo() {
return ((ItemStack) stack).getDisplayName();
public String getRecipeInfo(Entry<String, RecipeAnvil> recipe) {
return InputHelper.getStackDescription(recipe.getValue().output);
}
}
}
@@ -1,26 +1,36 @@
package modtweaker2.mods.mariculture.handlers;
import static modtweaker2.helpers.InputHelper.toFluid;
import static modtweaker2.helpers.InputHelper.toIItemStack;
import static modtweaker2.helpers.InputHelper.toILiquidStack;
import static modtweaker2.helpers.InputHelper.toStack;
import static modtweaker2.helpers.StackHelper.matches;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import mariculture.api.core.MaricultureHandlers;
import mariculture.api.core.RecipeCasting;
import mariculture.api.core.RecipeCasting.RecipeNuggetCasting;
import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import minetweaker.api.liquid.ILiquidStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.mods.mariculture.MaricultureHelper;
import modtweaker2.utils.BaseMapAddition;
import modtweaker2.utils.BaseMapRemoval;
import net.minecraft.item.ItemStack;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
@ZenClass("mods.mariculture.Casting")
public class Casting {
//Adding Mariculture Casting Recipes
public static final String name = "Mariculture Casting";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ZenMethod
public static void addNuggetRecipe(ILiquidStack input, IItemStack output) {
MineTweakerAPI.apply(new Add(new RecipeNuggetCasting(toFluid(input), toStack(output)), MaricultureHandlers.casting.getNuggetRecipes()));
@@ -36,43 +46,63 @@ public class Casting {
MineTweakerAPI.apply(new Add(new RecipeNuggetCasting(toFluid(input), toStack(output)), MaricultureHandlers.casting.getBlockRecipes()));
}
private static class Add extends BaseMapAddition {
public Add(RecipeCasting recipe, HashMap map) {
super("Mariculture Casting", map, MaricultureHelper.getKey(recipe.fluid), recipe);
private static class Add extends BaseMapAddition<String, RecipeCasting> {
public Add(RecipeCasting recipe, HashMap<String, RecipeCasting> map) {
super(Casting.name, map);
recipes.put(MaricultureHelper.getKey(recipe.fluid), recipe);
}
@Override
public String getRecipeInfo() {
return ((RecipeCasting) recipe).output.getDisplayName();
public String getRecipeInfo(Entry<String, RecipeCasting> recipe) {
return InputHelper.getStackDescription(recipe.getValue().output);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Removing Mariculture Casting Recipes
@ZenMethod
public static void removeNuggetRecipe(IItemStack input) {
MineTweakerAPI.apply(new Remove(toStack(input), MaricultureHandlers.casting.getNuggetRecipes()));
public static void removeNuggetRecipe(IIngredient ingredient) {
removeRecipe(ingredient, MaricultureHandlers.casting.getNuggetRecipes());
}
@ZenMethod
public static void removeIngotRecipe(IItemStack input) {
MineTweakerAPI.apply(new Remove(toStack(input), MaricultureHandlers.casting.getIngotRecipes()));
public static void removeIngotRecipe(IIngredient ingredient) {
removeRecipe(ingredient, MaricultureHandlers.casting.getIngotRecipes());
}
@ZenMethod
public static void removeBlockRecipe(IItemStack input) {
MineTweakerAPI.apply(new Remove(toStack(input), MaricultureHandlers.casting.getBlockRecipes()));
public static void removeBlockRecipe(IIngredient ingredient) {
removeRecipe(ingredient, MaricultureHandlers.casting.getBlockRecipes());
}
public static void removeRecipe(IIngredient ingredient, HashMap<String, RecipeCasting> map) {
Map<String, RecipeCasting> recipes = new HashMap<String, RecipeCasting>();
for(Entry<String, RecipeCasting> recipe : map.entrySet()) {
if(recipe != null && recipe.getValue() != null) {
if(recipe.getValue().fluid != null && matches(ingredient, toILiquidStack(recipe.getValue().fluid))) {
recipes.put(recipe.getKey(), recipe.getValue());
}
if(recipe.getValue().output != null && matches(ingredient, toIItemStack(recipe.getValue().output))) {
recipes.put(recipe.getKey(), recipe.getValue());
}
}
}
if(!recipes.isEmpty()) {
MineTweakerAPI.apply(new Remove(map, recipes));
}
}
private static class Remove extends BaseMapRemoval {
public Remove(ItemStack stack, HashMap map) {
super("Mariculture Casting", map, MaricultureHelper.getKey(stack), stack);
private static class Remove extends BaseMapRemoval<String, RecipeCasting> {
public Remove(Map<String, RecipeCasting> map, Map<String, RecipeCasting> recipes) {
super(Casting.name, map, recipes);
}
@Override
public String getRecipeInfo() {
return ((ItemStack) stack).getDisplayName();
public String getRecipeInfo(Entry<String, RecipeCasting> recipe) {
return InputHelper.getStackDescription(recipe.getValue().output);
}
}
}
@@ -1,14 +1,25 @@
package modtweaker2.mods.mariculture.handlers;
import static modtweaker2.helpers.InputHelper.toFluid;
import static modtweaker2.helpers.InputHelper.toIItemStack;
import static modtweaker2.helpers.InputHelper.toStack;
import static modtweaker2.helpers.StackHelper.areEqual;
import static modtweaker2.helpers.StackHelper.matches;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import mariculture.api.core.FuelInfo;
import mariculture.api.core.MaricultureHandlers;
import mariculture.api.core.RecipeSmelter;
import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import minetweaker.api.liquid.ILiquidStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.mods.mariculture.MaricultureHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
@@ -21,6 +32,9 @@ import stanhebben.zenscript.annotations.ZenMethod;
@ZenClass("mods.mariculture.Crucible")
public class Crucible {
public static final String nameSmelting = "Mariculture Crucible (Smelting)";
public static final String nameFuel = "Mariculture Crucible (Fuel)";
/********************************************** Mariculture Crucible Recipes **********************************************/
@@ -32,14 +46,15 @@ public class Crucible {
}
// Passes the list to the base list implementation, and adds the recipe
private static class AddRecipe extends BaseListAddition {
private static class AddRecipe extends BaseListAddition<RecipeSmelter> {
public AddRecipe(RecipeSmelter recipe) {
super("Mariculture Crucible", MaricultureHandlers.crucible.getRecipes(), recipe);
super(Crucible.nameSmelting, MaricultureHandlers.crucible.getRecipes());
recipes.add(recipe);
}
@Override
public String getRecipeInfo() {
return ((RecipeSmelter) recipe).input.getDisplayName();
public String getRecipeInfo(RecipeSmelter recipe) {
return InputHelper.getStackDescription(recipe.input);
}
}
@@ -47,35 +62,34 @@ public class Crucible {
// Removing a Mariculture Crucible recipe
@ZenMethod
public static void removeRecipe(IItemStack input) {
MineTweakerAPI.apply(new RemoveRecipe(toStack(input)));
public static void removeRecipe(IIngredient input) {
List<RecipeSmelter> recipes = new LinkedList<RecipeSmelter>();
for (RecipeSmelter r : MaricultureHandlers.crucible.getRecipes()) {
if (r != null) {
if (r.input != null && matches(input, toIItemStack(r.input))) {
recipes.add(r);
}
}
}
if(!recipes.isEmpty()) {
MineTweakerAPI.apply(new RemoveRecipe(recipes));
} else {
LogHelper.logWarning(String.format("No %s Recipe found for %s. Command ignored", Crucible.nameSmelting, input.toString()));
}
}
// Removes a recipe, apply is never the same for anything, so will always
// need to override it
private static class RemoveRecipe extends BaseListRemoval {
public RemoveRecipe(ItemStack stack) {
super("Mariculture Crucible", MaricultureHandlers.crucible.getRecipes(), stack);
}
// Loops through the registry, to find the item that matches, saves that
// recipe then removes it
@Override
public void apply() {
for (RecipeSmelter r : MaricultureHandlers.crucible.getRecipes()) {
if (r != null) {
if (r.input != null && stack != null && areEqual(r.input, stack)) {
recipes.add(r);
}
}
}
super.apply();
}
@Override
public String getRecipeInfo() {
return stack.getDisplayName();
private static class RemoveRecipe extends BaseListRemoval<RecipeSmelter> {
public RemoveRecipe(List<RecipeSmelter> recipes) {
super(Crucible.nameSmelting, MaricultureHandlers.crucible.getRecipes(), recipes);
}
@Override
public String getRecipeInfo(RecipeSmelter recipe) {
return InputHelper.getStackDescription(recipe.input);
}
}
/********************************************** Crucible Fuels **********************************************/
@@ -95,42 +109,58 @@ public class Crucible {
}
// Passes the list to the base map implementation, and adds the recipe
private static class AddFuel extends BaseMapAddition {
private static class AddFuel extends BaseMapAddition<Object, FuelInfo> {
public AddFuel(Object o, FuelInfo info) {
super("Mariculture Crucible Fuel", MaricultureHelper.fuels, MaricultureHelper.getKey(o), info);
super(Crucible.nameFuel, MaricultureHelper.fuels);
recipes.put(MaricultureHelper.getKey(o), info);
}
@Override
public String getRecipeInfo() {
return (String) key;
}
@Override
public String getRecipeInfo(Entry<Object, FuelInfo> recipe) {
return (String) recipe.getKey();
}
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ZenMethod
public static void removeFuel(IItemStack fuel) {
MineTweakerAPI.apply(new RemoveFuel(fuel));
removeFuelEntry(fuel);
}
@ZenMethod
public static void removeFuel(ILiquidStack fuel) {
MineTweakerAPI.apply(new RemoveFuel(fuel));
removeFuelEntry(fuel);
}
@ZenMethod
public static void removeFuel(String fuel) {
MineTweakerAPI.apply(new RemoveFuel(fuel));
removeFuelEntry(fuel);
}
public static void removeFuelEntry(Object o) {
String key = MaricultureHelper.getKey(o);
Map<Object, FuelInfo> recipes = new HashMap<Object, FuelInfo>();
for(Entry<Object, FuelInfo> entry : MaricultureHelper.fuels.entrySet()) {
if(key.matches((String)entry.getKey())) {
recipes.put(key, entry.getValue());
}
}
if(!recipes.isEmpty()) {
MineTweakerAPI.apply(new RemoveFuel(recipes));
}
}
// Removes a recipe, will always remove the key, so all should be good
private static class RemoveFuel extends BaseMapRemoval {
public RemoveFuel(Object o) {
super("Mariculture Crucible Fuel", MaricultureHelper.fuels, MaricultureHelper.getKey(o), null);
private static class RemoveFuel extends BaseMapRemoval<Object, FuelInfo> {
public RemoveFuel(Map<Object, FuelInfo> recipes) {
super(Crucible.nameFuel, MaricultureHelper.fuels, recipes);
}
@Override
public String getRecipeInfo() {
return (String) key;
}
@Override
public String getRecipeInfo(Entry<Object, FuelInfo> recipe) {
return (String) recipe.getKey();
}
}
}
@@ -14,8 +14,7 @@ import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IItemStack;
import modtweaker2.helpers.ReflectionHelper;
import modtweaker2.mods.mariculture.MaricultureHelper;
import modtweaker2.utils.BaseDescriptionAddition;
import modtweaker2.utils.BaseDescriptionRemoval;
import modtweaker2.utils.BaseUndoable;
import net.minecraft.item.ItemStack;
import stanhebben.zenscript.annotations.Optional;
import stanhebben.zenscript.annotations.ZenClass;
@@ -67,7 +66,7 @@ public class Fishing {
}
}
private static class AddLoot extends BaseDescriptionAddition {
private static class AddLoot extends BaseUndoable {
private final Loot loot;
public AddLoot(Loot loot, String description) {
@@ -80,6 +79,7 @@ public class Fishing {
ArrayList<Loot> list = MaricultureHelper.loot.get(loot.rarity);
list.add(loot);
MaricultureHelper.loot.put(loot.rarity, list);
success = true;
}
@Override
@@ -103,7 +103,7 @@ public class Fishing {
MineTweakerAPI.apply(new RemoveLoot(toStack(loot)));
}
private static class RemoveLoot extends BaseDescriptionRemoval {
private static class RemoveLoot extends BaseUndoable {
private HashMap<Rarity, Loot> loot;
private final ItemStack stack;
@@ -119,6 +119,7 @@ public class Fishing {
apply(Rarity.JUNK);
apply(Rarity.GOOD);
apply(Rarity.RARE);
success = true;
}
//Performs the apply function on all rarity types
@@ -1,22 +1,34 @@
package modtweaker2.mods.mariculture.handlers;
import static modtweaker2.helpers.InputHelper.toFluid;
import static modtweaker2.helpers.InputHelper.toIItemStack;
import static modtweaker2.helpers.InputHelper.toILiquidStack;
import static modtweaker2.helpers.InputHelper.toStack;
import static modtweaker2.helpers.StackHelper.areEqual;
import static modtweaker2.helpers.StackHelper.matches;
import java.util.LinkedList;
import java.util.List;
import mariculture.api.core.MaricultureHandlers;
import mariculture.api.core.RecipeVat;
import minetweaker.MineTweakerAPI;
import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import minetweaker.api.liquid.ILiquidStack;
import modtweaker2.helpers.InputHelper;
import modtweaker2.helpers.LogHelper;
import modtweaker2.utils.BaseListAddition;
import modtweaker2.utils.BaseListRemoval;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
@ZenClass("mods.mariculture.Vat")
public class Vat {
public static final String name = "Mariculture Vat";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Adding a Mariculture Vat recipe
@ZenMethod
public static void addRecipe(ILiquidStack fluid1, ILiquidStack fluid2, ILiquidStack outputFluid, int time) {
@@ -64,74 +76,82 @@ public class Vat {
}
// Passes the list to the base list implementation, and adds the recipe
private static class Add extends BaseListAddition {
private static class Add extends BaseListAddition<RecipeVat> {
public Add(RecipeVat recipe) {
super("Mariculture Vat", MaricultureHandlers.vat.getRecipes(), recipe);
super("Mariculture Vat", MaricultureHandlers.vat.getRecipes());
recipes.add(recipe);
}
@Override
public String getRecipeInfo() {
if (((RecipeVat) recipe).outputItem != null)
return ((RecipeVat) recipe).outputItem.getDisplayName();
else
return ((RecipeVat) recipe).outputFluid.getFluid().getLocalizedName();
public String getRecipeInfo(RecipeVat recipe) {
if(recipe.outputItem != null) {
return InputHelper.getStackDescription(recipe.outputItem);
} else {
return InputHelper.getStackDescription(recipe.outputFluid);
}
}
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Removing a Mariculture Vat recipe
@ZenMethod
public static void removeRecipe(IItemStack outputItem) {
MineTweakerAPI.apply(new Remove(toStack(outputItem), null));
public static void removeRecipe(IIngredient ingredient) {
List<RecipeVat> recipes = new LinkedList<RecipeVat>();
for(RecipeVat r : MaricultureHandlers.vat.getRecipes()) {
if(r != null) {
if(r.outputItem != null && matches(ingredient, toIItemStack(r.outputItem))) {
recipes.add(r);
}
else if(r.outputFluid != null && matches(ingredient, toILiquidStack(r.outputFluid))) {
recipes.add(r);
}
}
}
if(!recipes.isEmpty()) {
MineTweakerAPI.apply(new Remove(recipes));
} else {
LogHelper.logWarning(String.format("No %s Recipes found for %s. Command ignored!", Vat.name, ingredient.toString()));
}
}
@ZenMethod
public static void removeRecipe(ILiquidStack outputFluid) {
MineTweakerAPI.apply(new Remove(null, toFluid(outputFluid)));
}
@ZenMethod
public static void removeRecipe(IItemStack outputItem, ILiquidStack outputFluid) {
MineTweakerAPI.apply(new Remove(toStack(outputItem), toFluid(outputFluid)));
public static void removeRecipe(IIngredient outputItem, IIngredient outputFluid) {
List<RecipeVat> recipes = new LinkedList<RecipeVat>();
for (RecipeVat r : MaricultureHandlers.vat.getRecipes()) {
if (r != null) {
if (r.outputItem != null && matches(outputItem, toIItemStack(r.outputItem))) {
if (r.outputFluid != null && matches(outputFluid, toILiquidStack(r.outputFluid))) {
recipes.add(r);
}
}
}
}
if(!recipes.isEmpty()) {
MineTweakerAPI.apply(new Remove(recipes));
} else {
LogHelper.logWarning(String.format("No %s Recipes found for %s and %s. Command ignored!", Vat.name, outputItem.toString(), outputFluid.toString()));
}
}
// Removes a recipe, apply is never the same for anything, so will always
// need to override it
private static class Remove extends BaseListRemoval {
private FluidStack fluid = null;
public Remove(ItemStack stack, FluidStack fluid) {
super("Mariculture Vat", MaricultureHandlers.vat.getRecipes(), stack);
if (fluid != null)
this.fluid = fluid;
private static class Remove extends BaseListRemoval<RecipeVat> {
public Remove(List<RecipeVat> recipes) {
super("Mariculture Vat", MaricultureHandlers.vat.getRecipes(), recipes);
}
// Loops through the registry, to find the item that matches, saves that
// recipe then removes it
@Override
public void apply() {
for (RecipeVat r : MaricultureHandlers.vat.getRecipes()) {
if (r != null)
if (r.outputItem != null && stack != null && areEqual(r.outputItem, stack)) {
if (r.outputFluid == null || (fluid != null && r.outputFluid.isFluidStackIdentical(fluid))) {
recipes.add(r);
break;
}
}
if ((r.outputFluid != null && fluid != null && r.outputFluid.isFluidStackIdentical(fluid))) {
recipes.add(r);
break;
}
}
super.apply();
}
@Override
public String getRecipeInfo() {
return stack != null ? stack.getDisplayName() : fluid.getFluid().getLocalizedName();
}
@Override
public String getRecipeInfo(RecipeVat recipe) {
if(recipe.outputItem != null) {
return InputHelper.getStackDescription(recipe.outputItem);
} else {
return InputHelper.getStackDescription(recipe.outputFluid);
}
}
}
}