Update MjAPI

This commit is contained in:
Prototik 2014-05-10 15:00:35 +08:00
parent 2738fbaef0
commit 04859cce7d
7 changed files with 53 additions and 10 deletions

View file

@ -12,6 +12,8 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.logging.Level;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.JavaTools;
@ -125,6 +127,7 @@ public class BatteryObject implements IBatteryObject {
*/
@Override
public BatteryObject reconfigure(final double maxCapacity, final double maxReceivedPerCycle, final double minimumConsumption) {
final ForgeDirection[] sides = batteryData != null ? batteryData.sides() : new ForgeDirection[] { ForgeDirection.UNKNOWN };
batteryData = new MjBattery() {
@Override
public double maxCapacity() {
@ -150,8 +153,18 @@ public class BatteryObject implements IBatteryObject {
public String kind() {
return MjAPI.DEFAULT_POWER_FRAMEWORK;
}
@Override
public ForgeDirection[] sides() {
return sides;
}
};
return this;
}
@Override
public String kind() {
return batteryData.kind();
}
}

View file

@ -75,4 +75,9 @@ public interface IBatteryObject {
* @return Current battery object instance
*/
IBatteryObject reconfigure(double maxCapacity, double maxReceivedPerCycle, double minimumConsumption);
/**
* @return kind of this energy battery
*/
String kind();
}

View file

@ -9,5 +9,5 @@
package buildcraft.api.mj;
public interface IBatteryProvider {
IBatteryObject getMjBattery();
IBatteryObject getMjBattery(String kind);
}

View file

@ -11,5 +11,5 @@ package buildcraft.api.mj;
import net.minecraftforge.common.util.ForgeDirection;
public interface ISidedBatteryProvider extends IBatteryProvider {
IBatteryObject getMjBattery(ForgeDirection direction);
IBatteryObject getMjBattery(String kind, ForgeDirection direction);
}

View file

@ -69,12 +69,12 @@ public final class MjAPI {
IBatteryObject battery;
if (o instanceof ISidedBatteryProvider) {
battery = ((ISidedBatteryProvider) o).getMjBattery(side);
battery = ((ISidedBatteryProvider) o).getMjBattery(kind, side);
if (battery == null && side != ForgeDirection.UNKNOWN) {
battery = ((ISidedBatteryProvider) o).getMjBattery(ForgeDirection.UNKNOWN);
battery = ((ISidedBatteryProvider) o).getMjBattery(kind, ForgeDirection.UNKNOWN);
}
} else {
battery = ((IBatteryProvider) o).getMjBattery();
battery = ((IBatteryProvider) o).getMjBattery(kind);
}
if (battery != null) {
@ -82,7 +82,10 @@ public final class MjAPI {
}
}
BatteryField f = getMjBatteryField(o.getClass(), kind);
BatteryField f = getMjBatteryField(o.getClass(), kind, side);
if (f == null && side != ForgeDirection.UNKNOWN) {
f = getMjBatteryField(o.getClass(), kind, ForgeDirection.UNKNOWN);
}
if (f == null) {
return null;
@ -149,6 +152,7 @@ public final class MjAPI {
private static final class BatteryHolder {
private String kind;
private ForgeDirection side;
private Class clazz;
@Override
@ -162,13 +166,14 @@ public final class MjAPI {
BatteryHolder that = (BatteryHolder) o;
return kind.equals(that.kind) && clazz.equals(that.clazz);
return kind.equals(that.kind) && clazz.equals(that.clazz) && side.equals(that.side);
}
@Override
public int hashCode() {
int result = kind.hashCode();
result = 31 * result + clazz.hashCode();
result = 31 * result + side.hashCode();
return result;
}
}
@ -179,10 +184,11 @@ public final class MjAPI {
public BatteryKind kind;
}
private static BatteryField getMjBatteryField(Class c, String kind) {
private static BatteryField getMjBatteryField(Class c, String kind, ForgeDirection side) {
BatteryHolder holder = new BatteryHolder();
holder.clazz = c;
holder.kind = kind;
holder.side = side;
BatteryField bField = mjBatteries.get(holder);
@ -191,6 +197,9 @@ public final class MjAPI {
MjBattery battery = f.getAnnotation(MjBattery.class);
if (battery != null && kind.equals(battery.kind())) {
if (!contains(battery.sides(), side) && !contains(battery.sides(), ForgeDirection.UNKNOWN)) {
continue;
}
f.setAccessible(true);
bField = new BatteryField();
bField.field = f;
@ -216,6 +225,15 @@ public final class MjAPI {
return bField == invalidBatteryField ? null : bField;
}
private static <T> boolean contains(T[] array, T value) {
for (T t : array) {
if (t == value) {
return true;
}
}
return false;
}
static {
mjBatteryKinds.put(MjAPI.DEFAULT_POWER_FRAMEWORK, BatteryObject.class);
}

View file

@ -14,6 +14,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import net.minecraftforge.common.util.ForgeDirection;
/**
* This annotation is used for tiles that need to interface with BuildCraft
* energy framework, a.k.a MinecraftJoule or MJ. In order to receive power,
@ -55,4 +57,9 @@ public @interface MjBattery {
* coexist in the same tile.
*/
String kind() default MjAPI.DEFAULT_POWER_FRAMEWORK;
/**
* @return Sides on which this battery should works. Can be overrided by {@link ISidedBatteryProvider}
*/
ForgeDirection[] sides() default { ForgeDirection.UNKNOWN };
}

View file

@ -185,8 +185,8 @@ public final class PowerHandler implements IBatteryProvider {
}
@Override
public IBatteryObject getMjBattery() {
return battery;
public IBatteryObject getMjBattery(String kind) {
return battery.kind().equals(kind) ? battery : null;
}
/**