From 90c1f2c25fe077f3c93c543d5f7f6a1509125c89 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 24 Jul 2016 02:54:07 +0200 Subject: [PATCH] Added proper tesla support for phantom energyfaces and laser relays --- .../mod/blocks/BlockTinyTorch.java | 1 - .../mod/tile/TileEntityBase.java | 3 +- .../mod/tile/TileEntityLaserRelayEnergy.java | 56 ++++++--- .../mod/tile/TileEntityPhantomEnergyface.java | 111 ++++++++++++------ .../actuallyadditions/mod/util/WorldUtil.java | 2 +- .../mod/util/compat/TeslaUtil.java | 4 +- 6 files changed, 115 insertions(+), 62 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockTinyTorch.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockTinyTorch.java index e1814226c..c3a51696a 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockTinyTorch.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockTinyTorch.java @@ -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); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java index c98c8c7c5..11b347a29 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java @@ -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; } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java index 0b7cfc20e..59bbe060d 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java @@ -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 receiversAround = new HashMap(); + public final Map receiversAround = new HashMap(); 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 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 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; } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java index 3609a9822..81cbf83b2 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java @@ -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; diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java index dc0e5718a..5a5249038 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java @@ -119,7 +119,7 @@ public final class WorldUtil{ } } else if(TileEntityBase.teslaLoaded){ - TeslaUtil.doTeslaInteraction(tile, otherTile, side); + TeslaUtil.doWrappedTeslaRFInteraction(tile, otherTile, side); } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/compat/TeslaUtil.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/compat/TeslaUtil.java index 1ec8dce72..1461df0e4 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/compat/TeslaUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/compat/TeslaUtil.java @@ -35,7 +35,7 @@ public final class TeslaUtil{ @CapabilityInject(ITeslaHolder.class) public static Capability teslaHolder; - public static T getTeslaCapability(TileEntityBase tile, Capability capability, EnumFacing facing){ + public static T wrapTeslaToRF(TileEntityBase tile, Capability 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;