/MineTweaker3-MC172-Mod-Buildcraft/src/main/java/minetweaker/mods/buildcraft/Refinery.java

https://gitlab.com/MineYourMind/MineTweaker3 · Java · 202 lines · 158 code · 32 blank · 12 comment · 4 complexity · 38801e75ced428a9ee414ba61c1eae4b MD5 · raw file

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package minetweaker.mods.buildcraft;
  7. import buildcraft.api.recipes.BuildcraftRecipes;
  8. import buildcraft.core.recipes.RefineryRecipeManager;
  9. import buildcraft.core.recipes.RefineryRecipeManager.RefineryRecipe;
  10. import java.lang.reflect.Constructor;
  11. import java.lang.reflect.Field;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.SortedSet;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import minetweaker.IUndoableAction;
  19. import minetweaker.MineTweakerAPI;
  20. import minetweaker.annotations.ModOnly;
  21. import minetweaker.api.liquid.ILiquidStack;
  22. import minetweaker.api.minecraft.MineTweakerMC;
  23. import net.minecraftforge.fluids.Fluid;
  24. import net.minecraftforge.fluids.FluidStack;
  25. import stanhebben.zenscript.annotations.Optional;
  26. import stanhebben.zenscript.annotations.ZenClass;
  27. import stanhebben.zenscript.annotations.ZenMethod;
  28. /**
  29. *
  30. * @author Stan
  31. */
  32. @ZenClass("mods.buildcraft.Refinery")
  33. @ModOnly("BuildCraft|Core")
  34. public class Refinery {
  35. private static final Constructor CONSTRUCT_REFINERYRECIPE;
  36. private static final Field REFINERYRECIPEMANAGER_RECIPES;
  37. static {
  38. Constructor constructor = null;
  39. try {
  40. constructor = RefineryRecipe.class.getDeclaredConstructor(FluidStack.class, FluidStack.class, FluidStack.class, int.class, int.class);
  41. constructor.setAccessible(true);
  42. } catch (NoSuchMethodException ex) {
  43. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  44. } catch (SecurityException ex) {
  45. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  46. }
  47. Field recipes = null;
  48. try {
  49. recipes = RefineryRecipeManager.class.getDeclaredField("recipes");
  50. recipes.setAccessible(true);
  51. } catch (NoSuchFieldException ex) {
  52. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  53. } catch (SecurityException ex) {
  54. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  55. }
  56. CONSTRUCT_REFINERYRECIPE = constructor;
  57. REFINERYRECIPEMANAGER_RECIPES = recipes;
  58. }
  59. @ZenMethod
  60. public static void addRecipe(ILiquidStack output, int energyPerMB, int ticksPerMB, ILiquidStack input1, @Optional ILiquidStack input2) {
  61. MineTweakerAPI.apply(new AddRecipeAction(output, energyPerMB, ticksPerMB, input1, input2));
  62. }
  63. @ZenMethod
  64. public static void remove(ILiquidStack output) {
  65. Fluid fluid = MineTweakerMC.getLiquidStack(output).getFluid();
  66. List<RefineryRecipe> toRemove = new ArrayList<RefineryRecipe>();
  67. for (RefineryRecipe recipe : ((RefineryRecipeManager) BuildcraftRecipes.refinery).getRecipes()) {
  68. if (recipe.getResult().getFluid() == fluid) {
  69. toRemove.add(recipe);
  70. }
  71. }
  72. for (RefineryRecipe recipe : toRemove) {
  73. MineTweakerAPI.apply(new RemoveRecipeAction(recipe));
  74. }
  75. }
  76. private static SortedSet<RefineryRecipe> getRecipes() {
  77. try {
  78. return (SortedSet<RefineryRecipe>) REFINERYRECIPEMANAGER_RECIPES.get((RefineryRecipeManager) BuildcraftRecipes.refinery);
  79. } catch (IllegalArgumentException ex) {
  80. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  81. } catch (IllegalAccessException ex) {
  82. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  83. }
  84. throw new RuntimeException("Refinery recipes unavailable");
  85. }
  86. // ######################
  87. // ### Action Classes ###
  88. // ######################
  89. private static class AddRecipeAction implements IUndoableAction {
  90. private final ILiquidStack output;
  91. private final RefineryRecipe recipe;
  92. public AddRecipeAction(ILiquidStack output, int energyPerMB, int ticksPerMB, ILiquidStack input1, ILiquidStack input2) {
  93. this.output = output;
  94. RefineryRecipe rrecipe = null;
  95. try {
  96. rrecipe = (RefineryRecipe) CONSTRUCT_REFINERYRECIPE.newInstance(
  97. MineTweakerMC.getLiquidStack(input1),
  98. MineTweakerMC.getLiquidStack(input2),
  99. MineTweakerMC.getLiquidStack(output),
  100. energyPerMB,
  101. ticksPerMB);
  102. } catch (InstantiationException ex) {
  103. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  104. } catch (IllegalAccessException ex) {
  105. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  106. } catch (IllegalArgumentException ex) {
  107. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  108. } catch (InvocationTargetException ex) {
  109. Logger.getLogger(Refinery.class.getName()).log(Level.SEVERE, null, ex);
  110. }
  111. recipe = rrecipe;
  112. }
  113. @Override
  114. public void apply() {
  115. getRecipes().add(recipe);
  116. }
  117. @Override
  118. public boolean canUndo() {
  119. return true;
  120. }
  121. @Override
  122. public void undo() {
  123. getRecipes().add(recipe);
  124. }
  125. @Override
  126. public String describe() {
  127. return "Adding refinery recipe for " + output;
  128. }
  129. @Override
  130. public String describeUndo() {
  131. return "Removing refinery recipe for " + output;
  132. }
  133. @Override
  134. public Object getOverrideKey() {
  135. return null;
  136. }
  137. }
  138. private static class RemoveRecipeAction implements IUndoableAction {
  139. private final RefineryRecipe recipe;
  140. public RemoveRecipeAction(RefineryRecipe recipe) {
  141. this.recipe = recipe;
  142. }
  143. @Override
  144. public void apply() {
  145. getRecipes().remove(recipe);
  146. }
  147. @Override
  148. public boolean canUndo() {
  149. return true;
  150. }
  151. @Override
  152. public void undo() {
  153. getRecipes().add(recipe);
  154. }
  155. @Override
  156. public String describe() {
  157. return "Removing refinery recipe for "
  158. + recipe.getResult().getFluid().getLocalizedName();
  159. }
  160. @Override
  161. public String describeUndo() {
  162. return "Restoring refinery recipe for "
  163. + recipe.getResult().getFluid().getLocalizedName();
  164. }
  165. @Override
  166. public Object getOverrideKey() {
  167. return null;
  168. }
  169. }
  170. }