Merge branch 'Prototik-sided-mjbattery' into 6.0.x

This commit is contained in:
SpaceToad 2014-05-10 08:23:05 +02:00
commit 2738fbaef0
8 changed files with 101 additions and 35 deletions

View file

@ -124,8 +124,7 @@ public class BatteryObject implements IBatteryObject {
* {@inheritDoc}
*/
@Override
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle,
final double minimumConsumption) {
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption) {
batteryData = new MjBattery() {
@Override
public double maxCapacity() {

View file

@ -74,5 +74,5 @@ public interface IBatteryObject {
* @param minimumConsumption {@link #minimumConsumption()}
* @return Current battery object instance
*/
IBatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption);
IBatteryObject reconfigure(double maxCapacity, double maxReceivedPerCycle, double minimumConsumption);
}

View 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);
}

View file

@ -9,10 +9,13 @@
package buildcraft.api.mj;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.JavaTools;
@ -24,8 +27,9 @@ import buildcraft.api.core.JavaTools;
*/
public final class MjAPI {
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 final BatteryField invalidBatteryField = new BatteryField();
/**
* Deactivate constructor
@ -48,19 +52,37 @@ public final class MjAPI {
* power framework if possible.
*/
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) {
return null;
}
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) {
return battery;
}
}
BatteryField f = getMjBatteryField(o.getClass());
BatteryField f = getMjBatteryField(o.getClass(), kind);
if (f == null) {
return null;
@ -85,7 +107,7 @@ public final class MjAPI {
}
} else {
try {
return getMjBattery(f.field.get(o));
return getMjBattery(f.field.get(o), kind, side);
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
@ -94,16 +116,22 @@ public final class MjAPI {
}
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()];
int id = 0;
for (String kind : mjBatteryKinds.keySet()) {
result[id] = getMjBattery(o, kind);
id++;
result[id] = getMjBattery(o, kind, direction);
if (result[id] != null) {
id++;
}
}
return result;
return Arrays.copyOfRange(result, 0, id);
}
public static void registerMJBatteryKind(String kind, Class<? extends BatteryObject> clas) {
@ -119,20 +147,50 @@ public final class MjAPI {
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 {
public Field field;
public MjBattery battery;
public BatteryKind kind;
}
private static BatteryField getMjBatteryField(Class c) {
BatteryField bField = mjBatteries.get(c);
private static BatteryField getMjBatteryField(Class c, String kind) {
BatteryHolder holder = new BatteryHolder();
holder.clazz = c;
holder.kind = kind;
BatteryField bField = mjBatteries.get(holder);
if (bField == null) {
for (Field f : JavaTools.getAllFields(c)) {
MjBattery battery = f.getAnnotation(MjBattery.class);
if (battery != null) {
if (battery != null && kind.equals(battery.kind())) {
f.setAccessible(true);
bField = new BatteryField();
bField.field = f;
@ -147,16 +205,15 @@ public final class MjAPI {
bField.kind = BatteryKind.Container;
}
mjBatteries.put(c, bField);
mjBatteries.put(holder, bField);
return bField;
}
}
mjBatteries.put(c, null);
mjBatteries.put(holder, invalidBatteryField);
}
return bField;
return bField == invalidBatteryField ? null : bField;
}
static {

View file

@ -29,23 +29,18 @@ public class MjAPILegacy implements IPowerReceptor {
}
public static MjAPILegacy from(World world, IBatteryObject battery, PowerHandler.Type type) {
if (battery == null) {
return null;
}
return new MjAPILegacy(world, battery, 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) {
return new MjAPILegacy(tileEntity.getWorldObj(), battery(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;
return from(tileEntity.getWorldObj(), MjAPI.getMjBattery(tileEntity), type);
}
@Override

View file

@ -122,7 +122,7 @@ public class TileEnergyConverter extends TileBuildCraft implements IPowerRecepto
if (tile instanceof TileEnergyConverter) {
continue;
}
IBatteryObject object = MjAPI.getMjBattery(tile);
IBatteryObject object = MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, side.getOpposite());
if (object != null && mjStored > 0) {
double wantToUse = Math.min(mjStored, object.getEnergyRequested());
object.addEnergy(wantToUse);

View file

@ -285,7 +285,7 @@ public abstract class TileEngine extends TileBuildCraft implements IPowerRecepto
return extractEnergy(receptor.getMinEnergyReceived(),
receptor.getMaxEnergyReceived(), false);
} else {
return extractEnergy(0, MjAPI.getMjBattery(tile)
return extractEnergy(0, MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, orientation.getOpposite())
.getEnergyRequested(), false);
}
}
@ -307,7 +307,7 @@ public abstract class TileEngine extends TileBuildCraft implements IPowerRecepto
extractEnergy(receptor.getMinEnergyReceived(), needed, true);
}
} else {
IBatteryObject battery = MjAPI.getMjBattery(tile);
IBatteryObject battery = MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, orientation.getOpposite());
battery.addEnergy(extractEnergy(0, battery.maxReceivedPerCycle(),
true));
@ -522,7 +522,7 @@ public abstract class TileEngine extends TileBuildCraft implements IPowerRecepto
} else if (tile instanceof IPowerReceptor) {
return ((IPowerReceptor) tile).getPowerReceiver(side.getOpposite()) != null;
} else {
return MjAPI.getMjBattery(tile) != null;
return MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, orientation.getOpposite()) != null;
}
}

View file

@ -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;
}
@ -177,7 +177,7 @@ public class PipeTransportPower extends PipeTransport {
if (tiles[j] != null
&& (tiles[j] instanceof TileGenericPipe
|| 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];
}
}
@ -202,7 +202,7 @@ public class PipeTransportPower extends PipeTransport {
internalPower[i] -= watts;
} else if (tiles[j] != null) {
// 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) {
watts = (internalPower[i] / totalPowerQuery)
@ -250,7 +250,7 @@ public class PipeTransportPower extends PipeTransport {
}
if (tile != null) {
IBatteryObject battery = MjAPI.getMjBattery(tile);
IBatteryObject battery = MjAPI.getMjBattery(tile, MjAPI.DEFAULT_POWER_FRAMEWORK, dir.getOpposite());
if (battery != null) {
requestEnergy(dir, battery.getEnergyRequested());