I'm tired of seeing prs that make small code reformats so im doing a commit to fix it all at once

This commit is contained in:
ZeroEpoch1969
2018-07-30 23:41:56 -07:00
parent 8bd8efc665
commit 60c627c591
46 changed files with 457 additions and 273 deletions

View File

@ -30,20 +30,24 @@ import java.util.zip.GZIPOutputStream;
/**
* bStats collects some data for plugin authors.
*
* <p>
* Check out https://bStats.org/ to learn more about bStats!
*/
public class Metrics {
public class Metrics
{
static {
static
{
// You can use the property to disable the check in your test environment
if (System.getProperty("bstats.relocatecheck") == null || !System.getProperty("bstats.relocatecheck").equals("false")) {
if (System.getProperty("bstats.relocatecheck") == null || !System.getProperty("bstats.relocatecheck").equals("false"))
{
// Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D
final String defaultPackage = new String(
new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's', '.', 'b', 'u', 'k', 'k', 'i', 't'});
final String examplePackage = new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'});
// We want to make sure nobody just copy & pastes the example and use the wrong package names
if (Metrics.class.getPackage().getName().equals(defaultPackage) || Metrics.class.getPackage().getName().equals(examplePackage)) {
if (Metrics.class.getPackage().getName().equals(defaultPackage) || Metrics.class.getPackage().getName().equals(examplePackage))
{
throw new IllegalStateException("bStats Metrics class has not been relocated correctly!");
}
}
@ -72,8 +76,10 @@ public class Metrics {
*
* @param plugin The plugin which stats should be submitted.
*/
public Metrics(JavaPlugin plugin) {
if (plugin == null) {
public Metrics(JavaPlugin plugin)
{
if (plugin == null)
{
throw new IllegalArgumentException("Plugin cannot be null!");
}
this.plugin = plugin;
@ -84,7 +90,8 @@ public class Metrics {
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
// Check if the config file exists
if (!config.isSet("serverUuid")) {
if (!config.isSet("serverUuid"))
{
// Add default values
config.addDefault("enabled", true);
@ -100,27 +107,38 @@ public class Metrics {
"This has nearly no effect on the server performance!\n" +
"Check out https://bStats.org/ to learn more :)"
).copyDefaults(true);
try {
try
{
config.save(configFile);
} catch (IOException ignored) { }
}
catch (IOException ignored)
{
}
}
// Load the data
serverUUID = config.getString("serverUuid");
logFailedRequests = config.getBoolean("logFailedRequests", false);
if (config.getBoolean("enabled", true)) {
if (config.getBoolean("enabled", true))
{
boolean found = false;
// Search for all other bStats Metrics classes to see if we are the first one
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
try {
for (Class<?> service : Bukkit.getServicesManager().getKnownServices())
{
try
{
service.getField("B_STATS_VERSION"); // Our identifier :)
found = true; // We aren't the first
break;
} catch (NoSuchFieldException ignored) { }
}
catch (NoSuchFieldException ignored)
{
}
}
// Register our service
Bukkit.getServicesManager().register(Metrics.class, this, plugin, ServicePriority.Normal);
if (!found) {
if (!found)
{
// We are the first!
startSubmitting();
}
@ -132,8 +150,10 @@ public class Metrics {
*
* @param chart The chart to add.
*/
public void addCustomChart(CustomChart chart) {
if (chart == null) {
public void addCustomChart(CustomChart chart)
{
if (chart == null)
{
throw new IllegalArgumentException("Chart cannot be null!");
}
charts.add(chart);
@ -142,25 +162,31 @@ public class Metrics {
/**
* Starts the Scheduler which submits our data every 30 minutes.
*/
private void startSubmitting() {
private void startSubmitting()
{
final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
timer.scheduleAtFixedRate(new TimerTask() {
timer.scheduleAtFixedRate(new TimerTask()
{
@Override
public void run() {
if (!plugin.isEnabled()) { // Plugin was disabled
public void run()
{
if (!plugin.isEnabled())
{ // Plugin was disabled
timer.cancel();
return;
}
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
Bukkit.getScheduler().runTask(plugin, new Runnable() {
Bukkit.getScheduler().runTask(plugin, new Runnable()
{
@Override
public void run() {
public void run()
{
submitData();
}
});
}
}, 1000*60*5, 1000*60*30);
}, 1000 * 60 * 5, 1000 * 60 * 30);
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
// WARNING: Just don't do it!
@ -172,7 +198,8 @@ public class Metrics {
*
* @return The plugin specific data.
*/
public JSONObject getPluginData() {
public JSONObject getPluginData()
{
JSONObject data = new JSONObject();
String pluginName = plugin.getDescription().getName();
@ -181,10 +208,12 @@ public class Metrics {
data.put("pluginName", pluginName); // Append the name of the plugin
data.put("pluginVersion", pluginVersion); // Append the version of the plugin
JSONArray customCharts = new JSONArray();
for (CustomChart customChart : charts) {
for (CustomChart customChart : charts)
{
// Add the data of the custom charts
JSONObject chart = customChart.getRequestJsonObject();
if (chart == null) { // If the chart is null, we skip it
if (chart == null)
{ // If the chart is null, we skip it
continue;
}
customCharts.add(chart);
@ -199,17 +228,21 @@ public class Metrics {
*
* @return The server specific data.
*/
private JSONObject getServerData() {
private JSONObject getServerData()
{
// Minecraft specific data
int playerAmount;
try {
try
{
// Around MC 1.8 the return type was changed to a collection from an array,
// This fixes java.lang.NoSuchMethodError: org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection;
Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers");
playerAmount = onlinePlayersMethod.getReturnType().equals(Collection.class)
? ((Collection<?>) onlinePlayersMethod.invoke(Bukkit.getServer())).size()
: ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length;
} catch (Exception e) {
}
catch (Exception e)
{
playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed
}
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
@ -243,35 +276,52 @@ public class Metrics {
/**
* Collects the data and sends it afterwards.
*/
private void submitData() {
private void submitData()
{
final JSONObject data = getServerData();
JSONArray pluginData = new JSONArray();
// Search for all other bStats Metrics classes to get their plugin data
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
try {
for (Class<?> service : Bukkit.getServicesManager().getKnownServices())
{
try
{
service.getField("B_STATS_VERSION"); // Our identifier :)
for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(service)) {
try {
for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(service))
{
try
{
pluginData.add(provider.getService().getMethod("getPluginData").invoke(provider.getProvider()));
} catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { }
}
catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored)
{
}
}
} catch (NoSuchFieldException ignored) { }
}
catch (NoSuchFieldException ignored)
{
}
}
data.put("plugins", pluginData);
// Create a new thread for the connection to the bStats server
new Thread(new Runnable() {
new Thread(new Runnable()
{
@Override
public void run() {
try {
public void run()
{
try
{
// Send the data
sendData(data);
} catch (Exception e) {
}
catch (Exception e)
{
// Something went wrong! :(
if (logFailedRequests) {
if (logFailedRequests)
{
plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e);
}
}
@ -285,11 +335,14 @@ public class Metrics {
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(JSONObject data) throws Exception {
if (data == null) {
private static void sendData(JSONObject data) throws Exception
{
if (data == null)
{
throw new IllegalArgumentException("Data cannot be null!");
}
if (Bukkit.isPrimaryThread()) {
if (Bukkit.isPrimaryThread())
{
throw new IllegalAccessException("This method must not be called from the main thread!");
}
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
@ -323,8 +376,10 @@ public class Metrics {
* @return The gzipped String.
* @throws IOException If the compression failed.
*/
private static byte[] compress(final String str) throws IOException {
if (str == null) {
private static byte[] compress(final String str) throws IOException
{
if (str == null)
{
return null;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@ -337,7 +392,8 @@ public class Metrics {
/**
* Represents a custom chart.
*/
public static abstract class CustomChart {
public static abstract class CustomChart
{
// The id of the chart
final String chartId;
@ -347,25 +403,33 @@ public class Metrics {
*
* @param chartId The id of the chart.
*/
CustomChart(String chartId) {
if (chartId == null || chartId.isEmpty()) {
CustomChart(String chartId)
{
if (chartId == null || chartId.isEmpty())
{
throw new IllegalArgumentException("ChartId cannot be null or empty!");
}
this.chartId = chartId;
}
private JSONObject getRequestJsonObject() {
private JSONObject getRequestJsonObject()
{
JSONObject chart = new JSONObject();
chart.put("chartId", chartId);
try {
try
{
JSONObject data = getChartData();
if (data == null) {
if (data == null)
{
// If the data is null we don't send the chart.
return null;
}
chart.put("data", data);
} catch (Throwable t) {
if (logFailedRequests) {
}
catch (Throwable t)
{
if (logFailedRequests)
{
Bukkit.getLogger().log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t);
}
return null;
@ -380,26 +444,30 @@ public class Metrics {
/**
* Represents a custom simple pie.
*/
public static class SimplePie extends CustomChart {
public static class SimplePie extends CustomChart
{
private final Callable<String> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @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 SimplePie(String chartId, Callable<String> callable)
{
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
protected JSONObject getChartData() throws Exception
{
JSONObject data = new JSONObject();
String value = callable.call();
if (value == null || value.isEmpty()) {
if (value == null || value.isEmpty())
{
// Null = skip the chart
return null;
}
@ -411,39 +479,46 @@ public class Metrics {
/**
* Represents a custom advanced pie.
*/
public static class AdvancedPie extends CustomChart {
public static class AdvancedPie extends CustomChart
{
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @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 AdvancedPie(String chartId, Callable<Map<String, Integer>> callable)
{
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
protected JSONObject getChartData() throws Exception
{
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
if (map == null || map.isEmpty())
{
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 0) {
for (Map.Entry<String, Integer> entry : map.entrySet())
{
if (entry.getValue() == 0)
{
continue; // Skip this invalid
}
allSkipped = false;
values.put(entry.getKey(), entry.getValue());
}
if (allSkipped) {
if (allSkipped)
{
// Null = skip the chart
return null;
}
@ -455,44 +530,52 @@ public class Metrics {
/**
* Represents a custom drilldown pie.
*/
public static class DrilldownPie extends CustomChart {
public static class DrilldownPie extends CustomChart
{
private final Callable<Map<String, Map<String, Integer>>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @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 DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable)
{
super(chartId);
this.callable = callable;
}
@Override
public JSONObject getChartData() throws Exception {
public JSONObject getChartData() throws Exception
{
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Map<String, Integer>> map = callable.call();
if (map == null || map.isEmpty()) {
if (map == null || map.isEmpty())
{
// Null = skip the chart
return null;
}
boolean reallyAllSkipped = true;
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet())
{
JSONObject value = new JSONObject();
boolean allSkipped = true;
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet())
{
value.put(valueEntry.getKey(), valueEntry.getValue());
allSkipped = false;
}
if (!allSkipped) {
if (!allSkipped)
{
reallyAllSkipped = false;
values.put(entryValues.getKey(), value);
}
}
if (reallyAllSkipped) {
if (reallyAllSkipped)
{
// Null = skip the chart
return null;
}
@ -504,26 +587,30 @@ public class Metrics {
/**
* Represents a custom single line chart.
*/
public static class SingleLineChart extends CustomChart {
public static class SingleLineChart extends CustomChart
{
private final Callable<Integer> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @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 SingleLineChart(String chartId, Callable<Integer> callable)
{
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
protected JSONObject getChartData() throws Exception
{
JSONObject data = new JSONObject();
int value = callable.call();
if (value == 0) {
if (value == 0)
{
// Null = skip the chart
return null;
}
@ -536,39 +623,46 @@ public class Metrics {
/**
* Represents a custom multi line chart.
*/
public static class MultiLineChart extends CustomChart {
public static class MultiLineChart extends CustomChart
{
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public MultiLineChart(String chartId, Callable<Map<String, Integer>> callable) {
public MultiLineChart(String chartId, Callable<Map<String, Integer>> callable)
{
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
protected JSONObject getChartData() throws Exception
{
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
if (map == null || map.isEmpty())
{
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 0) {
for (Map.Entry<String, Integer> entry : map.entrySet())
{
if (entry.getValue() == 0)
{
continue; // Skip this invalid
}
allSkipped = false;
values.put(entry.getKey(), entry.getValue());
}
if (allSkipped) {
if (allSkipped)
{
// Null = skip the chart
return null;
}
@ -581,31 +675,36 @@ public class Metrics {
/**
* Represents a custom simple bar chart.
*/
public static class SimpleBarChart extends CustomChart {
public static class SimpleBarChart extends CustomChart
{
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @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 SimpleBarChart(String chartId, Callable<Map<String, Integer>> callable)
{
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
protected JSONObject getChartData() throws Exception
{
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
if (map == null || map.isEmpty())
{
// Null = skip the chart
return null;
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
for (Map.Entry<String, Integer> entry : map.entrySet())
{
JSONArray categoryValues = new JSONArray();
categoryValues.add(entry.getValue());
values.put(entry.getKey(), categoryValues);
@ -619,43 +718,51 @@ public class Metrics {
/**
* Represents a custom advanced bar chart.
*/
public static class AdvancedBarChart extends CustomChart {
public static class AdvancedBarChart extends CustomChart
{
private final Callable<Map<String, int[]>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @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 AdvancedBarChart(String chartId, Callable<Map<String, int[]>> callable)
{
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
protected JSONObject getChartData() throws Exception
{
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, int[]> map = callable.call();
if (map == null || map.isEmpty()) {
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) {
for (Map.Entry<String, int[]> entry : map.entrySet())
{
if (entry.getValue().length == 0)
{
continue; // Skip this invalid
}
allSkipped = false;
JSONArray categoryValues = new JSONArray();
for (int categoryValue : entry.getValue()) {
for (int categoryValue : entry.getValue())
{
categoryValues.add(categoryValue);
}
values.put(entry.getKey(), categoryValues);
}
if (allSkipped) {
if (allSkipped)
{
// Null = skip the chart
return null;
}