feat: first energy compat improvement
This commit is contained in:
parent
7913f44840
commit
95d0145266
|
@ -52,11 +52,11 @@ public abstract class CompatibilityModule
|
||||||
*
|
*
|
||||||
* @return The actual energy that was extract.
|
* @return The actual energy that was extract.
|
||||||
*/
|
*/
|
||||||
public static double extractEnergy(Object handler, ForgeDirection direction, double energy, boolean doReceive)
|
public static double extractEnergy(Object handler, ForgeDirection direction, double energy, boolean doExtract)
|
||||||
{
|
{
|
||||||
if (isHandler(handler))
|
if (isHandler(handler))
|
||||||
{
|
{
|
||||||
return energyHandlerCache.get(handler.getClass()).doExtractEnergy(handler, direction, energy, doReceive);
|
return energyHandlerCache.get(handler.getClass()).doExtractEnergy(handler, direction, energy, doExtract);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -192,11 +192,21 @@ public abstract class CompatibilityModule
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double getVoltage(Object handler)
|
public static double getInputVoltage(Object handler)
|
||||||
{
|
{
|
||||||
if (isHandler(handler))
|
if (isHandler(handler))
|
||||||
{
|
{
|
||||||
return energyHandlerCache.get(handler.getClass()).doGetVoltage(handler);
|
return energyHandlerCache.get(handler.getClass()).doGetInputVoltage(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getOutputVoltage(Object handler)
|
||||||
|
{
|
||||||
|
if (isHandler(handler))
|
||||||
|
{
|
||||||
|
return energyHandlerCache.get(handler.getClass()).doGetOutputVoltage(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -302,7 +312,9 @@ public abstract class CompatibilityModule
|
||||||
|
|
||||||
public abstract double doGetMaxEnergyItem(ItemStack is);
|
public abstract double doGetMaxEnergyItem(ItemStack is);
|
||||||
|
|
||||||
public abstract double doGetVoltage(Object handler);
|
public abstract double doGetInputVoltage(Object handler);
|
||||||
|
|
||||||
|
public abstract double doGetOutputVoltage(Object handler);
|
||||||
|
|
||||||
public abstract boolean doCanReceive(Object handler, ForgeDirection side);
|
public abstract boolean doCanReceive(Object handler, ForgeDirection side);
|
||||||
|
|
||||||
|
|
|
@ -70,4 +70,13 @@ public enum CompatibilityType
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double fromJoules(double joules) {
|
||||||
|
return this.ratio * joules;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double toJoules(double energy) {
|
||||||
|
return this.reciprocal_ratio * energy;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,15 +14,15 @@ public interface IEnergyContainer
|
||||||
*
|
*
|
||||||
* This function is NOT recommended for calling.
|
* This function is NOT recommended for calling.
|
||||||
*/
|
*/
|
||||||
public void setEnergy(ForgeDirection from, long energy);
|
public void setEnergy(ForgeDirection from, double energy);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* * @return Get the amount of energy currently stored in the block.
|
* * @return Get the amount of energy currently stored in the block.
|
||||||
*/
|
*/
|
||||||
public long getEnergy(ForgeDirection from);
|
public double getEnergy(ForgeDirection from);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Get the max amount of energy that can be stored in the block.
|
* @return Get the max amount of energy that can be stored in the block.
|
||||||
*/
|
*/
|
||||||
public long getEnergyCapacity(ForgeDirection from);
|
public double getEnergyCapacity(ForgeDirection from);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,53 +1,21 @@
|
||||||
package universalelectricity.compat;
|
package universalelectricity.compat;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import cpw.mods.fml.common.FMLCommonHandler;
|
import cpw.mods.fml.common.FMLCommonHandler;
|
||||||
import cpw.mods.fml.common.Loader;
|
import cpw.mods.fml.common.Loader;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
import net.minecraftforge.common.MinecraftForge;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import universalelectricity.api.CompatibilityModule;
|
||||||
import universalelectricity.compat.ic2.EventHandler;
|
|
||||||
import universalelectricity.compat.ic2.IC2CompatModule;
|
import universalelectricity.compat.ic2.IC2CompatModule;
|
||||||
|
import universalelectricity.compat.ic2.EventHandler;
|
||||||
import universalelectricity.compat.ic2.TickHandler;
|
import universalelectricity.compat.ic2.TickHandler;
|
||||||
import universalelectricity.core.block.IConnector;
|
|
||||||
|
|
||||||
public class CompatHandler {
|
public class CompatHandler {
|
||||||
|
|
||||||
private static List<CompatiblityModule> modules;
|
|
||||||
|
|
||||||
public static void initCompatHandlers() {
|
public static void initCompatHandlers() {
|
||||||
modules = new ArrayList<>();
|
|
||||||
if (Loader.isModLoaded("IC2")) {
|
if (Loader.isModLoaded("IC2")) {
|
||||||
modules.add(new IC2CompatModule());
|
CompatibilityModule.register(new IC2CompatModule());
|
||||||
MinecraftForge.EVENT_BUS.register(new EventHandler());
|
MinecraftForge.EVENT_BUS.register(new EventHandler());
|
||||||
FMLCommonHandler.instance().bus().register(new TickHandler());
|
FMLCommonHandler.instance().bus().register(new TickHandler());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void registerModule(CompatiblityModule module) {
|
|
||||||
if (modules != null)
|
|
||||||
modules.add(module);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean canConnect(TileEntity tileEntity, ForgeDirection side) {
|
|
||||||
if (tileEntity == null) return false;
|
|
||||||
if (tileEntity instanceof IConnector) return ((IConnector)tileEntity).canConnect(side);
|
|
||||||
for (CompatiblityModule module : modules) {
|
|
||||||
if (module.canConnect(tileEntity, side)) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IElectricityTileHandler getHandler(TileEntity tileEntity) {
|
|
||||||
if (tileEntity instanceof IConnector || tileEntity == null) return null;
|
|
||||||
for (CompatiblityModule module : modules) {
|
|
||||||
if (module.isHandledBy(tileEntity)) {
|
|
||||||
return module.getHandler(tileEntity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package universalelectricity.compat;
|
|
||||||
|
|
||||||
import net.minecraft.tileentity.TileEntity;
|
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
|
||||||
|
|
||||||
public abstract class CompatiblityModule {
|
|
||||||
|
|
||||||
public abstract boolean isHandledBy(TileEntity tile);
|
|
||||||
|
|
||||||
public abstract boolean canConnect(TileEntity tile, ForgeDirection side);
|
|
||||||
|
|
||||||
public abstract IElectricityTileHandler getHandler(TileEntity tileEntity);
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
package universalelectricity.compat;
|
|
||||||
|
|
||||||
import net.minecraft.tileentity.TileEntity;
|
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
|
||||||
import universalelectricity.core.electricity.ElectricityPack;
|
|
||||||
|
|
||||||
public interface IElectricityTileHandler {
|
|
||||||
|
|
||||||
boolean canInsert();
|
|
||||||
|
|
||||||
boolean canExtract();
|
|
||||||
|
|
||||||
boolean canInsertOn(ForgeDirection side);
|
|
||||||
|
|
||||||
boolean canExtractOn(ForgeDirection side);
|
|
||||||
|
|
||||||
void insert(ElectricityPack pack, ForgeDirection side);
|
|
||||||
|
|
||||||
void extract(ElectricityPack pack, ForgeDirection side);
|
|
||||||
|
|
||||||
ElectricityPack getDemandedJoules();
|
|
||||||
|
|
||||||
ElectricityPack getProvidedJoules();
|
|
||||||
|
|
||||||
TileEntity getTile();
|
|
||||||
|
|
||||||
double getVoltage();
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,85 +0,0 @@
|
||||||
package universalelectricity.compat.ic2;
|
|
||||||
|
|
||||||
import ic2.api.energy.tile.IEnergySink;
|
|
||||||
import ic2.api.energy.tile.IEnergySource;
|
|
||||||
import net.minecraft.tileentity.TileEntity;
|
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
|
||||||
import universalelectricity.compat.IElectricityTileHandler;
|
|
||||||
import universalelectricity.core.electricity.ElectricityPack;
|
|
||||||
|
|
||||||
public class ElectricityTileWrapper implements IElectricityTileHandler {
|
|
||||||
|
|
||||||
TileEntity baseTile;
|
|
||||||
|
|
||||||
public ElectricityTileWrapper(TileEntity baseTile) {
|
|
||||||
this.baseTile = baseTile;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canInsert() {
|
|
||||||
return baseTile instanceof IEnergySink;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canExtract() {
|
|
||||||
return baseTile instanceof IEnergySource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canInsertOn(ForgeDirection side) {
|
|
||||||
return baseTile instanceof IEnergySink && ((IEnergySink) baseTile).acceptsEnergyFrom(null, side);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canExtractOn(ForgeDirection side) {
|
|
||||||
return baseTile instanceof IEnergySource && ((IEnergySource) baseTile).emitsEnergyTo(null, side);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void insert(ElectricityPack pack, ForgeDirection side) {
|
|
||||||
if (baseTile instanceof IEnergySink) {
|
|
||||||
IEnergySink sink = (IEnergySink) baseTile;
|
|
||||||
sink.injectEnergy(side, IC2CompatHelper.joulesToEU(pack.getWatts()), IC2CompatHelper.voltToTier(pack.voltage));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void extract(ElectricityPack pack, ForgeDirection side) {
|
|
||||||
if (baseTile instanceof IEnergySource) {
|
|
||||||
IEnergySource source = (IEnergySource) baseTile;
|
|
||||||
source.drawEnergy(IC2CompatHelper.joulesToEU(pack.getWatts()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ElectricityPack getDemandedJoules() {
|
|
||||||
if (baseTile instanceof IEnergySink) {
|
|
||||||
IEnergySink sink = (IEnergySink) baseTile;
|
|
||||||
double voltage = IC2CompatHelper.tierToVolt(sink.getSinkTier());
|
|
||||||
return new ElectricityPack(IC2CompatHelper.EUToJoules(sink.getDemandedEnergy()) / voltage, voltage);
|
|
||||||
}
|
|
||||||
return new ElectricityPack();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ElectricityPack getProvidedJoules() {
|
|
||||||
if (baseTile instanceof IEnergySource) {
|
|
||||||
IEnergySource source = (IEnergySource) baseTile;
|
|
||||||
double voltage = IC2CompatHelper.tierToVolt(source.getSourceTier());
|
|
||||||
return new ElectricityPack(IC2CompatHelper.EUToJoules(source.getOfferedEnergy()) / voltage, voltage);
|
|
||||||
}
|
|
||||||
return new ElectricityPack();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TileEntity getTile() {
|
|
||||||
return baseTile;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getVoltage() {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,25 +1,24 @@
|
||||||
package universalelectricity.compat.ic2;
|
package universalelectricity.compat.ic2;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
|
||||||
import universalelectricity.core.Pair;
|
import universalelectricity.core.Pair;
|
||||||
import universalelectricity.core.vector.Vector3;
|
import universalelectricity.core.vector.Vector3;
|
||||||
import universalelectricity.prefab.tile.ElectricTileDriver;
|
import universalelectricity.prefab.tile.ElectricTileDriver;
|
||||||
|
|
||||||
public class EnergyNetCache {
|
public class EnergyNetCache {
|
||||||
|
|
||||||
private static Map<Pair<World, Vector3>, ElectricTileDriver> tiles = new HashMap<>();
|
private static Map<Pair<World, Vector3>, ElectricTileDriver> tiles = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public static Pair<World, Vector3> toKey(TileEntity tile) {
|
public static Pair<World, Vector3> toKey(TileEntity tile) {
|
||||||
return new Pair<World, Vector3>(tile.getWorldObj(), new Vector3(tile));
|
return new Pair<World, Vector3>(tile.getWorldObj(), new Vector3(tile));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void load(TileEntity tile) {
|
public static void load(TileEntity tile) {
|
||||||
tiles.put(toKey(tile), new ElectricTileDriver(new ElectricityTileWrapper(tile)));
|
tiles.put(toKey(tile), new ElectricTileDriver(tile));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void unload(TileEntity tile) {
|
public static void unload(TileEntity tile) {
|
||||||
|
@ -28,11 +27,6 @@ public class EnergyNetCache {
|
||||||
tiles.remove(toKey(tile));
|
tiles.remove(toKey(tile));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean canConnect(Pair<World, Vector3> pos, ForgeDirection side) {
|
|
||||||
ElectricTileDriver handler = tiles.get(pos);
|
|
||||||
return handler == null ? false : handler.canConnect(side);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void tickAll() {
|
public static void tickAll() {
|
||||||
for (ElectricTileDriver handler : tiles.values()) {
|
for (ElectricTileDriver handler : tiles.values()) {
|
||||||
handler.tick();
|
handler.tick();
|
||||||
|
|
|
@ -1,17 +1,7 @@
|
||||||
package universalelectricity.compat.ic2;
|
package universalelectricity.compat.ic2;
|
||||||
|
|
||||||
import universalelectricity.core.UniversalElectricity;
|
|
||||||
|
|
||||||
public class IC2CompatHelper {
|
public class IC2CompatHelper {
|
||||||
|
|
||||||
public static double EUToJoules(double eu) {
|
|
||||||
return eu * UniversalElectricity.UE_IC2_RATIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static double joulesToEU(double joules) {
|
|
||||||
return joules / UniversalElectricity.UE_IC2_RATIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int voltToTier(double volt) {
|
public static int voltToTier(double volt) {
|
||||||
if (volt <= 120.0) return 1;
|
if (volt <= 120.0) return 1;
|
||||||
if (volt <= 240.0) return 2;
|
if (volt <= 240.0) return 2;
|
||||||
|
|
|
@ -1,25 +1,226 @@
|
||||||
package universalelectricity.compat.ic2;
|
package universalelectricity.compat.ic2;
|
||||||
|
|
||||||
|
import ic2.api.energy.EnergyNet;
|
||||||
|
import ic2.api.energy.tile.IEnergySink;
|
||||||
|
import ic2.api.energy.tile.IEnergySource;
|
||||||
|
import ic2.api.energy.tile.IEnergyTile;
|
||||||
|
import ic2.api.item.ElectricItem;
|
||||||
|
import ic2.api.item.IElectricItem;
|
||||||
|
import ic2.api.tile.IEnergyStorage;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
import universalelectricity.compat.CompatiblityModule;
|
import universalelectricity.api.CompatibilityModule;
|
||||||
import universalelectricity.compat.IElectricityTileHandler;
|
import universalelectricity.api.CompatibilityType;
|
||||||
|
|
||||||
public class IC2CompatModule extends CompatiblityModule {
|
public class IC2CompatModule extends CompatibilityModule {
|
||||||
|
|
||||||
@Override
|
private TileEntity getENetTile(Object handler) {
|
||||||
public boolean isHandledBy(TileEntity tile) {
|
if (handler instanceof TileEntity) {
|
||||||
return EnergyNetCache.contains(tile);
|
TileEntity te = (TileEntity) handler;
|
||||||
|
if (te instanceof IEnergyTile) {
|
||||||
|
return te;
|
||||||
}
|
}
|
||||||
|
return EnergyNet.instance.getTileEntity(te.getWorldObj(), te.xCoord, te.yCoord, te.zCoord);
|
||||||
@Override
|
|
||||||
public boolean canConnect(TileEntity tile, ForgeDirection side) {
|
|
||||||
return EnergyNetCache.canConnect(EnergyNetCache.toKey(tile), side);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public IElectricityTileHandler getHandler(TileEntity tileEntity) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doReceiveEnergy(Object handler, ForgeDirection direction, double energy, boolean doReceive) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
|
||||||
|
if (tile instanceof IEnergySink) {
|
||||||
|
IEnergySink sink = (IEnergySink) tile;
|
||||||
|
double demand = sink.getDemandedEnergy();
|
||||||
|
double toReceive = Math.min(demand, CompatibilityType.INDUSTRIALCRAFT.fromJoules(energy));
|
||||||
|
|
||||||
|
if (doReceive) {
|
||||||
|
double leftover = sink.injectEnergy(direction, toReceive, 1);
|
||||||
|
return CompatibilityType.INDUSTRIALCRAFT.toJoules(Math.max(0, toReceive - leftover));
|
||||||
|
}
|
||||||
|
|
||||||
|
return CompatibilityType.INDUSTRIALCRAFT.toJoules(toReceive);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doExtractEnergy(Object handler, ForgeDirection direction, double energy, boolean doExtract) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
if (tile instanceof IEnergySource)
|
||||||
|
{
|
||||||
|
double demand = Math.min(((IEnergySource) tile).getOfferedEnergy(), energy * CompatibilityType.INDUSTRIALCRAFT.ratio);
|
||||||
|
|
||||||
|
if (doExtract)
|
||||||
|
{
|
||||||
|
((IEnergySource) tile).drawEnergy(demand);
|
||||||
|
}
|
||||||
|
|
||||||
|
return demand * CompatibilityType.INDUSTRIALCRAFT.reciprocal_ratio;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doChargeItem(ItemStack itemStack, double joules, boolean docharge) {
|
||||||
|
if (itemStack.getItem() instanceof IElectricItem)
|
||||||
|
{
|
||||||
|
return (ElectricItem.manager.charge(itemStack, joules * CompatibilityType.INDUSTRIALCRAFT.ratio, 4, true, !docharge) * CompatibilityType.INDUSTRIALCRAFT.reciprocal_ratio);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doDischargeItem(ItemStack itemStack, double joules, boolean doDischarge) {
|
||||||
|
if (itemStack.getItem() instanceof IElectricItem)
|
||||||
|
{
|
||||||
|
IElectricItem item = (IElectricItem) itemStack.getItem();
|
||||||
|
|
||||||
|
if (item.canProvideEnergy(itemStack))
|
||||||
|
{
|
||||||
|
return (long) (ElectricItem.manager.discharge(itemStack, joules * CompatibilityType.INDUSTRIALCRAFT.ratio, 4, true, false, !doDischarge) * CompatibilityType.INDUSTRIALCRAFT.reciprocal_ratio);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doIsHandler(Object obj) {
|
||||||
|
return getENetTile(obj) != null || obj instanceof IElectricItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doIsEnergyContainer(Object obj) {
|
||||||
|
TileEntity tile = getENetTile(obj);
|
||||||
|
return tile instanceof IEnergyStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doGetEnergy(Object obj, ForgeDirection direction) {
|
||||||
|
TileEntity tile = getENetTile(obj);
|
||||||
|
|
||||||
|
if (tile instanceof IEnergyStorage) {
|
||||||
|
return CompatibilityType.INDUSTRIALCRAFT.toJoules(((IEnergyStorage)tile).getStored());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doCanConnect(Object obj, ForgeDirection direction, Object source) {
|
||||||
|
TileEntity tile = getENetTile(obj);
|
||||||
|
|
||||||
|
if (tile instanceof IEnergySink)
|
||||||
|
{
|
||||||
|
if (((IEnergySink) tile).acceptsEnergyFrom((TileEntity) source, direction))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tile instanceof IEnergySource)
|
||||||
|
{
|
||||||
|
if (((IEnergySource) tile).emitsEnergyTo((TileEntity) source, direction))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack doGetItemWithCharge(ItemStack itemStack, double energy) {
|
||||||
|
ItemStack is = itemStack.copy();
|
||||||
|
|
||||||
|
ElectricItem.manager.discharge(is, Integer.MAX_VALUE, 1, true, false, false);
|
||||||
|
ElectricItem.manager.charge(is, (int) (energy * CompatibilityType.INDUSTRIALCRAFT.ratio), 1, true, false);
|
||||||
|
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doGetMaxEnergy(Object handler, ForgeDirection direction) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
|
||||||
|
if (tile instanceof IEnergyStorage) {
|
||||||
|
return CompatibilityType.INDUSTRIALCRAFT.toJoules(((IEnergyStorage)tile).getCapacity());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doGetEnergyItem(ItemStack is) {
|
||||||
|
return ElectricItem.manager.getCharge(is) * CompatibilityType.INDUSTRIALCRAFT.reciprocal_ratio;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doGetMaxEnergyItem(ItemStack is) {
|
||||||
|
return ((IElectricItem) is.getItem()).getMaxCharge(is) * CompatibilityType.INDUSTRIALCRAFT.reciprocal_ratio;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doGetInputVoltage(Object handler) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
|
||||||
|
if (tile instanceof IEnergySink) {
|
||||||
|
return IC2CompatHelper.tierToVolt(((IEnergySink)tile).getSinkTier());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doGetOutputVoltage(Object handler) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
|
||||||
|
if (tile instanceof IEnergySource) {
|
||||||
|
return IC2CompatHelper.tierToVolt(((IEnergySource)tile).getSourceTier());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doCanReceive(Object handler, ForgeDirection side) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
if (!(tile instanceof IEnergySink)) return false;
|
||||||
|
|
||||||
|
if (side != ForgeDirection.UNKNOWN) {
|
||||||
|
return ((IEnergySink)tile).acceptsEnergyFrom(null, side);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doCanExtract(Object handler, ForgeDirection side) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
if (!(tile instanceof IEnergySource)) return false;
|
||||||
|
|
||||||
|
if (side != ForgeDirection.UNKNOWN) {
|
||||||
|
return ((IEnergySource)tile).emitsEnergyTo(null, side);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doGetDemandedJoules(Object handler) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
if (tile instanceof IEnergySink) {
|
||||||
|
IEnergySink sink = (IEnergySink) tile;
|
||||||
|
return CompatibilityType.INDUSTRIALCRAFT.toJoules(sink.getDemandedEnergy());
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double doGetProvidedJoules(Object handler) {
|
||||||
|
TileEntity tile = getENetTile(handler);
|
||||||
|
if (tile instanceof IEnergySource) {
|
||||||
|
IEnergySource source = (IEnergySource) tile;
|
||||||
|
return CompatibilityType.INDUSTRIALCRAFT.toJoules(source.getOfferedEnergy());
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package universalelectricity.core.block;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
import universalelectricity.api.net.IConnectable;
|
import universalelectricity.api.net.IConnectable;
|
||||||
|
|
||||||
public interface IConnector extends IConnectable {
|
public interface IConnector extends IConnectable, ISelfDriven {
|
||||||
|
|
||||||
boolean canConnect(ForgeDirection from);
|
boolean canConnect(ForgeDirection from);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package universalelectricity.core.block;
|
package universalelectricity.core.block;
|
||||||
|
|
||||||
import mekanism.api.energy.IStrictEnergyStorage;
|
import mekanism.api.energy.IStrictEnergyStorage;
|
||||||
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
import universalelectricity.api.energy.IEnergyContainer;
|
||||||
|
|
||||||
public interface IElectricityStorage extends IStrictEnergyStorage {
|
@Deprecated
|
||||||
|
public interface IElectricityStorage extends IEnergyContainer, IStrictEnergyStorage {
|
||||||
|
|
||||||
double getJoules();
|
double getJoules();
|
||||||
|
|
||||||
|
@ -10,6 +13,21 @@ public interface IElectricityStorage extends IStrictEnergyStorage {
|
||||||
|
|
||||||
double getMaxJoules();
|
double getMaxJoules();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default double getEnergy(ForgeDirection from) {
|
||||||
|
return getJoules();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default void setEnergy(ForgeDirection from, double energy) {
|
||||||
|
setJoules(energy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default double getEnergyCapacity(ForgeDirection from) {
|
||||||
|
return getMaxEnergy();
|
||||||
|
}
|
||||||
|
|
||||||
default double getEnergy() {
|
default double getEnergy() {
|
||||||
return getJoules();
|
return getJoules();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package universalelectricity.core.block;
|
||||||
|
|
||||||
|
public interface ISelfDriven {
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import java.util.List;
|
||||||
|
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
|
import universalelectricity.api.CompatibilityModule;
|
||||||
import universalelectricity.core.block.IConnector;
|
import universalelectricity.core.block.IConnector;
|
||||||
import universalelectricity.core.block.INetworkProvider;
|
import universalelectricity.core.block.INetworkProvider;
|
||||||
import universalelectricity.core.vector.Vector3;
|
import universalelectricity.core.vector.Vector3;
|
||||||
|
@ -136,4 +137,15 @@ public class ElectricityNetworkHelper {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean canConnect(TileEntity tileEntity, ForgeDirection side, TileEntity source) {
|
||||||
|
if (tileEntity == null) {
|
||||||
|
return false;
|
||||||
|
} else if (tileEntity instanceof IConnector) {
|
||||||
|
return ((IConnector)tileEntity).canConnect(side, source);
|
||||||
|
} else {
|
||||||
|
return CompatibilityModule.canConnect(tileEntity, side, source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package universalelectricity.prefab.tile;
|
package universalelectricity.prefab.tile;
|
||||||
|
|
||||||
|
import java.util.EnumSet;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -7,38 +8,31 @@ import java.util.Set;
|
||||||
|
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
import scala.languageFeature.reflectiveCalls;
|
import universalelectricity.api.CompatibilityModule;
|
||||||
import universalelectricity.compat.IElectricityTileHandler;
|
|
||||||
import universalelectricity.core.block.IConnector;
|
|
||||||
import universalelectricity.core.electricity.ElectricityNetworkHelper;
|
import universalelectricity.core.electricity.ElectricityNetworkHelper;
|
||||||
import universalelectricity.core.electricity.ElectricityPack;
|
import universalelectricity.core.electricity.ElectricityPack;
|
||||||
import universalelectricity.core.electricity.IElectricityNetwork;
|
import universalelectricity.core.electricity.IElectricityNetwork;
|
||||||
import universalelectricity.core.vector.Vector3;
|
import universalelectricity.core.vector.Vector3;
|
||||||
|
|
||||||
public class ElectricTileDriver implements IConnector {
|
public class ElectricTileDriver {
|
||||||
|
|
||||||
IElectricityTileHandler handler;
|
TileEntity handler;
|
||||||
|
|
||||||
public ElectricTileDriver(IElectricityTileHandler handler) {
|
public ElectricTileDriver(TileEntity handler) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canConnect(ForgeDirection side) {
|
|
||||||
return handler.canInsertOn(side) || handler.canExtractOn(side);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void invalidate() {
|
public void invalidate() {
|
||||||
ElectricityNetworkHelper.invalidate(handler.getTile());
|
ElectricityNetworkHelper.invalidate(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tick() {
|
public void tick() {
|
||||||
Map<ForgeDirection, IElectricityNetwork> networks = getNetworks();
|
Map<ForgeDirection, IElectricityNetwork> networks = getNetworks();
|
||||||
Set<ForgeDirection> inputSides = new HashSet<>();
|
Set<ForgeDirection> inputSides = new HashSet<>();
|
||||||
if (handler.canInsert()) {
|
if (CompatibilityModule.canReceive(handler, ForgeDirection.UNKNOWN)) {
|
||||||
inputSides = consume(networks);
|
inputSides = consume(networks);
|
||||||
}
|
}
|
||||||
if (handler.canExtract()) {
|
if (CompatibilityModule.canExtract(handler, ForgeDirection.UNKNOWN)) {
|
||||||
produce(networks, inputSides);
|
produce(networks, inputSides);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,18 +41,22 @@ public class ElectricTileDriver implements IConnector {
|
||||||
Set<ForgeDirection> inputSides = new HashSet<>();
|
Set<ForgeDirection> inputSides = new HashSet<>();
|
||||||
|
|
||||||
if (networks.size() > 0) {
|
if (networks.size() > 0) {
|
||||||
ElectricityPack demand = handler.getDemandedJoules();
|
double demand = CompatibilityModule.getDemandedJoules(handler);
|
||||||
double voltage = demand.voltage;
|
double voltage = CompatibilityModule.getInputVoltage(handler);
|
||||||
double wattsPerSide = demand.getWatts() / networks.size();
|
double wattsPerSide = demand / networks.size();
|
||||||
for (ForgeDirection side : networks.keySet()) {
|
for (ForgeDirection side : networks.keySet()) {
|
||||||
IElectricityNetwork net = networks.get(side);
|
IElectricityNetwork net = networks.get(side);
|
||||||
if (handler.canInsertOn(side) && wattsPerSide > 0 && demand.getWatts() > 0) {
|
if (CompatibilityModule.canReceive(handler, side) && wattsPerSide > 0 && demand > 0) {
|
||||||
inputSides.add(side);
|
inputSides.add(side);
|
||||||
net.startRequesting(handler.getTile(), wattsPerSide / voltage, voltage);
|
net.startRequesting(handler, wattsPerSide / voltage, voltage);
|
||||||
ElectricityPack receivedPack = net.consumeElectricity(handler.getTile());
|
ElectricityPack receivedPack = net.consumeElectricity(handler);
|
||||||
handler.insert(receivedPack, side);
|
if (receivedPack.voltage > voltage) {
|
||||||
|
handler.getWorldObj().createExplosion(null, handler.xCoord, handler.yCoord, handler.zCoord, 1, true);
|
||||||
|
return EnumSet.allOf(ForgeDirection.class);
|
||||||
|
}
|
||||||
|
CompatibilityModule.receiveEnergy(handler, side, receivedPack.getWatts(), true);
|
||||||
} else {
|
} else {
|
||||||
net.stopRequesting(handler.getTile());
|
net.stopRequesting(handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,17 +67,17 @@ public class ElectricTileDriver implements IConnector {
|
||||||
|
|
||||||
public void produce(Map<ForgeDirection, IElectricityNetwork> networks, Set<ForgeDirection> inputSides) {
|
public void produce(Map<ForgeDirection, IElectricityNetwork> networks, Set<ForgeDirection> inputSides) {
|
||||||
if ((networks.size() - inputSides.size()) > 0) {
|
if ((networks.size() - inputSides.size()) > 0) {
|
||||||
ElectricityPack provided = handler.getProvidedJoules();
|
double provided = CompatibilityModule.getProvidedJoules(handler);
|
||||||
double voltage = provided.voltage;
|
double voltage = CompatibilityModule.getOutputVoltage(handler);
|
||||||
double wattsPerSide = provided.getWatts() / (networks.size() - inputSides.size());
|
double wattsPerSide = provided / (networks.size() - inputSides.size());
|
||||||
for (ForgeDirection side : networks.keySet()) {
|
for (ForgeDirection side : networks.keySet()) {
|
||||||
IElectricityNetwork net = networks.get(side);
|
IElectricityNetwork net = networks.get(side);
|
||||||
if (!inputSides.contains(side) && handler.canExtractOn(side) && wattsPerSide > 0 && provided.getWatts() > 0) {
|
if (!inputSides.contains(side) && CompatibilityModule.canExtract(handler, side) && wattsPerSide > 0 && provided > 0) {
|
||||||
double amperes = Math.min(wattsPerSide / voltage, net.getRequest(new TileEntity[]{handler.getTile()}).amperes);
|
double amperes = Math.min(wattsPerSide / voltage, net.getRequest(new TileEntity[]{handler}).amperes);
|
||||||
net.startProducing(handler.getTile(), amperes, voltage);
|
net.startProducing(handler, amperes, voltage);
|
||||||
handler.extract(new ElectricityPack(amperes, voltage), side);
|
CompatibilityModule.extractEnergy(handler, side, new ElectricityPack(amperes, voltage).getWatts(), true);
|
||||||
} else {
|
} else {
|
||||||
net.stopProducing(handler.getTile());
|
net.stopProducing(handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,10 +87,10 @@ public class ElectricTileDriver implements IConnector {
|
||||||
Map<ForgeDirection, IElectricityNetwork> networks = new HashMap<>();
|
Map<ForgeDirection, IElectricityNetwork> networks = new HashMap<>();
|
||||||
|
|
||||||
for(ForgeDirection dir : ForgeDirection.values()) {
|
for(ForgeDirection dir : ForgeDirection.values()) {
|
||||||
if (canConnect(dir)) {
|
if (CompatibilityModule.canReceive(handler, dir) || CompatibilityModule.canExtract(handler, dir)) {
|
||||||
Vector3 position = new Vector3(handler.getTile());
|
Vector3 position = new Vector3(handler);
|
||||||
position.modifyPositionFromSide(dir);
|
position.modifyPositionFromSide(dir);
|
||||||
TileEntity outputConductor = position.getTileEntity(handler.getTile().getWorldObj());
|
TileEntity outputConductor = position.getTileEntity(handler.getWorldObj());
|
||||||
IElectricityNetwork electricityNetwork = ElectricityNetworkHelper.getNetworkFromTileEntity(outputConductor, dir);
|
IElectricityNetwork electricityNetwork = ElectricityNetworkHelper.getNetworkFromTileEntity(outputConductor, dir);
|
||||||
if(electricityNetwork != null && !networks.containsValue(electricityNetwork)) {
|
if(electricityNetwork != null && !networks.containsValue(electricityNetwork)) {
|
||||||
networks.put(dir, electricityNetwork);
|
networks.put(dir, electricityNetwork);
|
||||||
|
|
|
@ -11,7 +11,6 @@ import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.util.AxisAlignedBB;
|
import net.minecraft.util.AxisAlignedBB;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
import universalelectricity.compat.CompatHandler;
|
|
||||||
import universalelectricity.core.block.IConductor;
|
import universalelectricity.core.block.IConductor;
|
||||||
import universalelectricity.core.block.INetworkProvider;
|
import universalelectricity.core.block.INetworkProvider;
|
||||||
import universalelectricity.core.electricity.ElectricityNetwork;
|
import universalelectricity.core.electricity.ElectricityNetwork;
|
||||||
|
@ -30,7 +29,7 @@ public abstract class TileEntityConductor extends TileEntityAdvanced implements
|
||||||
|
|
||||||
public void updateConnection(TileEntity tileEntity, ForgeDirection side) {
|
public void updateConnection(TileEntity tileEntity, ForgeDirection side) {
|
||||||
if(!this.worldObj.isRemote) {
|
if(!this.worldObj.isRemote) {
|
||||||
if(CompatHandler.canConnect(tileEntity, side.getOpposite())) {
|
if(ElectricityNetworkHelper.canConnect(tileEntity, side.getOpposite(), this)) {
|
||||||
this.connectedBlocks[side.ordinal()] = tileEntity;
|
this.connectedBlocks[side.ordinal()] = tileEntity;
|
||||||
this.visuallyConnected[side.ordinal()] = true;
|
this.visuallyConnected[side.ordinal()] = true;
|
||||||
if(tileEntity.getClass() == this.getClass() && tileEntity instanceof INetworkProvider) {
|
if(tileEntity.getClass() == this.getClass() && tileEntity instanceof INetworkProvider) {
|
||||||
|
|
|
@ -12,6 +12,18 @@ public abstract class TileEntityElectricityRunnable extends TileEntityElectrical
|
||||||
public double prevWatts;
|
public double prevWatts;
|
||||||
public double wattsReceived = 0.0D;
|
public double wattsReceived = 0.0D;
|
||||||
|
|
||||||
|
/*@Override
|
||||||
|
public void initiate() {
|
||||||
|
super.initiate();
|
||||||
|
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void invalidate() {
|
||||||
|
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
|
||||||
|
super.invalidate();
|
||||||
|
}*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateEntity() {
|
public void updateEntity() {
|
||||||
super.updateEntity();
|
super.updateEntity();
|
||||||
|
@ -46,4 +58,33 @@ public abstract class TileEntityElectricityRunnable extends TileEntityElectrical
|
||||||
public double getWattBuffer() {
|
public double getWattBuffer() {
|
||||||
return this.getRequest().getWatts() * 2.0D;
|
return this.getRequest().getWatts() * 2.0D;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IC2 START
|
||||||
|
|
||||||
|
/* @Override
|
||||||
|
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) {
|
||||||
|
return getConsumingSides().contains(direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getDemandedEnergy() {
|
||||||
|
return Math.ceil(this.getRequest().getWatts() * UniversalElectricity.TO_IC2_RATIO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSinkTier() {
|
||||||
|
return 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double injectEnergy(ForgeDirection direction, double i, double voltage) {
|
||||||
|
double givenElectricity = (double)i * UniversalElectricity.IC2_RATIO;
|
||||||
|
double rejects = 0.0;
|
||||||
|
if (givenElectricity > this.getWattBuffer()) {
|
||||||
|
rejects = givenElectricity - this.getRequest().getWatts();
|
||||||
|
}
|
||||||
|
this.onReceive(new ElectricityPack(givenElectricity / this.getVoltage(), this.getVoltage()));
|
||||||
|
return (rejects * UniversalElectricity.TO_IC2_RATIO);
|
||||||
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue