From 4b0e42f912aa6f7374069611e962da8ae882a52e Mon Sep 17 00:00:00 2001 From: Gregory Maddra Date: Sun, 3 Sep 2017 21:59:25 -0500 Subject: [PATCH] Update marcin212's minetweaker integration to use the new API --- build.gradle | 4 +- src/main/java/mods/eln/Eln.java | 7 +-- src/main/java/mods/eln/api/Misc.java | 6 ++- .../eln/api/recipe/CompressorRecipeList.java | 13 +++-- .../eln/api/recipe/IELNMachineRecipeList.java | 2 + .../eln/api/recipe/MaceratorRecipeList.java | 13 +++-- .../eln/api/recipe/MagnetizerRecipeList.java | 17 ++++--- .../api/recipe/PlateMachineRecipeList.java | 13 +++-- .../minetweaker/MinetweakerIntegration.java | 20 -------- .../minetweaker/MinetweakerIntegration.kt | 18 +++++++ .../minetweaker/utils/AddRecipe.java | 6 +-- .../minetweaker/utils/MinetweakerMachine.java | 47 +++++++------------ .../minetweaker/utils/RemoveRecipe.java | 34 ++++++++------ 13 files changed, 106 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/mods/eln/integration/minetweaker/MinetweakerIntegration.java create mode 100644 src/main/java/mods/eln/integration/minetweaker/MinetweakerIntegration.kt diff --git a/build.gradle b/build.gradle index bead6e01f..10edf5e69 100644 --- a/build.gradle +++ b/build.gradle @@ -84,8 +84,8 @@ repositories { url = "http://default.mobiusstrip.eu/maven/" } ivy { - name = 'MineTweaker3' - artifactPattern = "http://minetweaker3.powerofbytes.com/download/[module]-[classifier]-[revision].[ext]" + name 'MineTweaker3' + artifactPattern "http://minetweaker3.powerofbytes.com/download/[module]-[classifier]-[revision].[ext]" } } diff --git a/src/main/java/mods/eln/Eln.java b/src/main/java/mods/eln/Eln.java index 35f19e27d..e8ad5c5e0 100644 --- a/src/main/java/mods/eln/Eln.java +++ b/src/main/java/mods/eln/Eln.java @@ -13,7 +13,6 @@ import mods.eln.api.Utilities; import mods.eln.api.recipe.Recipe; import mods.eln.api.recipe.RecipesList; -import mods.eln.packets.*; import mods.eln.cable.CableRenderDescriptor; import mods.eln.client.ClientKeyHandler; import mods.eln.client.SoundLoader; @@ -149,7 +148,6 @@ import mods.eln.transparentnode.waterturbine.WaterTurbineDescriptor; import mods.eln.transparentnode.windturbine.WindTurbineDescriptor; import mods.eln.wiki.Data; - import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.command.ICommandManager; @@ -170,7 +168,6 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.WorldServer; - import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.config.Configuration; import net.minecraftforge.common.config.Property; @@ -965,8 +962,8 @@ public void postInit(FMLPostInitializationEvent event) { serverEventListener = new ServerEventListener(); if(Loader.isModLoaded("MineTweaker3")){ - MinetweakerIntegration.instance.initialize(); - } + MinetweakerIntegration.INSTANCE.initialize(); + } } /* diff --git a/src/main/java/mods/eln/api/Misc.java b/src/main/java/mods/eln/api/Misc.java index 1da5bb192..0da460baf 100644 --- a/src/main/java/mods/eln/api/Misc.java +++ b/src/main/java/mods/eln/api/Misc.java @@ -1,5 +1,7 @@ package mods.eln.api; +import java.lang.reflect.Field; + /** * Created by Gregory Maddra on 2016-11-16. */ @@ -9,7 +11,9 @@ public static Object getRecipeList(String list){ try { Class Eln = getEln(); Object instanceObject = getElnInstance(Eln); - return Eln != null ? Eln.getDeclaredField(list).get(instanceObject) : null; + Field recipeList = Eln.getDeclaredField(list); + recipeList.setAccessible(true); + return Eln != null ? recipeList.get(instanceObject) : null; } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } diff --git a/src/main/java/mods/eln/api/recipe/CompressorRecipeList.java b/src/main/java/mods/eln/api/recipe/CompressorRecipeList.java index 0b9547c9e..3935e076e 100644 --- a/src/main/java/mods/eln/api/recipe/CompressorRecipeList.java +++ b/src/main/java/mods/eln/api/recipe/CompressorRecipeList.java @@ -1,10 +1,10 @@ package mods.eln.api.recipe; -import java.util.ArrayList; - import mods.eln.api.Misc; import net.minecraft.item.ItemStack; +import java.util.ArrayList; + public class CompressorRecipeList implements IELNMachineRecipeList { private static RecipesList recipes = (RecipesList) Misc.getRecipeList("compressorRecipes"); @@ -38,8 +38,13 @@ public void addRecipe(ItemStack input, ItemStack output, double energy){ Recipe recipe = new Recipe(input, output, energy); recipes.addRecipe(recipe); } - - /** + + public void addRecipe(Recipe recipe) { + //Maintain existing checks + addRecipe(recipe.input, recipe.output, recipe.energy); + } + + /** * Removes a recipe from the compressor * @param input an ItemStack representing the input of the recipe */ diff --git a/src/main/java/mods/eln/api/recipe/IELNMachineRecipeList.java b/src/main/java/mods/eln/api/recipe/IELNMachineRecipeList.java index eb44f3968..5e8d8c714 100644 --- a/src/main/java/mods/eln/api/recipe/IELNMachineRecipeList.java +++ b/src/main/java/mods/eln/api/recipe/IELNMachineRecipeList.java @@ -16,6 +16,8 @@ public interface IELNMachineRecipeList { void addRecipe(ItemStack input, ItemStack output, double energy) throws IllegalArgumentException; + void addRecipe(Recipe recipe); + void removeRecipe(ItemStack input); void removeRecipeByOutput(ItemStack output); diff --git a/src/main/java/mods/eln/api/recipe/MaceratorRecipeList.java b/src/main/java/mods/eln/api/recipe/MaceratorRecipeList.java index 63a097289..28559171e 100644 --- a/src/main/java/mods/eln/api/recipe/MaceratorRecipeList.java +++ b/src/main/java/mods/eln/api/recipe/MaceratorRecipeList.java @@ -1,10 +1,10 @@ package mods.eln.api.recipe; -import java.util.ArrayList; - import mods.eln.api.Misc; import net.minecraft.item.ItemStack; +import java.util.ArrayList; + public class MaceratorRecipeList implements IELNMachineRecipeList { private static RecipesList recipes = (RecipesList) Misc.getRecipeList("maceratorRecipes"); @@ -42,8 +42,13 @@ public void addRecipe(ItemStack input, ItemStack output, double energy) { recipes.addRecipe(recipe); System.out.println("Added recipe to macerator!"); } - - /** + + public void addRecipe(Recipe recipe) { + //Maintain existing checks + addRecipe(recipe.input, recipe.output, recipe.energy); + } + + /** * Removes a recipe from the macerator * @param input an ItemStack representing the input of the recipe */ diff --git a/src/main/java/mods/eln/api/recipe/MagnetizerRecipeList.java b/src/main/java/mods/eln/api/recipe/MagnetizerRecipeList.java index c10dfdac2..a499054a6 100644 --- a/src/main/java/mods/eln/api/recipe/MagnetizerRecipeList.java +++ b/src/main/java/mods/eln/api/recipe/MagnetizerRecipeList.java @@ -1,14 +1,14 @@ package mods.eln.api.recipe; -import java.util.ArrayList; - import mods.eln.api.Misc; import net.minecraft.item.ItemStack; +import java.util.ArrayList; + public class MagnetizerRecipeList implements IELNMachineRecipeList { - private static RecipesList recipes = (RecipesList) Misc.getRecipeList("magnetiserRecipes"); - - /** + private static RecipesList recipes = (RecipesList) Misc.getRecipeList("magnetizerRecipes"); + + /** * Adds a new recipe to the magnetizer * @param input an ItemStack representing the input of the recipe * @param output an ItemStack[] representing the outputs of the recipe @@ -38,7 +38,12 @@ public void addRecipe(ItemStack input, ItemStack output, double energy){ Recipe recipe = new Recipe(input, output, energy); recipes.addRecipe(recipe); } - + + public void addRecipe(Recipe recipe) { + //Maintain existing checks + addRecipe(recipe.input, recipe.output, recipe.energy); + } + /** * Removes a recipe from the magnetizer * @param input an ItemStack representing the input of the recipe diff --git a/src/main/java/mods/eln/api/recipe/PlateMachineRecipeList.java b/src/main/java/mods/eln/api/recipe/PlateMachineRecipeList.java index a20a0c358..f917e7e65 100644 --- a/src/main/java/mods/eln/api/recipe/PlateMachineRecipeList.java +++ b/src/main/java/mods/eln/api/recipe/PlateMachineRecipeList.java @@ -1,10 +1,10 @@ package mods.eln.api.recipe; -import java.util.ArrayList; - import mods.eln.api.Misc; import net.minecraft.item.ItemStack; +import java.util.ArrayList; + public class PlateMachineRecipeList implements IELNMachineRecipeList { private static RecipesList recipes = (RecipesList) Misc.getRecipeList("plateMachineRecipes"); @@ -38,8 +38,13 @@ public void addRecipe(ItemStack input, ItemStack output, double energy){ Recipe recipe = new Recipe(input, output, energy); recipes.addRecipe(recipe); } - - /** + + public void addRecipe(Recipe recipe) { + //Maintain existing checks + addRecipe(recipe.input, recipe.output, recipe.energy); + } + + /** * Removes a recipe from the plate machine * @param input an ItemStack representing the input of the recipe */ diff --git a/src/main/java/mods/eln/integration/minetweaker/MinetweakerIntegration.java b/src/main/java/mods/eln/integration/minetweaker/MinetweakerIntegration.java deleted file mode 100644 index 778563817..000000000 --- a/src/main/java/mods/eln/integration/minetweaker/MinetweakerIntegration.java +++ /dev/null @@ -1,20 +0,0 @@ -package mods.eln.integration.minetweaker; - -import minetweaker.MineTweakerAPI; -import mods.eln.integration.minetweaker.machines.Compressor; -import mods.eln.integration.minetweaker.machines.Macerator; -import mods.eln.integration.minetweaker.machines.Magnetizer; -import mods.eln.integration.minetweaker.machines.PlateMachine; - -public class MinetweakerIntegration { - public static MinetweakerIntegration instance = new MinetweakerIntegration(); - - - public void initialize(){ - MineTweakerAPI.registerClass(Macerator.class); - MineTweakerAPI.registerClass(Compressor.class); - MineTweakerAPI.registerClass(Magnetizer.class); - MineTweakerAPI.registerClass(PlateMachine.class); - } - -} diff --git a/src/main/java/mods/eln/integration/minetweaker/MinetweakerIntegration.kt b/src/main/java/mods/eln/integration/minetweaker/MinetweakerIntegration.kt new file mode 100644 index 000000000..19ef2521f --- /dev/null +++ b/src/main/java/mods/eln/integration/minetweaker/MinetweakerIntegration.kt @@ -0,0 +1,18 @@ +package mods.eln.integration.minetweaker + +import minetweaker.MineTweakerAPI +import mods.eln.integration.minetweaker.machines.Compressor +import mods.eln.integration.minetweaker.machines.Macerator +import mods.eln.integration.minetweaker.machines.Magnetizer +import mods.eln.integration.minetweaker.machines.PlateMachine + +object MinetweakerIntegration { + + fun initialize() { + MineTweakerAPI.registerClass(Macerator::class.java) + MineTweakerAPI.registerClass(Compressor::class.java) + MineTweakerAPI.registerClass(Magnetizer::class.java) + MineTweakerAPI.registerClass(PlateMachine::class.java) + } + +} diff --git a/src/main/java/mods/eln/integration/minetweaker/utils/AddRecipe.java b/src/main/java/mods/eln/integration/minetweaker/utils/AddRecipe.java index f7123431e..79339d595 100644 --- a/src/main/java/mods/eln/integration/minetweaker/utils/AddRecipe.java +++ b/src/main/java/mods/eln/integration/minetweaker/utils/AddRecipe.java @@ -1,6 +1,6 @@ package mods.eln.integration.minetweaker.utils; -import mods.eln.misc.Recipe; +import mods.eln.api.recipe.Recipe; public class AddRecipe extends BasicUndoableAction { Recipe recipe; @@ -17,7 +17,7 @@ public void apply() { @Override public void undo() { - machine.recipes.getRecipes().remove(recipe); - } + machine.recipes.removeRecipe(recipe.input); + } } diff --git a/src/main/java/mods/eln/integration/minetweaker/utils/MinetweakerMachine.java b/src/main/java/mods/eln/integration/minetweaker/utils/MinetweakerMachine.java index 39406eb4c..6d43e54a9 100644 --- a/src/main/java/mods/eln/integration/minetweaker/utils/MinetweakerMachine.java +++ b/src/main/java/mods/eln/integration/minetweaker/utils/MinetweakerMachine.java @@ -1,51 +1,38 @@ package mods.eln.integration.minetweaker.utils; -import java.util.ArrayList; - import minetweaker.MineTweakerAPI; import minetweaker.api.item.IIngredient; import minetweaker.api.item.IItemStack; import minetweaker.api.minecraft.MineTweakerMC; -import mods.eln.Eln; -import mods.eln.misc.Recipe; -import mods.eln.misc.RecipesList; - +import mods.eln.api.recipe.*; import net.minecraft.item.ItemStack; public class MinetweakerMachine { - public static MinetweakerMachine MACERATOR = new MinetweakerMachine(Eln.instance.maceratorRecipes, "Adding Macerator Recipe", "Removing Macerator Recipe"); - public static MinetweakerMachine COMPRESSOR = new MinetweakerMachine(Eln.instance.compressorRecipes, "Adding Compressor Recipe", "Removing Compressor Recipe"); - public static MinetweakerMachine MAGNETIZER = new MinetweakerMachine(Eln.instance.magnetiserRecipes, "Adding Magnetizer Recipe", "Removing Magnetizer Recipe"); - public static MinetweakerMachine PLATEMACHINE = new MinetweakerMachine(Eln.instance.plateMachineRecipes, "Adding Plate Machine Recipe", "Removing Plate Machine Recipe"); + public static MinetweakerMachine MACERATOR = new MinetweakerMachine(new MaceratorRecipeList(), "Adding Macerator Recipe", "Removing Macerator Recipe"); + public static MinetweakerMachine COMPRESSOR = new MinetweakerMachine(new CompressorRecipeList(), "Adding Compressor Recipe", "Removing Compressor Recipe"); + public static MinetweakerMachine MAGNETIZER = new MinetweakerMachine(new MagnetizerRecipeList(), "Adding Magnetizer Recipe", "Removing Magnetizer Recipe"); + public static MinetweakerMachine PLATEMACHINE = new MinetweakerMachine(new PlateMachineRecipeList(), "Adding Plate Machine Recipe", "Removing Plate Machine Recipe"); - public RecipesList recipes; - public String removeDesc; + public IELNMachineRecipeList recipes; + public String removeDesc; public String addDesc; - public MinetweakerMachine(RecipesList recipes, String addDesc, String removeDesc) { - this.recipes = recipes; - this.addDesc = addDesc; + public MinetweakerMachine(IELNMachineRecipeList recipeList, String addDesc, String removeDesc) { + this.recipes = recipeList; + this.addDesc = addDesc; this.removeDesc = removeDesc; } public void addRecipe(IIngredient input, double energy, IItemStack[] output) { - if (input == null || output == null || energy < 0) - return; - if (output.length > 4) { - throw new IllegalArgumentException("Too much outputs"); - } - - ArrayList outs = new ArrayList(); - for (IIngredient i : output) { - ItemStack stack = MineTweakerMC.getItemStack(i); - if (stack != null) { - outs.add(stack); - } - } - - Recipe recipe = new Recipe(MineTweakerMC.getItemStack(input), outs.toArray(new ItemStack[] {}), energy); + ItemStack inStack = MineTweakerMC.getItemStack(input); + ItemStack[] outStacks = new ItemStack[output.length]; + for (int i = 0; i < output.length; i++) { + outStacks[i] = MineTweakerMC.getItemStack(output[i]); + } + + Recipe recipe = new Recipe(inStack, outStacks, energy); MineTweakerAPI.apply(new AddRecipe(this, recipe)); } diff --git a/src/main/java/mods/eln/integration/minetweaker/utils/RemoveRecipe.java b/src/main/java/mods/eln/integration/minetweaker/utils/RemoveRecipe.java index 0403bdbe0..4918efcbc 100644 --- a/src/main/java/mods/eln/integration/minetweaker/utils/RemoveRecipe.java +++ b/src/main/java/mods/eln/integration/minetweaker/utils/RemoveRecipe.java @@ -1,11 +1,10 @@ package mods.eln.integration.minetweaker.utils; -import java.util.ArrayList; - import minetweaker.api.item.IIngredient; import minetweaker.api.minecraft.MineTweakerMC; -import mods.eln.Eln; -import mods.eln.misc.Recipe; +import mods.eln.api.recipe.Recipe; + +import java.util.ArrayList; public class RemoveRecipe extends BasicUndoableAction{ @@ -15,22 +14,27 @@ public class RemoveRecipe extends BasicUndoableAction{ public RemoveRecipe(MinetweakerMachine machine, IIngredient input) { super(machine); this.input = input; - } + this.toRemove = new ArrayList<>(); + } @Override public void apply() { - toRemove = new ArrayList(); - for (Recipe r : Eln.instance.maceratorRecipes.getRecipes()){ - if(input.matches(MineTweakerMC.getIItemStack(r.input))){ - toRemove.add(r); - } - } - machine.recipes.getRecipes().removeAll(toRemove); - } + for (Recipe recipe : machine.recipes.getRecipes()) { + if (input.matches(MineTweakerMC.getIItemStack(recipe.input))) { + toRemove.add(recipe); + } + } + for (Recipe recipe : toRemove) { + machine.recipes.removeRecipe(recipe.input); + } + } @Override public void undo() { - machine.recipes.getRecipes().addAll(toRemove); - } + for (Recipe recipe : toRemove) { + machine.recipes.addRecipe(recipe); + } + toRemove = new ArrayList<>(); + } }