PageRenderTime 37ms CodeModel.GetById 22ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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