77import xyz .webmc .originblacklist .base .events .OriginBlacklistLoginEvent ;
88import xyz .webmc .originblacklist .base .events .OriginBlacklistMOTDEvent ;
99import xyz .webmc .originblacklist .base .http .OriginBlacklistRequestHandler ;
10+ import xyz .webmc .originblacklist .base .metrics .GenericMetricsAdapter ;
1011import xyz .webmc .originblacklist .base .util .BuildInfo ;
1112import xyz .webmc .originblacklist .base .util .IOriginBlacklistPlugin ;
1213import xyz .webmc .originblacklist .base .util .OPlayer ;
1516import java .io .InputStream ;
1617import java .io .OutputStream ;
1718import java .net .HttpURLConnection ;
19+ import java .net .URI ;
1820import java .net .URL ;
1921import java .net .URLDecoder ;
2022import java .nio .charset .StandardCharsets ;
2628import java .time .Instant ;
2729import java .util .ArrayList ;
2830import java .util .Base64 ;
31+ import java .util .HashMap ;
2932import java .util .List ;
33+ import java .util .Map ;
3034import java .util .concurrent .TimeUnit ;
3135
3236import de .marhali .json5 .Json5 ;
3842import net .kyori .adventure .text .Component ;
3943import net .kyori .adventure .text .minimessage .MiniMessage ;
4044import net .kyori .adventure .text .serializer .legacy .LegacyComponentSerializer ;
45+ import net .lax1dude .eaglercraft .backend .server .api .IBasePlayer ;
46+ import net .lax1dude .eaglercraft .backend .server .api .IEaglerPlayer ;
4147import net .lax1dude .eaglercraft .backend .server .api .IEaglerXServerAPI ;
4248import net .lax1dude .eaglercraft .backend .server .api .query .IMOTDConnection ;
49+ import org .bstats .charts .AdvancedPie ;
4350import org .semver4j .Semver ;
4451
4552@ SuppressWarnings ({ "rawtypes" })
@@ -55,13 +62,15 @@ public final class OriginBlacklist {
5562
5663 private final IOriginBlacklistPlugin plugin ;
5764 private final OriginBlacklistConfig config ;
65+ private final GenericMetricsAdapter metrics ;
5866 private final Json5 json5 ;
5967 private String updateURL ;
6068 private Path jarFile ;
6169
6270 public OriginBlacklist (final IOriginBlacklistPlugin plugin ) {
6371 this .plugin = plugin ;
6472 this .config = new OriginBlacklistConfig (this );
73+ this .metrics = plugin .getMetrics ();
6574 this .json5 = Json5 .builder (builder -> builder .prettyPrinting ().indentFactor (0 ).build ());
6675 }
6776
@@ -73,15 +82,34 @@ public final void init() {
7382 if (this .isBlacklistAPIEnabled ()) {
7483 OriginBlacklistRequestHandler .register (this );
7584 }
85+ this .metrics .addCustomChart (new AdvancedPie ("player_types" , () -> {
86+ final Map <String , Integer > playerMap = new HashMap <>();
87+ for (final Object player : this .getEaglerAPI ().getAllPlayers ()) {
88+ if (player instanceof IBasePlayer bPlayer ) {
89+ final String key = (bPlayer instanceof IEaglerPlayer ) ? "Eagler" : "Java" ;
90+ playerMap .put (key , playerMap .getOrDefault (key , 0 ) + 1 );
91+ }
92+ }
93+ return playerMap ;
94+ }));
7695 this .plugin .log (EnumLogLevel .INFO , "Initialized Plugin" );
7796 this .plugin .log (EnumLogLevel .DEBUG , "Commit " + COMMIT_L );
97+ if (this .isMetricsEnabled ()) {
98+ this .metrics .start ();
99+ }
100+ this .plugin .scheduleRepeat (() -> {
101+ this .plugin .log (EnumLogLevel .INFO , String .valueOf (this .isMetricsEnabled ()));
102+ }, 1 , TimeUnit .SECONDS );
78103 }
79104
80105 public final void shutdown () {
81106 this .plugin .log (EnumLogLevel .INFO , "Shutting down..." );
82107 if (this .isBlacklistAPIEnabled ()) {
83108 OriginBlacklistRequestHandler .unRegister (this );
84109 }
110+ if (this .isMetricsEnabled ()) {
111+ this .metrics .shutdown ();
112+ }
85113 this .plugin .shutdown ();
86114 }
87115
@@ -94,6 +122,14 @@ public final void handleReload() {
94122 }
95123 } catch (final Throwable t ) {
96124 }
125+ try {
126+ if (this .isMetricsEnabled ()) {
127+ this .metrics .start ();
128+ } else {
129+ this .metrics .shutdown ();
130+ }
131+ } catch (final Throwable t ) {
132+ }
97133 }
98134
99135 public final void handleLogin (final OriginBlacklistLoginEvent event ) {
@@ -117,7 +153,7 @@ public final void handleLogin(final OriginBlacklistLoginEvent event) {
117153 blacklisted_value , blacklisted .getActionString (), false ), event );
118154 this .sendWebhooks (event , blacklisted );
119155 final String name = player .getName ();
120- if (isNonNull (name )) {
156+ if (isNonNullStr (name )) {
121157 this .plugin .log (EnumLogLevel .INFO , "Prevented blacklisted player " + name + " from joining." );
122158 this .updateLogFile (event , blacklisted );
123159 }
@@ -181,7 +217,8 @@ public final void setEaglerMOTD(final Component comp, final OriginBlacklistMOTDE
181217 }
182218 final List <String > pLst = new ArrayList <>();
183219 for (final Json5Element ln : this .config .getArray ("motd.players.hover" ).getAsJson5Array ()) {
184- pLst .add (getLegacyFromMiniMessage (ln .getAsString ().replaceAll ("%discord_invite%" , this .config .getString ("discord.invite" ))));
220+ pLst .add (getLegacyFromMiniMessage (
221+ ln .getAsString ().replaceAll ("%discord_invite%" , this .config .getString ("discord.invite" ))));
185222 }
186223 conn .setServerMOTD (lst );
187224 conn .setPlayerTotal (this .config .getInteger ("motd.players.online" ));
@@ -197,7 +234,7 @@ public final void checkForUpdates(final Runnable action1, final Runnable action2
197234 this .plugin .runAsync (() -> {
198235 this .updateURL = UpdateChecker .checkForUpdates (PLUGIN_REPO , this .plugin .getPluginVersion (),
199236 this .config .getBoolean ("update_checker.allow_snapshots" ));
200- if (isNonNull ((this .updateURL ))) {
237+ if (isNonNullStr ((this .updateURL ))) {
201238 action1 .run ();
202239 return ;
203240 }
@@ -210,7 +247,7 @@ public final void checkForUpdates(final Runnable action1, final Runnable action2
210247
211248 public final void updatePlugin (final Runnable action1 , final Runnable action2 ) {
212249 try {
213- final URL url = new URL (this .updateURL );
250+ final URL url = ( new URI (this .updateURL )). toURL ( );
214251 final Path jar = this .jarFile ;
215252 final Path bak = jar .resolveSibling (jar .getFileName ().toString () + ".bak" );
216253 final Path upd = jar
@@ -240,8 +277,8 @@ public final void updatePlugin(final Runnable action1, final Runnable action2) {
240277 action1 .run ();
241278 return ;
242279 } catch (final Throwable t ) {
243- t .printStackTrace ();
244280 Files .move (bak , jar , StandardCopyOption .REPLACE_EXISTING );
281+ throw t ;
245282 }
246283 } catch (final Throwable t ) {
247284 t .printStackTrace ();
@@ -264,7 +301,7 @@ public final EnumBlacklistType testBlacklist(final OPlayer player) {
264301 final boolean whitelist = this .config .getBoolean ("blacklist_to_whitelist" );
265302 EnumBlacklistType type = EnumBlacklistType .NONE ;
266303
267- if (isNonNull (origin )) {
304+ if (isNonNullStr (origin )) {
268305 if (whitelist && !type .isBlacklisted ())
269306 type = EnumBlacklistType .ORIGIN ;
270307 for (final Json5Element element : this .config .getArray ("blacklist.origins" ).getAsJson5Array ()) {
@@ -280,7 +317,7 @@ else if (!type.isBlacklisted())
280317 return whitelist ? EnumBlacklistType .NONE : EnumBlacklistType .ORIGIN ;
281318 }
282319
283- if (isNonNull (brand )) {
320+ if (isNonNullStr (brand )) {
284321 if (whitelist && !type .isBlacklisted ())
285322 type = EnumBlacklistType .BRAND ;
286323 for (final Json5Element element : this .config .getArray ("blacklist.brands" )) {
@@ -294,7 +331,7 @@ else if (!type.isBlacklisted())
294331 }
295332 }
296333
297- if (isNonNull (name )) {
334+ if (isNonNullStr (name )) {
298335 if (whitelist && !type .isBlacklisted ())
299336 type = EnumBlacklistType .NAME ;
300337 for (final Json5Element element : this .config .getArray ("blacklist.player_names" )) {
@@ -308,7 +345,7 @@ else if (!type.isBlacklisted())
308345 }
309346 }
310347
311- if (isNonNull (addr )) {
348+ if (isNonNullStr (addr )) {
312349 if (whitelist && !type .isBlacklisted ())
313350 type = EnumBlacklistType .ADDR ;
314351 for (final Json5Element element : this .config .getArray ("blacklist.ip_addresses" )) {
@@ -446,7 +483,7 @@ private final void sendWebhooks(final OriginBlacklistLoginEvent event, final Enu
446483 for (final Json5Element element : arr ) {
447484 this .plugin .runAsync (() -> {
448485 try {
449- final URL url = new URL (element .getAsString ());
486+ final URL url = ( new URI (element .getAsString ())). toURL ( );
450487 final HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
451488 conn .setRequestMethod ("POST" );
452489 conn .setRequestProperty ("Content-Type" , "application/json" );
@@ -519,7 +556,7 @@ public static final String getUserAgent() {
519556 return BuildInfo .get ("plugin_name" ) + "/" + BuildInfo .get ("plugin_vers" ) + "+" + BuildInfo .get ("git_cm_hash" );
520557 }
521558
522- public static final boolean isNonNull (final String str ) {
559+ public static final boolean isNonNullStr (final String str ) {
523560 return str != null && !str .isEmpty () && !str .isBlank () && !str .equals ("null" );
524561 }
525562
0 commit comments