diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java index 2b046246d..6a4317309 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java @@ -118,10 +118,11 @@ public class BlockLaserRelay extends BlockContainerBase{ @Override public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block){ - if((this.type == Type.ITEM || this.type == Type.ITEM_WHITELIST) && !world.isRemote){ + if(!world.isRemote){ TileEntity tile = world.getTileEntity(pos); - if(tile instanceof TileEntityLaserRelayItem){ - ((TileEntityLaserRelayItem)tile).saveAllHandlersAround(); + if(tile instanceof TileEntityLaserRelay){ + ((TileEntityLaserRelay)tile).saveAllHandlersAround(); + System.out.println("Checked handlers around!"); } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java index 97519184a..d59a498a5 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java @@ -23,7 +23,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; -import net.minecraft.util.math.BlockPos; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -39,6 +38,8 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{ private Set tempConnectionStorage; + private boolean hasCheckedHandlersAround; + public TileEntityLaserRelay(String name, boolean isItem){ super(name); this.isItem = isItem; @@ -82,6 +83,15 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{ if(this.worldObj.isRemote){ this.renderParticles(); } + else if(!this.hasCheckedHandlersAround){ + this.saveAllHandlersAround(); + this.hasCheckedHandlersAround = true; + System.out.println("Checked handlers around!"); + } + } + + public void saveAllHandlersAround(){ + } @SideOnly(Side.CLIENT) 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 1549cfd0b..c62515d97 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java @@ -13,24 +13,27 @@ package de.ellpeck.actuallyadditions.mod.tile; import cofh.api.energy.IEnergyReceiver; import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler; -import de.ellpeck.actuallyadditions.mod.util.PosUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements IEnergyReceiver{ + public Map receiversAround = new HashMap(); + public TileEntityLaserRelayEnergy(){ super("laserRelay", false); } @Override public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){ - return this.transmitEnergy(WorldUtil.getCoordsFromSide(from, this.pos, 0), maxReceive, simulate); + return this.transmitEnergy(from, maxReceive, simulate); } @Override @@ -43,12 +46,12 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements return 0; } - public int transmitEnergy(BlockPos blockFrom, int maxTransmit, boolean simulate){ + public int transmitEnergy(EnumFacing from, int maxTransmit, boolean simulate){ int transmitted = 0; if(maxTransmit > 0){ LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getNetworkFor(this.pos, this.worldObj); if(network != null){ - transmitted = this.transferEnergyToReceiverInNeed(blockFrom, network, Math.min(ConfigIntValues.LASER_RELAY_MAX_TRANSFER.getValue(), maxTransmit), simulate); + transmitted = this.transferEnergyToReceiverInNeed(from, network, Math.min(ConfigIntValues.LASER_RELAY_MAX_TRANSFER.getValue(), maxTransmit), simulate); } } return transmitted; @@ -59,7 +62,20 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements return true; } - private int transferEnergyToReceiverInNeed(BlockPos energyGottenFrom, LaserRelayConnectionHandler.Network network, int maxTransfer, boolean simulate){ + @Override + public void saveAllHandlersAround(){ + this.receiversAround.clear(); + + for(EnumFacing side : EnumFacing.values()){ + BlockPos pos = WorldUtil.getCoordsFromSide(side, this.getPos(), 0); + TileEntity tile = this.worldObj.getTileEntity(pos); + if(tile instanceof IEnergyReceiver && !(tile instanceof TileEntityLaserRelay)){ + this.receiversAround.put(side, (IEnergyReceiver)tile); + } + } + } + + private int transferEnergyToReceiverInNeed(EnumFacing from, LaserRelayConnectionHandler.Network network, int maxTransfer, boolean simulate){ int transmitted = 0; List alreadyChecked = new ArrayList(); //Go through all of the connections in the network @@ -68,27 +84,24 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements for(BlockPos relay : pair.positions){ if(relay != null && !alreadyChecked.contains(relay)){ alreadyChecked.add(relay); - //Get every side of the relay - for(int i = 0; i <= 5; i++){ - EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i); - //Get the Position at the side - BlockPos pos = WorldUtil.getCoordsFromSide(side, relay, 0); - if(!PosUtil.areSamePos(pos, energyGottenFrom)){ - TileEntity tile = this.worldObj.getTileEntity(pos); - if(tile instanceof IEnergyReceiver && !(tile instanceof TileEntityLaserRelay)){ - IEnergyReceiver receiver = (IEnergyReceiver)tile; - if(receiver.canConnectEnergy(side.getOpposite())){ - //Transfer the energy (with the energy loss!) - int theoreticalReceived = ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), maxTransfer-transmitted, true); - //The amount of energy lost during a transfer - int deduct = (int)(theoreticalReceived*((double)ConfigIntValues.LASER_RELAY_LOSS.getValue()/100)); + TileEntity relayTile = this.worldObj.getTileEntity(relay); + if(relayTile instanceof TileEntityLaserRelayEnergy){ + for(Map.Entry receiver : ((TileEntityLaserRelayEnergy)relayTile).receiversAround.entrySet()){ + if(receiver != null && receiver.getKey() != null && receiver.getValue() != null){ + if(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(), maxTransfer-transmitted, true); + //The amount of energy lost during a transfer + int deduct = (int)(theoreticalReceived*((double)ConfigIntValues.LASER_RELAY_LOSS.getValue()/100)); - transmitted += ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), theoreticalReceived-deduct, simulate); - transmitted += deduct; + 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; + //If everything that could be transmitted was transmitted + if(transmitted >= maxTransfer){ + return transmitted; + } } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java index ce26b54dc..5adc60c5f 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java @@ -27,7 +27,6 @@ import java.util.List; public class TileEntityLaserRelayItem extends TileEntityLaserRelay{ public List handlersAround = new ArrayList(); - private boolean hasCheckedHandlersOnLoad; public TileEntityLaserRelayItem(String name){ super(name, true); @@ -42,15 +41,6 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{ } @Override - public void updateEntity(){ - super.updateEntity(); - - if(!this.worldObj.isRemote && !this.hasCheckedHandlersOnLoad){ - this.saveAllHandlersAround(); - this.hasCheckedHandlersOnLoad = true; - } - } - public void saveAllHandlersAround(){ this.handlersAround.clear(); 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 5c636ef9a..497c466c6 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java @@ -105,7 +105,7 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement TileEntity tile = WorldUtil.getTileEntityFromSide(side, this.worldObj, this.getPos()); if(tile != null && tile instanceof IEnergyReceiver && this.getProvider().getEnergyStored(side.getOpposite()) > 0){ if(((IEnergyReceiver)tile).canConnectEnergy(side.getOpposite()) && this.canConnectEnergy(side)){ - int receive = this.extractEnergy(side, Math.min(((IEnergyReceiver)tile).getMaxEnergyStored(side.getOpposite())-((IEnergyReceiver)tile).getEnergyStored(side.getOpposite()), this.getEnergyStored(side)), true); + int receive = this.extractEnergy(side, Integer.MAX_VALUE, true); int actualReceive = ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), receive, false); this.extractEnergy(side, actualReceive, false); }