From 7093343d7597fd33f426a2275e53f5a31b0fe4da Mon Sep 17 00:00:00 2001 From: LemADEC Date: Sat, 13 Jun 2020 13:06:37 +0200 Subject: [PATCH] Improved force field projector CPU load (less calls to getTileEntity) --- .../warpdrive/block/forcefield/BlockForceField.java | 7 ++++--- .../block/forcefield/TileEntityForceField.java | 10 ++++++++-- .../forcefield/TileEntityForceFieldProjector.java | 10 +++++----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/block/forcefield/BlockForceField.java b/src/main/java/cr0s/warpdrive/block/forcefield/BlockForceField.java index 6aae62b9..18f40c07 100644 --- a/src/main/java/cr0s/warpdrive/block/forcefield/BlockForceField.java +++ b/src/main/java/cr0s/warpdrive/block/forcefield/BlockForceField.java @@ -237,10 +237,11 @@ public class BlockForceField extends BlockAbstractForceField implements IDamageR return !blockAccess.isSideSolid(blockPosSide, opposite, false); } - protected TileEntityForceFieldProjector getProjector(final World world, @Nonnull final BlockPos blockPos) { + protected TileEntityForceFieldProjector getProjector(@Nonnull final World world, @Nonnull final BlockPos blockPos, + @Nullable final TileEntityForceFieldProjector tileEntityForceFieldProjectorCandidate) { final TileEntity tileEntity = world.getTileEntity(blockPos); if (tileEntity instanceof TileEntityForceField) { - return ((TileEntityForceField) tileEntity).getProjector(); + return ((TileEntityForceField) tileEntity).getProjector(tileEntityForceFieldProjectorCandidate); } return null; } @@ -389,7 +390,7 @@ public class BlockForceField extends BlockAbstractForceField implements IDamageR private void downgrade(final World world, final BlockPos blockPos) { if (enumTier.getIndex() > 1) { - final TileEntityForceFieldProjector tileEntityForceFieldProjector = getProjector(world, blockPos); + final TileEntityForceFieldProjector tileEntityForceFieldProjector = getProjector(world, blockPos, null); final IBlockState blockState = world.getBlockState(blockPos); final int frequency = blockState.getBlock() == this ? blockState.getValue(FREQUENCY) : 0; world.setBlockState(blockPos, WarpDrive.blockForceFields[enumTier.getIndex() - 1].getDefaultState().withProperty(FREQUENCY, (frequency + 1) % 16), 2); diff --git a/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceField.java b/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceField.java index b540b7fc..2cd12350 100644 --- a/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceField.java +++ b/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceField.java @@ -134,8 +134,14 @@ public class TileEntityForceField extends TileEntity { world.notifyBlockUpdate(pos, blockState, blockState, 3); } - public TileEntityForceFieldProjector getProjector() { + public TileEntityForceFieldProjector getProjector(@Nullable final TileEntityForceFieldProjector tileEntityForceFieldProjectorCandidate) { if (blockPosProjector != null) { + // test candidate to save a call to getTileEntity() + if ( tileEntityForceFieldProjectorCandidate != null + && blockPosProjector.equals(tileEntityForceFieldProjectorCandidate.getPos()) ) { + return tileEntityForceFieldProjectorCandidate; + } + final TileEntity tileEntity = world.getTileEntity(blockPosProjector); if (tileEntity instanceof TileEntityForceFieldProjector) { final TileEntityForceFieldProjector tileEntityForceFieldProjector = (TileEntityForceFieldProjector) tileEntity; @@ -172,7 +178,7 @@ public class TileEntityForceField extends TileEntity { } public ForceFieldSetup getForceFieldSetup() { - final TileEntityForceFieldProjector tileEntityForceFieldProjector = getProjector(); + final TileEntityForceFieldProjector tileEntityForceFieldProjector = getProjector(null); if (tileEntityForceFieldProjector == null) { return null; } diff --git a/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceFieldProjector.java b/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceFieldProjector.java index 3932d4b2..a1820030 100644 --- a/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceFieldProjector.java +++ b/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceFieldProjector.java @@ -485,8 +485,8 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField blockState = Blocks.AIR.getDefaultState(); } else { - final TileEntityForceField tileEntityForceField = ((TileEntityForceField)tileEntity); - final TileEntityForceFieldProjector tileEntityForceFieldProjector = tileEntityForceField.getProjector(); + final TileEntityForceField tileEntityForceField = ((TileEntityForceField) tileEntity); + final TileEntityForceFieldProjector tileEntityForceFieldProjector = tileEntityForceField.getProjector(this); if (tileEntityForceFieldProjector == null) { // orphan force field, probably from an explosion // => recover it @@ -571,7 +571,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField // recover forcefield blocks from recalculation or chunk loading if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()] && !vForceFields.contains(vector)) { final TileEntity tileEntity = world.getTileEntity(vector.getBlockPos()); - if (tileEntity instanceof TileEntityForceField && (((TileEntityForceField) tileEntity).getProjector() == this)) { + if (tileEntity instanceof TileEntityForceField && (((TileEntityForceField) tileEntity).getProjector(this) == this)) { vForceFields.add(vector); } } @@ -584,7 +584,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField // remove our own force field block if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()]) { assert blockState.getBlock() instanceof BlockForceField; - if (((BlockForceField) blockState.getBlock()).getProjector(world, vector.getBlockPos()) == this) { + if (((BlockForceField) blockState.getBlock()).getProjector(world, vector.getBlockPos(), this) == this) { world.setBlockToAir(vector.getBlockPos()); vForceFields.remove(vector); } @@ -814,7 +814,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()]) { final TileEntity tileEntity = world.getTileEntity(blockPos); if ( tileEntity instanceof TileEntityForceField - && (((TileEntityForceField) tileEntity).getProjector() == this) ) { + && (((TileEntityForceField) tileEntity).getProjector(this) == this) ) { world.setBlockToAir(vector.getBlockPos()); } }