Merge branch 'Prototik-sided-mjbattery' into 6.0.x
This commit is contained in:
commit
2738fbaef0
8 changed files with 101 additions and 35 deletions
|
@ -124,8 +124,7 @@ public class BatteryObject implements IBatteryObject {
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle,
|
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption) {
|
||||||
final double minimumConsumption) {
|
|
||||||
batteryData = new MjBattery() {
|
batteryData = new MjBattery() {
|
||||||
@Override
|
@Override
|
||||||
public double maxCapacity() {
|
public double maxCapacity() {
|
||||||
|
|
|
@ -74,5 +74,5 @@ public interface IBatteryObject {
|
||||||
* @param minimumConsumption {@link #minimumConsumption()}
|
* @param minimumConsumption {@link #minimumConsumption()}
|
||||||
* @return Current battery object instance
|
* @return Current battery object instance
|
||||||
*/
|
*/
|
||||||
IBatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption);
|
IBatteryObject reconfigure(double maxCapacity, double maxReceivedPerCycle, double minimumConsumption);
|
||||||
}
|
}
|
||||||
|
|
15
api/buildcraft/api/mj/ISidedBatteryProvider.java
Normal file
15
api/buildcraft/api/mj/ISidedBatteryProvider.java
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2014, Prototik and the BuildFactory Team
|
||||||
|
* http://buildfactory.org/
|
||||||
|
*
|
||||||
|
* BuildFactory 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://buildfactory.org/license
|
||||||
|
*/
|
||||||
|
package buildcraft.api.mj;
|
||||||
|
|
||||||
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
|
||||||
|
public interface ISidedBatteryProvider extends IBatteryProvider {
|
||||||
|
IBatteryObject getMjBattery(ForgeDirection direction);
|
||||||
|
}
|
|
@ -9,10 +9,13 @@
|
||||||
package buildcraft.api.mj;
|
package buildcraft.api.mj;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
|
||||||
import buildcraft.api.core.BCLog;
|
import buildcraft.api.core.BCLog;
|
||||||
import buildcraft.api.core.JavaTools;
|
import buildcraft.api.core.JavaTools;
|
||||||
|
|
||||||
|
@ -24,8 +27,9 @@ import buildcraft.api.core.JavaTools;
|
||||||
*/
|
*/
|
||||||
public final class MjAPI {
|
public final class MjAPI {
|
||||||
public static final String DEFAULT_POWER_FRAMEWORK = "buildcraft.kinesis";
|
public static final String DEFAULT_POWER_FRAMEWORK = "buildcraft.kinesis";
|
||||||
private static Map<Class, BatteryField> mjBatteries = new HashMap<Class, BatteryField>();
|
private static Map<BatteryHolder, BatteryField> mjBatteries = new HashMap<BatteryHolder, BatteryField>();
|
||||||
private static Map<String, Class<? extends BatteryObject>> mjBatteryKinds = new HashMap<String, Class<? extends BatteryObject>>();
|
private static Map<String, Class<? extends BatteryObject>> mjBatteryKinds = new HashMap<String, Class<? extends BatteryObject>>();
|
||||||
|
private static final BatteryField invalidBatteryField = new BatteryField();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deactivate constructor
|
* Deactivate constructor
|
||||||
|
@ -48,19 +52,37 @@ public final class MjAPI {
|
||||||
* power framework if possible.
|
* power framework if possible.
|
||||||
*/
|
*/
|
||||||
public static IBatteryObject getMjBattery(Object o, String kind) {
|
public static IBatteryObject getMjBattery(Object o, String kind) {
|
||||||
|
return getMjBattery(o, kind, ForgeDirection.UNKNOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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, ForgeDirection side) {
|
||||||
if (o == null) {
|
if (o == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (o instanceof IBatteryProvider) {
|
if (o instanceof IBatteryProvider) {
|
||||||
IBatteryObject battery = ((IBatteryProvider) o).getMjBattery();
|
IBatteryObject battery;
|
||||||
|
|
||||||
|
if (o instanceof ISidedBatteryProvider) {
|
||||||
|
battery = ((ISidedBatteryProvider) o).getMjBattery(side);
|
||||||
|
if (battery == null && side != ForgeDirection.UNKNOWN) {
|
||||||
|
battery = ((ISidedBatteryProvider) o).getMjBattery(ForgeDirection.UNKNOWN);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
battery = ((IBatteryProvider) o).getMjBattery();
|
||||||
|
}
|
||||||
|
|
||||||
if (battery != null) {
|
if (battery != null) {
|
||||||
return battery;
|
return battery;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BatteryField f = getMjBatteryField(o.getClass());
|
BatteryField f = getMjBatteryField(o.getClass(), kind);
|
||||||
|
|
||||||
if (f == null) {
|
if (f == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -85,7 +107,7 @@ public final class MjAPI {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
return getMjBattery(f.field.get(o));
|
return getMjBattery(f.field.get(o), kind, side);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -94,16 +116,22 @@ public final class MjAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IBatteryObject[] getAllMjBatteries(Object o) {
|
public static IBatteryObject[] getAllMjBatteries(Object o) {
|
||||||
|
return getAllMjBatteries(o, ForgeDirection.UNKNOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IBatteryObject[] getAllMjBatteries(Object o, ForgeDirection direction) {
|
||||||
IBatteryObject[] result = new IBatteryObject[mjBatteries.size()];
|
IBatteryObject[] result = new IBatteryObject[mjBatteries.size()];
|
||||||
|
|
||||||
int id = 0;
|
int id = 0;
|
||||||
|
|
||||||
for (String kind : mjBatteryKinds.keySet()) {
|
for (String kind : mjBatteryKinds.keySet()) {
|
||||||
result[id] = getMjBattery(o, kind);
|
result[id] = getMjBattery(o, kind, direction);
|
||||||
id++;
|
if (result[id] != null) {
|
||||||
|
id++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return Arrays.copyOfRange(result, 0, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void registerMJBatteryKind(String kind, Class<? extends BatteryObject> clas) {
|
public static void registerMJBatteryKind(String kind, Class<? extends BatteryObject> clas) {
|
||||||
|
@ -119,20 +147,50 @@ public final class MjAPI {
|
||||||
Value, Container
|
Value, Container
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final class BatteryHolder {
|
||||||
|
private String kind;
|
||||||
|
private Class clazz;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
BatteryHolder that = (BatteryHolder) o;
|
||||||
|
|
||||||
|
return kind.equals(that.kind) && clazz.equals(that.clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = kind.hashCode();
|
||||||
|
result = 31 * result + clazz.hashCode();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class BatteryField {
|
private static class BatteryField {
|
||||||
public Field field;
|
public Field field;
|
||||||
public MjBattery battery;
|
public MjBattery battery;
|
||||||
public BatteryKind kind;
|
public BatteryKind kind;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BatteryField getMjBatteryField(Class c) {
|
private static BatteryField getMjBatteryField(Class c, String kind) {
|
||||||
BatteryField bField = mjBatteries.get(c);
|
BatteryHolder holder = new BatteryHolder();
|
||||||
|
holder.clazz = c;
|
||||||
|
holder.kind = kind;
|
||||||
|
|
||||||
|
BatteryField bField = mjBatteries.get(holder);
|
||||||
|
|
||||||
if (bField == null) {
|
if (bField == null) {
|
||||||
for (Field f : JavaTools.getAllFields(c)) {
|
for (Field f : JavaTools.getAllFields(c)) {
|
||||||
MjBattery battery = f.getAnnotation(MjBattery.class);
|
MjBattery battery = f.getAnnotation(MjBattery.class);
|
||||||
|
|
||||||
if (battery != null) {
|
if (battery != null && kind.equals(battery.kind())) {
|
||||||
f.setAccessible(true);
|
f.setAccessible(true);
|
||||||
bField = new BatteryField();
|
bField = new BatteryField();
|
||||||
bField.field = f;
|
bField.field = f;
|
||||||
|
@ -147,16 +205,15 @@ public final class MjAPI {
|
||||||
bField.kind = BatteryKind.Container;
|
bField.kind = BatteryKind.Container;
|
||||||
}
|
}
|
||||||
|
|
||||||
mjBatteries.put(c, bField);
|
mjBatteries.put(holder, bField);
|
||||||
|
|
||||||
return bField;
|
return bField;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mjBatteries.put(holder, invalidBatteryField);
|
||||||
mjBatteries.put(c, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bField;
|
return bField == invalidBatteryField ? null : bField;
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
|
|
@ -29,23 +29,18 @@ public class MjAPILegacy implements IPowerReceptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MjAPILegacy from(World world, IBatteryObject battery, PowerHandler.Type type) {
|
public static MjAPILegacy from(World world, IBatteryObject battery, PowerHandler.Type type) {
|
||||||
|
if (battery == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return new MjAPILegacy(world, battery, type);
|
return new MjAPILegacy(world, battery, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MjAPILegacy from(World world, Object object, PowerHandler.Type type) {
|
public static MjAPILegacy from(World world, Object object, PowerHandler.Type type) {
|
||||||
return new MjAPILegacy(world, battery(object), type);
|
return from(world, MjAPI.getMjBattery(object), type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MjAPILegacy from(TileEntity tileEntity, PowerHandler.Type type) {
|
public static MjAPILegacy from(TileEntity tileEntity, PowerHandler.Type type) {
|
||||||
return new MjAPILegacy(tileEntity.getWorldObj(), battery(tileEntity), type);
|
return from(tileEntity.getWorldObj(), MjAPI.getMjBattery(tileEntity), type);
|
||||||
}
|
|
||||||
|
|
||||||
private static IBatteryObject battery(Object object) {
|
|
||||||
IBatteryObject battery = MjAPI.getMjBattery(object);
|
|
||||||
if (battery == null) {
|
|
||||||
throw new IllegalArgumentException(String.format("Object %s not using MjAPI, can't create legacy wrapper", object));
|
|
||||||
}
|
|
||||||
return battery;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -122,7 +122,7 @@ public class TileEnergyConverter extends TileBuildCraft implements IPowerRecepto
|
||||||
if (tile instanceof TileEnergyConverter) {
|
if (tile instanceof TileEnergyConverter) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
IBatteryObject object = MjAPI.getMjBattery(tile);
|
IBatteryObject object = MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, side.getOpposite());
|
||||||
if (object != null && mjStored > 0) {
|
if (object != null && mjStored > 0) {
|
||||||
double wantToUse = Math.min(mjStored, object.getEnergyRequested());
|
double wantToUse = Math.min(mjStored, object.getEnergyRequested());
|
||||||
object.addEnergy(wantToUse);
|
object.addEnergy(wantToUse);
|
||||||
|
|
|
@ -285,7 +285,7 @@ public abstract class TileEngine extends TileBuildCraft implements IPowerRecepto
|
||||||
return extractEnergy(receptor.getMinEnergyReceived(),
|
return extractEnergy(receptor.getMinEnergyReceived(),
|
||||||
receptor.getMaxEnergyReceived(), false);
|
receptor.getMaxEnergyReceived(), false);
|
||||||
} else {
|
} else {
|
||||||
return extractEnergy(0, MjAPI.getMjBattery(tile)
|
return extractEnergy(0, MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, orientation.getOpposite())
|
||||||
.getEnergyRequested(), false);
|
.getEnergyRequested(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -307,7 +307,7 @@ public abstract class TileEngine extends TileBuildCraft implements IPowerRecepto
|
||||||
extractEnergy(receptor.getMinEnergyReceived(), needed, true);
|
extractEnergy(receptor.getMinEnergyReceived(), needed, true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
IBatteryObject battery = MjAPI.getMjBattery(tile);
|
IBatteryObject battery = MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, orientation.getOpposite());
|
||||||
|
|
||||||
battery.addEnergy(extractEnergy(0, battery.maxReceivedPerCycle(),
|
battery.addEnergy(extractEnergy(0, battery.maxReceivedPerCycle(),
|
||||||
true));
|
true));
|
||||||
|
@ -522,7 +522,7 @@ public abstract class TileEngine extends TileBuildCraft implements IPowerRecepto
|
||||||
} else if (tile instanceof IPowerReceptor) {
|
} else if (tile instanceof IPowerReceptor) {
|
||||||
return ((IPowerReceptor) tile).getPowerReceiver(side.getOpposite()) != null;
|
return ((IPowerReceptor) tile).getPowerReceiver(side.getOpposite()) != null;
|
||||||
} else {
|
} else {
|
||||||
return MjAPI.getMjBattery(tile) != null;
|
return MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, orientation.getOpposite()) != null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ public class PipeTransportPower extends PipeTransport {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MjAPI.getMjBattery(tile) != null) {
|
if (MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, side.getOpposite()) != null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,7 +177,7 @@ public class PipeTransportPower extends PipeTransport {
|
||||||
if (tiles[j] != null
|
if (tiles[j] != null
|
||||||
&& (tiles[j] instanceof TileGenericPipe
|
&& (tiles[j] instanceof TileGenericPipe
|
||||||
|| tiles[j] instanceof IPowerReceptor || MjAPI
|
|| tiles[j] instanceof IPowerReceptor || MjAPI
|
||||||
.getMjBattery(tiles[j]) != null)) {
|
.getMjBattery(tiles[j], MjAPI.DEFAULT_POWER_FRAMEWORK, ForgeDirection.VALID_DIRECTIONS[j].getOpposite()) != null)) {
|
||||||
totalPowerQuery += powerQuery[j];
|
totalPowerQuery += powerQuery[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -202,7 +202,7 @@ public class PipeTransportPower extends PipeTransport {
|
||||||
internalPower[i] -= watts;
|
internalPower[i] -= watts;
|
||||||
} else if (tiles[j] != null) {
|
} else if (tiles[j] != null) {
|
||||||
// Look for the simplified power framework
|
// Look for the simplified power framework
|
||||||
IBatteryObject battery = MjAPI.getMjBattery(tiles [j]);
|
IBatteryObject battery = MjAPI.getMjBattery(tiles[j], MjAPI.DEFAULT_POWER_FRAMEWORK, ForgeDirection.VALID_DIRECTIONS[j].getOpposite());
|
||||||
|
|
||||||
if (battery != null) {
|
if (battery != null) {
|
||||||
watts = (internalPower[i] / totalPowerQuery)
|
watts = (internalPower[i] / totalPowerQuery)
|
||||||
|
@ -250,7 +250,7 @@ public class PipeTransportPower extends PipeTransport {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tile != null) {
|
if (tile != null) {
|
||||||
IBatteryObject battery = MjAPI.getMjBattery(tile);
|
IBatteryObject battery = MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, dir.getOpposite());
|
||||||
|
|
||||||
if (battery != null) {
|
if (battery != null) {
|
||||||
requestEnergy(dir, battery.getEnergyRequested());
|
requestEnergy(dir, battery.getEnergyRequested());
|
||||||
|
|
Loading…
Reference in a new issue