Skip to content

Commit

Permalink
Update fuel registry handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bloxgate committed Feb 12, 2017
1 parent 054f902 commit 19eac9d
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions src/main/java/mods/eln/api/Fuel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;

public class Fuel {
/**
Expand All @@ -34,17 +35,23 @@ public static double getEnergyEquivalent(ItemStack fuel){
* Add a fuel to the gasoline fuels list (turbine and fuel generator).
* Must be called before Eln starts preinit.
* @param name The name of the fuel in the fuel registry
* @param heatingValue The energy for 1L of the fuel
* @return true if the addition succeeded, false otherwise.
*/
public static boolean addGasolineFuel(String name){
public static boolean addGasolineFuel(String name, Double heatingValue) {
try {
Class<?> FuelRegistry = Class.forName("mods.eln.misc.FuelRegistryKt");
Class<?> FuelRegistry = Class.forName("mods.eln.fluid.FuelRegistry");
Field gasolineList = FuelRegistry.getDeclaredField("gasolineList");
Utilities.makeModifiable(gasolineList);
String[] gasolineArray = ((String[]) gasolineList.get(null));
String[] newArray = Arrays.copyOf(gasolineArray, gasolineArray.length + 1);
newArray[newArray.length - 1] = name;
gasolineList.set(null, newArray);

Field gasolineFuels = FuelRegistry.getDeclaredField("gasolineFuels");
Utilities.makeModifiable(gasolineFuels);
Map<String, Double> gasolineMap = (Map<String, Double>) gasolineFuels.get(null);
gasolineMap.put(name, heatingValue);
return true;
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
Expand All @@ -56,17 +63,24 @@ public static boolean addGasolineFuel(String name){
* Add a fuel to the gas fuels list (gas turbine).
* Must be called before Eln starts preinit.
* @param name The name of the fuel in the fuel registry
* @param heatingValue The energy of the fuel in MJ per cubic meter
* @return true if the addition succeeded, false otherwise.
*/
public static boolean addGasFuel(String name){
public static boolean addGasFuel(String name, Double heatingValue) {
try {
Class<?> FuelRegistry = Class.forName("mods.eln.misc.FuelRegistryKt");
Class<?> FuelRegistry = Class.forName("mods.eln.fluid.FuelRegistry");
Field gasList = FuelRegistry.getDeclaredField("gasList");
Utilities.makeModifiable(gasList);
String[] gasArray = (String[]) gasList.get(null);
String[] newList = Arrays.copyOf(gasArray, gasArray.length + 1);
newList[newList.length - 1] = name;
gasList.set(null, newList);

Field gasFuels = FuelRegistry.getDeclaredField("gasFuels");
Utilities.makeModifiable(gasFuels);
Map<String, Double> gasMap = (Map<String, Double>) gasFuels.get(null);
gasMap.put(name, heatingValue);
gasFuels.set(null, gasMap);
return true;
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
Expand All @@ -75,7 +89,34 @@ public static boolean addGasFuel(String name){
}


public static boolean addDieselFuel(String name) throws NotImplementedException {
throw new NotImplementedException(); //Diesel fuels aren't used yet
/**
* Add a fuel to the diesels list.
* Must be called before Eln starts preinit.
*
* @param name The name of the fuel in the fuel registry
* @param heatingValue Energy for 1L of the fuel
* @return true if the addition succeeded, false otherwise.
*/
public static boolean addDieselFuel(String name, Double heatingValue) throws NotImplementedException {
try {
Class<?> FuelRegistry = Class.forName("mods.eln.fluid.FuelRegistry");
Field dieselFuels = FuelRegistry.getDeclaredField("dieselFuels");
Utilities.makeModifiable(dieselFuels);
Map<String, Double> dieselMap = (Map<String, Double>) dieselFuels.get(null);
dieselMap.put(name, heatingValue);
dieselFuels.set(null, dieselMap);

Field dieselList = FuelRegistry.getDeclaredField("dieselList");
Utilities.makeModifiable(dieselList);
String[] dieselArray = (String[]) dieselList.get(null);
String[] newList = Arrays.copyOf(dieselArray, dieselArray.length + 1);
newList[newList.length - 1] = name;
dieselList.set(null, newList);
return true;
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}

return false;
}
}

0 comments on commit 19eac9d

Please sign in to comment.