32
32
import net .md_5 .bungee .protocol .ProtocolConstants ;
33
33
import org .checkerframework .checker .nullness .qual .NonNull ;
34
34
import org .checkerframework .checker .nullness .qual .Nullable ;
35
- import org .geysermc .geyser .api .util .PlatformType ;
36
35
import org .geysermc .geyser .GeyserBootstrap ;
37
36
import org .geysermc .geyser .GeyserImpl ;
38
37
import org .geysermc .geyser .api .command .Command ;
39
38
import org .geysermc .geyser .api .extension .Extension ;
39
+ import org .geysermc .geyser .api .util .PlatformType ;
40
40
import org .geysermc .geyser .command .GeyserCommandManager ;
41
41
import org .geysermc .geyser .configuration .GeyserConfiguration ;
42
42
import org .geysermc .geyser .dump .BootstrapDumpInfo ;
@@ -70,11 +70,13 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap {
70
70
71
71
private GeyserImpl geyser ;
72
72
73
- private static boolean INITIALIZED = false ;
74
-
75
- @ SuppressWarnings ({"JavaReflectionMemberAccess" , "ResultOfMethodCallIgnored" })
76
73
@ Override
77
74
public void onLoad () {
75
+ onGeyserInitialize ();
76
+ }
77
+
78
+ @ Override
79
+ public void onGeyserInitialize () {
78
80
GeyserLocale .init (this );
79
81
80
82
// Copied from ViaVersion.
@@ -91,68 +93,31 @@ public void onLoad() {
91
93
getLogger ().warning ("/_____________\\ " );
92
94
}
93
95
94
- if (!getDataFolder ().exists ())
95
- getDataFolder ().mkdir ();
96
-
97
- try {
98
- if (!getDataFolder ().exists ())
99
- getDataFolder ().mkdir ();
100
- File configFile = FileUtils .fileOrCopiedFromResource (new File (getDataFolder (), "config.yml" ),
101
- "config.yml" , (x ) -> x .replaceAll ("generateduuid" , UUID .randomUUID ().toString ()), this );
102
- this .geyserConfig = FileUtils .loadConfig (configFile , GeyserBungeeConfiguration .class );
103
- } catch (IOException ex ) {
104
- getLogger ().log (Level .SEVERE , GeyserLocale .getLocaleStringLog ("geyser.config.failed" ), ex );
105
- ex .printStackTrace ();
96
+ if (!this .loadConfig ()) {
106
97
return ;
107
98
}
108
-
109
99
this .geyserLogger = new GeyserBungeeLogger (getLogger (), geyserConfig .isDebugMode ());
110
100
GeyserConfiguration .checkGeyserConfiguration (geyserConfig , geyserLogger );
111
-
112
101
this .geyser = GeyserImpl .load (PlatformType .BUNGEECORD , this );
102
+ this .geyserInjector = new GeyserBungeeInjector (this );
113
103
}
114
104
115
105
@ Override
116
106
public void onEnable () {
117
-
118
- // Force-disable query if enabled, or else Geyser won't enable
119
- for (ListenerInfo info : getProxy ().getConfig ().getListeners ()) {
120
- if (info .isQueryEnabled () && info .getQueryPort () == geyserConfig .getBedrock ().port ()) {
121
- try {
122
- Field queryField = ListenerInfo .class .getDeclaredField ("queryEnabled" );
123
- queryField .setAccessible (true );
124
- queryField .setBoolean (info , false );
125
- geyserLogger .warning ("We force-disabled query on port " + info .getQueryPort () + " in order for Geyser to boot up successfully. " +
126
- "To remove this message, disable query in your proxy's config." );
127
- } catch (NoSuchFieldException | IllegalAccessException e ) {
128
- geyserLogger .warning ("Could not force-disable query. Geyser may not start correctly!" );
129
- if (geyserLogger .isDebug ()) {
130
- e .printStackTrace ();
131
- }
132
- }
133
- }
134
- }
135
-
136
107
// Big hack - Bungee does not provide us an event to listen to, so schedule a repeating
137
108
// task that waits for a field to be filled which is set after the plugin enable
138
109
// process is complete
139
- if (!INITIALIZED ) {
140
- this .awaitStartupCompletion (0 );
141
- } else {
142
- // No need to "wait" for startup completion, just start Geyser - we're reloading.
143
- this .postStartup ();
144
- }
110
+ this .awaitStartupCompletion (0 );
145
111
}
146
112
147
113
@ SuppressWarnings ("unchecked" )
148
114
private void awaitStartupCompletion (int tries ) {
149
- // After 20 tries give up waiting. This will happen
150
- // just after 3 minutes approximately
115
+ // After 20 tries give up waiting. This will happen just after 3 minutes approximately
151
116
if (tries >= 20 ) {
152
117
this .geyserLogger .warning ("BungeeCord plugin startup is taking abnormally long, so Geyser is starting now. " +
153
118
"If all your plugins are loaded properly, this is a bug! " +
154
119
"If not, consider cutting down the amount of plugins on your proxy as it is causing abnormally slow starting times." );
155
- this .postStartup ();
120
+ this .onGeyserEnable ();
156
121
return ;
157
122
}
158
123
@@ -162,7 +127,7 @@ private void awaitStartupCompletion(int tries) {
162
127
163
128
Collection <Channel > listeners = (Collection <Channel >) listenersField .get (BungeeCord .getInstance ());
164
129
if (listeners .isEmpty ()) {
165
- this .getProxy ().getScheduler ().schedule (this , this ::postStartup , tries , TimeUnit .SECONDS );
130
+ this .getProxy ().getScheduler ().schedule (this , this ::onGeyserEnable , tries , TimeUnit .SECONDS );
166
131
} else {
167
132
this .awaitStartupCompletion (++tries );
168
133
}
@@ -171,16 +136,52 @@ private void awaitStartupCompletion(int tries) {
171
136
}
172
137
}
173
138
174
- private void postStartup () {
139
+ public void onGeyserEnable () {
140
+ if (GeyserImpl .getInstance ().isReloading ()) {
141
+ if (!loadConfig ()) {
142
+ return ;
143
+ }
144
+ this .geyserLogger .setDebug (geyserConfig .isDebugMode ());
145
+ GeyserConfiguration .checkGeyserConfiguration (geyserConfig , geyserLogger );
146
+ } else {
147
+ // For consistency with other platforms - create command manager before GeyserImpl#start()
148
+ // This ensures the command events are called before the item/block ones are
149
+ this .geyserCommandManager = new GeyserCommandManager (geyser );
150
+ this .geyserCommandManager .init ();
151
+ }
152
+
153
+ // Force-disable query if enabled, or else Geyser won't enable
154
+ for (ListenerInfo info : getProxy ().getConfig ().getListeners ()) {
155
+ if (info .isQueryEnabled () && info .getQueryPort () == geyserConfig .getBedrock ().port ()) {
156
+ try {
157
+ Field queryField = ListenerInfo .class .getDeclaredField ("queryEnabled" );
158
+ queryField .setAccessible (true );
159
+ queryField .setBoolean (info , false );
160
+ geyserLogger .warning ("We force-disabled query on port " + info .getQueryPort () + " in order for Geyser to boot up successfully. " +
161
+ "To remove this message, disable query in your proxy's config." );
162
+ } catch (NoSuchFieldException | IllegalAccessException e ) {
163
+ geyserLogger .warning ("Could not force-disable query. Geyser may not start correctly!" );
164
+ if (geyserLogger .isDebug ()) {
165
+ e .printStackTrace ();
166
+ }
167
+ }
168
+ }
169
+ }
170
+
175
171
GeyserImpl .start ();
176
172
177
- if (!INITIALIZED ) {
178
- this .geyserInjector = new GeyserBungeeInjector (this );
179
- this .geyserInjector .initializeLocalChannel (this );
173
+ if (geyserConfig .isLegacyPingPassthrough ()) {
174
+ this .geyserBungeePingPassthrough = GeyserLegacyPingPassthrough .init (geyser );
175
+ } else {
176
+ this .geyserBungeePingPassthrough = new GeyserBungeePingPassthrough (getProxy ());
180
177
}
181
178
182
- this .geyserCommandManager = new GeyserCommandManager (geyser );
183
- this .geyserCommandManager .init ();
179
+ // No need to re-register commands or re-init injector when reloading
180
+ if (GeyserImpl .getInstance ().isReloading ()) {
181
+ return ;
182
+ }
183
+
184
+ this .geyserInjector .initializeLocalChannel (this );
184
185
185
186
this .getProxy ().getPluginManager ().registerCommand (this , new GeyserBungeeCommandExecutor ("geyser" , this .geyser , this .geyserCommandManager .getCommands ()));
186
187
for (Map .Entry <Extension , Map <String , Command >> entry : this .geyserCommandManager .extensionCommands ().entrySet ()) {
@@ -191,18 +192,17 @@ private void postStartup() {
191
192
192
193
this .getProxy ().getPluginManager ().registerCommand (this , new GeyserBungeeCommandExecutor (entry .getKey ().description ().id (), this .geyser , commands ));
193
194
}
195
+ }
194
196
195
- if ( geyserConfig . isLegacyPingPassthrough ()) {
196
- this . geyserBungeePingPassthrough = GeyserLegacyPingPassthrough . init ( geyser );
197
- } else {
198
- this . geyserBungeePingPassthrough = new GeyserBungeePingPassthrough ( getProxy () );
197
+ @ Override
198
+ public void onGeyserDisable () {
199
+ if ( geyser != null ) {
200
+ geyser . disable ( );
199
201
}
200
-
201
- INITIALIZED = true ;
202
202
}
203
203
204
204
@ Override
205
- public void onDisable () {
205
+ public void onGeyserShutdown () {
206
206
if (geyser != null ) {
207
207
geyser .shutdown ();
208
208
}
@@ -211,6 +211,11 @@ public void onDisable() {
211
211
}
212
212
}
213
213
214
+ @ Override
215
+ public void onDisable () {
216
+ this .onGeyserShutdown ();
217
+ }
218
+
214
219
@ Override
215
220
public GeyserBungeeConfiguration getGeyserConfig () {
216
221
return geyserConfig ;
@@ -278,4 +283,20 @@ private Optional<InetSocketAddress> findCompatibleListener() {
278
283
.map (info -> (InetSocketAddress ) info .getSocketAddress ())
279
284
.findFirst ();
280
285
}
286
+
287
+ @ SuppressWarnings ("BooleanMethodIsAlwaysInverted" )
288
+ private boolean loadConfig () {
289
+ try {
290
+ if (!getDataFolder ().exists ()) //noinspection ResultOfMethodCallIgnored
291
+ getDataFolder ().mkdir ();
292
+ File configFile = FileUtils .fileOrCopiedFromResource (new File (getDataFolder (), "config.yml" ),
293
+ "config.yml" , (x ) -> x .replaceAll ("generateduuid" , UUID .randomUUID ().toString ()), this );
294
+ this .geyserConfig = FileUtils .loadConfig (configFile , GeyserBungeeConfiguration .class );
295
+ } catch (IOException ex ) {
296
+ getLogger ().log (Level .SEVERE , GeyserLocale .getLocaleStringLog ("geyser.config.failed" ), ex );
297
+ ex .printStackTrace ();
298
+ return false ;
299
+ }
300
+ return true ;
301
+ }
281
302
}
0 commit comments