Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ready for Review] A basic API #652

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,18 @@ repositories {
name = "mobiusstrip"
url = "http://default.mobiusstrip.eu/maven/"
}
ivy {
name 'MineTweaker3'
artifactPattern "http://minetweaker3.powerofbytes.com/download/[module]-[classifier]-[revision].[ext]"
}
}

dependencies {
external files("libs/commons-math3-3.3.jar")
shade "org.jetbrains.kotlin:kotlin-runtime:$kotlin_version"
shade "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "mcp.mobius.waila:Waila:1.5.11-RC2-NONEI_1.7.10:dev"
compile name: 'MineTweaker3', version: '1.7.10-3.0.10B', classifier: 'Dev', ext: 'jar'
}

jar {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/mods/eln/Eln.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import cpw.mods.fml.common.registry.EntityRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import mods.eln.api.Utilities;
import mods.eln.api.recipe.Recipe;
import mods.eln.api.recipe.RecipesList;
import mods.eln.cable.CableRenderDescriptor;
import mods.eln.client.ClientKeyHandler;
import mods.eln.client.SoundLoader;
Expand All @@ -25,6 +28,7 @@
import mods.eln.ghost.GhostManagerNbt;
import mods.eln.gridnode.electricalpole.ElectricalPoleDescriptor;
import mods.eln.i18n.I18N;
import mods.eln.integration.minetweaker.MinetweakerIntegration;
import mods.eln.item.*;
import mods.eln.item.electricalinterface.ItemEnergyInventoryProcess;
import mods.eln.item.electricalitem.*;
Expand Down Expand Up @@ -941,7 +945,7 @@ private boolean recipeExists(ItemStack stack) {
IRecipe r = (IRecipe) o;
if (r.getRecipeOutput() == null)
continue;
if (Utils.areSame(stack, r.getRecipeOutput()))
if (Utilities.areSame(stack, r.getRecipeOutput()))
return true;
}
}
Expand All @@ -956,7 +960,10 @@ private boolean recipeExists(ItemStack stack) {
public void postInit(FMLPostInitializationEvent event) {

serverEventListener = new ServerEventListener();


if(Loader.isModLoaded("MineTweaker3")){
MinetweakerIntegration.INSTANCE.initialize();
}
}

/*
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/mods/eln/api/Fuel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package mods.eln.api;

import cpw.mods.fml.common.FMLLog;
import net.minecraft.item.ItemStack;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;

public class Fuel {
/**
* Gives the energy equivalent of a fuel in J. Returns 0 if the item cannot be burned.
* @param fuel The ItemStack of fuel to determine the energy equivalent of.
* @return fuel's energy equivalent in J.
*/
public static double getEnergyEquivalent(ItemStack fuel){
try {
Class<?> c = Class.forName("mods.eln.misc.Utils");
Method energy = c.getDeclaredMethod("getItemEnergie", ItemStack.class);
return Double.valueOf(energy.invoke(c, fuel).toString());
}
catch(ClassNotFoundException e){
FMLLog.warning("ELN isn't loaded. Someone just tried to use its API.");
e.printStackTrace();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return 0;
}

/**
* Add a fuel to the gasoline fuels list (turbine and fuel generator).
* @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, Double heatingValue) {
try {
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();
}
return false;
}

/**
* Add a fuel to the gas fuels list (gas turbine).
* @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, Double heatingValue) {
try {
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();
}
return false;
}


/**
* Add a fuel to the diesels list.
* @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) {
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;
}

}
48 changes: 48 additions & 0 deletions src/main/java/mods/eln/api/Misc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mods.eln.api;

import java.lang.reflect.Field;

/**
* Created by Gregory Maddra on 2016-11-16.
*/
public class Misc {

public static Object getRecipeList(String list){
try {
Class<?> Eln = getEln();
Object instanceObject = getElnInstance(Eln);
Field recipeList = Eln.getDeclaredField(list);
recipeList.setAccessible(true);
return Eln != null ? recipeList.get(instanceObject) : null;
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}

public static Double getElectricalFurnaceProcessEnergyNeededPerSmelt(){
try {
Class<?> ElectricalFurnaceProcess = Class.forName("mods.eln.transparentnode.electricalfurnace.ElectricalFurnaceProcess");
return ElectricalFurnaceProcess.getDeclaredField("energyNeededPerSmelt").getDouble(null);
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return 0D;
}

public static Class<?> getEln(){
try {
return Class.forName("mods.eln.Eln");
} catch (ClassNotFoundException e) {
return null;
}
}

public static Object getElnInstance(Class<?> Eln){
try {
return Eln.getDeclaredField("instance").get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
return null;
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/mods/eln/api/Utilities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mods.eln.api;

import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
* Utilities
* @author Gregory Maddra
* 2016-11-18
*/
public class Utilities {

public static void makeModifiable(Field field){
field.setAccessible(true);
int modifiers = field.getModifiers();
try {
Field modifiersField = field.getClass().getDeclaredField("modifiers");
modifiers = modifiers & ~Modifier.FINAL;
modifiersField.setAccessible(true);
modifiersField.setInt(field, modifiers);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}

}

public static boolean areSame(ItemStack stack, ItemStack output) {
try {
if (stack.getItem() == output.getItem() && stack.getItemDamage() == output.getItemDamage()) return true;
int[] stackIds = OreDictionary.getOreIDs(stack);
int[] outputIds = OreDictionary.getOreIDs(output);
// System.out.println(Arrays.toString(stackIds) + " " + Arrays.toString(outputIds));
for (int i : outputIds) {
for (int j : stackIds) {
if (i == j) return true;
}
}
} catch (Exception ignored) {
}
return false;
}
}
3 changes: 3 additions & 0 deletions src/main/java/mods/eln/api/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@API(owner = "Eln", apiVersion = "1.0", provides = "ElectricalAge|API")
package mods.eln.api;
import cpw.mods.fml.common.API;
82 changes: 82 additions & 0 deletions src/main/java/mods/eln/api/recipe/CompressorRecipeList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package mods.eln.api.recipe;

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");

/**
* Adds a new recipe to the compressor
* @param input an ItemStack representing the input of the recipe
* @param output an ItemStack[] representing the outputs of the recipe
* @param energy a double representing the energy cost of the recipe in J.
*/
public void addRecipe(ItemStack input, ItemStack[] output, double energy){
if(input == null || output == null || energy < 0){
throw new IllegalArgumentException("Invalid Arguments");
}
if(output.length > 4){
throw new IllegalArgumentException("Too many recipe outputs: " + output.length);
}
if(energy == 0){
energy = 0.001;
}
Recipe recipe = new Recipe(input, output, energy);
recipes.addRecipe(recipe);
}

public void addRecipe(ItemStack input, ItemStack output, double energy){
if(input == null || output == null || energy < 0){
throw new IllegalArgumentException("Invalid Inputs");
}
if(energy == 0){
energy = 0.001;
}
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
*/
public void removeRecipe(ItemStack input){
if(input == null){
System.out.println("Unable to remove recipe: invalid input ItemStack");
return;
}
Recipe r = recipes.getRecipe(input);
recipes.getRecipes().remove(r);
}

/**
* Removes recipes from the compressor by their output ItemStacks
* @param output the ItemStack who's recipes will be removed
*/
public void removeRecipeByOutput(ItemStack output){
if (output == null){
System.out.println("Unable to remove recipe: invalid output ItemStack");
return;
}
ArrayList<Recipe> r = recipes.getRecipeFromOutput(output);
for (Recipe recipe : r){
recipes.getRecipes().remove(recipe);
}
}

/**
* Returns all recipes from the compressor
* @return an ArrayList<Recipe> of all the machine's recipes
*/
public ArrayList<Recipe> getRecipes(){
return recipes.getRecipes();
}
}
28 changes: 28 additions & 0 deletions src/main/java/mods/eln/api/recipe/IELNMachineRecipeList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mods.eln.api.recipe;

import net.minecraft.item.ItemStack;

import java.util.ArrayList;

/**
* Interface for ELN machines to use in the API
* @author bloxgate
*
*/
public interface IELNMachineRecipeList {


void addRecipe(ItemStack input, ItemStack[] output, double energy) throws IllegalArgumentException;

void addRecipe(ItemStack input, ItemStack output, double energy) throws IllegalArgumentException;

void addRecipe(Recipe recipe);

void removeRecipe(ItemStack input);

void removeRecipeByOutput(ItemStack output);

ArrayList<Recipe> getRecipes();


}
Loading