Improved force field projector CPU load (less calls to getTileEntity)

This commit is contained in:
LemADEC 2020-06-13 13:06:37 +02:00
parent b796502b4a
commit 7093343d75
3 changed files with 17 additions and 10 deletions

View file

@ -237,10 +237,11 @@ public class BlockForceField extends BlockAbstractForceField implements IDamageR
return !blockAccess.isSideSolid(blockPosSide, opposite, false); 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); final TileEntity tileEntity = world.getTileEntity(blockPos);
if (tileEntity instanceof TileEntityForceField) { if (tileEntity instanceof TileEntityForceField) {
return ((TileEntityForceField) tileEntity).getProjector(); return ((TileEntityForceField) tileEntity).getProjector(tileEntityForceFieldProjectorCandidate);
} }
return null; return null;
} }
@ -389,7 +390,7 @@ public class BlockForceField extends BlockAbstractForceField implements IDamageR
private void downgrade(final World world, final BlockPos blockPos) { private void downgrade(final World world, final BlockPos blockPos) {
if (enumTier.getIndex() > 1) { if (enumTier.getIndex() > 1) {
final TileEntityForceFieldProjector tileEntityForceFieldProjector = getProjector(world, blockPos); final TileEntityForceFieldProjector tileEntityForceFieldProjector = getProjector(world, blockPos, null);
final IBlockState blockState = world.getBlockState(blockPos); final IBlockState blockState = world.getBlockState(blockPos);
final int frequency = blockState.getBlock() == this ? blockState.getValue(FREQUENCY) : 0; 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); world.setBlockState(blockPos, WarpDrive.blockForceFields[enumTier.getIndex() - 1].getDefaultState().withProperty(FREQUENCY, (frequency + 1) % 16), 2);

View file

@ -134,8 +134,14 @@ public class TileEntityForceField extends TileEntity {
world.notifyBlockUpdate(pos, blockState, blockState, 3); world.notifyBlockUpdate(pos, blockState, blockState, 3);
} }
public TileEntityForceFieldProjector getProjector() { public TileEntityForceFieldProjector getProjector(@Nullable final TileEntityForceFieldProjector tileEntityForceFieldProjectorCandidate) {
if (blockPosProjector != null) { 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); final TileEntity tileEntity = world.getTileEntity(blockPosProjector);
if (tileEntity instanceof TileEntityForceFieldProjector) { if (tileEntity instanceof TileEntityForceFieldProjector) {
final TileEntityForceFieldProjector tileEntityForceFieldProjector = (TileEntityForceFieldProjector) tileEntity; final TileEntityForceFieldProjector tileEntityForceFieldProjector = (TileEntityForceFieldProjector) tileEntity;
@ -172,7 +178,7 @@ public class TileEntityForceField extends TileEntity {
} }
public ForceFieldSetup getForceFieldSetup() { public ForceFieldSetup getForceFieldSetup() {
final TileEntityForceFieldProjector tileEntityForceFieldProjector = getProjector(); final TileEntityForceFieldProjector tileEntityForceFieldProjector = getProjector(null);
if (tileEntityForceFieldProjector == null) { if (tileEntityForceFieldProjector == null) {
return null; return null;
} }

View file

@ -485,8 +485,8 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
blockState = Blocks.AIR.getDefaultState(); blockState = Blocks.AIR.getDefaultState();
} else { } else {
final TileEntityForceField tileEntityForceField = ((TileEntityForceField)tileEntity); final TileEntityForceField tileEntityForceField = ((TileEntityForceField) tileEntity);
final TileEntityForceFieldProjector tileEntityForceFieldProjector = tileEntityForceField.getProjector(); final TileEntityForceFieldProjector tileEntityForceFieldProjector = tileEntityForceField.getProjector(this);
if (tileEntityForceFieldProjector == null) { if (tileEntityForceFieldProjector == null) {
// orphan force field, probably from an explosion // orphan force field, probably from an explosion
// => recover it // => recover it
@ -571,7 +571,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
// recover forcefield blocks from recalculation or chunk loading // recover forcefield blocks from recalculation or chunk loading
if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()] && !vForceFields.contains(vector)) { if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()] && !vForceFields.contains(vector)) {
final TileEntity tileEntity = world.getTileEntity(vector.getBlockPos()); 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); vForceFields.add(vector);
} }
} }
@ -584,7 +584,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
// remove our own force field block // remove our own force field block
if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()]) { if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()]) {
assert blockState.getBlock() instanceof BlockForceField; 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()); world.setBlockToAir(vector.getBlockPos());
vForceFields.remove(vector); vForceFields.remove(vector);
} }
@ -814,7 +814,7 @@ public class TileEntityForceFieldProjector extends TileEntityAbstractForceField
if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()]) { if (blockState.getBlock() == WarpDrive.blockForceFields[enumTier.getIndex()]) {
final TileEntity tileEntity = world.getTileEntity(blockPos); final TileEntity tileEntity = world.getTileEntity(blockPos);
if ( tileEntity instanceof TileEntityForceField if ( tileEntity instanceof TileEntityForceField
&& (((TileEntityForceField) tileEntity).getProjector() == this) ) { && (((TileEntityForceField) tileEntity).getProjector(this) == this) ) {
world.setBlockToAir(vector.getBlockPos()); world.setBlockToAir(vector.getBlockPos());
} }
} }