Skip to content

Commit

Permalink
QQ音乐API初步完成
Browse files Browse the repository at this point in the history
插件消息发送完成
升级Gradle 6.8
  • Loading branch information
RealHeart committed Jan 9, 2021
1 parent c93218d commit 60cd57f
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 301 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
214 changes: 103 additions & 111 deletions src/main/java/me/zhenxin/zmusic/bstats/MetricsBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
@SuppressWarnings({"WeakerAccess", "unused"})
public class MetricsBC {

// The version of this bStats class
public static final int B_STATS_VERSION = 1;
// The url to which the data is sent
private static final String URL = "https://bStats.org/submitData/bungeecord";
// A list with all known metrics class objects including this one
private static final List<Object> knownMetricsInstances = new ArrayList<>();
// Should the sent data be logged?
private static boolean logSentData;
// Should the response text be logged?
private static boolean logResponseStatusText;

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")) {
Expand All @@ -45,43 +56,23 @@ public class MetricsBC {
}
}

// The version of this bStats class
public static final int B_STATS_VERSION = 1;

// The url to which the data is sent
private static final String URL = "https://bStats.org/submitData/bungeecord";

// The plugin
private final Plugin plugin;

// The plugin id
private final int pluginId;

// A list with all custom charts
private final List<CustomChart> charts = new ArrayList<>();
// Is bStats enabled on this server?
private boolean enabled;

// The uuid of the server
private String serverUUID;

// Should failed requests be logged?
private boolean logFailedRequests = false;

// Should the sent data be logged?
private static boolean logSentData;

// Should the response text be logged?
private static boolean logResponseStatusText;

// A list with all known metrics class objects including this one
private static final List<Object> knownMetricsInstances = new ArrayList<>();

// A list with all custom charts
private final List<CustomChart> charts = new ArrayList<>();

/**
* Class constructor.
*
* @param plugin The plugin which stats should be submitted.
* @param plugin The plugin which stats should be submitted.
* @param pluginId The id of the plugin.
* It can be found at <a href="https://bstats.org/what-is-my-plugin-id">What is my plugin id?</a>
*/
Expand Down Expand Up @@ -123,6 +114,83 @@ public MetricsBC(Plugin plugin, int pluginId) {
}
}

/**
* Links an other metrics class with this class.
* This method is called using Reflection.
*
* @param metrics An object of the metrics class to link.
*/
public static void linkMetrics(Object metrics) {
knownMetricsInstances.add(metrics);
}

/**
* Sends the data to the bStats server.
*
* @param plugin Any plugin. It's just used to get a logger instance.
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(Plugin plugin, JsonObject data) throws Exception {
if (data == null) {
throw new IllegalArgumentException("Data cannot be null");
}
if (logSentData) {
plugin.getLogger().info("Sending data to bStats: " + data);
}

HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();

// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());

// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);

// Send data
connection.setDoOutput(true);
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
outputStream.write(compressedData);
}

StringBuilder builder = new StringBuilder();

try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
}

if (logResponseStatusText) {
plugin.getLogger().info("Sent data to bStats and received response: " + builder);
}
}

/**
* Gzips the given String.
*
* @param str The string to gzip.
* @return The gzipped String.
* @throws IOException If the compression failed.
*/
private static byte[] compress(final String str) throws IOException {
if (str == null) {
return null;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
gzip.write(str.getBytes(StandardCharsets.UTF_8));
}
return outputStream.toByteArray();
}

/**
* Checks if bStats is enabled.
*
Expand All @@ -144,16 +212,6 @@ public void addCustomChart(CustomChart chart) {
charts.add(chart);
}

/**
* Links an other metrics class with this class.
* This method is called using Reflection.
*
* @param metrics An object of the metrics class to link.
*/
public static void linkMetrics(Object metrics) {
knownMetricsInstances.add(metrics);
}

/**
* Gets the plugin specific data.
* This method is called using Reflection.
Expand Down Expand Up @@ -244,7 +302,8 @@ private void submitData() {
if (plugin instanceof JsonObject) {
pluginData.add((JsonObject) plugin);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { }
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
}
}

data.add("plugins", pluginData);
Expand Down Expand Up @@ -308,7 +367,8 @@ private Class<?> getFirstBStatsClass() {
try {
// Let's check if a class with the given name exists.
return Class.forName(className);
} catch (ClassNotFoundException ignored) { }
} catch (ClassNotFoundException ignored) {
}
}
writeFile(tempFile, getClass().getName());
return getClass();
Expand Down Expand Up @@ -339,7 +399,7 @@ private String readFile(File file) throws IOException {
/**
* Writes a String to a file. It also adds a note for the user,
*
* @param file The file to write to. Cannot be null.
* @param file The file to write to. Cannot be null.
* @param lines The lines to write.
* @throws IOException If something did not work :(
*/
Expand All @@ -352,74 +412,6 @@ private void writeFile(File file, String... lines) throws IOException {
}
}

/**
* Sends the data to the bStats server.
*
* @param plugin Any plugin. It's just used to get a logger instance.
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(Plugin plugin, JsonObject data) throws Exception {
if (data == null) {
throw new IllegalArgumentException("Data cannot be null");
}
if (logSentData) {
plugin.getLogger().info("Sending data to bStats: " + data);
}

HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();

// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());

// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);

// Send data
connection.setDoOutput(true);
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
outputStream.write(compressedData);
}

StringBuilder builder = new StringBuilder();

try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
}

if (logResponseStatusText) {
plugin.getLogger().info("Sent data to bStats and received response: " + builder);
}
}

/**
* Gzips the given String.
*
* @param str The string to gzip.
* @return The gzipped String.
* @throws IOException If the compression failed.
*/
private static byte[] compress(final String str) throws IOException {
if (str == null) {
return null;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
gzip.write(str.getBytes(StandardCharsets.UTF_8));
}
return outputStream.toByteArray();
}


/**
* Represents a custom chart.
*/
Expand Down Expand Up @@ -473,7 +465,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 Down Expand Up @@ -504,7 +496,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 @@ -548,7 +540,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 @@ -597,7 +589,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 @@ -629,7 +621,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 @@ -674,7 +666,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 Down Expand Up @@ -712,7 +704,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
Loading

0 comments on commit 60cd57f

Please sign in to comment.