2525import org .itxtech .nemisys .Player ;
2626import org .itxtech .nemisys .Server ;
2727import org .itxtech .nemisys .event .player .PlayerCreationEvent ;
28+ import org .itxtech .nemisys .event .player .PlayerCreationEvent .PlayerFactory ;
2829import org .itxtech .nemisys .event .server .*;
2930import org .itxtech .nemisys .network .protocol .mcpe .*;
3031import org .itxtech .nemisys .utils .Binary ;
3738import javax .crypto .SecretKey ;
3839import javax .security .auth .DestroyFailedException ;
3940import java .io .IOException ;
40- import java .lang .reflect .Constructor ;
41- import java .lang .reflect .InvocationTargetException ;
4241import java .net .InetAddress ;
4342import java .net .InetSocketAddress ;
4443import java .net .URI ;
@@ -109,10 +108,10 @@ public RakNetInterface(Server server, NemisysMetrics metrics) {
109108 InetSocketAddress bindAddress = new InetSocketAddress (Strings .isNullOrEmpty (this .server .getIp ()) ? "0.0.0.0" : this .server .getIp (), this .server .getPort ());
110109
111110 this .raknet = new RakNetServer (bindAddress , Runtime .getRuntime ().availableProcessors ());
112- this .raknet .setMaxConnections (- 1 );
111+ this .raknet .setMaxConnections (0 );
113112// this.customRakNetServer(raknet);
114- this .raknet .bind ().join ();
115113 this .raknet .setListener (this );
114+ this .raknet .bind ().join ();
116115
117116 for (EventExecutor executor : this .raknet .getBootstrap ().config ().group ()) {
118117 this .tickFutures .add (executor .scheduleAtFixedRate (() -> {
@@ -135,17 +134,16 @@ public boolean process() {
135134 session .lastPacketBudgetUpdateTimeNs = System .nanoTime ();
136135
137136 InetSocketAddress address = session .raknet .getAddress ();
138- PlayerCreationEvent ev = new PlayerCreationEvent (this , Player .class , Player .class , -1 , address );
139- this .server .getPluginManager ().callEvent (ev );
140- Class <? extends Player > clazz = ev .getPlayerClass ();
141-
137+ PlayerCreationEvent ev = new PlayerCreationEvent (this , Player ::new , -1 , address );
142138 try {
143- Constructor <? extends Player > constructor = clazz .getConstructor (NetworkSession .class , long .class , InetSocketAddress .class , Compressor .class );
144- Player player = constructor .newInstance (session , ev .getClientId (), ev .getSocketAddress (), session .initCompression .compressor );
139+ this .server .getPluginManager ().callEvent (ev );
140+ PlayerFactory factory = ev .getPlayerFactory ();
141+
142+ Player player = factory .create (session , ev .getClientId (), ev .getSocketAddress (), session .initCompression .compressor );
145143 this .server .addPlayer (address , player );
146144 session .player = player ;
147145 this .sessions .put (address , session );
148- } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e ) {
146+ } catch (Exception e ) {
149147 Server .getInstance ().getLogger ().logException (e );
150148 }
151149 }
@@ -155,9 +153,9 @@ public boolean process() {
155153 NemisysRakNetSession nemisysSession = iterator .next ();
156154 Player player = nemisysSession .player ;
157155
158- String disconnectReason = nemisysSession .disconnectReason ;
159- if (disconnectReason != null ) {
160- player .close (disconnectReason , false );
156+ Disconnection disconnection = nemisysSession .disconnection ;
157+ if (disconnection != null ) {
158+ player .close (disconnection . message , disconnection . reason , false );
161159 iterator .remove ();
162160 continue ;
163161 }
@@ -304,7 +302,7 @@ private class NemisysRakNetSession implements NetworkSession, RakNetSessionListe
304302 private final Queue <DataPacket > outbound = PlatformDependent .newMpscQueue ();
305303 private volatile boolean readable = true ;
306304 @ Nullable
307- private volatile String disconnectReason ;
305+ private volatile Disconnection disconnection ;
308306 private Player player ;
309307
310308 private final Compression initCompression ;
@@ -388,9 +386,9 @@ public void onPreDisconnect(DisconnectReason reason) {
388386 @ Override
389387 public void onDisconnect (DisconnectReason disconnectReason ) {
390388 if (disconnectReason == DisconnectReason .TIMED_OUT ) {
391- this .disconnect ("Timed out" );
389+ this .disconnect ("Timed out" , "disconnectionScreen.timeout" );
392390 } else {
393- this .disconnect ("Disconnected from Server" );
391+ this .disconnect ("Disconnected from Server" , "disconnectionScreen.disconnected" );
394392 }
395393
396394 Encryption encryption = this .encryption ;
@@ -423,7 +421,7 @@ public void onEncapsulated(EncapsulatedPacket packet) {
423421 int size = buffer .readableBytes ();
424422 if (size >= Compressor .MAX_SIZE ) {
425423 this .readable = false ;
426- this .disconnect ("Packet content exceeds maximum size" );
424+ this .disconnect ("Packet content exceeds maximum size" , "disconnect.overflow" );
427425 log .debug ("[{}] Packet size exceeds limit: {}" , raknet .getAddress (), size );
428426 server .getPluginManager ().callEvent (new RakNetExceptionEvent (raknet .getAddress (), "batch_size" ));
429427 return ;
@@ -442,7 +440,7 @@ public void onEncapsulated(EncapsulatedPacket packet) {
442440 decryptCipher .update (inBuffer , outBuffer );
443441 } catch (GeneralSecurityException e ) {
444442 this .readable = false ;
445- this .disconnect ("Bad decrypt" );
443+ this .disconnect ("Bad decrypt" , "disconnectionScreen.unexpectedPacket" );
446444 log .debug ("[{}] Unable to decrypt packet" , raknet .getAddress (), e );
447445 server .getPluginManager ().callEvent (new RakNetExceptionEvent (raknet .getAddress (), "decrypt" ));
448446 return ;
@@ -457,7 +455,7 @@ public void onEncapsulated(EncapsulatedPacket packet) {
457455 buffer .readBytes (checksum );
458456 } catch (Exception e ) {
459457 this .readable = false ;
460- this .disconnect ("Bad checksum" );
458+ this .disconnect ("Bad checksum" , "disconnectionScreen.unexpectedPacket" );
461459 log .debug ("[{}] Unable to verify checksum" , raknet .getAddress (), e );
462460 server .getPluginManager ().callEvent (new RakNetExceptionEvent (raknet .getAddress (), "checksum_corrupt" ));
463461 return ;
@@ -467,7 +465,7 @@ public void onEncapsulated(EncapsulatedPacket packet) {
467465 byte [] expected = calculateChecksum (count , payload , encryption .secretKey );
468466 if (!Arrays .equals (checksum , expected )) {
469467 this .readable = false ;
470- this .disconnect ("Invalid checksum" );
468+ this .disconnect ("Invalid checksum" , "disconnectionScreen.unexpectedPacket" );
471469 log .debug ("[{}] Encrypted packet {} has invalid checksum (expected {}, got {})" , raknet .getAddress (),
472470 count , Binary .bytesToHexString (expected ), Binary .bytesToHexString (checksum ));
473471 server .getPluginManager ().callEvent (new RakNetExceptionEvent (raknet .getAddress (), "checksum_mismatch" ));
@@ -493,7 +491,7 @@ public void onEncapsulated(EncapsulatedPacket packet) {
493491 if (incomingPacketBatchBudget <= 0 ) {
494492 this .readable = false ;
495493 log .warn ("{} receiving packets too fast" , raknet .getAddress ());
496- this .disconnect ("Receiving packets too fast" );
494+ this .disconnect ("Receiving packets too fast" , "disconnectionScreen.connectionLost" );
497495 server .getPluginManager ().callEvent (new RakNetExceptionEvent (raknet .getAddress (), "batch_fast" ));
498496 return ;
499497 }
@@ -509,7 +507,7 @@ public void onEncapsulated(EncapsulatedPacket packet) {
509507 compressionAlgorithm = buffer .readByte ();
510508 if (compressionAlgorithm < CompressionAlgorithm .NONE || compressionAlgorithm > CompressionAlgorithm .SNAPPY ) {
511509 this .readable = false ;
512- this .disconnect ("Unknown compression algorithm" );
510+ this .disconnect ("Unknown compression algorithm" , "disconnectionScreen.unexpectedPacket" );
513511 log .debug ("[{}] Unknown compression algorithm: {}" , raknet .getAddress (), compressionAlgorithm );
514512 server .getPluginManager ().callEvent (new RakNetExceptionEvent (raknet .getAddress (), "decompress" ));
515513 return ;
@@ -567,8 +565,8 @@ public void onDirect(ByteBuf byteBuf) {
567565 // We don't allow any direct packets so ignore.
568566 }
569567
570- private void disconnect (String message ) {
571- this .disconnectReason = message ;
568+ private void disconnect (String message , String reason ) {
569+ this .disconnection = new Disconnection ( message , reason ) ;
572570 RakNetInterface .this .sessionsToTick .get ().remove (this );
573571 }
574572
@@ -764,4 +762,7 @@ private record Encryption(SecretKey secretKey, Cipher encryptCipher, Cipher decr
764762
765763 private record Compression (Compressor compressor , boolean dynamicCompressor ) {
766764 }
765+
766+ private record Disconnection (String message , String reason ) {
767+ }
767768}
0 commit comments