Skip to content

Commit

Permalink
Check method existance before calling it
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamerboy59 authored Feb 14, 2022
1 parent 05e9299 commit b9a7fb7
Showing 1 changed file with 110 additions and 75 deletions.
185 changes: 110 additions & 75 deletions bukkit/Metrics.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* This Metrics class was auto-generated and can be copied into your project if you are
* not using a build tool like Gradle or Maven for dependency management.
*
* IMPORTANT: You are not allowed to modify this class, except changing the package.
*
* Unallowed modifications include but are not limited to:
* - Remove the option for users to opt-out
* - Change the frequency for data submission
* - Obfuscate the code (every obfucator should allow you to make an exception for specific files)
* - Reformat the code (if you use a linter, add an exception)
*
* Violations will result in a ban of your plugin and account from bStats.
*/
package org.bstats.bukkit;

import java.io.BufferedReader;
Expand Down Expand Up @@ -30,7 +44,7 @@
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.FileConfigurationOptions;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
Expand All @@ -51,27 +65,47 @@ public class Metrics {
*/
public Metrics(JavaPlugin plugin, int serviceId) {
this.plugin = plugin;
Method methodSetHeader = null;
try {
methodSetHeader = FileConfigurationOptions.class.getDeclaredMethod("setHeader", new Class[] { String.class });
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the config file
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats");
File configFile = new File(bStatsFolder, "config.yml");
FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
//FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
if (!config.isSet("serverUuid")) {
config.addDefault("enabled", true);
config.addDefault("serverUuid", UUID.randomUUID().toString());
config.addDefault("logFailedRequests", false);
config.addDefault("logSentData", false);
config.addDefault("logResponseStatusText", false);
// Inform the server owners about bStats
List<String> header = new ArrayList<String>();
header.add("bStats (https://bStats.org) collects some basic information for plugin authors, like how");
header.add("many people use their plugin and their total player count. It's recommended to keep bStats");
header.add("enabled, but if you're not comfortable with this, you can turn this setting off. There is no");
header.add("performance penalty associated with having metrics enabled, and data sent to bStats is fully");
header.add("anonymous.");
config
if(methodSetHeader != null) {
List<String> header = new ArrayList<String>();
header.add("bStats (https://bStats.org) collects some basic information for plugin authors, like how");
header.add("many people use their plugin and their total player count. It's recommended to keep bStats");
header.add("enabled, but if you're not comfortable with this, you can turn this setting off. There is no");
header.add("performance penalty associated with having metrics enabled, and data sent to bStats is fully");
header.add("anonymous.");
config
.options()
.setHeader(header)
.copyDefaults(true);
} else {
config
.options()
.header(
"bStats (https://bStats.org) collects some basic information for plugin authors, like how\n"
+ "many people use their plugin and their total player count. It's recommended to keep bStats\n"
+ "enabled, but if you're not comfortable with this, you can turn this setting off. There is no\n"
+ "performance penalty associated with having metrics enabled, and data sent to bStats is fully\n"
+ "anonymous.")
.copyDefaults(true);
}
try {
config.save(configFile);
} catch (IOException ignored) {
Expand Down Expand Up @@ -143,7 +177,7 @@ private int getPlayerAmount() {
public static class MetricsBase {

/** The version of the Metrics class. */
public static final String METRICS_VERSION = "2.2.1";
public static final String METRICS_VERSION = "3.0.0";

private static final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1, task -> new Thread(task, "bStats-Metrics"));
Expand Down Expand Up @@ -228,6 +262,7 @@ public MetricsBase(
this.logResponseStatusText = logResponseStatusText;
checkRelocation();
if (enabled) {
// WARNING: Removing the option to opt-out will get your plugin banned from bStats
startSubmitting();
}
}
Expand Down Expand Up @@ -364,47 +399,51 @@ private static byte[] compress(final String str) throws IOException {
}
}

public static class AdvancedBarChart extends CustomChart {
public static class DrilldownPie extends CustomChart {

private final Callable<Map<String, int[]>> callable;
private final Callable<Map<String, Map<String, Integer>>> callable;

/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public AdvancedBarChart(String chartId, Callable<Map<String, int[]>> callable) {
public DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable) {
super(chartId);
this.callable = callable;
}

@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
public JsonObjectBuilder.JsonObject getChartData() throws Exception {
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
Map<String, int[]> map = callable.call();
Map<String, Map<String, Integer>> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, int[]> entry : map.entrySet()) {
if (entry.getValue().length == 0) {
// Skip this invalid
continue;
boolean reallyAllSkipped = true;
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
JsonObjectBuilder valueBuilder = new JsonObjectBuilder();
boolean allSkipped = true;
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
valueBuilder.appendField(valueEntry.getKey(), valueEntry.getValue());
allSkipped = false;
}
if (!allSkipped) {
reallyAllSkipped = false;
valuesBuilder.appendField(entryValues.getKey(), valueBuilder.build());
}
allSkipped = false;
valuesBuilder.appendField(entry.getKey(), entry.getValue());
}
if (allSkipped) {
if (reallyAllSkipped) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
}

public static class SimpleBarChart extends CustomChart {
public static class AdvancedPie extends CustomChart {

private final Callable<Map<String, Integer>> callable;

Expand All @@ -414,7 +453,7 @@ public static class SimpleBarChart extends CustomChart {
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SimpleBarChart(String chartId, Callable<Map<String, Integer>> callable) {
public AdvancedPie(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
Expand All @@ -427,8 +466,18 @@ protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
valuesBuilder.appendField(entry.getKey(), new int[] {entry.getValue()});
if (entry.getValue() == 0) {
// Skip this invalid
continue;
}
allSkipped = false;
valuesBuilder.appendField(entry.getKey(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
Expand Down Expand Up @@ -474,7 +523,7 @@ protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
}
}

public static class AdvancedPie extends CustomChart {
public static class SimpleBarChart extends CustomChart {

private final Callable<Map<String, Integer>> callable;

Expand All @@ -484,7 +533,7 @@ public static class AdvancedPie extends CustomChart {
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public AdvancedPie(String chartId, Callable<Map<String, Integer>> callable) {
public SimpleBarChart(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
Expand All @@ -497,18 +546,8 @@ protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 0) {
// Skip this invalid
continue;
}
allSkipped = false;
valuesBuilder.appendField(entry.getKey(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
valuesBuilder.appendField(entry.getKey(), new int[] {entry.getValue()});
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
Expand Down Expand Up @@ -548,99 +587,95 @@ public JsonObjectBuilder.JsonObject getRequestJsonObject(
protected abstract JsonObjectBuilder.JsonObject getChartData() throws Exception;
}

public static class SingleLineChart extends CustomChart {
public static class SimplePie extends CustomChart {

private final Callable<Integer> callable;
private final Callable<String> callable;

/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SingleLineChart(String chartId, Callable<Integer> callable) {
public SimplePie(String chartId, Callable<String> callable) {
super(chartId);
this.callable = callable;
}

@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
int value = callable.call();
if (value == 0) {
String value = callable.call();
if (value == null || value.isEmpty()) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("value", value).build();
}
}

public static class SimplePie extends CustomChart {
public static class AdvancedBarChart extends CustomChart {

private final Callable<String> callable;
private final Callable<Map<String, int[]>> callable;

/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SimplePie(String chartId, Callable<String> callable) {
public AdvancedBarChart(String chartId, Callable<Map<String, int[]>> callable) {
super(chartId);
this.callable = callable;
}

@Override
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
String value = callable.call();
if (value == null || value.isEmpty()) {
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
Map<String, int[]> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("value", value).build();
boolean allSkipped = true;
for (Map.Entry<String, int[]> entry : map.entrySet()) {
if (entry.getValue().length == 0) {
// Skip this invalid
continue;
}
allSkipped = false;
valuesBuilder.appendField(entry.getKey(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
}

public static class DrilldownPie extends CustomChart {
public static class SingleLineChart extends CustomChart {

private final Callable<Map<String, Map<String, Integer>>> callable;
private final Callable<Integer> callable;

/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable) {
public SingleLineChart(String chartId, Callable<Integer> callable) {
super(chartId);
this.callable = callable;
}

@Override
public JsonObjectBuilder.JsonObject getChartData() throws Exception {
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
Map<String, Map<String, Integer>> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean reallyAllSkipped = true;
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
JsonObjectBuilder valueBuilder = new JsonObjectBuilder();
boolean allSkipped = true;
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
valueBuilder.appendField(valueEntry.getKey(), valueEntry.getValue());
allSkipped = false;
}
if (!allSkipped) {
reallyAllSkipped = false;
valuesBuilder.appendField(entryValues.getKey(), valueBuilder.build());
}
}
if (reallyAllSkipped) {
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
int value = callable.call();
if (value == 0) {
// Null = skip the chart
return null;
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
return new JsonObjectBuilder().appendField("value", value).build();
}
}

Expand Down

0 comments on commit b9a7fb7

Please sign in to comment.