Skip to content

Commit fdf3638

Browse files
committed
Add new bStats graphs
1 parent b240e88 commit fdf3638

File tree

4 files changed

+111
-9
lines changed

4 files changed

+111
-9
lines changed

item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/MinecraftVersion.java

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
import java.util.logging.Logger;
77

88
import org.bstats.bukkit.Metrics;
9+
import org.bstats.charts.DrilldownPie;
10+
import org.bstats.charts.SimplePie;
911
import org.bukkit.Bukkit;
1012
import org.bukkit.plugin.Plugin;
1113
import org.bukkit.plugin.java.JavaPlugin;
1214

15+
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper;
16+
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod;
17+
1318
/**
1419
* This class acts as the "Brain" of the NBTApi. It contains the main logger for
1520
* other classes,registers bStats and checks rather Maven shading was done
@@ -30,6 +35,8 @@ public enum MinecraftVersion {
3035
private static MinecraftVersion version;
3136
private static Boolean hasGsonSupport;
3237
private static Boolean isForgePresent;
38+
private static Boolean isNeoForgePresent;
39+
private static Boolean isFabricPresent;
3340
private static Boolean isFoliaPresent;
3441
private static boolean bStatsDisabled = false;
3542
private static boolean disablePackageWarning = false;
@@ -163,13 +170,67 @@ public static String getNBTAPIVersion() {
163170
}
164171

165172
private static void init() {
173+
// Maven's Relocate is clever and changes strings, too. So we have to use this
174+
// little "trick" ... :D (from bStats)
175+
final String defaultPackage = new String(new byte[] { 'd', 'e', '.', 't', 'r', '7', 'z', 'w', '.', 'c', 'h',
176+
'a', 'n', 'g', 'e', 'm', 'e', '.', 'n', 'b', 't', 'a', 'p', 'i', '.', 'u', 't', 'i', 'l', 's' });
177+
final String reservedPackage = new String(new byte[] { 'd', 'e', '.', 't', 'r', '7', 'z', 'w', '.', 'n', 'b',
178+
't', 'a', 'p', 'i', '.', 'u', 't', 'i', 'l', 's' });
166179
try {
167180
if (hasGsonSupport() && !bStatsDisabled) {
168181
Plugin plugin = Bukkit.getPluginManager().getPlugin(VersionChecker.getPlugin());
169182
if (plugin != null && plugin instanceof JavaPlugin) {
170183
getLogger()
171184
.info("[NBTAPI] Using the plugin '" + plugin.getName() + "' to create a bStats instance!");
172-
new Metrics((JavaPlugin) plugin, 1058);
185+
Metrics metrics = new Metrics((JavaPlugin) plugin, 1058);
186+
metrics.addCustomChart(new SimplePie("nbtapi_version", () -> {
187+
return VERSION;
188+
}));
189+
metrics.addCustomChart(new DrilldownPie("nms_version", () -> {
190+
Map<String, Map<String, Integer>> map = new HashMap<>();
191+
Map<String, Integer> entry = new HashMap<>();
192+
entry.put(Bukkit.getName(), 1);
193+
map.put(getVersion().name(), entry);
194+
return map;
195+
}));
196+
metrics.addCustomChart(new SimplePie("shaded", () -> {
197+
return Boolean.toString(!"NBTAPI".equals(VersionChecker.getPlugin()));
198+
}));
199+
metrics.addCustomChart(new SimplePie("server_software", () -> {
200+
return Bukkit.getName();
201+
}));
202+
metrics.addCustomChart(new SimplePie("parent_plugin", () -> {
203+
return VersionChecker.getPluginforBStats();
204+
}));
205+
metrics.addCustomChart(new SimplePie("special_environment", () -> {
206+
if (isFoliaPresent()) {
207+
return "Folia";
208+
} else if (isForgePresent()) {
209+
return "Forge";
210+
} else if (isFabricPresent()) {
211+
return "Fabric";
212+
} else if (isNeoForgePresent()) {
213+
return "NeoForge";
214+
} else {
215+
return "None";
216+
}
217+
}));
218+
metrics.addCustomChart(new SimplePie("bindings_check", () -> {
219+
220+
boolean failedBinding = false;
221+
for (ClassWrapper c : ClassWrapper.values()) {
222+
if (c.isEnabled() && c.getClazz() == null) {
223+
failedBinding = true;
224+
}
225+
}
226+
for (ReflectionMethod method : ReflectionMethod.values()) {
227+
if (method.isCompatible() && !method.isLoaded()) {
228+
failedBinding = true;
229+
}
230+
}
231+
232+
return failedBinding ? "Failed" : "Pass";
233+
}));
173234
} else if (plugin == null) {
174235
getLogger().info("[NBTAPI] Unable to create a bStats instance!!");
175236
}
@@ -186,12 +247,6 @@ private static void init() {
186247
logger.log(Level.WARNING, "[NBTAPI] Error while checking for updates! Error: " + ex.getMessage());
187248
}
188249
}).start();
189-
// Maven's Relocate is clever and changes strings, too. So we have to use this
190-
// little "trick" ... :D (from bStats)
191-
final String defaultPackage = new String(new byte[] { 'd', 'e', '.', 't', 'r', '7', 'z', 'w', '.', 'c', 'h',
192-
'a', 'n', 'g', 'e', 'm', 'e', '.', 'n', 'b', 't', 'a', 'p', 'i', '.', 'u', 't', 'i', 'l', 's' });
193-
final String reservedPackage = new String(new byte[] { 'd', 'e', '.', 't', 'r', '7', 'z', 'w', '.', 'n', 'b',
194-
't', 'a', 'p', 'i', '.', 'u', 't', 'i', 'l', 's' });
195250
if (!disablePackageWarning && MinecraftVersion.class.getPackage().getName().equals(defaultPackage)) {
196251
logger.warning(
197252
"#########################################- NBTAPI -#########################################");
@@ -251,6 +306,22 @@ public static boolean hasGsonSupport() {
251306
return hasGsonSupport;
252307
}
253308

309+
/**
310+
* @return True, if Fabric is present
311+
*/
312+
public static boolean isFabricPresent() {
313+
if (isFabricPresent != null) {
314+
return isFabricPresent;
315+
}
316+
try {
317+
logger.info("[NBTAPI] Found Fabric: " + Class.forName("net.fabricmc.api.ModInitializer"));
318+
isFabricPresent = true;
319+
} catch (Exception ex) {
320+
isFabricPresent = false;
321+
}
322+
return isFabricPresent;
323+
}
324+
254325
/**
255326
* @return True, if Forge is present
256327
*/
@@ -268,6 +339,22 @@ public static boolean isForgePresent() {
268339
}
269340
return isForgePresent;
270341
}
342+
343+
/**
344+
* @return True, if NeoForge is present
345+
*/
346+
public static boolean isNeoForgePresent() {
347+
if (isNeoForgePresent != null) {
348+
return isNeoForgePresent;
349+
}
350+
try {
351+
logger.info("[NBTAPI] Found NeoForge: " + Class.forName("net.neoforged.neoforge.common.NeoForge"));
352+
isNeoForgePresent = true;
353+
} catch (Exception ex) {
354+
isNeoForgePresent = false;
355+
}
356+
return isNeoForgePresent;
357+
}
271358

272359
/**
273360
* @return True, if Folia is present

item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/VersionChecker.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,20 @@ protected static String getPlugin() {
121121
}
122122
return NBTItem.class.getPackage().getName();
123123
}
124+
125+
protected static String getPluginforBStats() {
126+
ClassLoader classLoader = VersionChecker.class.getClassLoader();
127+
InputStream inputStream = classLoader.getResourceAsStream("plugin.yml");
128+
129+
if (inputStream != null) {
130+
try (InputStreamReader reader = new InputStreamReader(inputStream)) {
131+
YamlConfiguration pluginYml = YamlConfiguration.loadConfiguration(reader);
132+
return pluginYml.getString("name");
133+
} catch (IOException e) {
134+
// ignored
135+
}
136+
}
137+
return "UnknownPlugin";
138+
}
124139

125140
}

wiki/Using-Gradle.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ To start using NB-API, you either need to depend on its plugin version, or shade
22

33
> [!IMPORTANT]
44
> Plugin and shaded versions have different ``artifactId``. Make sure to correctly choose the one you need!
5-
5+
>
66
> [!IMPORTANT]
77
> Alternative ways of loading the api like Libby are not supported on Discord/Github issues.
88

wiki/Using-Maven.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ To start using NB-API, you either need to depend on its plugin version, or shade
22

33
> [!IMPORTANT]
44
> Plugin and shaded versions have different ``artifactId``. Make sure to correctly choose the one you need!
5-
5+
>
66
> [!IMPORTANT]
77
> Alternative ways of loading the api like Libby are not supported on Discord/Github issues.
88

0 commit comments

Comments
 (0)