Made Energy Laser Relay less performance intensive

This commit is contained in:
Ellpeck 2016-06-11 16:07:06 +02:00
parent b8051e6483
commit 174d2aca7e
5 changed files with 53 additions and 39 deletions

View file

@ -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!");
}
}
}

View file

@ -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<ConnectionPair> 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)

View file

@ -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<EnumFacing, IEnergyReceiver> receiversAround = new HashMap<EnumFacing, IEnergyReceiver>();
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<BlockPos> alreadyChecked = new ArrayList<BlockPos>();
//Go through all of the connections in the network
@ -68,22 +84,18 @@ 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())){
TileEntity relayTile = this.worldObj.getTileEntity(relay);
if(relayTile instanceof TileEntityLaserRelayEnergy){
for(Map.Entry<EnumFacing, IEnergyReceiver> 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 = ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), maxTransfer-transmitted, true);
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 += receiver.getValue().receiveEnergy(receiver.getKey().getOpposite(), theoreticalReceived-deduct, simulate);
transmitted += deduct;
//If everything that could be transmitted was transmitted
@ -97,6 +109,7 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
}
}
}
}
return transmitted;
}
}

View file

@ -27,7 +27,6 @@ import java.util.List;
public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
public List<IItemHandler> handlersAround = new ArrayList<IItemHandler>();
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();

View file

@ -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);
}