Add mc logs & version notifications.

This commit is contained in:
Badbird5907
2022-11-06 15:14:44 -05:00
parent bd9328d452
commit dbb7afc5b7
5 changed files with 93 additions and 53 deletions

View File

@@ -1,52 +0,0 @@
package net.nuggetmc.tplus.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.HttpsURLConnection;
/**
* Used for debug logs.
* https://github.com/kaimu-kun/hastebin.java/blob/master/src/me/kaimu/hastebin/Hastebin.java
*/
public class Hastebin {
public String post(String text, boolean raw) throws IOException {
byte[] postData = text.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
String requestURL = "https://hastebin.com/documents";
URL url = new URL(requestURL);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Hastebin Java Api");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setUseCaches(false);
String response = null;
DataOutputStream wr;
try {
wr = new DataOutputStream(conn.getOutputStream());
wr.write(postData);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (response.contains("\"key\"")) {
response = response.substring(response.indexOf(":") + 2, response.length() - 2);
String postURL = raw ? "https://hastebin.com/raw/" : "https://hastebin.com/";
response = postURL + response;
}
return response;
}
}

View File

@@ -0,0 +1,60 @@
package net.nuggetmc.tplus.utils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.nuggetmc.tplus.TerminatorPlus;
import org.bukkit.Bukkit;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
/**
* Used for debug logs.
*/
public class MCLogs {
private static final String FORMAT =
"""
====== TERMINATOR PLUS DEBUG INFO ======
Plugin Version: %s
Server Version: %s
Server Software: %s
Server Plugins: %s
Server TPS: %s
Server Memory: %s
Correct Version: %s
Required Version: %s
====== TERMINATOR PLUS DEBUG INFO ======
""";
public static String postInfo() throws IOException {
String serverVersion = Bukkit.getVersion();
String pluginVersion = TerminatorPlus.getVersion();
String serverSoftware = Bukkit.getName();
String serverPlugins = Arrays.stream(Bukkit.getPluginManager().getPlugins()).map(plugin -> plugin.getName() + " v" + plugin.getDescription().getVersion()).reduce((s, s2) -> s + ", " + s2).orElse("No plugins");
String serverTPS = Arrays.stream(Bukkit.getTPS()).mapToObj(tps -> String.format("%.2f", tps)).reduce((s, s2) -> s + ", " + s2).orElse("No TPS");
String serverMemory = String.format("%.2f", (double) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024) + "MB";
String info = String.format(FORMAT, pluginVersion, serverVersion, serverSoftware, serverPlugins, serverTPS, serverMemory, TerminatorPlus.isCorrectVersion(), TerminatorPlus.REQUIRED_VERSION);
return pasteText(info);
}
private static String pasteText(String text) throws IOException {
URL url = new URL("https://api.mclo.gs/1/log"); // application/x-www-form-urlencoded
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
os.write(("content=" + text).getBytes());
}
String response = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
JsonObject json = JsonParser.parseString(response).getAsJsonObject();
return json.get("url").getAsString();
}
}