Added proper tesla support for phantom energyfaces and laser relays

This commit is contained in:
Ellpeck 2016-07-24 02:54:07 +02:00
parent c61785e307
commit 90c1f2c25f
6 changed files with 115 additions and 62 deletions

View file

@ -48,7 +48,6 @@ public class BlockTinyTorch extends BlockBase{
super(Material.CIRCUITS, name); super(Material.CIRCUITS, name);
this.setDefaultState(this.blockState.getBaseState().withProperty(BlockTorch.FACING, EnumFacing.UP)); this.setDefaultState(this.blockState.getBaseState().withProperty(BlockTorch.FACING, EnumFacing.UP));
this.setTickRandomly(true); this.setTickRandomly(true);
this.setCreativeTab(CreativeTabs.DECORATIONS);
this.setHardness(0.0F); this.setHardness(0.0F);
this.setLightLevel(0.67F); this.setLightLevel(0.67F);

View file

@ -34,7 +34,6 @@ import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModAPIManager;
import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.registry.GameRegistry;
@ -282,7 +281,7 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
} }
} }
else if(teslaLoaded){ else if(teslaLoaded){
T cap = TeslaUtil.getTeslaCapability(this, capability, facing); T cap = TeslaUtil.wrapTeslaToRF(this, capability, facing);
if(cap != null){ if(cap != null){
return cap; return cap;
} }

View file

@ -14,6 +14,8 @@ import cofh.api.energy.IEnergyReceiver;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues; import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler; import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
import de.ellpeck.actuallyadditions.mod.util.compat.TeslaUtil;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -25,7 +27,7 @@ import java.util.Map;
public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements IEnergyReceiver{ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements IEnergyReceiver{
public final Map<EnumFacing, IEnergyReceiver> receiversAround = new HashMap<EnumFacing, IEnergyReceiver>(); public final Map<EnumFacing, TileEntity> receiversAround = new HashMap<EnumFacing, TileEntity>();
public static final int CAP = 1000; public static final int CAP = 1000;
@ -75,8 +77,10 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
for(EnumFacing side : EnumFacing.values()){ for(EnumFacing side : EnumFacing.values()){
BlockPos pos = this.getPos().offset(side); BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.worldObj.getTileEntity(pos); TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile instanceof IEnergyReceiver && !(tile instanceof TileEntityLaserRelay)){ if(tile != null && !(tile instanceof TileEntityLaserRelay)){
this.receiversAround.put(side, (IEnergyReceiver)tile); if(tile instanceof IEnergyReceiver || (teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite()))){
this.receiversAround.put(side, tile);
}
} }
} }
} }
@ -95,17 +99,30 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
TileEntityLaserRelayEnergy theRelay = (TileEntityLaserRelayEnergy)relayTile; TileEntityLaserRelayEnergy theRelay = (TileEntityLaserRelayEnergy)relayTile;
double highestLoss = Math.max(theRelay.getLossPercentage(), this.getLossPercentage()); double highestLoss = Math.max(theRelay.getLossPercentage(), this.getLossPercentage());
int lowestCap = Math.min(theRelay.getEnergyCap(), this.getEnergyCap()); int lowestCap = Math.min(theRelay.getEnergyCap(), this.getEnergyCap());
for(Map.Entry<EnumFacing, IEnergyReceiver> receiver : theRelay.receiversAround.entrySet()){ for(Map.Entry<EnumFacing, TileEntity> receiver : theRelay.receiversAround.entrySet()){
if(receiver != null && receiver.getKey() != null && receiver.getValue() != null){ if(receiver != null){
if(theRelay != this || receiver.getKey() != from){ EnumFacing side = receiver.getKey();
if(receiver.getValue().canConnectEnergy(receiver.getKey().getOpposite())){ EnumFacing opp = side.getOpposite();
//Transfer the energy (with the energy loss!) TileEntity tile = receiver.getValue();
int theoreticalReceived = receiver.getValue().receiveEnergy(receiver.getKey().getOpposite(), Math.min(maxTransfer, lowestCap)-transmitted, true); if(theRelay != this || side != from){
//The amount of energy lost during a transfer if(tile instanceof IEnergyReceiver){
int deduct = ConfigBoolValues.LASER_RELAY_LOSS.isEnabled() ? (int)(theoreticalReceived*(highestLoss/100)) : 0; IEnergyReceiver iReceiver = (IEnergyReceiver)tile;
if(iReceiver.canConnectEnergy(opp)){
transmitted += receiver.getValue().receiveEnergy(receiver.getKey().getOpposite(), theoreticalReceived-deduct, simulate); int theoreticalReceived = iReceiver.receiveEnergy(opp, Math.min(maxTransfer, lowestCap)-transmitted, true);
int deduct = this.calcDeduction(theoreticalReceived, highestLoss);
transmitted += iReceiver.receiveEnergy(opp, theoreticalReceived-deduct, simulate);
transmitted += deduct; transmitted += deduct;
}
}
else if(teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, opp)){
ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, opp);
if(cap != null){
int theoreticalReceived = (int)cap.givePower(Math.min(maxTransfer, lowestCap)-transmitted, true);
int deduct = this.calcDeduction(theoreticalReceived, highestLoss);
transmitted += cap.givePower(theoreticalReceived-deduct, simulate);
transmitted += deduct;
}
}
//If everything that could be transmitted was transmitted //If everything that could be transmitted was transmitted
if(transmitted >= maxTransfer){ if(transmitted >= maxTransfer){
@ -118,10 +135,13 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
} }
} }
} }
}
return transmitted; return transmitted;
} }
private int calcDeduction(int theoreticalReceived, double highestLoss){
return ConfigBoolValues.LASER_RELAY_LOSS.isEnabled() ? (int)(theoreticalReceived*(highestLoss/100)) : 0;
}
public int getEnergyCap(){ public int getEnergyCap(){
return CAP; return CAP;
} }

View file

@ -10,9 +10,15 @@
package de.ellpeck.actuallyadditions.mod.tile; package de.ellpeck.actuallyadditions.mod.tile;
import cofh.api.energy.IEnergyConnection;
import cofh.api.energy.IEnergyHandler;
import cofh.api.energy.IEnergyProvider; import cofh.api.energy.IEnergyProvider;
import cofh.api.energy.IEnergyReceiver; import cofh.api.energy.IEnergyReceiver;
import de.ellpeck.actuallyadditions.mod.blocks.BlockPhantom; import de.ellpeck.actuallyadditions.mod.blocks.BlockPhantom;
import de.ellpeck.actuallyadditions.mod.util.compat.TeslaUtil;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.darkhax.tesla.api.ITeslaProducer;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
@ -25,22 +31,56 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
@Override @Override
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){ public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
return this.isBoundThingInRange() && this.getReceiver() != null ? this.getReceiver().receiveEnergy(from, maxReceive, simulate) : 0; if(this.isBoundThingInRange()){
TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
if(tile != null){
if(tile instanceof IEnergyReceiver){
return ((IEnergyReceiver)tile).receiveEnergy(from, maxReceive, simulate);
}
else if(teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, from)){
ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, from);
if(cap != null){
return (int)cap.givePower(maxReceive, simulate);
}
}
}
}
return 0;
} }
@Override @Override
public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate){ public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate){
return this.isBoundThingInRange() && this.getProvider() != null ? this.getProvider().extractEnergy(from, maxExtract, simulate) : 0; if(this.isBoundThingInRange()){
TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
if(tile != null){
if(tile instanceof IEnergyProvider){
return ((IEnergyProvider)tile).extractEnergy(from, maxExtract, simulate);
}
else if(teslaLoaded && tile.hasCapability(TeslaUtil.teslaProducer, from)){
ITeslaProducer cap = tile.getCapability(TeslaUtil.teslaProducer, from);
if(cap != null){
return (int)cap.takePower(maxExtract, simulate);
}
}
}
}
return 0;
} }
@Override @Override
public int getEnergyStored(EnumFacing from){ public int getEnergyStored(EnumFacing from){
if(this.isBoundThingInRange()){ if(this.isBoundThingInRange()){
if(this.getProvider() != null){ TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
return this.getProvider().getEnergyStored(from); if(tile != null){
if(tile instanceof IEnergyHandler){
return ((IEnergyHandler)tile).getEnergyStored(from);
}
else if(teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, from)){
ITeslaHolder cap = tile.getCapability(TeslaUtil.teslaHolder, from);
if(cap != null){
return (int)cap.getStoredPower();
}
} }
if(this.getReceiver() != null){
return this.getReceiver().getEnergyStored(from);
} }
} }
return 0; return 0;
@ -49,49 +89,44 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
@Override @Override
public int getMaxEnergyStored(EnumFacing from){ public int getMaxEnergyStored(EnumFacing from){
if(this.isBoundThingInRange()){ if(this.isBoundThingInRange()){
if(this.getProvider() != null){ TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
return this.getProvider().getMaxEnergyStored(from); if(tile != null){
if(tile instanceof IEnergyHandler){
return ((IEnergyHandler)tile).getMaxEnergyStored(from);
}
else if(teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, from)){
ITeslaHolder cap = tile.getCapability(TeslaUtil.teslaHolder, from);
if(cap != null){
return (int)cap.getCapacity();
}
} }
if(this.getReceiver() != null){
return this.getReceiver().getMaxEnergyStored(from);
} }
} }
return 0; return 0;
} }
public IEnergyProvider getProvider(){
if(this.boundPosition != null){
TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
if(tile instanceof IEnergyProvider){
return (IEnergyProvider)tile;
}
}
return null;
}
public IEnergyReceiver getReceiver(){
if(this.boundPosition != null){
TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
if(tile instanceof IEnergyReceiver){
return (IEnergyReceiver)tile;
}
}
return null;
}
@Override @Override
public boolean isBoundThingInRange(){ public boolean isBoundThingInRange(){
return super.isBoundThingInRange() && (this.worldObj.getTileEntity(this.boundPosition) instanceof IEnergyReceiver || this.worldObj.getTileEntity(this.boundPosition) instanceof IEnergyProvider); if(super.isBoundThingInRange()){
TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
return tile != null && (tile instanceof IEnergyHandler || (teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, null)));
}
else{
return false;
}
} }
@Override @Override
public boolean canConnectEnergy(EnumFacing from){ public boolean canConnectEnergy(EnumFacing from){
if(this.isBoundThingInRange()){ if(this.isBoundThingInRange()){
if(this.getProvider() != null){ TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
return this.getProvider().canConnectEnergy(from); if(tile != null){
if(tile instanceof IEnergyConnection){
return ((IEnergyConnection)tile).canConnectEnergy(from);
}
else{
return teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, from);
} }
if(this.getReceiver() != null){
return this.getReceiver().canConnectEnergy(from);
} }
} }
return false; return false;

View file

@ -119,7 +119,7 @@ public final class WorldUtil{
} }
} }
else if(TileEntityBase.teslaLoaded){ else if(TileEntityBase.teslaLoaded){
TeslaUtil.doTeslaInteraction(tile, otherTile, side); TeslaUtil.doWrappedTeslaRFInteraction(tile, otherTile, side);
} }
} }
} }

View file

@ -35,7 +35,7 @@ public final class TeslaUtil{
@CapabilityInject(ITeslaHolder.class) @CapabilityInject(ITeslaHolder.class)
public static Capability<ITeslaHolder> teslaHolder; public static Capability<ITeslaHolder> teslaHolder;
public static <T> T getTeslaCapability(TileEntityBase tile, Capability<T> capability, EnumFacing facing){ public static <T> T wrapTeslaToRF(TileEntityBase tile, Capability<T> capability, EnumFacing facing){
boolean receive = tile instanceof IEnergyReceiver && capability == teslaConsumer; boolean receive = tile instanceof IEnergyReceiver && capability == teslaConsumer;
boolean provide = tile instanceof IEnergyProvider && capability == teslaProducer; boolean provide = tile instanceof IEnergyProvider && capability == teslaProducer;
boolean hold = tile instanceof IEnergyHandler && capability == teslaHolder; boolean hold = tile instanceof IEnergyHandler && capability == teslaHolder;
@ -47,7 +47,7 @@ public final class TeslaUtil{
} }
} }
public static void doTeslaInteraction(TileEntity tile, TileEntity otherTile, EnumFacing side){ public static void doWrappedTeslaRFInteraction(TileEntity tile, TileEntity otherTile, EnumFacing side){
ITeslaConsumer handlerTo = null; ITeslaConsumer handlerTo = null;
ITeslaProducer handlerFrom = null; ITeslaProducer handlerFrom = null;