From 3929dba4a53558e6ac3bed29bda0d3ca7d7257d2 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Tue, 30 Nov 2021 22:01:09 +0000 Subject: [PATCH] Only send update packets on the TEs which need it More bits of #658 --- .../shared/common/TileGeneric.java | 37 ++++--------------- .../computer/blocks/TileComputerBase.java | 18 +++++++-- .../peripheral/monitor/TileMonitor.java | 20 +++++++--- .../shared/turtle/blocks/TileTurtle.java | 10 +++-- 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/common/TileGeneric.java b/src/main/java/dan200/computercraft/shared/common/TileGeneric.java index 0654044a0..e593f64e7 100644 --- a/src/main/java/dan200/computercraft/shared/common/TileGeneric.java +++ b/src/main/java/dan200/computercraft/shared/common/TileGeneric.java @@ -73,42 +73,19 @@ public boolean isUsable( PlayerEntity player, boolean ignoreRange ) player.distanceToSqr( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 ) <= range * range; } - protected void writeDescription( @Nonnull CompoundNBT nbt ) - { - } - - protected void readDescription( @Nonnull CompoundNBT nbt ) - { - } - - @Nonnull - @Override - public final SUpdateTileEntityPacket getUpdatePacket() - { - CompoundNBT nbt = new CompoundNBT(); - writeDescription( nbt ); - return new SUpdateTileEntityPacket( worldPosition, 0, nbt ); - } - @Override public final void onDataPacket( NetworkManager net, SUpdateTileEntityPacket packet ) { - if( packet.getType() == 0 ) readDescription( packet.getTag() ); - } - - @Nonnull - @Override - public CompoundNBT getUpdateTag() - { - CompoundNBT tag = super.getUpdateTag(); - writeDescription( tag ); - return tag; + if( packet.getType() == 0 ) handleUpdateTag( packet.getTag() ); } @Override - public void handleUpdateTag( @Nonnull BlockState state, @Nonnull CompoundNBT tag ) + public final void handleUpdateTag( @Nonnull BlockState state, @Nonnull CompoundNBT tag ) + { + handleUpdateTag( tag ); + } + + protected void handleUpdateTag( @Nonnull CompoundNBT tag ) { - super.handleUpdateTag( state, tag ); - readDescription( tag ); } } 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 1a2a9f07f..ed7b7566f 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -24,6 +24,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.play.server.SUpdateTileEntityPacket; import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.ActionResultType; @@ -387,18 +388,27 @@ public ServerComputer getServerComputer() // Networking stuff + @Nonnull @Override - protected void writeDescription( @Nonnull CompoundNBT nbt ) + public final SUpdateTileEntityPacket getUpdatePacket() { - super.writeDescription( nbt ); + return new SUpdateTileEntityPacket( worldPosition, 0, getUpdateTag() ); + } + + @Nonnull + @Override + public CompoundNBT getUpdateTag() + { + // We need this for pick block on the client side. + CompoundNBT nbt = super.getUpdateTag(); if( label != null ) nbt.putString( NBT_LABEL, label ); if( computerID >= 0 ) nbt.putInt( NBT_ID, computerID ); + return nbt; } @Override - protected void readDescription( @Nonnull CompoundNBT nbt ) + public void handleUpdateTag( @Nonnull CompoundNBT nbt ) { - super.readDescription( nbt ); label = nbt.contains( NBT_LABEL ) ? nbt.getString( NBT_LABEL ) : null; computerID = nbt.contains( NBT_ID ) ? nbt.getInt( NBT_ID ) : -1; } 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 358669640..d3a65ddc2 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java @@ -17,6 +17,7 @@ import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.play.server.SUpdateTileEntityPacket; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.ActionResultType; @@ -236,7 +237,6 @@ private ServerMonitor createServerMonitor() { // Otherwise fetch the origin and attempt to get its monitor // Note this may load chunks, but we don't really have a choice here. - BlockPos pos = getBlockPos(); TileEntity te = level.getBlockEntity( toWorldPos( 0, 0 ) ); if( !(te instanceof TileMonitor) ) return null; @@ -249,7 +249,6 @@ public ClientMonitor getClientMonitor() { if( clientMonitor != null ) return clientMonitor; - BlockPos pos = getBlockPos(); TileEntity te = level.getBlockEntity( toWorldPos( 0, 0 ) ); if( !(te instanceof TileMonitor) ) return null; @@ -258,20 +257,29 @@ public ClientMonitor getClientMonitor() // Networking stuff + @Nonnull @Override - protected void writeDescription( @Nonnull CompoundNBT nbt ) + public final SUpdateTileEntityPacket getUpdatePacket() { - super.writeDescription( nbt ); + return new SUpdateTileEntityPacket( worldPosition, 0, getUpdateTag() ); + } + + @Nonnull + @Override + public final CompoundNBT getUpdateTag() + { + CompoundNBT nbt = super.getUpdateTag(); nbt.putInt( NBT_X, xIndex ); nbt.putInt( NBT_Y, yIndex ); nbt.putInt( NBT_WIDTH, width ); nbt.putInt( NBT_HEIGHT, height ); + return nbt; } @Override - protected final void readDescription( @Nonnull CompoundNBT nbt ) + public final void handleUpdateTag( @Nonnull CompoundNBT nbt ) { - super.readDescription( nbt ); + super.handleUpdateTag( nbt ); int oldXIndex = xIndex; int oldYIndex = yIndex; diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java index 7b0ce1994..de8f29559 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java @@ -495,17 +495,19 @@ public void onTileEntityChange() // Networking stuff + @Nonnull @Override - protected void writeDescription( @Nonnull CompoundNBT nbt ) + public CompoundNBT getUpdateTag() { - super.writeDescription( nbt ); + CompoundNBT nbt = super.getUpdateTag(); brain.writeDescription( nbt ); + return nbt; } @Override - protected void readDescription( @Nonnull CompoundNBT nbt ) + public void handleUpdateTag( @Nonnull CompoundNBT nbt ) { - super.readDescription( nbt ); + super.handleUpdateTag( nbt ); brain.readDescription( nbt ); }