33import dev .plex .assets .MinecraftAssetsManager ;
44import dev .plex .authentication .AuthenticationManager ;
55import dev .plex .cache .FileCache ;
6- import dev .plex .api .PlexApi ;
76import dev .plex .config .ModuleConfig ;
87import dev .plex .logging .Log ;
98import dev .plex .module .PlexModule ;
3332
3433public class HTTPDModule extends PlexModule
3534{
36- public static ServletContextHandler context ;
35+ @ Getter
36+ private ServletContextHandler context ;
3737 private Thread serverThread ;
38- private AtomicReference <Server > atomicServer = new AtomicReference <>();
38+ private final AtomicReference <Server > atomicServer = new AtomicReference <>();
3939
40- public static ModuleConfig moduleConfig ;
41- private static PlexApi plexApi ;
40+ @ Getter
41+ private ModuleConfig moduleConfig ;
4242
43- public static PlexApi plexApi ()
44- {
45- return plexApi ;
46- }
43+ @ Getter
44+ private final FileCache fileCache = new FileCache ();
45+
46+ @ Getter
47+ private final String template = AbstractServlet .readFileReal (HTTPDModule .class .getResourceAsStream ("/httpd/template.html" ));
48+
49+ @ Getter
50+ private AuthenticationManager authenticationManager ;
4751
48- public static final FileCache fileCache = new FileCache ();
52+ @ Getter
53+ private File accessLogFile ;
4954
50- public static final String template = AbstractServlet .readFileReal (HTTPDModule .class .getResourceAsStream ("/httpd/template.html" ));
55+ @ Getter
56+ private MinecraftAssetsManager minecraftAssetsManager ;
5157
5258 @ Getter
53- private static AuthenticationManager authenticationManager ;
59+ private StatsBroadcaster statsBroadcaster ;
5460
5561 @ Getter
56- private static File accessLogFile ;
62+ private PlayersBroadcaster playersBroadcaster ;
5763
5864 @ Getter
59- private static MinecraftAssetsManager minecraftAssetsManager ;
65+ private PlayerInventoryBroadcaster playerInventoryBroadcaster ;
6066
6167 @ Override
6268 public void load ()
6369 {
64- plexApi = api ();
6570 // Move it from /httpd/config.yml to /plugins/Plex/modules/Plex-HTTPD/config.yml
6671 moduleConfig = new ModuleConfig (this , "httpd/config.yml" , "config.yml" );
6772 }
@@ -70,17 +75,18 @@ public void load()
7075 public void enable ()
7176 {
7277 moduleConfig .load ();
73- HTTPDModule . plexApi ().logging ().debug ("HTTPD Module Port: {0}" , moduleConfig .getInt ("server.port" ));
78+ api ().logging ().debug ("HTTPD Module Port: {0}" , moduleConfig .getInt ("server.port" ));
7479
7580 accessLogFile = new File (getDataFolder (), moduleConfig .getString ("server.logging.file-path" , "httpd.log" ));
81+ Log .configure (moduleConfig , accessLogFile );
7682
77- minecraftAssetsManager = new MinecraftAssetsManager (getDataFolder ().toPath ());
83+ minecraftAssetsManager = new MinecraftAssetsManager (getDataFolder ().toPath (), api () );
7884 minecraftAssetsManager .refreshAsync ();
7985
80- authenticationManager = new AuthenticationManager ();
86+ authenticationManager = new AuthenticationManager (this );
8187 if (authenticationManager .provider () == null )
8288 {
83- HTTPDModule . plexApi ().logging ().debug ("Authentication is disabled or misconfigured" );
89+ api ().logging ().debug ("Authentication is disabled or misconfigured" );
8490 }
8591
8692
@@ -110,33 +116,37 @@ public void enable()
110116 connector .setIdleTimeout (moduleConfig .getLong ("server.limits.idle-timeout-ms" , 15_000L ));
111117 connector .setAcceptQueueSize (moduleConfig .getInt ("server.limits.accept-queue" , 32 ));
112118
113- context .addFilter (new FilterHolder (new RateLimitFilter ()), "/*" , EnumSet .of (DispatcherType .REQUEST ));
114-
115- StatsBroadcaster .get ().start ();
116- PlayersBroadcaster .get ().start ();
117- PlayerInventoryBroadcaster .get ().start ();
118-
119- new IndefBansEndpoint ();
120- new IndexEndpoint ();
121- new ListEndpoint ();
122- new PunishmentsEndpoint ();
123- new CommandsEndpoint ();
124- new SchematicDownloadEndpoint ();
125- new SchematicUploadEndpoint ();
126- new PlayersEndpoint ();
127- new PlayerAdminEndpoint ();
128- new AssetsEndpoint ();
129- new PunishmentsUIEndpoint ();
130- new IndefBansUIEndpoint ();
131- new AuthenticationEndpoint ();
132-
133- HTTPDModule .context .addServlet (StatsStreamServlet .class , "/api/stats/stream" );
134- HTTPDModule .context .addServlet (PlayersStreamServlet .class , "/api/players/stream" );
135- HTTPDModule .context .addServlet (StaffPlayersStreamServlet .class , "/api/players/stream/staff" );
136- HTTPDModule .context .addServlet (PlayerActionServlet .class , "/api/admin/action" );
137- HTTPDModule .context .addServlet (PlayerInventoryStreamServlet .class , "/api/player/inventory/stream" );
138-
139- ServletHolder uploadHolder = HTTPDModule .context .addServlet (SchematicUploadServlet .class , "/api/schematics/uploading" );
119+ context .addFilter (new FilterHolder (new RateLimitFilter (moduleConfig )), "/*" , EnumSet .of (DispatcherType .REQUEST ));
120+
121+ statsBroadcaster = new StatsBroadcaster (this );
122+ playersBroadcaster = new PlayersBroadcaster (this );
123+ playerInventoryBroadcaster = new PlayerInventoryBroadcaster (this );
124+ statsBroadcaster .start ();
125+ playersBroadcaster .start ();
126+ playerInventoryBroadcaster .start ();
127+
128+ new IndefBansEndpoint (this );
129+ new IndexEndpoint (this );
130+ new ListEndpoint (this );
131+ new PunishmentsEndpoint (this );
132+ new CommandsEndpoint (this );
133+ new SchematicDownloadEndpoint (this );
134+ new SchematicUploadEndpoint (this );
135+ new PlayersEndpoint (this );
136+ new PlayerAdminEndpoint (this );
137+ new AssetsEndpoint (this );
138+ new PunishmentsUIEndpoint (this );
139+ new IndefBansUIEndpoint (this );
140+ new AuthenticationEndpoint (this );
141+
142+ context .addServlet (new ServletHolder (new StatsStreamServlet (statsBroadcaster )), "/api/stats/stream" );
143+ context .addServlet (new ServletHolder (new PlayersStreamServlet (playersBroadcaster )), "/api/players/stream" );
144+ context .addServlet (new ServletHolder (new StaffPlayersStreamServlet (this , playersBroadcaster )), "/api/players/stream/staff" );
145+ context .addServlet (new ServletHolder (new PlayerActionServlet (this )), "/api/admin/action" );
146+ context .addServlet (new ServletHolder (new PlayerInventoryStreamServlet (this , playerInventoryBroadcaster )), "/api/player/inventory/stream" );
147+
148+ ServletHolder uploadHolder = new ServletHolder (new SchematicUploadServlet (this ));
149+ context .addServlet (uploadHolder , "/api/schematics/uploading" );
140150
141151 File uploadLoc = new File (System .getProperty ("java.io.tmpdir" ), "schematic-temp-dir" );
142152 if (!uploadLoc .exists ())
@@ -160,41 +170,54 @@ public void enable()
160170 }
161171 }, "Jetty-Server" );
162172 serverThread .start ();
163- HTTPDModule . plexApi ().logging ().info ("Starting Jetty server on port " + moduleConfig .getInt ("server.port" ));
173+ api ().logging ().info ("Starting Jetty server on port " + moduleConfig .getInt ("server.port" ));
164174 }
165175
166176 @ Override
167177 public void disable ()
168178 {
169- HTTPDModule . plexApi ().logging ().debug ("Stopping Jetty server" );
179+ api ().logging ().debug ("Stopping Jetty server" );
170180 try
171181 {
172- StatsBroadcaster .get ().shutdown ();
182+ if (statsBroadcaster != null )
183+ {
184+ statsBroadcaster .shutdown ();
185+ }
173186 }
174187 catch (Throwable t )
175188 {
176189 t .printStackTrace ();
177190 }
178191 try
179192 {
180- PlayersBroadcaster .get ().shutdown ();
193+ if (playersBroadcaster != null )
194+ {
195+ playersBroadcaster .shutdown ();
196+ }
181197 }
182198 catch (Throwable t )
183199 {
184200 t .printStackTrace ();
185201 }
186202 try
187203 {
188- PlayerInventoryBroadcaster .get ().shutdown ();
204+ if (playerInventoryBroadcaster != null )
205+ {
206+ playerInventoryBroadcaster .shutdown ();
207+ }
189208 }
190209 catch (Throwable t )
191210 {
192211 t .printStackTrace ();
193212 }
194213 try
195214 {
196- atomicServer .get ().stop ();
197- atomicServer .get ().destroy ();
215+ Server server = atomicServer .get ();
216+ if (server != null )
217+ {
218+ server .stop ();
219+ server .destroy ();
220+ }
198221 }
199222 catch (Exception e )
200223 {
0 commit comments