mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-06 07:20:30 +00:00
Remove NetworkMessage.fromBytes
Always build packets using a PacketBuffer constructor instead.
This commit is contained in:
parent
a735f23e1f
commit
2227845658
@ -43,17 +43,17 @@ public final class NetworkHandler
|
||||
.simpleChannel();
|
||||
|
||||
// Server messages
|
||||
registerMainThread( 0, NetworkDirection.PLAY_TO_SERVER, ComputerActionServerMessage::new );
|
||||
registerMainThread( 1, NetworkDirection.PLAY_TO_SERVER, QueueEventServerMessage::new );
|
||||
registerMainThread( 2, NetworkDirection.PLAY_TO_SERVER, RequestComputerMessage::new );
|
||||
registerMainThread( 3, NetworkDirection.PLAY_TO_SERVER, KeyEventServerMessage::new );
|
||||
registerMainThread( 4, NetworkDirection.PLAY_TO_SERVER, MouseEventServerMessage::new );
|
||||
registerMainThread( 0, NetworkDirection.PLAY_TO_SERVER, ComputerActionServerMessage.class, ComputerActionServerMessage::new );
|
||||
registerMainThread( 1, NetworkDirection.PLAY_TO_SERVER, QueueEventServerMessage.class, QueueEventServerMessage::new );
|
||||
registerMainThread( 2, NetworkDirection.PLAY_TO_SERVER, RequestComputerMessage.class, RequestComputerMessage::new );
|
||||
registerMainThread( 3, NetworkDirection.PLAY_TO_SERVER, KeyEventServerMessage.class, KeyEventServerMessage::new );
|
||||
registerMainThread( 4, NetworkDirection.PLAY_TO_SERVER, MouseEventServerMessage.class, MouseEventServerMessage::new );
|
||||
|
||||
// Client messages
|
||||
registerMainThread( 10, NetworkDirection.PLAY_TO_CLIENT, ChatTableClientMessage::new );
|
||||
registerMainThread( 11, NetworkDirection.PLAY_TO_CLIENT, ComputerDataClientMessage::new );
|
||||
registerMainThread( 12, NetworkDirection.PLAY_TO_CLIENT, ComputerDeletedClientMessage::new );
|
||||
registerMainThread( 13, NetworkDirection.PLAY_TO_CLIENT, ComputerTerminalClientMessage::new );
|
||||
registerMainThread( 10, NetworkDirection.PLAY_TO_CLIENT, ChatTableClientMessage.class, ChatTableClientMessage::new );
|
||||
registerMainThread( 11, NetworkDirection.PLAY_TO_CLIENT, ComputerDataClientMessage.class, ComputerDataClientMessage::new );
|
||||
registerMainThread( 12, NetworkDirection.PLAY_TO_CLIENT, ComputerDeletedClientMessage.class, ComputerDeletedClientMessage::new );
|
||||
registerMainThread( 13, NetworkDirection.PLAY_TO_CLIENT, ComputerTerminalClientMessage.class, ComputerTerminalClientMessage::new );
|
||||
registerMainThread( 14, NetworkDirection.PLAY_TO_CLIENT, PlayRecordClientMessage.class, PlayRecordClientMessage::new );
|
||||
registerMainThread( 15, NetworkDirection.PLAY_TO_CLIENT, MonitorClientMessage.class, MonitorClientMessage::new );
|
||||
registerMainThread( 16, NetworkDirection.PLAY_TO_CLIENT, SpeakerPlayClientMessage.class, SpeakerPlayClientMessage::new );
|
||||
@ -90,24 +90,6 @@ public final class NetworkHandler
|
||||
network.send( PacketDistributor.TRACKING_CHUNK.with( () -> chunk ), packet );
|
||||
}
|
||||
|
||||
/**
|
||||
* /**
|
||||
* Register packet, and a thread-unsafe handler for it.
|
||||
*
|
||||
* @param <T> The type of the packet to send.
|
||||
* @param id The identifier for this packet type.
|
||||
* @param direction A network direction which will be asserted before any processing of this message occurs.
|
||||
* @param factory The factory for this type of packet.
|
||||
*/
|
||||
private static <T extends NetworkMessage> void registerMainThread( int id, NetworkDirection direction, Supplier<T> factory )
|
||||
{
|
||||
registerMainThread( id, direction, getType( factory ), buf -> {
|
||||
T instance = factory.get();
|
||||
instance.fromBytes( buf );
|
||||
return instance;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* /**
|
||||
* Register packet, and a thread-unsafe handler for it.
|
||||
|
@ -27,18 +27,6 @@ public interface NetworkMessage
|
||||
*/
|
||||
void toBytes( @Nonnull PacketBuffer buf );
|
||||
|
||||
/**
|
||||
* Read this packet from a buffer.
|
||||
*
|
||||
* This may be called on any thread, so this should be a pure operation.
|
||||
*
|
||||
* @param buf The buffer to read data from.
|
||||
*/
|
||||
default void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
throw new IllegalStateException( "Should have been registered using a \"from bytes\" method" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle this {@link NetworkMessage}.
|
||||
*
|
||||
|
@ -16,7 +16,7 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public class ChatTableClientMessage implements NetworkMessage
|
||||
{
|
||||
private TableBuilder table;
|
||||
private final TableBuilder table;
|
||||
|
||||
public ChatTableClientMessage( TableBuilder table )
|
||||
{
|
||||
@ -24,32 +24,7 @@ public class ChatTableClientMessage implements NetworkMessage
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
public ChatTableClientMessage()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeVarInt( table.getId() );
|
||||
buf.writeVarInt( table.getColumns() );
|
||||
buf.writeBoolean( table.getHeaders() != null );
|
||||
if( table.getHeaders() != null )
|
||||
{
|
||||
for( ITextComponent header : table.getHeaders() ) buf.writeComponent( header );
|
||||
}
|
||||
|
||||
buf.writeVarInt( table.getRows().size() );
|
||||
for( ITextComponent[] row : table.getRows() )
|
||||
{
|
||||
for( ITextComponent column : row ) buf.writeComponent( column );
|
||||
}
|
||||
|
||||
buf.writeVarInt( table.getAdditional() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
public ChatTableClientMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
int id = buf.readVarInt();
|
||||
int columns = buf.readVarInt();
|
||||
@ -77,6 +52,26 @@ public class ChatTableClientMessage implements NetworkMessage
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
buf.writeVarInt( table.getId() );
|
||||
buf.writeVarInt( table.getColumns() );
|
||||
buf.writeBoolean( table.getHeaders() != null );
|
||||
if( table.getHeaders() != null )
|
||||
{
|
||||
for( ITextComponent header : table.getHeaders() ) buf.writeComponent( header );
|
||||
}
|
||||
|
||||
buf.writeVarInt( table.getRows().size() );
|
||||
for( ITextComponent[] row : table.getRows() )
|
||||
{
|
||||
for( ITextComponent column : row ) buf.writeComponent( column );
|
||||
}
|
||||
|
||||
buf.writeVarInt( table.getAdditional() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
@ -17,15 +17,16 @@ import javax.annotation.Nonnull;
|
||||
*/
|
||||
public abstract class ComputerClientMessage implements NetworkMessage
|
||||
{
|
||||
private int instanceId;
|
||||
private final int instanceId;
|
||||
|
||||
public ComputerClientMessage( int instanceId )
|
||||
{
|
||||
this.instanceId = instanceId;
|
||||
}
|
||||
|
||||
public ComputerClientMessage()
|
||||
public ComputerClientMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
public int getInstanceId()
|
||||
@ -39,12 +40,6 @@ public abstract class ComputerClientMessage implements NetworkMessage
|
||||
buf.writeVarInt( instanceId );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
public ClientComputer getComputer()
|
||||
{
|
||||
ClientComputer computer = ComputerCraft.clientComputerRegistry.get( instanceId );
|
||||
|
@ -18,8 +18,8 @@ import javax.annotation.Nonnull;
|
||||
*/
|
||||
public class ComputerDataClientMessage extends ComputerClientMessage
|
||||
{
|
||||
private ComputerState state;
|
||||
private CompoundNBT userData;
|
||||
private final ComputerState state;
|
||||
private final CompoundNBT userData;
|
||||
|
||||
public ComputerDataClientMessage( ServerComputer computer )
|
||||
{
|
||||
@ -28,8 +28,11 @@ public class ComputerDataClientMessage extends ComputerClientMessage
|
||||
userData = computer.getUserData();
|
||||
}
|
||||
|
||||
public ComputerDataClientMessage()
|
||||
public ComputerDataClientMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
state = buf.readEnum( ComputerState.class );
|
||||
userData = buf.readNbt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -40,14 +43,6 @@ public class ComputerDataClientMessage extends ComputerClientMessage
|
||||
buf.writeNbt( userData );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
state = buf.readEnum( ComputerState.class );
|
||||
userData = buf.readNbt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
@ -6,6 +6,7 @@
|
||||
package dan200.computercraft.shared.network.client;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
|
||||
public class ComputerDeletedClientMessage extends ComputerClientMessage
|
||||
@ -15,8 +16,9 @@ public class ComputerDeletedClientMessage extends ComputerClientMessage
|
||||
super( instanceId );
|
||||
}
|
||||
|
||||
public ComputerDeletedClientMessage()
|
||||
public ComputerDeletedClientMessage( PacketBuffer buffer )
|
||||
{
|
||||
super( buffer );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -12,7 +12,7 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public class ComputerTerminalClientMessage extends ComputerClientMessage
|
||||
{
|
||||
private TerminalState state;
|
||||
private final TerminalState state;
|
||||
|
||||
public ComputerTerminalClientMessage( int instanceId, TerminalState state )
|
||||
{
|
||||
@ -20,8 +20,10 @@ public class ComputerTerminalClientMessage extends ComputerClientMessage
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public ComputerTerminalClientMessage()
|
||||
public ComputerTerminalClientMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
state = new TerminalState( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -31,13 +33,6 @@ public class ComputerTerminalClientMessage extends ComputerClientMessage
|
||||
state.write( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
state = new TerminalState( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
@ -13,7 +13,7 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
{
|
||||
private Action action;
|
||||
private final Action action;
|
||||
|
||||
public ComputerActionServerMessage( int instanceId, Action action )
|
||||
{
|
||||
@ -21,8 +21,10 @@ public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public ComputerActionServerMessage()
|
||||
public ComputerActionServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
action = buf.readEnum( Action.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -32,13 +34,6 @@ public class ComputerActionServerMessage extends ComputerServerMessage
|
||||
buf.writeEnum( action );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
action = buf.readEnum( Action.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
|
@ -22,15 +22,16 @@ import javax.annotation.Nonnull;
|
||||
*/
|
||||
public abstract class ComputerServerMessage implements NetworkMessage
|
||||
{
|
||||
private int instanceId;
|
||||
private final int instanceId;
|
||||
|
||||
public ComputerServerMessage( int instanceId )
|
||||
{
|
||||
this.instanceId = instanceId;
|
||||
}
|
||||
|
||||
public ComputerServerMessage()
|
||||
public ComputerServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -39,12 +40,6 @@ public abstract class ComputerServerMessage implements NetworkMessage
|
||||
buf.writeVarInt( instanceId );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instanceId = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
@ -18,8 +18,8 @@ public class KeyEventServerMessage extends ComputerServerMessage
|
||||
public static final int TYPE_REPEAT = 1;
|
||||
public static final int TYPE_UP = 2;
|
||||
|
||||
private int type;
|
||||
private int key;
|
||||
private final int type;
|
||||
private final int key;
|
||||
|
||||
public KeyEventServerMessage( int instanceId, int type, int key )
|
||||
{
|
||||
@ -28,8 +28,11 @@ public class KeyEventServerMessage extends ComputerServerMessage
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public KeyEventServerMessage()
|
||||
public KeyEventServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
type = buf.readByte();
|
||||
key = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -40,14 +43,6 @@ public class KeyEventServerMessage extends ComputerServerMessage
|
||||
buf.writeVarInt( key );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
type = buf.readByte();
|
||||
key = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
|
@ -19,10 +19,10 @@ public class MouseEventServerMessage extends ComputerServerMessage
|
||||
public static final int TYPE_UP = 2;
|
||||
public static final int TYPE_SCROLL = 3;
|
||||
|
||||
private int type;
|
||||
private int x;
|
||||
private int y;
|
||||
private int arg;
|
||||
private final int type;
|
||||
private final int x;
|
||||
private final int y;
|
||||
private final int arg;
|
||||
|
||||
public MouseEventServerMessage( int instanceId, int type, int arg, int x, int y )
|
||||
{
|
||||
@ -33,8 +33,13 @@ public class MouseEventServerMessage extends ComputerServerMessage
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public MouseEventServerMessage()
|
||||
public MouseEventServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
type = buf.readByte();
|
||||
arg = buf.readVarInt();
|
||||
x = buf.readVarInt();
|
||||
y = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -47,16 +52,6 @@ public class MouseEventServerMessage extends ComputerServerMessage
|
||||
buf.writeVarInt( y );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
type = buf.readByte();
|
||||
arg = buf.readVarInt();
|
||||
x = buf.readVarInt();
|
||||
y = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
|
@ -22,8 +22,8 @@ import javax.annotation.Nullable;
|
||||
*/
|
||||
public class QueueEventServerMessage extends ComputerServerMessage
|
||||
{
|
||||
private String event;
|
||||
private Object[] args;
|
||||
private final String event;
|
||||
private final Object[] args;
|
||||
|
||||
public QueueEventServerMessage( int instanceId, @Nonnull String event, @Nullable Object[] args )
|
||||
{
|
||||
@ -32,8 +32,13 @@ public class QueueEventServerMessage extends ComputerServerMessage
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public QueueEventServerMessage()
|
||||
public QueueEventServerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super( buf );
|
||||
event = buf.readUtf( Short.MAX_VALUE );
|
||||
|
||||
CompoundNBT args = buf.readNbt();
|
||||
this.args = args == null ? null : NBTUtil.decodeObjects( args );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -44,16 +49,6 @@ public class QueueEventServerMessage extends ComputerServerMessage
|
||||
buf.writeNbt( args == null ? null : NBTUtil.encodeObjects( args ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
super.fromBytes( buf );
|
||||
event = buf.readUtf( Short.MAX_VALUE );
|
||||
|
||||
CompoundNBT args = buf.readNbt();
|
||||
this.args = args == null ? null : NBTUtil.decodeObjects( args );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle( @Nonnull ServerComputer computer, @Nonnull IContainerComputer container )
|
||||
{
|
||||
|
@ -15,15 +15,16 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public class RequestComputerMessage implements NetworkMessage
|
||||
{
|
||||
private int instance;
|
||||
private final int instance;
|
||||
|
||||
public RequestComputerMessage( int instance )
|
||||
{
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
public RequestComputerMessage()
|
||||
public RequestComputerMessage( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instance = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -32,12 +33,6 @@ public class RequestComputerMessage implements NetworkMessage
|
||||
buf.writeVarInt( instance );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes( @Nonnull PacketBuffer buf )
|
||||
{
|
||||
instance = buf.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle( NetworkEvent.Context context )
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user