updated energy framework to be openned to different channels, close #1756

This commit is contained in:
SpaceToad 2014-05-09 18:13:02 +02:00
parent 5c8bd331d3
commit 1febf3c714
3 changed files with 136 additions and 38 deletions

View file

@ -1,14 +1,29 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.api.mj;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.logging.Level;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.JavaTools;
/**
* A battery object is a wrapper around a battery field in an object. This
* battery field is of type double, and is the only piece of data specific to
* this object. Others are class-wide.
*/
public class BatteryObject implements IBatteryObject {
protected Field f;
protected Object o;
protected MjBattery b;
protected Field energyStored;
protected Object obj;
protected MjBattery batteryData;
/**
* {@inheritDoc}
@ -16,9 +31,10 @@ public class BatteryObject implements IBatteryObject {
@Override
public double getEnergyRequested() {
try {
return JavaTools.bounds(b.maxCapacity() - f.getDouble(o), b.minimumConsumption(), b.maxReceivedPerCycle());
return JavaTools.bounds(batteryData.maxCapacity() - energyStored.getDouble(obj),
batteryData.minimumConsumption(), batteryData.maxReceivedPerCycle());
} catch (IllegalAccessException e) {
e.printStackTrace();
BCLog.logger.log(Level.WARNING, "can't get energy requested", e);
}
return 0;
}
@ -37,18 +53,18 @@ public class BatteryObject implements IBatteryObject {
@Override
public double addEnergy(double mj, boolean ignoreCycleLimit) {
try {
double contained = f.getDouble(o);
double maxAccepted = b.maxCapacity() - contained + b.minimumConsumption();
if (!ignoreCycleLimit && maxAccepted > b.maxReceivedPerCycle()) {
maxAccepted = b.maxReceivedPerCycle();
double contained = energyStored.getDouble(obj);
double maxAccepted = batteryData.maxCapacity() - contained + batteryData.minimumConsumption();
if (!ignoreCycleLimit && maxAccepted > batteryData.maxReceivedPerCycle()) {
maxAccepted = batteryData.maxReceivedPerCycle();
}
double used = Math.min(maxAccepted, mj);
if (used > 0) {
f.setDouble(o, Math.min(contained + used, b.maxCapacity()));
energyStored.setDouble(obj, Math.min(contained + used, batteryData.maxCapacity()));
return used;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
BCLog.logger.log(Level.WARNING, "can't add energy", e);
}
return 0;
@ -60,9 +76,9 @@ public class BatteryObject implements IBatteryObject {
@Override
public double getEnergyStored() {
try {
return f.getDouble(o);
return energyStored.getDouble(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
BCLog.logger.log(Level.WARNING, "can't get return energy stored", e);
return 0;
}
}
@ -73,8 +89,9 @@ public class BatteryObject implements IBatteryObject {
@Override
public void setEnergyStored(double mj) {
try {
f.setDouble(o, mj);
energyStored.setDouble(obj, mj);
} catch (IllegalAccessException e) {
BCLog.logger.log(Level.WARNING, "can't set energy stored", e);
throw new RuntimeException(e);
}
}
@ -84,7 +101,7 @@ public class BatteryObject implements IBatteryObject {
*/
@Override
public double maxCapacity() {
return b.maxCapacity();
return batteryData.maxCapacity();
}
/**
@ -92,7 +109,7 @@ public class BatteryObject implements IBatteryObject {
*/
@Override
public double minimumConsumption() {
return b.minimumConsumption();
return batteryData.minimumConsumption();
}
/**
@ -100,15 +117,16 @@ public class BatteryObject implements IBatteryObject {
*/
@Override
public double maxReceivedPerCycle() {
return b.maxReceivedPerCycle();
return batteryData.maxReceivedPerCycle();
}
/**
* {@inheritDoc}
*/
@Override
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption) {
b = new MjBattery() {
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle,
final double minimumConsumption) {
batteryData = new MjBattery() {
@Override
public double maxCapacity() {
return maxCapacity;
@ -128,7 +146,13 @@ public class BatteryObject implements IBatteryObject {
public Class<? extends Annotation> annotationType() {
return MjBattery.class;
}
@Override
public String kind() {
return MjAPI.DEFAULT_POWER_FRAMEWORK;
}
};
return this;
}
}

View file

@ -11,21 +11,21 @@ package buildcraft.api.mj;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.JavaTools;
/**
* The class MjAPI provides services to the Minecraft Joules power framework.
* BuildCraft implements a default power model on top of this, the "kinesis"
* power model. Third party mods may provide they own version of Minecraft
* Joules batteries and provide different models.
*/
public final class MjAPI {
private static Map<Class, BatteryField> MjBatteries = new HashMap<Class, BatteryField>();
private enum BatteryKind {
Value, Container
}
private static class BatteryField {
Field field;
MjBattery battery;
BatteryKind kind;
}
public static final String DEFAULT_POWER_FRAMEWORK = "buildcraft.kinesis";
private static Map<Class, BatteryField> mjBatteries = new HashMap<Class, BatteryField>();
private static Map<String, Class<? extends BatteryObject>> mjBatteryKinds = new HashMap<String, Class<? extends BatteryObject>>();
/**
* Deactivate constructor
@ -33,13 +33,28 @@ public final class MjAPI {
private MjAPI() {
}
/**
* Returns the default battery related to the object given in parameter. For
* performance optimization, it's good to cache this object in the providing
* power framework if possible.
*/
public static IBatteryObject getMjBattery(Object o) {
return getMjBattery(o, DEFAULT_POWER_FRAMEWORK);
}
/**
* Returns the battery related to the object given in parameter. For
* performance optimization, it's good to cache this object in the providing
* power framework if possible.
*/
public static IBatteryObject getMjBattery(Object o, String kind) {
if (o == null) {
return null;
}
if (o instanceof IBatteryProvider) {
IBatteryObject battery = ((IBatteryProvider) o).getMjBattery();
if (battery != null) {
return battery;
}
@ -49,12 +64,25 @@ public final class MjAPI {
if (f == null) {
return null;
} else if (!mjBatteryKinds.containsKey(kind)) {
return null;
} else if (f.kind == BatteryKind.Value) {
BatteryObject obj = new BatteryObject();
obj.o = o;
obj.f = f.field;
obj.b = f.battery;
return obj;
try {
BatteryObject obj = mjBatteryKinds.get(kind).newInstance();
obj.obj = o;
obj.energyStored = f.field;
obj.batteryData = f.battery;
return obj;
} catch (InstantiationException e) {
BCLog.logger.log(Level.WARNING, "can't instantiate class for energy kind \"" + kind + "\"");
return null;
} catch (IllegalAccessException e) {
BCLog.logger.log(Level.WARNING, "can't instantiate class for energy kind \"" + kind + "\"");
return null;
}
} else {
try {
return getMjBattery(f.field.get(o));
@ -65,8 +93,41 @@ public final class MjAPI {
}
}
public static IBatteryObject[] getAllMjBatteries(Object o) {
IBatteryObject[] result = new IBatteryObject[mjBatteries.size()];
int id = 0;
for (String kind : mjBatteryKinds.keySet()) {
result[id] = getMjBattery(o, kind);
id++;
}
return result;
}
public static void registerMJBatteryKind(String kind, Class<? extends BatteryObject> clas) {
if (!mjBatteryKinds.containsKey(kind)) {
mjBatteryKinds.put(kind, clas);
} else {
BCLog.logger.log(Level.WARNING,
"energy kind \"" + kind + "\" already registered with " + clas.getCanonicalName());
}
}
private enum BatteryKind {
Value, Container
}
private static class BatteryField {
public Field field;
public MjBattery battery;
public BatteryKind kind;
}
private static BatteryField getMjBatteryField(Class c) {
BatteryField bField = MjBatteries.get(c);
BatteryField bField = mjBatteries.get(c);
if (bField == null) {
for (Field f : JavaTools.getAllFields(c)) {
MjBattery battery = f.getAnnotation(MjBattery.class);
@ -86,14 +147,20 @@ public final class MjAPI {
bField.kind = BatteryKind.Container;
}
MjBatteries.put(c, bField);
mjBatteries.put(c, bField);
return bField;
}
}
MjBatteries.put(c, null);
mjBatteries.put(c, null);
}
return bField;
}
static {
mjBatteryKinds.put(MjAPI.DEFAULT_POWER_FRAMEWORK, BatteryObject.class);
}
}

View file

@ -48,4 +48,11 @@ public @interface MjBattery {
* @return Minimal energy for keep machine is active
*/
double minimumConsumption() default 0.1;
/**
* @return The kind of battery stored. Specific power systems can be created
* through this system, as several battery of different kind can
* coexist in the same tile.
*/
String kind() default MjAPI.DEFAULT_POWER_FRAMEWORK;
}