77import com .google .gson .stream .JsonWriter ;
88import org .bukkit .plugin .Plugin ;
99import org .faststats .chart .Chart ;
10+ import org .jetbrains .annotations .TestOnly ;
1011import org .jspecify .annotations .NullMarked ;
1112import org .jspecify .annotations .Nullable ;
1213
1819import java .nio .file .StandardOpenOption ;
1920import java .util .Optional ;
2021import java .util .UUID ;
22+ import java .util .concurrent .TimeUnit ;
2123import java .util .function .Supplier ;
2224import java .util .logging .Level ;
2325
@@ -54,6 +56,30 @@ public BukkitMetrics(Plugin plugin, String token) throws IOException {
5456 startSubmitting ();
5557 }
5658
59+ @ TestOnly
60+ @ SuppressWarnings ("deprecation" )
61+ public BukkitMetrics (Plugin plugin , String token , UUID serverId , boolean enabled , boolean debug ) {
62+ this .serverId = serverId ;
63+ this .enabled = enabled ;
64+ this .debug = debug ;
65+
66+ this .plugin = plugin ;
67+ this .onlineMode = checkOnlineMode ();
68+
69+ this .token = token ;
70+
71+ addChart (Chart .pie ("online_mode" , () -> String .valueOf (onlineMode )));
72+ addChart (Chart .pie ("plugin_version" , () -> plugin .getDescription ().getVersion ()));
73+ addChart (Chart .pie ("server_type" , () -> plugin .getServer ().getName ()));
74+ addChart (Chart .pie ("minecraft_version" , () -> plugin .getServer ().getMinecraftVersion ()));
75+ addChart (Chart .line ("player_count" , () -> plugin .getServer ().getOnlinePlayers ().size ()));
76+ startSubmitting ();
77+ }
78+
79+ protected void startSubmitting () {
80+ startSubmitting (0 , 30 , TimeUnit .MINUTES );
81+ }
82+
5783 private static Optional <JsonObject > readOrCreate (Path path ) throws IOException {
5884 if (Files .isRegularFile (path )) {
5985 return Optional .of (read (path ));
@@ -101,14 +127,6 @@ private boolean isProxyOnlineMode() {
101127 return settings .getBoolean ("bungeecord" ) && proxies .getBoolean ("bungee-cord.online-mode" );
102128 }
103129
104- private <T > Optional <T > tryOrEmpty (Supplier <@ Nullable T > supplier ) {
105- try {
106- return Optional .ofNullable (supplier .get ());
107- } catch (NoSuchMethodError | Exception e ) {
108- return Optional .empty ();
109- }
110- }
111-
112130 @ Override
113131 protected UUID getServerId () {
114132 return serverId ;
@@ -121,16 +139,29 @@ protected boolean isEnabled() {
121139
122140 @ Override
123141 protected void error (String message , Throwable throwable ) {
124- if (debug ) plugin .getLogger ().log (Level .SEVERE , message , throwable );
142+ if (!debug ) return ;
143+ var msg = "[" + BukkitMetrics .class .getName () + "]: " + message ;
144+ plugin .getLogger ().log (Level .SEVERE , msg , throwable );
125145 }
126146
127147 @ Override
128148 protected void debug (String message ) {
129- if (debug ) plugin .getLogger ().log (Level .INFO , message );
149+ if (!debug ) return ;
150+ var msg = "[" + BukkitMetrics .class .getName () + "]: " + message ;
151+ plugin .getLogger ().log (Level .INFO , msg );
130152 }
131153
132154 @ Override
133155 public String getToken () {
134156 return token ;
135157 }
158+
159+ private <T > Optional <T > tryOrEmpty (Supplier <@ Nullable T > supplier ) {
160+ try {
161+ return Optional .ofNullable (supplier .get ());
162+ } catch (NoSuchMethodError | Exception e ) {
163+ error ("Failed to call supplier" , e );
164+ return Optional .empty ();
165+ }
166+ }
136167}
0 commit comments