1818import net .minecraft .network .Varint21FrameDecoder ;
1919import net .minecraft .network .Varint21LengthFieldPrepender ;
2020import net .minecraft .server .level .ServerPlayer ;
21- import net .minecraft .server .players .PlayerList ;
2221import net .minecraft .world .entity .player .Player ;
2322import net .neoforged .neoforge .server .ServerLifecycleHooks ;
2423
2524import java .security .SecureRandom ;
26- import java .util .concurrent .CompletableFuture ;
2725import java .util .concurrent .TimeUnit ;
2826import java .util .concurrent .locks .LockSupport ;
2927
3028/**
3129 * @author USS_Shenzhou
3230 */
3331public class SubspaceConnection {
34- private static EventLoopGroup group ;
32+ private static final EventLoopGroup EVENT_LOOP_GROUP = new MultiThreadIoEventLoopGroup ( 1 , new DefaultThreadFactory ( "Channel-Server-Subspace" , true ), NioIoHandler . newFactory ()) ;
3533 private static volatile Channel channel ;
3634 private static volatile boolean activelyDisconnect ;
3735
@@ -52,9 +50,8 @@ public class SubspaceConnection {
5250 */
5351 public static void connect () {
5452 var cfg = ChannelServerConfig .get ();
55- group = new MultiThreadIoEventLoopGroup (1 , new DefaultThreadFactory ("Channel-Server-Subspace" , true ), NioIoHandler .newFactory ());
5653 new Bootstrap ()
57- .group (group )
54+ .group (EVENT_LOOP_GROUP )
5855 .channel (NioSocketChannel .class )
5956 .handler (new ChannelInitializer <SocketChannel >() {
6057 @ Override
@@ -72,23 +69,25 @@ protected void initChannel(SocketChannel ch) {
7269 if (future .isSuccess ()) {
7370 channel = future .channel ();
7471 channel .closeFuture ().addListener ((ChannelFutureListener ) f -> {
75- if (!activelyDisconnect ) {
72+ if (activelyDisconnect ) {
73+ activelyDisconnect = false ;
74+ } else {
7675 LogUtils .getLogger ().warn ("Disconnected from subspace. Reconnecting in 10s..." );
77- group .schedule (SubspaceConnection ::connect , 10 , TimeUnit .SECONDS );
76+ EVENT_LOOP_GROUP .schedule (SubspaceConnection ::connect , 10 , TimeUnit .SECONDS );
7877 channel = null ;
7978 }
8079 });
8180 send (new InitPacket ());
8281 LogUtils .getLogger ().info ("Subspace server connected." );
83- group .schedule (() -> {
82+ EVENT_LOOP_GROUP .schedule (() -> {
8483 LockSupport .parkNanos (TimeUnit .SECONDS .toNanos (3000 ));
8584 if (channel .isActive () && ServerLifecycleHooks .getCurrentServer () != null ) {
8685 ServerLifecycleHooks .getCurrentServer ().getPlayerList ().getPlayers ().forEach (SubspaceConnection ::newPlayer );
8786 }
8887 }, 1 , TimeUnit .SECONDS );
8988 } else {
9089 LogUtils .getLogger ().error ("Failed to connect to subspace server. Try again in 10s..." );
91- group .schedule (SubspaceConnection ::connect , 10 , TimeUnit .SECONDS );
90+ EVENT_LOOP_GROUP .schedule (SubspaceConnection ::connect , 10 , TimeUnit .SECONDS );
9291 channel = null ;
9392 }
9493 });
@@ -108,7 +107,6 @@ public static void changeSubspace() {
108107 if (channel != null ) {
109108 channel .close ();
110109 }
111- activelyDisconnect = false ;
112110 connect ();
113111 }
114112
@@ -117,20 +115,20 @@ public static void shutdown() {
117115 if (channel != null ) {
118116 channel .close ();
119117 }
120- if (group != null ) {
121- group .shutdownGracefully ();
122- }
123- activelyDisconnect = false ;
118+ EVENT_LOOP_GROUP .shutdownGracefully ();
124119 }
125120
126121 public static void newPlayer (Player player ) {
122+ if (!using ()) {
123+ return ;
124+ }
127125 byte [] token = new SecureRandom ().generateSeed (32 );
128126 SubspaceConnection .send (new PlayerLoginPacket (token , player .getUUID (), player .getId ()));
129127 var cfg = ChannelServerConfig .get ();
130128 NetworkHelper .sendToPlayer ((ServerPlayer ) player , new SubspaceInitPacket (token , cfg .subspaceProtocol , cfg .subspaceAddress , cfg .subspaceClientPort , cfg .subspaceSecurityLevel ));
131129 }
132130
133- public static boolean using (){
131+ public static boolean using () {
134132 return channel != null && channel .isActive ();
135133 }
136134}
0 commit comments