#3104: Use lambdas rather than reflection to create packets

This commit is contained in:
Janmm14 2021-06-11 16:53:31 +10:00 committed by md_5
parent 088b2045d0
commit 00982f3620
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11

View File

@ -6,7 +6,7 @@ import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.TObjectIntMap; import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TIntObjectHashMap; import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.map.hash.TObjectIntHashMap; import gnu.trove.map.hash.TObjectIntHashMap;
import java.lang.reflect.Constructor; import java.util.function.Supplier;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
import net.md_5.bungee.protocol.packet.BossBar; import net.md_5.bungee.protocol.packet.BossBar;
@ -55,6 +55,7 @@ public enum Protocol
{ {
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
Handshake.class, Handshake.class,
Handshake::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x00 ) map( ProtocolConstants.MINECRAFT_1_8, 0x00 )
); );
} }
@ -66,6 +67,7 @@ public enum Protocol
{ {
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
KeepAlive.class, KeepAlive.class,
KeepAlive::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x00 ), map( ProtocolConstants.MINECRAFT_1_8, 0x00 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x1F ), map( ProtocolConstants.MINECRAFT_1_9, 0x1F ),
map( ProtocolConstants.MINECRAFT_1_13, 0x21 ), map( ProtocolConstants.MINECRAFT_1_13, 0x21 ),
@ -77,6 +79,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Login.class, Login.class,
Login::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x01 ), map( ProtocolConstants.MINECRAFT_1_8, 0x01 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x23 ), map( ProtocolConstants.MINECRAFT_1_9, 0x23 ),
map( ProtocolConstants.MINECRAFT_1_13, 0x25 ), map( ProtocolConstants.MINECRAFT_1_13, 0x25 ),
@ -87,6 +90,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Chat.class, Chat.class,
Chat::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x02 ), map( ProtocolConstants.MINECRAFT_1_8, 0x02 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x0F ), map( ProtocolConstants.MINECRAFT_1_9, 0x0F ),
map( ProtocolConstants.MINECRAFT_1_13, 0x0E ), map( ProtocolConstants.MINECRAFT_1_13, 0x0E ),
@ -96,6 +100,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Respawn.class, Respawn.class,
Respawn::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x07 ), map( ProtocolConstants.MINECRAFT_1_8, 0x07 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x33 ), map( ProtocolConstants.MINECRAFT_1_9, 0x33 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x34 ), map( ProtocolConstants.MINECRAFT_1_12, 0x34 ),
@ -109,6 +114,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
BossBar.class, BossBar.class,
BossBar::new,
map( ProtocolConstants.MINECRAFT_1_9, 0x0C ), map( ProtocolConstants.MINECRAFT_1_9, 0x0C ),
map( ProtocolConstants.MINECRAFT_1_15, 0x0D ), map( ProtocolConstants.MINECRAFT_1_15, 0x0D ),
map( ProtocolConstants.MINECRAFT_1_16, 0x0C ), map( ProtocolConstants.MINECRAFT_1_16, 0x0C ),
@ -116,6 +122,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
PlayerListItem.class, // PlayerInfo PlayerListItem.class, // PlayerInfo
PlayerListItem::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x38 ), map( ProtocolConstants.MINECRAFT_1_8, 0x38 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x2D ), map( ProtocolConstants.MINECRAFT_1_9, 0x2D ),
map( ProtocolConstants.MINECRAFT_1_12_1, 0x2E ), map( ProtocolConstants.MINECRAFT_1_12_1, 0x2E ),
@ -128,6 +135,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
TabCompleteResponse.class, TabCompleteResponse.class,
TabCompleteResponse::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x3A ), map( ProtocolConstants.MINECRAFT_1_8, 0x3A ),
map( ProtocolConstants.MINECRAFT_1_9, 0x0E ), map( ProtocolConstants.MINECRAFT_1_9, 0x0E ),
map( ProtocolConstants.MINECRAFT_1_13, 0x10 ), map( ProtocolConstants.MINECRAFT_1_13, 0x10 ),
@ -138,6 +146,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
ScoreboardObjective.class, ScoreboardObjective.class,
ScoreboardObjective::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x3B ), map( ProtocolConstants.MINECRAFT_1_8, 0x3B ),
map( ProtocolConstants.MINECRAFT_1_9, 0x3F ), map( ProtocolConstants.MINECRAFT_1_9, 0x3F ),
map( ProtocolConstants.MINECRAFT_1_12, 0x41 ), map( ProtocolConstants.MINECRAFT_1_12, 0x41 ),
@ -149,6 +158,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
ScoreboardScore.class, ScoreboardScore.class,
ScoreboardScore::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x3C ), map( ProtocolConstants.MINECRAFT_1_8, 0x3C ),
map( ProtocolConstants.MINECRAFT_1_9, 0x42 ), map( ProtocolConstants.MINECRAFT_1_9, 0x42 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x44 ), map( ProtocolConstants.MINECRAFT_1_12, 0x44 ),
@ -160,6 +170,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
ScoreboardDisplay.class, ScoreboardDisplay.class,
ScoreboardDisplay::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x3D ), map( ProtocolConstants.MINECRAFT_1_8, 0x3D ),
map( ProtocolConstants.MINECRAFT_1_9, 0x38 ), map( ProtocolConstants.MINECRAFT_1_9, 0x38 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x3A ), map( ProtocolConstants.MINECRAFT_1_12, 0x3A ),
@ -171,6 +182,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Team.class, Team.class,
Team::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x3E ), map( ProtocolConstants.MINECRAFT_1_8, 0x3E ),
map( ProtocolConstants.MINECRAFT_1_9, 0x41 ), map( ProtocolConstants.MINECRAFT_1_9, 0x41 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x43 ), map( ProtocolConstants.MINECRAFT_1_12, 0x43 ),
@ -182,6 +194,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
PluginMessage.class, PluginMessage.class,
PluginMessage::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x3F ), map( ProtocolConstants.MINECRAFT_1_8, 0x3F ),
map( ProtocolConstants.MINECRAFT_1_9, 0x18 ), map( ProtocolConstants.MINECRAFT_1_9, 0x18 ),
map( ProtocolConstants.MINECRAFT_1_13, 0x19 ), map( ProtocolConstants.MINECRAFT_1_13, 0x19 ),
@ -193,6 +206,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Kick.class, Kick.class,
Kick::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x40 ), map( ProtocolConstants.MINECRAFT_1_8, 0x40 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x1A ), map( ProtocolConstants.MINECRAFT_1_9, 0x1A ),
map( ProtocolConstants.MINECRAFT_1_13, 0x1B ), map( ProtocolConstants.MINECRAFT_1_13, 0x1B ),
@ -204,6 +218,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Title.class, Title.class,
Title::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x45 ), map( ProtocolConstants.MINECRAFT_1_8, 0x45 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x47 ), map( ProtocolConstants.MINECRAFT_1_12, 0x47 ),
map( ProtocolConstants.MINECRAFT_1_12_1, 0x48 ), map( ProtocolConstants.MINECRAFT_1_12_1, 0x48 ),
@ -213,18 +228,24 @@ public enum Protocol
map( ProtocolConstants.MINECRAFT_1_16, 0x4F ), map( ProtocolConstants.MINECRAFT_1_16, 0x4F ),
map( ProtocolConstants.MINECRAFT_1_17, 0x59 ) map( ProtocolConstants.MINECRAFT_1_17, 0x59 )
); );
TO_CLIENT.registerPacket( ClearTitles.class, TO_CLIENT.registerPacket(
ClearTitles.class,
ClearTitles::new,
map( ProtocolConstants.MINECRAFT_1_17, 0x10 ) map( ProtocolConstants.MINECRAFT_1_17, 0x10 )
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Subtitle.class, Subtitle.class,
Subtitle::new,
map( ProtocolConstants.MINECRAFT_1_17, 0x57 ) map( ProtocolConstants.MINECRAFT_1_17, 0x57 )
); );
TO_CLIENT.registerPacket( TitleTimes.class, TO_CLIENT.registerPacket(
TitleTimes.class,
TitleTimes::new,
map( ProtocolConstants.MINECRAFT_1_17, 0x5A ) map( ProtocolConstants.MINECRAFT_1_17, 0x5A )
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
PlayerListHeaderFooter.class, PlayerListHeaderFooter.class,
PlayerListHeaderFooter::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x47 ), map( ProtocolConstants.MINECRAFT_1_8, 0x47 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x48 ), map( ProtocolConstants.MINECRAFT_1_9, 0x48 ),
map( ProtocolConstants.MINECRAFT_1_9_4, 0x47 ), map( ProtocolConstants.MINECRAFT_1_9_4, 0x47 ),
@ -238,6 +259,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
EntityStatus.class, EntityStatus.class,
EntityStatus::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x1A ), map( ProtocolConstants.MINECRAFT_1_8, 0x1A ),
map( ProtocolConstants.MINECRAFT_1_9, 0x1B ), map( ProtocolConstants.MINECRAFT_1_9, 0x1B ),
map( ProtocolConstants.MINECRAFT_1_13, 0x1C ), map( ProtocolConstants.MINECRAFT_1_13, 0x1C ),
@ -249,6 +271,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Commands.class, Commands.class,
Commands::new,
map( ProtocolConstants.MINECRAFT_1_13, 0x11 ), map( ProtocolConstants.MINECRAFT_1_13, 0x11 ),
map( ProtocolConstants.MINECRAFT_1_15, 0x12 ), map( ProtocolConstants.MINECRAFT_1_15, 0x12 ),
map( ProtocolConstants.MINECRAFT_1_16, 0x11 ), map( ProtocolConstants.MINECRAFT_1_16, 0x11 ),
@ -257,6 +280,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
GameState.class, GameState.class,
GameState::new,
map( ProtocolConstants.MINECRAFT_1_15, 0x1F ), map( ProtocolConstants.MINECRAFT_1_15, 0x1F ),
map( ProtocolConstants.MINECRAFT_1_16, 0x1E ), map( ProtocolConstants.MINECRAFT_1_16, 0x1E ),
map( ProtocolConstants.MINECRAFT_1_16_2, 0x1D ), map( ProtocolConstants.MINECRAFT_1_16_2, 0x1D ),
@ -264,6 +288,7 @@ public enum Protocol
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
ViewDistance.class, ViewDistance.class,
ViewDistance::new,
map( ProtocolConstants.MINECRAFT_1_14, 0x41 ), map( ProtocolConstants.MINECRAFT_1_14, 0x41 ),
map( ProtocolConstants.MINECRAFT_1_15, 0x42 ), map( ProtocolConstants.MINECRAFT_1_15, 0x42 ),
map( ProtocolConstants.MINECRAFT_1_16, 0x41 ), map( ProtocolConstants.MINECRAFT_1_16, 0x41 ),
@ -272,6 +297,7 @@ public enum Protocol
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
KeepAlive.class, KeepAlive.class,
KeepAlive::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x00 ), map( ProtocolConstants.MINECRAFT_1_8, 0x00 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x0B ), map( ProtocolConstants.MINECRAFT_1_9, 0x0B ),
map( ProtocolConstants.MINECRAFT_1_12, 0x0C ), map( ProtocolConstants.MINECRAFT_1_12, 0x0C ),
@ -283,6 +309,7 @@ public enum Protocol
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
Chat.class, Chat.class,
Chat::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x01 ), map( ProtocolConstants.MINECRAFT_1_8, 0x01 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x02 ), map( ProtocolConstants.MINECRAFT_1_9, 0x02 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x03 ), map( ProtocolConstants.MINECRAFT_1_12, 0x03 ),
@ -291,6 +318,7 @@ public enum Protocol
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
TabCompleteRequest.class, TabCompleteRequest.class,
TabCompleteRequest::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x14 ), map( ProtocolConstants.MINECRAFT_1_8, 0x14 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x01 ), map( ProtocolConstants.MINECRAFT_1_9, 0x01 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x02 ), map( ProtocolConstants.MINECRAFT_1_12, 0x02 ),
@ -300,6 +328,7 @@ public enum Protocol
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
ClientSettings.class, ClientSettings.class,
ClientSettings::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x15 ), map( ProtocolConstants.MINECRAFT_1_8, 0x15 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x04 ), map( ProtocolConstants.MINECRAFT_1_9, 0x04 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x05 ), map( ProtocolConstants.MINECRAFT_1_12, 0x05 ),
@ -308,6 +337,7 @@ public enum Protocol
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
PluginMessage.class, PluginMessage.class,
PluginMessage::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x17 ), map( ProtocolConstants.MINECRAFT_1_8, 0x17 ),
map( ProtocolConstants.MINECRAFT_1_9, 0x09 ), map( ProtocolConstants.MINECRAFT_1_9, 0x09 ),
map( ProtocolConstants.MINECRAFT_1_12, 0x0A ), map( ProtocolConstants.MINECRAFT_1_12, 0x0A ),
@ -325,19 +355,23 @@ public enum Protocol
{ {
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
StatusResponse.class, StatusResponse.class,
StatusResponse::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x00 ) map( ProtocolConstants.MINECRAFT_1_8, 0x00 )
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
PingPacket.class, PingPacket.class,
PingPacket::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x01 ) map( ProtocolConstants.MINECRAFT_1_8, 0x01 )
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
StatusRequest.class, StatusRequest.class,
StatusRequest::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x00 ) map( ProtocolConstants.MINECRAFT_1_8, 0x00 )
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
PingPacket.class, PingPacket.class,
PingPacket::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x01 ) map( ProtocolConstants.MINECRAFT_1_8, 0x01 )
); );
} }
@ -349,35 +383,43 @@ public enum Protocol
{ {
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
Kick.class, Kick.class,
Kick::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x00 ) map( ProtocolConstants.MINECRAFT_1_8, 0x00 )
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
EncryptionRequest.class, EncryptionRequest.class,
EncryptionRequest::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x01 ) map( ProtocolConstants.MINECRAFT_1_8, 0x01 )
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
LoginSuccess.class, LoginSuccess.class,
LoginSuccess::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x02 ) map( ProtocolConstants.MINECRAFT_1_8, 0x02 )
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
SetCompression.class, SetCompression.class,
SetCompression::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x03 ) map( ProtocolConstants.MINECRAFT_1_8, 0x03 )
); );
TO_CLIENT.registerPacket( TO_CLIENT.registerPacket(
LoginPayloadRequest.class, LoginPayloadRequest.class,
LoginPayloadRequest::new,
map( ProtocolConstants.MINECRAFT_1_13, 0x04 ) map( ProtocolConstants.MINECRAFT_1_13, 0x04 )
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
LoginRequest.class, LoginRequest.class,
LoginRequest::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x00 ) map( ProtocolConstants.MINECRAFT_1_8, 0x00 )
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
EncryptionResponse.class, EncryptionResponse.class,
EncryptionResponse::new,
map( ProtocolConstants.MINECRAFT_1_8, 0x01 ) map( ProtocolConstants.MINECRAFT_1_8, 0x01 )
); );
TO_SERVER.registerPacket( TO_SERVER.registerPacket(
LoginPayloadResponse.class, LoginPayloadResponse.class,
LoginPayloadResponse::new,
map( ProtocolConstants.MINECRAFT_1_13, 0x02 ) map( ProtocolConstants.MINECRAFT_1_13, 0x02 )
); );
} }
@ -428,7 +470,8 @@ public enum Protocol
private final int protocolVersion; private final int protocolVersion;
private final TObjectIntMap<Class<? extends DefinedPacket>> packetMap = new TObjectIntHashMap<>( MAX_PACKET_ID ); private final TObjectIntMap<Class<? extends DefinedPacket>> packetMap = new TObjectIntHashMap<>( MAX_PACKET_ID );
private final Constructor<? extends DefinedPacket>[] packetConstructors = new Constructor[ MAX_PACKET_ID ]; @SuppressWarnings("unchecked")
private final Supplier<? extends DefinedPacket>[] packetConstructors = new Supplier[ MAX_PACKET_ID ];
} }
@Data @Data
@ -484,25 +527,15 @@ public enum Protocol
} }
if ( id > MAX_PACKET_ID ) if ( id > MAX_PACKET_ID )
{ {
throw new BadPacketException( "Packet with id " + id + " outside of range " ); throw new BadPacketException( "Packet with id " + id + " outside of range" );
} }
Constructor<? extends DefinedPacket> constructor = protocolData.packetConstructors[id]; Supplier<? extends DefinedPacket> constructor = protocolData.packetConstructors[id];
try return ( constructor == null ) ? null : constructor.get();
{
return ( constructor == null ) ? null : constructor.newInstance();
} catch ( ReflectiveOperationException ex )
{
throw new BadPacketException( "Could not construct packet with id " + id, ex );
}
} }
private void registerPacket(Class<? extends DefinedPacket> packetClass, ProtocolMapping... mappings) private void registerPacket(Class<? extends DefinedPacket> packetClass, Supplier<? extends DefinedPacket> constructor, ProtocolMapping... mappings)
{ {
try
{
Constructor<? extends DefinedPacket> constructor = packetClass.getDeclaredConstructor();
int mappingIndex = 0; int mappingIndex = 0;
ProtocolMapping mapping = mappings[mappingIndex]; ProtocolMapping mapping = mappings[mappingIndex];
for ( int protocol : ProtocolConstants.SUPPORTED_VERSION_IDS ) for ( int protocol : ProtocolConstants.SUPPORTED_VERSION_IDS )
@ -530,10 +563,6 @@ public enum Protocol
data.packetMap.put( packetClass, mapping.packetID ); data.packetMap.put( packetClass, mapping.packetID );
data.packetConstructors[mapping.packetID] = constructor; data.packetConstructors[mapping.packetID] = constructor;
} }
} catch ( NoSuchMethodException ex )
{
throw new BadPacketException( "No NoArgsConstructor for packet class " + packetClass );
}
} }
final int getId(Class<? extends DefinedPacket> packet, int version) final int getId(Class<? extends DefinedPacket> packet, int version)