Add a bunch of loadedness checks and add network caching to Laser Relays

This commit is contained in:
Ellpeck 2017-01-18 14:26:56 +01:00
parent ccb88fd416
commit 996b997363
9 changed files with 125 additions and 78 deletions

View file

@ -10,7 +10,6 @@
package de.ellpeck.actuallyadditions.mod.blocks;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.laser.Network;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
@ -167,7 +166,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

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

View file

@ -20,6 +20,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;
@ -139,15 +140,22 @@ 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.world.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.world.isBlockLoaded(offset)){
this.placeToPull = this.world.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();
}
}
}
}
@ -155,13 +163,17 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
if(this.sideToPut != -1){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPut);
this.placeToPut = this.world.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.world.isBlockLoaded(offset)){
this.placeToPut = this.world.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();
}
}
}
}

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.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient;
@ -32,7 +31,6 @@ public class TileEntityItemViewer extends TileEntityBase{
private final List<GenericItemHandlerInfo> genericInfos = new ArrayList<GenericItemHandlerInfo>();
private final Map<Integer, SpecificItemHandlerInfo> specificInfos = new HashMap<Integer, SpecificItemHandlerInfo>();
public TileEntityLaserRelayItem connectedRelay;
private Network oldNetwork;
private int lastNetworkChangeAmount = -1;
public TileEntityItemViewer(String name){
@ -45,8 +43,10 @@ public class TileEntityItemViewer extends TileEntityBase{
List<GenericItemHandlerInfo> infos = TileEntityItemViewer.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();
}
}
}
}
@ -56,7 +56,7 @@ public class TileEntityItemViewer extends TileEntityBase{
@Override
public ItemStack getStackInSlot(int slot){
SpecificItemHandlerInfo handler = TileEntityItemViewer.this.getSwitchedIndexHandler(slot);
if(handler != null){
if(handler != null && handler.isLoaded()){
return handler.handler.getStackInSlot(handler.switchedIndex);
}
return StackUtil.getNull();
@ -65,7 +65,7 @@ public class TileEntityItemViewer extends TileEntityBase{
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate){
SpecificItemHandlerInfo info = TileEntityItemViewer.this.getSwitchedIndexHandler(slot);
if(info != null && TileEntityItemViewer.this.isWhitelisted(info, stack, false)){
if(info != null && info.isLoaded() && TileEntityItemViewer.this.isWhitelisted(info, stack, false)){
ItemStack remain = info.handler.insertItem(info.switchedIndex, stack, simulate);
if(!ItemStack.areItemStacksEqual(remain, stack) && !simulate){
TileEntityItemViewer.this.markDirty();
@ -81,7 +81,7 @@ public class TileEntityItemViewer extends TileEntityBase{
ItemStack stackIn = this.getStackInSlot(slot);
if(StackUtil.isValid(stackIn)){
SpecificItemHandlerInfo info = TileEntityItemViewer.this.getSwitchedIndexHandler(slot);
if(info != null && TileEntityItemViewer.this.isWhitelisted(info, stackIn, true)){
if(info != null && info.isLoaded() && TileEntityItemViewer.this.isWhitelisted(info, stackIn, true)){
ItemStack extracted = info.handler.extractItem(info.switchedIndex, amount, simulate);
if(StackUtil.isValid(extracted) && !simulate){
TileEntityItemViewer.this.markDirty();
@ -96,7 +96,7 @@ public class TileEntityItemViewer extends TileEntityBase{
@Override
public int getSlotLimit(int slot){
SpecificItemHandlerInfo info = TileEntityItemViewer.this.getSwitchedIndexHandler(slot);
if(info != null){
if(info != null && info.isLoaded()){
return info.handler.getSlotLimit(info.switchedIndex);
}
else{
@ -139,9 +139,9 @@ public class TileEntityItemViewer extends TileEntityBase{
private void queryAndSaveData(){
if(this.connectedRelay != null){
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.connectedRelay.getPos(), this.world);
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);
@ -158,7 +158,6 @@ public class TileEntityItemViewer extends TileEntityBase{
}
}
}
this.oldNetwork = network;
this.lastNetworkChangeAmount = network.changeAmount;
}
@ -167,6 +166,7 @@ public class TileEntityItemViewer extends TileEntityBase{
}
this.clearInfos();
this.lastNetworkChangeAmount = -1;
}
private void clearInfos(){
@ -196,15 +196,18 @@ public class TileEntityItemViewer extends TileEntityBase{
for(int i = 0; i <= 5; i++){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.world.getTileEntity(pos);
if(tile instanceof TileEntityLaserRelayItem){
if(tileFound != null){
this.connectedRelay = null;
return;
}
else{
tileFound = (TileEntityLaserRelayItem)tile;
if(this.world.isBlockLoaded(pos)){
TileEntity tile = this.world.getTileEntity(pos);
if(tile instanceof TileEntityLaserRelayItem){
if(tileFound != null){
this.connectedRelay = null;
return;
}
else{
tileFound = (TileEntityLaserRelayItem)tile;
}
}
}
}
@ -234,6 +237,10 @@ public class TileEntityItemViewer extends TileEntityBase{
this.switchedIndex = switchedIndex;
this.relayInQuestion = relayInQuestion;
}
public boolean isLoaded(){
return this.relayInQuestion.hasWorld() && this.relayInQuestion.getWorld().isBlockLoaded(this.relayInQuestion.getPos());
}
}
public static class GenericItemHandlerInfo implements Comparable<GenericItemHandlerInfo>{
@ -245,6 +252,10 @@ public class TileEntityItemViewer extends TileEntityBase{
this.relayInQuestion = relayInQuestion;
}
public boolean isLoaded(){
return this.relayInQuestion.hasWorld() && this.relayInQuestion.getWorld().isBlockLoaded(this.relayInQuestion.getPos());
}
@Override
public int compareTo(GenericItemHandlerInfo other){
int thisPrio = this.relayInQuestion.getPriority();

View file

@ -19,6 +19,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
@ -90,23 +91,23 @@ public class TileEntityItemViewerHopping extends TileEntityItemViewer{
public void saveDataOnChangeOrWorldStart(){
super.saveDataOnChangeOrWorldStart();
this.handlerToPullFrom = null;
this.handlerToPushTo = null;
TileEntity from = this.world.getTileEntity(this.pos.offset(EnumFacing.UP));
if(from != null && !(from instanceof TileEntityItemViewer) && from.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN)){
this.handlerToPullFrom = from.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN);
}
else{
this.handlerToPullFrom = null;
}
IBlockState state = this.world.getBlockState(this.pos);
EnumFacing facing = state.getValue(BlockHopper.FACING);
TileEntity to = this.world.getTileEntity(this.pos.offset(facing));
if(to != null && !(to instanceof TileEntityItemViewer) && to.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite())){
this.handlerToPushTo = to.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite());
}
else{
this.handlerToPushTo = null;
BlockPos toPos = this.pos.offset(facing);
if(this.world.isBlockLoaded(toPos)){
TileEntity to = this.world.getTileEntity(toPos);
if(to != null && !(to instanceof TileEntityItemViewer) && to.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite())){
this.handlerToPushTo = to.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite());
}
}
}
}

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.apiimpl.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.world);
if(this.cachedNetwork != null){
this.changeAmountAtCaching = this.cachedNetwork.changeAmount;
}
else{
this.changeAmountAtCaching = -1;
}
}
return this.cachedNetwork;
}
@Override
public void invalidate(){
super.invalidate();

View file

@ -10,7 +10,6 @@
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;
@ -90,7 +89,7 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay{
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.world);
Network network = this.getNetwork();
if(network != null){
transmitted = this.transferEnergyToReceiverInNeed(from, network, maxTransmit, simulate);
}
@ -116,21 +115,23 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay{
this.receiversAround.clear();
for(EnumFacing side : EnumFacing.values()){
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.world.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if((ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite())) || tile.hasCapability(CapabilityEnergy.ENERGY, side.getOpposite())){
this.receiversAround.put(side, tile);
if(this.world.isBlockLoaded(pos)){
TileEntity tile = this.world.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if((ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite())) || tile.hasCapability(CapabilityEnergy.ENERGY, 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++;
}
@ -147,7 +148,7 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay{
for(IConnectionPair pair : network.connections){
for(BlockPos relay : pair.getPositions()){
if(relay != null && !alreadyChecked.contains(relay)){
if(relay != null && this.world.isBlockLoaded(relay) && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
TileEntity relayTile = this.world.getTileEntity(relay);
if(relayTile instanceof TileEntityLaserRelayEnergy){

View file

@ -10,7 +10,6 @@
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;
@ -96,21 +95,23 @@ public class TileEntityLaserRelayFluids extends TileEntityLaserRelay{
this.handlersAround.clear();
for(EnumFacing side : EnumFacing.values()){
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.world.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if(tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite())){
this.handlersAround.put(side, tile);
if(this.world.isBlockLoaded(pos)){
TileEntity tile = this.world.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
if(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++;
}
@ -125,7 +126,7 @@ public class TileEntityLaserRelayFluids extends TileEntityLaserRelay{
private int transmitFluid(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.world);
Network network = this.getNetwork();
if(network != null){
transmitted = this.transferFluidToReceiverInNeed(from, network, stack, doFill);
}
@ -143,7 +144,7 @@ public class TileEntityLaserRelayFluids extends TileEntityLaserRelay{
for(IConnectionPair pair : network.connections){
for(BlockPos relay : pair.getPositions()){
if(relay != null && !alreadyChecked.contains(relay)){
if(relay != null && this.world.isBlockLoaded(relay) && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
TileEntity relayTile = this.world.getTileEntity(relay);
if(relayTile instanceof TileEntityLaserRelayFluids){

View file

@ -10,7 +10,6 @@
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;
@ -69,22 +68,24 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
for(int i = 0; i <= 5; i++){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.world.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityItemViewer) && !(tile instanceof TileEntityLaserRelay)){
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
if(handler != null){
this.handlersAround.put(pos, handler);
if(this.world.isBlockLoaded(pos)){
TileEntity tile = this.world.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityItemViewer) && !(tile instanceof TileEntityLaserRelay)){
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
if(handler != null){
this.handlersAround.put(pos, handler);
IItemHandler oldHandler = old.get(pos);
if(oldHandler == null || !handler.equals(oldHandler)){
change = true;
IItemHandler oldHandler = old.get(pos);
if(oldHandler == null || !handler.equals(oldHandler)){
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++;
}
@ -97,15 +98,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.world.isBlockLoaded(relay) && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
TileEntity aRelayTile = this.world.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());