From 02695aea51bd4e81083e20b478e6746c2cdf9c9a Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Tue, 1 Jun 2021 18:55:12 +0100 Subject: [PATCH] Reduce some block updates - Fix double updateOutput() call in TileComputerBase - I guess a merge/rebase gone wrong in the past. - Don't call updateBlock() when creating a server computer. This used to be needed when we sent the computer to the client, but this is no longer the case. - Don't call updateBlock() on TileMonitors when updating from the client. We don't need to do a redraw here, as this is all stored in the block state now. - Don't update the block when reading turtle upgrades. See #643 for some background here. See #658 --- .../computer/blocks/TileComputerBase.java | 11 ++++------ .../peripheral/monitor/TileMonitor.java | 11 +--------- .../shared/turtle/core/TurtleBrain.java | 21 ++++++++++++++----- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java index fa40d3aab..0be80ba60 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -175,12 +175,12 @@ public void tick() label = computer.getLabel(); on = computer.isOn(); - if( computer.hasOutputChanged() ) updateOutput(); - // Update the block state if needed. We don't fire a block update intentionally, // as this only really is needed on the client side. updateBlockState( computer.getState() ); + // TODO: This should ideally be split up into label/id/on (which should save NBT and sync to client) and + // redstone (which should update outputs) if( computer.hasOutputChanged() ) updateOutput(); } } @@ -376,11 +376,8 @@ public ServerComputer createServerComputer() fresh = true; changed = true; } - if( changed ) - { - updateBlock(); - updateInput(); - } + + if( changed ) updateInput(); return ComputerCraft.serverComputerRegistry.get( instanceID ); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java index 1c0b10075..0fc154e0e 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java @@ -148,7 +148,7 @@ public void load( @Nonnull CompoundNBT tag ) @Override public void blockTick() { - if ( needsUpdate ) + if( needsUpdate ) { needsUpdate = false; updateNeighbors(); @@ -278,8 +278,6 @@ protected final void readDescription( @Nonnull CompoundNBT nbt ) int oldXIndex = xIndex; int oldYIndex = yIndex; - int oldWidth = width; - int oldHeight = height; xIndex = nbt.getInt( NBT_X ); yIndex = nbt.getInt( NBT_Y ); @@ -299,13 +297,6 @@ protected final void readDescription( @Nonnull CompoundNBT nbt ) // If we're the origin terminal then create it. if( clientMonitor == null ) clientMonitor = new ClientMonitor( advanced, this ); } - - if( oldXIndex != xIndex || oldYIndex != yIndex || - oldWidth != width || oldHeight != height ) - { - // One of our properties has changed, so ensure we redraw the block - updateBlock(); - } } public final void read( TerminalState state ) diff --git a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java index 5efd001ea..fb7c5c8d4 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java @@ -160,8 +160,8 @@ private void readCommon( CompoundNBT nbt ) overlay = nbt.contains( NBT_OVERLAY ) ? new ResourceLocation( nbt.getString( NBT_OVERLAY ) ) : null; // Read upgrades - setUpgrade( TurtleSide.LEFT, nbt.contains( NBT_LEFT_UPGRADE ) ? TurtleUpgrades.get( nbt.getString( NBT_LEFT_UPGRADE ) ) : null ); - setUpgrade( TurtleSide.RIGHT, nbt.contains( NBT_RIGHT_UPGRADE ) ? TurtleUpgrades.get( nbt.getString( NBT_RIGHT_UPGRADE ) ) : null ); + setUpgradeDirect( TurtleSide.LEFT, nbt.contains( NBT_LEFT_UPGRADE ) ? TurtleUpgrades.get( nbt.getString( NBT_LEFT_UPGRADE ) ) : null ); + setUpgradeDirect( TurtleSide.RIGHT, nbt.contains( NBT_RIGHT_UPGRADE ) ? TurtleUpgrades.get( nbt.getString( NBT_RIGHT_UPGRADE ) ) : null ); // NBT upgradeNBTData.clear(); @@ -618,16 +618,26 @@ public ITurtleUpgrade getUpgrade( @Nonnull TurtleSide side ) @Override public void setUpgrade( @Nonnull TurtleSide side, ITurtleUpgrade upgrade ) + { + if( !setUpgradeDirect( side, upgrade ) ) return; + + // This is a separate function to avoid updating the block when reading the NBT. We don't need to do this as + // either the block is newly placed (and so won't have changed) or is being updated with /data, which calls + // updateBlock for us. + if( owner.getLevel() != null ) owner.updateBlock(); + } + + private boolean setUpgradeDirect( @Nonnull TurtleSide side, ITurtleUpgrade upgrade ) { // Remove old upgrade if( upgrades.containsKey( side ) ) { - if( upgrades.get( side ) == upgrade ) return; + if( upgrades.get( side ) == upgrade ) return false; upgrades.remove( side ); } else { - if( upgrade == null ) return; + if( upgrade == null ) return false; } upgradeNBTData.remove( side ); @@ -639,8 +649,9 @@ public void setUpgrade( @Nonnull TurtleSide side, ITurtleUpgrade upgrade ) if( owner.getLevel() != null ) { updatePeripherals( owner.createServerComputer() ); - owner.updateBlock(); } + + return true; } @Override