mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-12-26 21:09:23 +01:00
Added proper tesla support for phantom energyfaces and laser relays
This commit is contained in:
parent
c61785e307
commit
90c1f2c25f
6 changed files with 115 additions and 62 deletions
|
@ -48,7 +48,6 @@ public class BlockTinyTorch extends BlockBase{
|
|||
super(Material.CIRCUITS, name);
|
||||
this.setDefaultState(this.blockState.getBaseState().withProperty(BlockTorch.FACING, EnumFacing.UP));
|
||||
this.setTickRandomly(true);
|
||||
this.setCreativeTab(CreativeTabs.DECORATIONS);
|
||||
|
||||
this.setHardness(0.0F);
|
||||
this.setLightLevel(0.67F);
|
||||
|
|
|
@ -34,7 +34,6 @@ import net.minecraftforge.common.capabilities.Capability;
|
|||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
import net.minecraftforge.fml.common.Loader;
|
||||
import net.minecraftforge.fml.common.ModAPIManager;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
|
||||
|
@ -282,7 +281,7 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
|
|||
}
|
||||
}
|
||||
else if(teslaLoaded){
|
||||
T cap = TeslaUtil.getTeslaCapability(this, capability, facing);
|
||||
T cap = TeslaUtil.wrapTeslaToRF(this, capability, facing);
|
||||
if(cap != null){
|
||||
return cap;
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@ import cofh.api.energy.IEnergyReceiver;
|
|||
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
|
||||
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
||||
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.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
@ -25,7 +27,7 @@ import java.util.Map;
|
|||
|
||||
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;
|
||||
|
||||
|
@ -75,8 +77,10 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
|
|||
for(EnumFacing side : EnumFacing.values()){
|
||||
BlockPos pos = this.getPos().offset(side);
|
||||
TileEntity tile = this.worldObj.getTileEntity(pos);
|
||||
if(tile instanceof IEnergyReceiver && !(tile instanceof TileEntityLaserRelay)){
|
||||
this.receiversAround.put(side, (IEnergyReceiver)tile);
|
||||
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
|
||||
if(tile instanceof IEnergyReceiver || (teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite()))){
|
||||
this.receiversAround.put(side, tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -95,23 +99,35 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
|
|||
TileEntityLaserRelayEnergy theRelay = (TileEntityLaserRelayEnergy)relayTile;
|
||||
double highestLoss = Math.max(theRelay.getLossPercentage(), this.getLossPercentage());
|
||||
int lowestCap = Math.min(theRelay.getEnergyCap(), this.getEnergyCap());
|
||||
for(Map.Entry<EnumFacing, IEnergyReceiver> receiver : theRelay.receiversAround.entrySet()){
|
||||
if(receiver != null && receiver.getKey() != null && receiver.getValue() != null){
|
||||
if(theRelay != this || receiver.getKey() != from){
|
||||
if(receiver.getValue().canConnectEnergy(receiver.getKey().getOpposite())){
|
||||
//Transfer the energy (with the energy loss!)
|
||||
int theoreticalReceived = receiver.getValue().receiveEnergy(receiver.getKey().getOpposite(), Math.min(maxTransfer, lowestCap)-transmitted, true);
|
||||
//The amount of energy lost during a transfer
|
||||
int deduct = ConfigBoolValues.LASER_RELAY_LOSS.isEnabled() ? (int)(theoreticalReceived*(highestLoss/100)) : 0;
|
||||
|
||||
transmitted += receiver.getValue().receiveEnergy(receiver.getKey().getOpposite(), theoreticalReceived-deduct, simulate);
|
||||
transmitted += deduct;
|
||||
|
||||
//If everything that could be transmitted was transmitted
|
||||
if(transmitted >= maxTransfer){
|
||||
return transmitted;
|
||||
for(Map.Entry<EnumFacing, TileEntity> receiver : theRelay.receiversAround.entrySet()){
|
||||
if(receiver != null){
|
||||
EnumFacing side = receiver.getKey();
|
||||
EnumFacing opp = side.getOpposite();
|
||||
TileEntity tile = receiver.getValue();
|
||||
if(theRelay != this || side != from){
|
||||
if(tile instanceof IEnergyReceiver){
|
||||
IEnergyReceiver iReceiver = (IEnergyReceiver)tile;
|
||||
if(iReceiver.canConnectEnergy(opp)){
|
||||
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;
|
||||
}
|
||||
}
|
||||
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(transmitted >= maxTransfer){
|
||||
return transmitted;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -122,6 +138,10 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
|
|||
return transmitted;
|
||||
}
|
||||
|
||||
private int calcDeduction(int theoreticalReceived, double highestLoss){
|
||||
return ConfigBoolValues.LASER_RELAY_LOSS.isEnabled() ? (int)(theoreticalReceived*(highestLoss/100)) : 0;
|
||||
}
|
||||
|
||||
public int getEnergyCap(){
|
||||
return CAP;
|
||||
}
|
||||
|
|
|
@ -10,9 +10,15 @@
|
|||
|
||||
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.IEnergyReceiver;
|
||||
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.util.EnumFacing;
|
||||
|
||||
|
@ -25,22 +31,56 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
|
|||
|
||||
@Override
|
||||
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
|
||||
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
|
||||
public int getEnergyStored(EnumFacing from){
|
||||
if(this.isBoundThingInRange()){
|
||||
if(this.getProvider() != null){
|
||||
return this.getProvider().getEnergyStored(from);
|
||||
}
|
||||
if(this.getReceiver() != null){
|
||||
return this.getReceiver().getEnergyStored(from);
|
||||
TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
@ -49,49 +89,44 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
|
|||
@Override
|
||||
public int getMaxEnergyStored(EnumFacing from){
|
||||
if(this.isBoundThingInRange()){
|
||||
if(this.getProvider() != null){
|
||||
return this.getProvider().getMaxEnergyStored(from);
|
||||
}
|
||||
if(this.getReceiver() != null){
|
||||
return this.getReceiver().getMaxEnergyStored(from);
|
||||
TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
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
|
||||
public boolean canConnectEnergy(EnumFacing from){
|
||||
if(this.isBoundThingInRange()){
|
||||
if(this.getProvider() != null){
|
||||
return this.getProvider().canConnectEnergy(from);
|
||||
}
|
||||
if(this.getReceiver() != null){
|
||||
return this.getReceiver().canConnectEnergy(from);
|
||||
TileEntity tile = this.worldObj.getTileEntity(this.boundPosition);
|
||||
if(tile != null){
|
||||
if(tile instanceof IEnergyConnection){
|
||||
return ((IEnergyConnection)tile).canConnectEnergy(from);
|
||||
}
|
||||
else{
|
||||
return teslaLoaded && tile.hasCapability(TeslaUtil.teslaHolder, from);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -119,7 +119,7 @@ public final class WorldUtil{
|
|||
}
|
||||
}
|
||||
else if(TileEntityBase.teslaLoaded){
|
||||
TeslaUtil.doTeslaInteraction(tile, otherTile, side);
|
||||
TeslaUtil.doWrappedTeslaRFInteraction(tile, otherTile, side);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ public final class TeslaUtil{
|
|||
@CapabilityInject(ITeslaHolder.class)
|
||||
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 provide = tile instanceof IEnergyProvider && capability == teslaProducer;
|
||||
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;
|
||||
ITeslaProducer handlerFrom = null;
|
||||
|
||||
|
|
Loading…
Reference in a new issue