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 @Override
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block){ 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); TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileEntityLaserRelayItem){ if(tile instanceof TileEntityLaserRelay){
((TileEntityLaserRelayItem)tile).saveAllHandlersAround(); ((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.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
@ -39,6 +38,8 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
private Set<ConnectionPair> tempConnectionStorage; private Set<ConnectionPair> tempConnectionStorage;
private boolean hasCheckedHandlersAround;
public TileEntityLaserRelay(String name, boolean isItem){ public TileEntityLaserRelay(String name, boolean isItem){
super(name); super(name);
this.isItem = isItem; this.isItem = isItem;
@ -82,6 +83,15 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
if(this.worldObj.isRemote){ if(this.worldObj.isRemote){
this.renderParticles(); this.renderParticles();
} }
else if(!this.hasCheckedHandlersAround){
this.saveAllHandlersAround();
this.hasCheckedHandlersAround = true;
System.out.println("Checked handlers around!");
}
}
public void saveAllHandlersAround(){
} }
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)

View file

@ -13,24 +13,27 @@ package de.ellpeck.actuallyadditions.mod.tile;
import cofh.api.energy.IEnergyReceiver; import cofh.api.energy.IEnergyReceiver;
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.PosUtil;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
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;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements IEnergyReceiver{ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements IEnergyReceiver{
public Map<EnumFacing, IEnergyReceiver> receiversAround = new HashMap<EnumFacing, IEnergyReceiver>();
public TileEntityLaserRelayEnergy(){ public TileEntityLaserRelayEnergy(){
super("laserRelay", false); super("laserRelay", false);
} }
@Override @Override
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){ 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 @Override
@ -43,12 +46,12 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
return 0; return 0;
} }
public int transmitEnergy(BlockPos blockFrom, int maxTransmit, boolean simulate){ public int transmitEnergy(EnumFacing from, int maxTransmit, boolean simulate){
int transmitted = 0; int transmitted = 0;
if(maxTransmit > 0){ if(maxTransmit > 0){
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getNetworkFor(this.pos, this.worldObj); LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getNetworkFor(this.pos, this.worldObj);
if(network != null){ 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; return transmitted;
@ -59,7 +62,20 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
return true; 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; int transmitted = 0;
List<BlockPos> alreadyChecked = new ArrayList<BlockPos>(); List<BlockPos> alreadyChecked = new ArrayList<BlockPos>();
//Go through all of the connections in the network //Go through all of the connections in the network
@ -68,22 +84,18 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
for(BlockPos relay : pair.positions){ for(BlockPos relay : pair.positions){
if(relay != null && !alreadyChecked.contains(relay)){ if(relay != null && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay); alreadyChecked.add(relay);
//Get every side of the relay TileEntity relayTile = this.worldObj.getTileEntity(relay);
for(int i = 0; i <= 5; i++){ if(relayTile instanceof TileEntityLaserRelayEnergy){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i); for(Map.Entry<EnumFacing, IEnergyReceiver> receiver : ((TileEntityLaserRelayEnergy)relayTile).receiversAround.entrySet()){
//Get the Position at the side if(receiver != null && receiver.getKey() != null && receiver.getValue() != null){
BlockPos pos = WorldUtil.getCoordsFromSide(side, relay, 0); if(receiver.getKey() != from){
if(!PosUtil.areSamePos(pos, energyGottenFrom)){ if(receiver.getValue().canConnectEnergy(receiver.getKey().getOpposite())){
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!) //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 //The amount of energy lost during a transfer
int deduct = (int)(theoreticalReceived*((double)ConfigIntValues.LASER_RELAY_LOSS.getValue()/100)); 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; transmitted += deduct;
//If everything that could be transmitted was transmitted //If everything that could be transmitted was transmitted
@ -97,6 +109,7 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
} }
} }
} }
}
return transmitted; return transmitted;
} }
} }

View file

@ -27,7 +27,6 @@ import java.util.List;
public class TileEntityLaserRelayItem extends TileEntityLaserRelay{ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
public List<IItemHandler> handlersAround = new ArrayList<IItemHandler>(); public List<IItemHandler> handlersAround = new ArrayList<IItemHandler>();
private boolean hasCheckedHandlersOnLoad;
public TileEntityLaserRelayItem(String name){ public TileEntityLaserRelayItem(String name){
super(name, true); super(name, true);
@ -42,15 +41,6 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
} }
@Override @Override
public void updateEntity(){
super.updateEntity();
if(!this.worldObj.isRemote && !this.hasCheckedHandlersOnLoad){
this.saveAllHandlersAround();
this.hasCheckedHandlersOnLoad = true;
}
}
public void saveAllHandlersAround(){ public void saveAllHandlersAround(){
this.handlersAround.clear(); this.handlersAround.clear();

View file

@ -105,7 +105,7 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
TileEntity tile = WorldUtil.getTileEntityFromSide(side, this.worldObj, this.getPos()); TileEntity tile = WorldUtil.getTileEntityFromSide(side, this.worldObj, this.getPos());
if(tile != null && tile instanceof IEnergyReceiver && this.getProvider().getEnergyStored(side.getOpposite()) > 0){ if(tile != null && tile instanceof IEnergyReceiver && this.getProvider().getEnergyStored(side.getOpposite()) > 0){
if(((IEnergyReceiver)tile).canConnectEnergy(side.getOpposite()) && this.canConnectEnergy(side)){ 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); int actualReceive = ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), receive, false);
this.extractEnergy(side, actualReceive, false); this.extractEnergy(side, actualReceive, false);
} }