PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/MineTweaker3-MC1710-Mod-Buildcraft/src/main/java/minetweaker/mods/buildcraft/Fuels.java

https://gitlab.com/MineYourMind/MineTweaker3
Java | 375 lines | 296 code | 67 blank | 12 comment | 10 complexity | a660fed0bf18b8ef2263a803935533df 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.core.StackKey;
  8. import buildcraft.api.fuels.IronEngineCoolant;
  9. import buildcraft.api.fuels.IronEngineFuel;
  10. import java.lang.reflect.Constructor;
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import minetweaker.IUndoableAction;
  15. import minetweaker.MineTweakerAPI;
  16. import minetweaker.annotations.ModOnly;
  17. import minetweaker.api.item.IIngredient;
  18. import minetweaker.api.item.IItemStack;
  19. import minetweaker.api.liquid.ILiquidStack;
  20. import minetweaker.api.minecraft.MineTweakerMC;
  21. import net.minecraftforge.fluids.Fluid;
  22. import net.minecraftforge.fluids.FluidStack;
  23. import stanhebben.zenscript.annotations.ZenClass;
  24. import stanhebben.zenscript.annotations.ZenMethod;
  25. /**
  26. *
  27. * @author Stan
  28. */
  29. @ZenClass("mods.buildcraft.Fuels")
  30. @ModOnly(value="BuildCraft|Core", version="6.0")
  31. public class Fuels {
  32. private static final Constructor CONSTRUCT_FUEL;
  33. static {
  34. Constructor newFuel = null;
  35. try {
  36. newFuel = IronEngineFuel.Fuel.class.getDeclaredConstructor(Fluid.class, float.class, int.class);
  37. newFuel.setAccessible(true);
  38. } catch (NoSuchMethodException ex) {
  39. MineTweakerAPI.logError("Could not load combustion engine fuel constructor", ex);
  40. } catch (SecurityException ex) {
  41. MineTweakerAPI.logError("Could not load combustion engine fuel constructor", ex);
  42. }
  43. CONSTRUCT_FUEL = newFuel;
  44. }
  45. @ZenMethod
  46. public static void addCombustionEngineFuel(ILiquidStack fuel, double powerPerCycle, int totalBurningTime) {
  47. MineTweakerAPI.apply(new AddFuelAction(fuel, powerPerCycle, totalBurningTime));
  48. }
  49. @ZenMethod
  50. public static void removeCombustionEngineFuel(ILiquidStack fuel) {
  51. Fluid fluid = MineTweakerMC.getLiquidStack(fuel).getFluid();
  52. if (IronEngineFuel.fuels.containsKey(fluid.getName())) {
  53. MineTweakerAPI.apply(new RemoveFuelAction(fluid.getName(), IronEngineFuel.fuels.get(fluid.getName())));
  54. } else {
  55. MineTweakerAPI.logWarning("No such iron engine fuel: " + fluid.getName());
  56. }
  57. }
  58. @ZenMethod
  59. public static void addCombustionEngineCoolant(ILiquidStack coolant, float coolingPerMB) {
  60. MineTweakerAPI.apply(new AddLiquidCoolantAction(coolant.getName(), new MTCoolant(coolingPerMB)));
  61. }
  62. @ZenMethod
  63. public static void addCoolantItem(IItemStack coolantItem, ILiquidStack coolantLiquid) {
  64. MineTweakerAPI.apply(new AddCoolantItemAction(new StackKey(MineTweakerMC.getItemStack(coolantItem)), MineTweakerMC.getLiquidStack(coolantLiquid)));
  65. }
  66. @ZenMethod
  67. public static void removeCombustionEngineCoolant(ILiquidStack coolant) {
  68. FluidStack fluid = MineTweakerMC.getLiquidStack(coolant);
  69. if (IronEngineCoolant.isCoolant(fluid.getFluid())) {
  70. MineTweakerAPI.apply(new RemoveLiquidCoolantAction(fluid.getFluid().getName(), IronEngineCoolant.getCoolant(fluid)));
  71. } else {
  72. MineTweakerAPI.logWarning("No such iron engine coolant: " + fluid.getFluid().getName());
  73. }
  74. }
  75. @ZenMethod
  76. public static void removeCoolantItem(IIngredient item) {
  77. int numRemoved = 0;
  78. for (IItemStack itemStack : item.getItems()) {
  79. StackKey key = new StackKey(MineTweakerMC.getItemStack(itemStack));
  80. if (IronEngineCoolant.solidCoolants.containsKey(key)) {
  81. MineTweakerAPI.apply(new RemoveCoolantItemAction(key, IronEngineCoolant.solidCoolants.get(key)));
  82. numRemoved++;
  83. }
  84. }
  85. if (numRemoved == 0) {
  86. MineTweakerAPI.logWarning("No such iron engine coolant: " + item);
  87. }
  88. }
  89. // ######################
  90. // ### Action classes ###
  91. // ######################
  92. private static class AddFuelAction implements IUndoableAction {
  93. private final String fuelName;
  94. private final IronEngineFuel.Fuel fuel;
  95. public AddFuelAction(ILiquidStack fuel, double powerPerCycle, int totalBurningTime) {
  96. fuelName = fuel.getName();
  97. IronEngineFuel.Fuel ieFuel = null;
  98. try {
  99. if (CONSTRUCT_FUEL == null)
  100. System.out.println("No constructor for fuel");
  101. ieFuel = (IronEngineFuel.Fuel) CONSTRUCT_FUEL.newInstance(MineTweakerMC.getLiquidStack(fuel).getFluid(), (float) powerPerCycle, totalBurningTime);
  102. } catch (InstantiationException ex) {
  103. Logger.getLogger(Fuels.class.getName()).log(Level.SEVERE, null, ex);
  104. } catch (IllegalAccessException ex) {
  105. Logger.getLogger(Fuels.class.getName()).log(Level.SEVERE, null, ex);
  106. } catch (IllegalArgumentException ex) {
  107. Logger.getLogger(Fuels.class.getName()).log(Level.SEVERE, null, ex);
  108. } catch (InvocationTargetException ex) {
  109. Logger.getLogger(Fuels.class.getName()).log(Level.SEVERE, null, ex);
  110. }
  111. this.fuel = ieFuel;
  112. }
  113. @Override
  114. public void apply() {
  115. IronEngineFuel.fuels.put(fuelName, fuel);
  116. }
  117. @Override
  118. public boolean canUndo() {
  119. return true;
  120. }
  121. @Override
  122. public void undo() {
  123. IronEngineFuel.fuels.remove(fuelName);
  124. }
  125. @Override
  126. public String describe() {
  127. return "Adding iron engine fuel " + fuelName;
  128. }
  129. @Override
  130. public String describeUndo() {
  131. return "Removing iron engine fuel " + fuelName;
  132. }
  133. @Override
  134. public Object getOverrideKey() {
  135. return null;
  136. }
  137. }
  138. private static class RemoveFuelAction implements IUndoableAction {
  139. private final String fuelName;
  140. private final IronEngineFuel.Fuel fuel;
  141. public RemoveFuelAction(String fuelName, IronEngineFuel.Fuel fuel) {
  142. this.fuelName = fuelName;
  143. this.fuel = fuel;
  144. }
  145. @Override
  146. public void apply() {
  147. IronEngineFuel.fuels.remove(fuelName);
  148. }
  149. @Override
  150. public boolean canUndo() {
  151. return true;
  152. }
  153. @Override
  154. public void undo() {
  155. IronEngineFuel.fuels.put(fuelName, fuel);
  156. }
  157. @Override
  158. public String describe() {
  159. return "Removing iron engine fuel " + fuelName;
  160. }
  161. @Override
  162. public String describeUndo() {
  163. return "Restoring iron engine fuel " + fuelName;
  164. }
  165. @Override
  166. public Object getOverrideKey() {
  167. return null;
  168. }
  169. }
  170. private static class AddLiquidCoolantAction implements IUndoableAction {
  171. private final String liquid;
  172. private final IronEngineCoolant.Coolant coolant;
  173. public AddLiquidCoolantAction(String liquid, IronEngineCoolant.Coolant coolant) {
  174. this.liquid = liquid;
  175. this.coolant = coolant;
  176. }
  177. @Override
  178. public void apply() {
  179. IronEngineCoolant.liquidCoolants.put(liquid, coolant);
  180. }
  181. @Override
  182. public boolean canUndo() {
  183. return false;
  184. }
  185. @Override
  186. public void undo() {
  187. IronEngineCoolant.liquidCoolants.remove(liquid);
  188. }
  189. @Override
  190. public String describe() {
  191. return "Adding iron engine coolant " + liquid;
  192. }
  193. @Override
  194. public String describeUndo() {
  195. return "Removing iron engine coolant " + liquid;
  196. }
  197. @Override
  198. public Object getOverrideKey() {
  199. return null;
  200. }
  201. }
  202. private static class AddCoolantItemAction implements IUndoableAction {
  203. private final StackKey item;
  204. private final FluidStack liquid;
  205. public AddCoolantItemAction(StackKey item, FluidStack liquid) {
  206. this.item = item;
  207. this.liquid = liquid;
  208. }
  209. @Override
  210. public void apply() {
  211. IronEngineCoolant.solidCoolants.put(item, liquid);
  212. }
  213. @Override
  214. public boolean canUndo() {
  215. return true;
  216. }
  217. @Override
  218. public void undo() {
  219. IronEngineCoolant.solidCoolants.remove(item);
  220. }
  221. @Override
  222. public String describe() {
  223. return "Adding iron engine coolant item " + MineTweakerMC.getIItemStack(item.stack);
  224. }
  225. @Override
  226. public String describeUndo() {
  227. return "Removing iron engine coolant item " + MineTweakerMC.getIItemStack(item.stack);
  228. }
  229. @Override
  230. public Object getOverrideKey() {
  231. return null;
  232. }
  233. }
  234. private static class RemoveLiquidCoolantAction implements IUndoableAction {
  235. private final String liquid;
  236. private final IronEngineCoolant.Coolant coolant;
  237. public RemoveLiquidCoolantAction(String liquid, IronEngineCoolant.Coolant coolant) {
  238. this.liquid = liquid;
  239. this.coolant = coolant;
  240. }
  241. @Override
  242. public void apply() {
  243. IronEngineCoolant.liquidCoolants.remove(liquid);
  244. }
  245. @Override
  246. public boolean canUndo() {
  247. return true;
  248. }
  249. @Override
  250. public void undo() {
  251. IronEngineCoolant.liquidCoolants.put(liquid, coolant);
  252. }
  253. @Override
  254. public String describe() {
  255. return "Removing iron engine coolant " + liquid;
  256. }
  257. @Override
  258. public String describeUndo() {
  259. return "Restoring iron engine coolant " + liquid;
  260. }
  261. @Override
  262. public Object getOverrideKey() {
  263. return null;
  264. }
  265. }
  266. private static class RemoveCoolantItemAction implements IUndoableAction {
  267. private final StackKey item;
  268. private final FluidStack liquid;
  269. public RemoveCoolantItemAction(StackKey item, FluidStack liquid) {
  270. this.item = item;
  271. this.liquid = liquid;
  272. }
  273. @Override
  274. public void apply() {
  275. IronEngineCoolant.solidCoolants.remove(item);
  276. }
  277. @Override
  278. public boolean canUndo() {
  279. return true;
  280. }
  281. @Override
  282. public void undo() {
  283. IronEngineCoolant.solidCoolants.put(item, liquid);
  284. }
  285. @Override
  286. public String describe() {
  287. return "Removing iron engine coolant item " + item.stack.getDisplayName();
  288. }
  289. @Override
  290. public String describeUndo() {
  291. return "Restoring iron engine coolant item " + item.stack.getDisplayName();
  292. }
  293. @Override
  294. public Object getOverrideKey() {
  295. return null;
  296. }
  297. }
  298. private static class MTCoolant implements IronEngineCoolant.Coolant {
  299. private final float coolingPerMB;
  300. public MTCoolant(float coolingPerMB) {
  301. this.coolingPerMB = coolingPerMB;
  302. }
  303. @Override
  304. public float getDegreesCoolingPerMB(float f) {
  305. return coolingPerMB;
  306. }
  307. }
  308. }