Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Smudgge committed Jan 12, 2023
1 parent fd9b63a commit 06dfb89
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 72 deletions.
129 changes: 63 additions & 66 deletions src/main/java/com/github/smuddgge/leaf/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,14 @@
import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import org.slf4j.Logger;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -50,12 +37,12 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;
import org.slf4j.Logger;

public class Metrics {

/** A factory to create new Metrics classes. */
/**
* A factory to create new Metrics classes.
*/
public static class Factory {

private final ProxyServer server;
Expand All @@ -76,12 +63,12 @@ private Factory(ProxyServer server, Logger logger, @DataDirectory Path dataDirec
/**
* Creates a new Metrics class.
*
* @param plugin The plugin instance.
* @param plugin The plugin instance.
* @param serviceId The id of the service. It can be found at <a
* href="https://bstats.org/what-is-my-plugin-id">What is my plugin id?</a>
* <p>Not to be confused with Velocity's {@link PluginDescription#getId()} method!
* href="https://bstats.org/what-is-my-plugin-id">What is my plugin id?</a>
* <p>Not to be confused with Velocity's {@link PluginDescription#getId()} method!
* @return A Metrics instance that can be used to register custom charts.
* <p>The return value can be ignored, when you do not want to register custom charts.
* <p>The return value can be ignored, when you do not want to register custom charts.
*/
public Metrics make(Object plugin, int serviceId) {
return new Metrics(plugin, server, logger, dataDirectory, serviceId);
Expand Down Expand Up @@ -172,7 +159,9 @@ private void appendServiceData(JsonObjectBuilder builder) {

public static class MetricsBase {

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

private static final ScheduledExecutorService scheduler =
Expand Down Expand Up @@ -211,23 +200,23 @@ public static class MetricsBase {
/**
* Creates a new MetricsBase class instance.
*
* @param platform The platform of the service.
* @param serviceId The id of the service.
* @param serverUuid The server uuid.
* @param enabled Whether or not data sending is enabled.
* @param appendPlatformDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
* appends all platform-specific data.
* @param appendServiceDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
* appends all service-specific data.
* @param submitTaskConsumer A consumer that takes a runnable with the submit task. This can be
* used to delegate the data collection to a another thread to prevent errors caused by
* concurrency. Can be {@code null}.
* @param platform The platform of the service.
* @param serviceId The id of the service.
* @param serverUuid The server uuid.
* @param enabled Whether or not data sending is enabled.
* @param appendPlatformDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
* appends all platform-specific data.
* @param appendServiceDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
* appends all service-specific data.
* @param submitTaskConsumer A consumer that takes a runnable with the submit task. This can be
* used to delegate the data collection to a another thread to prevent errors caused by
* concurrency. Can be {@code null}.
* @param checkServiceEnabledSupplier A supplier to check if the service is still enabled.
* @param errorLogger A consumer that accepts log message and an error.
* @param infoLogger A consumer that accepts info log messages.
* @param logErrors Whether or not errors should be logged.
* @param logSentData Whether or not the sent data should be logged.
* @param logResponseStatusText Whether or not the response status text should be logged.
* @param errorLogger A consumer that accepts log message and an error.
* @param infoLogger A consumer that accepts info log messages.
* @param logErrors Whether or not errors should be logged.
* @param logSentData Whether or not the sent data should be logged.
* @param logResponseStatusText Whether or not the response status text should be logged.
*/
public MetricsBase(
String platform,
Expand Down Expand Up @@ -357,17 +346,19 @@ private void sendData(JsonObjectBuilder.JsonObject data) throws Exception {
}
}

/** Checks that the class was properly relocated. */
/**
* Checks that the class was properly relocated.
*/
private void checkRelocation() {
// 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")) {
// 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'});
new String(new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's'});
final String examplePackage =
new String(new byte[] {'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'});
new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'});
// We want to make sure no one just copy & pastes the example and uses the wrong package
// names
if (MetricsBase.class.getPackage().getName().startsWith(defaultPackage)
Expand Down Expand Up @@ -402,7 +393,7 @@ public static class DrilldownPie extends CustomChart {
/**
* 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) {
Expand Down Expand Up @@ -446,7 +437,7 @@ public static class AdvancedPie extends CustomChart {
/**
* 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) {
Expand Down Expand Up @@ -486,7 +477,7 @@ public static class MultiLineChart extends CustomChart {
/**
* 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) {
Expand Down Expand Up @@ -526,7 +517,7 @@ public static class SimpleBarChart extends CustomChart {
/**
* 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) {
Expand All @@ -543,7 +534,7 @@ protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
return null;
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
valuesBuilder.appendField(entry.getKey(), new int[] {entry.getValue()});
valuesBuilder.appendField(entry.getKey(), new int[]{entry.getValue()});
}
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
}
Expand Down Expand Up @@ -590,7 +581,7 @@ public static class SimplePie extends CustomChart {
/**
* 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) {
Expand All @@ -616,7 +607,7 @@ public static class AdvancedBarChart extends CustomChart {
/**
* 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) {
Expand Down Expand Up @@ -656,7 +647,7 @@ public static class SingleLineChart extends CustomChart {
/**
* 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) {
Expand Down Expand Up @@ -705,7 +696,7 @@ public JsonObjectBuilder appendNull(String key) {
/**
* Appends a string field to the JSON.
*
* @param key The key of the field.
* @param key The key of the field.
* @param value The value of the field.
* @return A reference to this object.
*/
Expand All @@ -720,7 +711,7 @@ public JsonObjectBuilder appendField(String key, String value) {
/**
* Appends an integer field to the JSON.
*
* @param key The key of the field.
* @param key The key of the field.
* @param value The value of the field.
* @return A reference to this object.
*/
Expand All @@ -732,7 +723,7 @@ public JsonObjectBuilder appendField(String key, int value) {
/**
* Appends an object to the JSON.
*
* @param key The key of the field.
* @param key The key of the field.
* @param object The object.
* @return A reference to this object.
*/
Expand All @@ -747,7 +738,7 @@ public JsonObjectBuilder appendField(String key, JsonObject object) {
/**
* Appends a string array to the JSON.
*
* @param key The key of the field.
* @param key The key of the field.
* @param values The string array.
* @return A reference to this object.
*/
Expand All @@ -766,7 +757,7 @@ public JsonObjectBuilder appendField(String key, String[] values) {
/**
* Appends an integer array to the JSON.
*
* @param key The key of the field.
* @param key The key of the field.
* @param values The integer array.
* @return A reference to this object.
*/
Expand All @@ -783,7 +774,7 @@ public JsonObjectBuilder appendField(String key, int[] values) {
/**
* Appends an object array to the JSON.
*
* @param key The key of the field.
* @param key The key of the field.
* @param values The integer array.
* @return A reference to this object.
*/
Expand All @@ -800,7 +791,7 @@ public JsonObjectBuilder appendField(String key, JsonObject[] values) {
/**
* Appends a field to the object.
*
* @param key The key of the field.
* @param key The key of the field.
* @param escapedValue The escaped value of the field.
*/
private void appendFieldUnescaped(String key, String escapedValue) {
Expand Down Expand Up @@ -939,7 +930,9 @@ public boolean didExistBefore() {
return didExistBefore;
}

/** Creates the config file if it does not exist and read its content. */
/**
* Creates the config file if it does not exist and read its content.
*/
private void setupConfig() throws IOException {
if (!file.exists()) {
// Looks like it's the first time we create it (or someone deleted it).
Expand All @@ -954,7 +947,9 @@ private void setupConfig() throws IOException {
}
}

/** Creates a config file with teh default content. */
/**
* Creates a config file with teh default content.
*/
private void writeConfig() throws IOException {
List<String> configContent = new ArrayList<>();
configContent.add(
Expand All @@ -967,14 +962,16 @@ private void writeConfig() throws IOException {
"# There is no performance penalty associated with having metrics enabled, and data sent to");
configContent.add("# bStats is fully anonymous.");
configContent.add("enabled=" + defaultEnabled);
configContent.add("server-uuid=" + UUID.randomUUID().toString());
configContent.add("server-uuid=" + UUID.randomUUID());
configContent.add("log-errors=false");
configContent.add("log-sent-data=false");
configContent.add("log-response-status-text=false");
writeFile(file, configContent);
}

/** Reads the content of the config file. */
/**
* Reads the content of the config file.
*/
private void readConfig() throws IOException {
List<String> lines = readFile(file);
if (lines == null) {
Expand All @@ -991,7 +988,7 @@ private void readConfig() throws IOException {
/**
* Gets a config setting from the given list of lines of the file.
*
* @param key The key for the setting.
* @param key The key for the setting.
* @param lines The lines of the file.
* @return The value of the setting.
*/
Expand Down Expand Up @@ -1021,7 +1018,7 @@ private List<String> readFile(File file) throws IOException {
/**
* Writes the given lines to the given file.
*
* @param file The file to write to.
* @param file The file to write to.
* @param lines The lines to write.
*/
private void writeFile(File file, List<String> lines) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.github.smuddgge.leaf.datatype.User;
import com.github.smuddgge.leaf.inventorys.CustomInventory;
import com.github.smuddgge.leaf.inventorys.InventoryItem;
import com.github.smuddgge.leaf.inventorys.inventorys.FriendListInventory;
import dev.simplix.protocolize.api.item.ItemStack;

public class Inventory extends BaseCommandType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ protected ItemStack onNextPage(InventoryItem inventoryItem, int recordsSize) {
if (recordsSize - 1 < recordIndex) {
if (!inventoryItem.getFunctionSection().getBoolean("always_show", false)) {
for (Integer slot : inventoryItem.getSlots(this.getInventoryType())) {
this.addAction(slot, () -> {});
this.addAction(slot, () -> {
});
}
return null;
}
Expand All @@ -172,7 +173,8 @@ protected ItemStack onLastPage(InventoryItem inventoryItem) {
if (page <= 1) {
if (!inventoryItem.getFunctionSection().getBoolean("always_show", false)) {
for (Integer slot : inventoryItem.getSlots(this.getInventoryType())) {
this.addAction(slot, () -> {});
this.addAction(slot, () -> {
});
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import dev.simplix.protocolize.data.inventory.InventoryType;
import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.ListTag;
import net.querz.nbt.tag.Tag;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.Tag;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private ItemStack onLoadPlayer(InventoryItem inventoryItem) {
if (this.requestRecords.size() - 1 < recordIndex) {
ItemStack item = this.appendNoPlayerItemStack(inventoryItem);
this.inventory.item(slot, item);
this.addAction(slot, () -> {});
this.addAction(slot, () -> {
});
} else {
FriendRequestRecord requestRecord = (FriendRequestRecord) this.requestRecords.get(recordIndex);
PlayerTable playerTable = (PlayerTable) Leaf.getDatabase().getTable("Player");
Expand Down

0 comments on commit 06dfb89

Please sign in to comment.