1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-14 20:20:30 +00:00

Only send update packets on the TEs which need it

More bits of #658
This commit is contained in:
Jonathan Coates 2021-11-30 22:01:09 +00:00
parent 5927e9bb10
commit 3929dba4a5
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
4 changed files with 41 additions and 44 deletions

View File

@ -73,42 +73,19 @@ public abstract class TileGeneric extends TileEntity
player.distanceToSqr( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 ) <= range * range; 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 @Override
public final void onDataPacket( NetworkManager net, SUpdateTileEntityPacket packet ) public final void onDataPacket( NetworkManager net, SUpdateTileEntityPacket packet )
{ {
if( packet.getType() == 0 ) readDescription( packet.getTag() ); if( packet.getType() == 0 ) handleUpdateTag( packet.getTag() );
}
@Nonnull
@Override
public CompoundNBT getUpdateTag()
{
CompoundNBT tag = super.getUpdateTag();
writeDescription( tag );
return tag;
} }
@Override @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 );
} }
} }

View File

@ -24,6 +24,7 @@ import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntityType; import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ActionResultType; import net.minecraft.util.ActionResultType;
@ -387,18 +388,27 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
// Networking stuff // Networking stuff
@Nonnull
@Override @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( label != null ) nbt.putString( NBT_LABEL, label );
if( computerID >= 0 ) nbt.putInt( NBT_ID, computerID ); if( computerID >= 0 ) nbt.putInt( NBT_ID, computerID );
return nbt;
} }
@Override @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; label = nbt.contains( NBT_LABEL ) ? nbt.getString( NBT_LABEL ) : null;
computerID = nbt.contains( NBT_ID ) ? nbt.getInt( NBT_ID ) : -1; computerID = nbt.contains( NBT_ID ) ? nbt.getInt( NBT_ID ) : -1;
} }

View File

@ -17,6 +17,7 @@ import dan200.computercraft.shared.util.TickScheduler;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType; import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ActionResultType; import net.minecraft.util.ActionResultType;
@ -236,7 +237,6 @@ public class TileMonitor extends TileGeneric
{ {
// Otherwise fetch the origin and attempt to get its monitor // Otherwise fetch the origin and attempt to get its monitor
// Note this may load chunks, but we don't really have a choice here. // Note this may load chunks, but we don't really have a choice here.
BlockPos pos = getBlockPos();
TileEntity te = level.getBlockEntity( toWorldPos( 0, 0 ) ); TileEntity te = level.getBlockEntity( toWorldPos( 0, 0 ) );
if( !(te instanceof TileMonitor) ) return null; if( !(te instanceof TileMonitor) ) return null;
@ -249,7 +249,6 @@ public class TileMonitor extends TileGeneric
{ {
if( clientMonitor != null ) return clientMonitor; if( clientMonitor != null ) return clientMonitor;
BlockPos pos = getBlockPos();
TileEntity te = level.getBlockEntity( toWorldPos( 0, 0 ) ); TileEntity te = level.getBlockEntity( toWorldPos( 0, 0 ) );
if( !(te instanceof TileMonitor) ) return null; if( !(te instanceof TileMonitor) ) return null;
@ -258,20 +257,29 @@ public class TileMonitor extends TileGeneric
// Networking stuff // Networking stuff
@Nonnull
@Override @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_X, xIndex );
nbt.putInt( NBT_Y, yIndex ); nbt.putInt( NBT_Y, yIndex );
nbt.putInt( NBT_WIDTH, width ); nbt.putInt( NBT_WIDTH, width );
nbt.putInt( NBT_HEIGHT, height ); nbt.putInt( NBT_HEIGHT, height );
return nbt;
} }
@Override @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 oldXIndex = xIndex;
int oldYIndex = yIndex; int oldYIndex = yIndex;

View File

@ -495,17 +495,19 @@ public class TileTurtle extends TileComputerBase implements ITurtleTile, Default
// Networking stuff // Networking stuff
@Nonnull
@Override @Override
protected void writeDescription( @Nonnull CompoundNBT nbt ) public CompoundNBT getUpdateTag()
{ {
super.writeDescription( nbt ); CompoundNBT nbt = super.getUpdateTag();
brain.writeDescription( nbt ); brain.writeDescription( nbt );
return nbt;
} }
@Override @Override
protected void readDescription( @Nonnull CompoundNBT nbt ) public void handleUpdateTag( @Nonnull CompoundNBT nbt )
{ {
super.readDescription( nbt ); super.handleUpdateTag( nbt );
brain.readDescription( nbt ); brain.readDescription( nbt );
} }