Added laser relay network cashing, added loadedness checks

This commit is contained in:
Ellpeck 2017-01-18 15:04:58 +01:00
parent ac13e6c243
commit cb4ebe3aaa
9 changed files with 110 additions and 66 deletions

View file

@ -145,7 +145,7 @@ public class BlockLaserRelay extends BlockContainerBase implements IHudDisplay{
if(!world.isRemote){
relay.onCompassAction(player);
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(relay.getPos(), relay.getWorld());
Network network = relay.getNetwork();
if(network != null){
network.changeAmount++;
}

View file

@ -57,6 +57,7 @@ public final class LaserRelayConnectionHandler implements ILaserRelayConnectionH
firstNetwork.connections.add(secondPair);
}
secondNetwork.changeAmount++;
WorldData.getDataForWorld(world).laserRelayNetworks.remove(secondNetwork);
//System.out.println("Merged Two Networks!");
}
@ -84,6 +85,8 @@ public final class LaserRelayConnectionHandler implements ILaserRelayConnectionH
public void removeRelayFromNetwork(BlockPos relay, World world){
Network network = this.getNetworkFor(relay, world);
if(network != null){
network.changeAmount++;
//Setup new network (so that splitting a network will cause it to break into two)
WorldData.getDataForWorld(world).laserRelayNetworks.remove(network);
for(IConnectionPair pair : network.connections){

View file

@ -277,7 +277,10 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
public void saveDataOnChangeOrWorldStart(){
for(EnumFacing side : EnumFacing.values()){
this.tilesAround[side.ordinal()] = this.worldObj.getTileEntity(this.pos.offset(side));
BlockPos pos = this.pos.offset(side);
if(this.worldObj.isBlockLoaded(pos)){
this.tilesAround[side.ordinal()] = this.worldObj.getTileEntity(pos);
}
}
}

View file

@ -21,6 +21,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
@ -140,37 +141,43 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
*/
@Override
public void saveDataOnChangeOrWorldStart(){
this.placeToPull = null;
this.placeToPut = null;
if(this.sideToPull != -1){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPull);
this.placeToPull = this.worldObj.getTileEntity(this.pos.offset(side));
BlockPos offset = this.pos.offset(side);
if(this.slotToPullEnd <= 0 && this.placeToPull != null){
if(this.placeToPull.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)){
IItemHandler cap = this.placeToPull.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if(cap != null){
this.slotToPullEnd = cap.getSlots();
if(this.worldObj.isBlockLoaded(offset)){
this.placeToPull = this.worldObj.getTileEntity(offset);
if(this.slotToPullEnd <= 0 && this.placeToPull != null){
if(this.placeToPull.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)){
IItemHandler cap = this.placeToPull.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if(cap != null){
this.slotToPullEnd = cap.getSlots();
}
}
}
else if(this.placeToPull instanceof IInventory){
this.slotToPullEnd = ((IInventory)this.placeToPull).getSizeInventory();
}
}
}
if(this.sideToPut != -1){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPut);
this.placeToPut = this.worldObj.getTileEntity(this.pos.offset(side));
BlockPos offset = this.pos.offset(side);
if(this.slotToPutEnd <= 0 && this.placeToPut != null){
if(this.placeToPut.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)){
IItemHandler cap = this.placeToPut.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if(cap != null){
this.slotToPutEnd = cap.getSlots();
if(this.worldObj.isBlockLoaded(offset)){
this.placeToPut = this.worldObj.getTileEntity(offset);
if(this.slotToPutEnd <= 0 && this.placeToPut != null){
if(this.placeToPut.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)){
IItemHandler cap = this.placeToPut.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if(cap != null){
this.slotToPutEnd = cap.getSlots();
}
}
}
else if(this.placeToPut instanceof IInventory){
this.slotToPutEnd = ((IInventory)this.placeToPut).getSizeInventory();
}
}
}
}

View file

@ -10,7 +10,6 @@
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.laser.Network;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
@ -29,7 +28,6 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
private final Map<Integer, SpecificItemHandlerInfo> specificInfos = new HashMap<Integer, SpecificItemHandlerInfo>();
public TileEntityLaserRelayItem connectedRelay;
private Network oldNetwork;
private int lastNetworkChangeAmount = -1;
public TileEntityItemViewer(){
@ -76,9 +74,9 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
private void queryAndSaveData(){
if(this.connectedRelay != null){
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.connectedRelay.getPos(), this.worldObj);
Network network = this.connectedRelay.getNetwork();
if(network != null){
if(this.oldNetwork != network || this.lastNetworkChangeAmount != network.changeAmount){
if(this.lastNetworkChangeAmount != network.changeAmount){
this.clearInfos();
this.connectedRelay.getItemHandlersInNetwork(network, this.genericInfos);
@ -96,7 +94,6 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
}
}
this.oldNetwork = network;
this.lastNetworkChangeAmount = network.changeAmount;
}
@ -105,6 +102,7 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
}
this.clearInfos();
this.lastNetworkChangeAmount = -1;
}
private void clearInfos(){
@ -134,15 +132,17 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
for(int i = 0; i <= 5; i++){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.worldObj.getTileEntity(pos);
if(this.worldObj.isBlockLoaded(pos)){
TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile instanceof TileEntityLaserRelayItem){
if(tileFound != null){
this.connectedRelay = null;
return;
}
else{
tileFound = (TileEntityLaserRelayItem)tile;
if(tile instanceof TileEntityLaserRelayItem){
if(tileFound != null){
this.connectedRelay = null;
return;
}
else{
tileFound = (TileEntityLaserRelayItem)tile;
}
}
}
}
@ -158,7 +158,7 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override
public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction){
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
if(handler != null){
if(handler != null && handler.isLoaded()){
if(this.isWhitelisted(handler, stack, true)){
if(ItemStack.areItemsEqual(handler.handler.getStackInSlot(handler.switchedIndex), stack)){
ItemStack gaveBack = handler.handler.extractItem(handler.switchedIndex, StackUtil.getStackSize(stack), true);
@ -183,7 +183,7 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override
public boolean isItemValidForSlot(int index, ItemStack stack){
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
if(handler != null){
if(handler != null && handler.isLoaded()){
if(this.isWhitelisted(handler, stack, false)){
ItemStack gaveBack = handler.handler.insertItem(handler.switchedIndex, stack, true);
return !ItemStack.areItemStacksEqual(gaveBack, stack);
@ -203,7 +203,7 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
public void setInventorySlotContents(int i, ItemStack stack){
if(StackUtil.isValid(stack)){
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
if(handler != null){
if(handler != null && handler.isLoaded()){
ItemStack toInsert = stack.copy();
ItemStack inSlot = handler.handler.getStackInSlot(handler.switchedIndex);
if(StackUtil.isValid(inSlot)){
@ -223,8 +223,10 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
List<GenericItemHandlerInfo> infos = this.getItemHandlerInfos();
if(infos != null){
for(GenericItemHandlerInfo info : infos){
for(IItemHandler handler : info.handlers){
size += handler.getSlots();
if(info.isLoaded()){
for(IItemHandler handler : info.handlers){
size += handler.getSlots();
}
}
}
}
@ -234,7 +236,7 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override
public ItemStack getStackInSlot(int i){
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
if(handler != null){
if(handler != null && handler.isLoaded()){
return handler.handler.getStackInSlot(handler.switchedIndex);
}
return null;
@ -243,7 +245,7 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override
public ItemStack decrStackSize(int i, int j){
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
if(handler != null){
if(handler != null && handler.isLoaded()){
return handler.handler.extractItem(handler.switchedIndex, j, false);
}
return null;
@ -252,7 +254,7 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override
public ItemStack removeStackFromSlot(int index){
SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
if(handler != null){
if(handler != null && handler.isLoaded()){
ItemStack stackInSlot = handler.handler.getStackInSlot(handler.switchedIndex);
if(StackUtil.isValid(stackInSlot)){
handler.handler.extractItem(handler.switchedIndex, StackUtil.getStackSize(stackInSlot), false);
@ -273,6 +275,10 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
this.switchedIndex = switchedIndex;
this.relayInQuestion = relayInQuestion;
}
public boolean isLoaded(){
return this.relayInQuestion.hasWorldObj() && this.relayInQuestion.getWorld().isBlockLoaded(this.relayInQuestion.getPos());
}
}
public static class GenericItemHandlerInfo implements Comparable<GenericItemHandlerInfo>{
@ -284,6 +290,10 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
this.relayInQuestion = relayInQuestion;
}
public boolean isLoaded(){
return this.relayInQuestion.hasWorldObj() && this.relayInQuestion.getWorld().isBlockLoaded(this.relayInQuestion.getPos());
}
@Override
public int compareTo(GenericItemHandlerInfo other){
int thisPrio = this.relayInQuestion.getPriority();

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
import de.ellpeck.actuallyadditions.api.laser.LaserType;
import de.ellpeck.actuallyadditions.api.laser.Network;
import de.ellpeck.actuallyadditions.mod.misc.ConnectionPair;
import io.netty.util.internal.ConcurrentSet;
import net.minecraft.entity.player.EntityPlayer;
@ -30,6 +31,9 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
public final LaserType type;
private Network cachedNetwork;
private int changeAmountAtCaching = -1;
private Set<IConnectionPair> tempConnectionStorage;
public TileEntityLaserRelay(String name, LaserType type){
@ -107,6 +111,21 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
}
}*/
public Network getNetwork(){
if(this.cachedNetwork == null || this.cachedNetwork.changeAmount != this.changeAmountAtCaching){
this.cachedNetwork = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.pos, this.worldObj);
if(this.cachedNetwork != null){
this.changeAmountAtCaching = this.cachedNetwork.changeAmount;
}
else{
this.changeAmountAtCaching = -1;
}
}
return this.cachedNetwork;
}
@Override
public void invalidate(){
super.invalidate();

View file

@ -11,7 +11,6 @@
package de.ellpeck.actuallyadditions.mod.tile;
import cofh.api.energy.IEnergyReceiver;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
import de.ellpeck.actuallyadditions.api.laser.LaserType;
import de.ellpeck.actuallyadditions.api.laser.Network;
@ -67,7 +66,7 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
private int transmitEnergy(EnumFacing from, int maxTransmit, boolean simulate){
int transmitted = 0;
if(maxTransmit > 0 && this.mode != Mode.OUTPUT_ONLY){
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.pos, this.worldObj);
Network network = this.getNetwork();
if(network != null){
transmitted = this.transferEnergyToReceiverInNeed(from, network, maxTransmit, simulate);
}
@ -93,21 +92,23 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
this.receiversAround.clear();
for(EnumFacing side : EnumFacing.values()){
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if(tile instanceof IEnergyReceiver || (ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite()))){
this.receiversAround.put(side, tile);
if(this.worldObj.isBlockLoaded(pos)){
TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if(tile instanceof IEnergyReceiver || (ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite()))){
this.receiversAround.put(side, tile);
TileEntity oldTile = old.get(side);
if(oldTile == null || !tile.equals(oldTile)){
change = true;
TileEntity oldTile = old.get(side);
if(oldTile == null || !tile.equals(oldTile)){
change = true;
}
}
}
}
}
if(change || old.size() != this.receiversAround.size()){
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.getPos(), this.getWorld());
Network network = this.getNetwork();
if(network != null){
network.changeAmount++;
}
@ -124,7 +125,7 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
for(IConnectionPair pair : network.connections){
for(BlockPos relay : pair.getPositions()){
if(relay != null && !alreadyChecked.contains(relay)){
if(relay != null && this.worldObj.isBlockLoaded(relay) && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
TileEntity relayTile = this.worldObj.getTileEntity(relay);
if(relayTile instanceof TileEntityLaserRelayEnergy){

View file

@ -71,21 +71,23 @@ public class TileEntityLaserRelayFluids extends TileEntityLaserRelay implements
this.handlersAround.clear();
for(EnumFacing side : EnumFacing.values()){
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if(tile instanceof net.minecraftforge.fluids.IFluidHandler || tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite())){
this.handlersAround.put(side, tile);
if(this.worldObj.isBlockLoaded(pos)){
TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if(tile instanceof net.minecraftforge.fluids.IFluidHandler || tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite())){
this.handlersAround.put(side, tile);
TileEntity oldTile = old.get(side);
if(oldTile == null || !tile.equals(oldTile)){
change = true;
TileEntity oldTile = old.get(side);
if(oldTile == null || !tile.equals(oldTile)){
change = true;
}
}
}
}
}
if(change || old.size() != this.handlersAround.size()){
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.getPos(), this.getWorld());
Network network = this.getNetwork();
if(network != null){
network.changeAmount++;
}
@ -115,7 +117,7 @@ public class TileEntityLaserRelayFluids extends TileEntityLaserRelay implements
private int transmitEnergy(EnumFacing from, FluidStack stack, boolean doFill){
int transmitted = 0;
if(stack != null && this.mode != Mode.OUTPUT_ONLY){
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.pos, this.worldObj);
Network network = this.getNetwork();
if(network != null){
transmitted = this.transferEnergyToReceiverInNeed(from, network, stack, doFill);
}
@ -133,7 +135,7 @@ public class TileEntityLaserRelayFluids extends TileEntityLaserRelay implements
for(IConnectionPair pair : network.connections){
for(BlockPos relay : pair.getPositions()){
if(relay != null && !alreadyChecked.contains(relay)){
if(relay != null && this.worldObj.isBlockLoaded(relay) && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
TileEntity relayTile = this.worldObj.getTileEntity(relay);
if(relayTile instanceof TileEntityLaserRelayFluids){

View file

@ -84,7 +84,7 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
}
if(change || old.size() != this.handlersAround.size()){
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.getPos(), this.getWorld());
Network network = this.getNetwork();
if(network != null){
network.changeAmount++;
}
@ -97,15 +97,14 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
for(IConnectionPair pair : network.connections){
for(BlockPos relay : pair.getPositions()){
if(relay != null && !alreadyChecked.contains(relay)){
if(relay != null && this.worldObj.isBlockLoaded(relay) && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
TileEntity aRelayTile = this.worldObj.getTileEntity(relay);
if(aRelayTile instanceof TileEntityLaserRelayItem){
TileEntityLaserRelayItem relayTile = (TileEntityLaserRelayItem)aRelayTile;
GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile);
Map<BlockPos, IItemHandler> handlersAroundTile = relayTile.handlersAround;
for(Map.Entry<BlockPos, IItemHandler> handler : handlersAroundTile.entrySet()){
for(Map.Entry<BlockPos, IItemHandler> handler : relayTile.handlersAround.entrySet()){
if(!alreadyChecked.contains(handler.getKey())){
alreadyChecked.add(handler.getKey());