diff --git a/res/plugin.yml b/res/plugin.yml index 457e870..b559145 100644 --- a/res/plugin.yml +++ b/res/plugin.yml @@ -1,5 +1,5 @@ name: MineOnlineBroadcast -version: 1.1.0 +version: 1.1.1 description: Lists a server on mineonline.codie.gg/servers author: codieradical authors: [Codie] diff --git a/src/MineOnlineBroadcast.java b/src/MineOnlineBroadcast.java index b4add25..b1d011e 100644 --- a/src/MineOnlineBroadcast.java +++ b/src/MineOnlineBroadcast.java @@ -1,12 +1,17 @@ import gg.codie.common.input.EColorCodeColor; import gg.codie.minecraft.server.MinecraftColorCodeProvider; +import gg.codie.mineonline.api.MineOnlineAPI; import gg.codie.mineonline.discord.DiscordChatBridge; import gg.codie.mineonline.discord.IMessageRecievedListener; import gg.codie.mineonline.discord.IShutdownListener; import gg.codie.mineonline.discord.MinotarAvatarProvider; import gg.codie.mineonline.plugin.ProxyThread; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import org.json.JSONObject; +import javax.imageio.IIOException; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.*; import java.lang.reflect.Constructor; import java.lang.reflect.Method; @@ -16,7 +21,9 @@ import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; +import java.util.Base64; import java.util.Properties; +import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -35,6 +42,7 @@ public class MineOnlineBroadcast extends Plugin { boolean initialized; String serverName = "Minecraft Server"; private MineOnlineConfig mineOnlineConfig; + String serverUUID; public void launchProxy() throws IOException { ServerSocket serverSocket = new ServerSocket(0); @@ -83,81 +91,6 @@ public class MineOnlineBroadcast extends Plugin { return result.toUpperCase(); } - public static void listServer( - String ip, - String port, - int users, - int maxUsers, - String name, - boolean onlineMode, - String md5, - boolean whitelisted, - String[] playerNames, - String motd, - boolean dontListPlayers, - boolean useBetaEvolutionsAuth - ) { - HttpURLConnection connection = null; - - try { - URLClassLoader classLoader = new URLClassLoader(new URL[]{MineOnlineBroadcast.class.getProtectionDomain().getCodeSource().getLocation()}); - - Class jsonObjectClass = classLoader.loadClass("org.json.JSONObject"); - - Constructor jsonObjectConstructor = jsonObjectClass.getConstructor(); - Method jsonObjectPut = jsonObjectClass.getMethod("put", String.class, Object.class); - Method jsonObjectToString = jsonObjectClass.getMethod("toString"); - - Object jsonObject = jsonObjectConstructor.newInstance(); - if (ip != null) - jsonObjectPut.invoke(jsonObject, "ip", ip); - jsonObjectPut.invoke(jsonObject, "port", port); - if (users > -1 && !dontListPlayers) - jsonObjectPut.invoke(jsonObject, "users", users); - jsonObjectPut.invoke(jsonObject, "max", maxUsers); - jsonObjectPut.invoke(jsonObject, "name", name); - jsonObjectPut.invoke(jsonObject, "onlinemode", onlineMode); - jsonObjectPut.invoke(jsonObject, "md5", md5); - jsonObjectPut.invoke(jsonObject, "whitelisted", whitelisted); - if (!dontListPlayers) - jsonObjectPut.invoke(jsonObject, "players", playerNames); - jsonObjectPut.invoke(jsonObject, "motd", motd); - jsonObjectPut.invoke(jsonObject, "dontListPlayers", dontListPlayers); - jsonObjectPut.invoke(jsonObject, "useBetaEvolutionsAuth", useBetaEvolutionsAuth); - - String json = (String) jsonObjectToString.invoke(jsonObject); - - URL url = new URL("https://mineonline.codie.gg/api/servers"); - connection = (HttpURLConnection) url.openConnection(); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setRequestMethod("POST"); - connection.setDoInput(true); - connection.setDoOutput(true); - - connection.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8)); - connection.getOutputStream().flush(); - connection.getOutputStream().close(); - - InputStream is = connection.getInputStream(); - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - - StringBuilder response = new StringBuilder(); - String line; - while ((line = rd.readLine()) != null) { - response.append(line); - response.append('\r'); - } - rd.close(); - } catch (Exception e) { - - e.printStackTrace(); - } finally { - - if (connection != null) - connection.disconnect(); - } - } - public void enable() { initialize(); @@ -208,7 +141,27 @@ public class MineOnlineBroadcast extends Plugin { String[] playerNames = etc.getServer().getPlayerList().stream().map(player -> player.getName()).collect(Collectors.toList()).toArray(new String[users]); - listServer( + String serverIcon = null; + + try { + BufferedImage serverIconBufferedImage = ImageIO.read(new File(System.getProperty("user.dir") + File.separator + "server-icon.png")); + if (serverIconBufferedImage.getHeight() <= 64 && serverIconBufferedImage.getWidth() <= 64) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + ImageIO.write(serverIconBufferedImage, "png", bos); + byte[] bytes = bos.toByteArray(); + Base64.Encoder encoder = Base64.getEncoder(); + serverIcon = encoder.encodeToString(bytes); + serverIcon = serverIcon.replace(System.lineSeparator(), ""); + } catch (IOException e) { + e.printStackTrace(); + } + } + } catch (IIOException ex) { + ex.printStackTrace(); + } + + serverUUID = MineOnlineAPI.listServer( ip, port, users, @@ -220,10 +173,11 @@ public class MineOnlineBroadcast extends Plugin { playerNames, motd, dontListPlayers, - useBetaEvolutionsAuth + useBetaEvolutionsAuth, + serverIcon ); } catch (IOException ex) { - //ex.printStackTrace(); + ex.printStackTrace(); // ignore. } } @@ -263,7 +217,6 @@ public class MineOnlineBroadcast extends Plugin { String discordWebhookURL = mineOnlineConfig.getConfigString("discord-webhook-url"); serverName = mineOnlineConfig.getConfigString("server-name"); - if (discordToken != null && discordChannelID != null) { // Create the discord bot if token and channel are present discord = new DiscordChatBridge(new MinotarAvatarProvider(), discordChannelID, discordToken, discordWebhookURL, new IMessageRecievedListener() { @Override @@ -350,6 +303,14 @@ public class MineOnlineBroadcast extends Plugin { if (!initialized) return; + if (serverUUID != null) { + try { + MineOnlineAPI.deleteServerListing(serverUUID); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + unregister(); if (discord != null) discord.shutdown(); diff --git a/src/gg/codie/mineonline/api/MineOnlineAPI.java b/src/gg/codie/mineonline/api/MineOnlineAPI.java new file mode 100644 index 0000000..2e2fdfb --- /dev/null +++ b/src/gg/codie/mineonline/api/MineOnlineAPI.java @@ -0,0 +1,119 @@ +package gg.codie.mineonline.api; + +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; + +public class MineOnlineAPI { + public static void deleteServerListing(String uuid) throws IOException { + HttpURLConnection connection; + + URL url = new URL("https://mineonline.codie.gg/api/servers/" + uuid); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("DELETE"); + connection.connect(); + + if (connection != null) + connection.disconnect(); + } + + public static String listServer( + String ip, + String port, + int users, + int maxUsers, + String name, + boolean onlineMode, + String md5, + boolean whitelisted, + String[] playerNames, + String motd, + boolean dontListPlayers, + boolean useBetaEvolutionsAuth, + String serverIcon + ) { + HttpURLConnection connection = null; + + try { + URLClassLoader classLoader = new URLClassLoader(new URL[]{MineOnlineAPI.class.getProtectionDomain().getCodeSource().getLocation()}); + + Class jsonObjectClass = classLoader.loadClass("org.json.JSONObject"); + + Constructor jsonObjectConstructor = jsonObjectClass.getConstructor(); + Method jsonObjectPut = jsonObjectClass.getMethod("put", String.class, Object.class); + Method jsonObjectHas = jsonObjectClass.getMethod("has", String.class); + Method jsonObjectGetString = jsonObjectClass.getMethod("getString", String.class); + Method jsonObjectToString = jsonObjectClass.getMethod("toString"); + + Object jsonObject = jsonObjectConstructor.newInstance(); + if (ip != null) + jsonObjectPut.invoke(jsonObject, "ip", ip); + jsonObjectPut.invoke(jsonObject, "port", port); + if (users > -1 && !dontListPlayers) + jsonObjectPut.invoke(jsonObject, "users", users); + jsonObjectPut.invoke(jsonObject, "max", maxUsers); + jsonObjectPut.invoke(jsonObject, "name", name); + jsonObjectPut.invoke(jsonObject, "onlinemode", onlineMode); + jsonObjectPut.invoke(jsonObject, "md5", md5.toUpperCase()); + jsonObjectPut.invoke(jsonObject, "whitelisted", whitelisted); + if (!dontListPlayers) + jsonObjectPut.invoke(jsonObject, "players", playerNames); + jsonObjectPut.invoke(jsonObject, "motd", motd); + jsonObjectPut.invoke(jsonObject, "dontListPlayers", dontListPlayers); + jsonObjectPut.invoke(jsonObject, "useBetaEvolutionsAuth", useBetaEvolutionsAuth); + jsonObjectPut.invoke(jsonObject, "serverIcon", serverIcon); + + String json = (String) jsonObjectToString.invoke(jsonObject); + + URL url = new URL("https://mineonline.codie.gg/api/servers"); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestMethod("POST"); + connection.setDoInput(true); + connection.setDoOutput(true); + + connection.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8)); + connection.getOutputStream().flush(); + connection.getOutputStream().close(); + + InputStream is = connection.getInputStream(); + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + + StringBuilder response = new StringBuilder(); + String line; + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + rd.close(); + + + jsonObjectConstructor = jsonObjectClass.getConstructor(String.class); + + Object resObject = jsonObjectConstructor.newInstance(response.toString()); + if ((boolean)jsonObjectHas.invoke(resObject, "uuid")) { + return (String)jsonObjectGetString.invoke(resObject, "uuid"); + } else { + return null; + } + } catch (Exception e) { + + e.printStackTrace(); + } finally { + + if (connection != null) + connection.disconnect(); + } + + return null; + } +} diff --git a/src/gg/codie/mineonline/plugin/bukkit/MineOnlineBroadcastPlugin.java b/src/gg/codie/mineonline/plugin/bukkit/MineOnlineBroadcastPlugin.java index c405df4..2f224e7 100644 --- a/src/gg/codie/mineonline/plugin/bukkit/MineOnlineBroadcastPlugin.java +++ b/src/gg/codie/mineonline/plugin/bukkit/MineOnlineBroadcastPlugin.java @@ -2,6 +2,7 @@ package gg.codie.mineonline.plugin.bukkit; import gg.codie.common.input.EColorCodeColor; import gg.codie.minecraft.server.MinecraftColorCodeProvider; +import gg.codie.mineonline.api.MineOnlineAPI; import gg.codie.mineonline.discord.DiscordChatBridge; import gg.codie.mineonline.discord.IMessageRecievedListener; import gg.codie.mineonline.discord.IShutdownListener; @@ -12,16 +13,14 @@ import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.plugin.java.JavaPlugin; +import javax.imageio.IIOException; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.*; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.net.HttpURLConnection; import java.net.ServerSocket; -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.util.Arrays; +import java.util.Base64; import java.util.Properties; import java.util.logging.Logger; import java.util.regex.Matcher; @@ -36,6 +35,7 @@ public class MineOnlineBroadcastPlugin extends JavaPlugin { ProxyThread proxyThread; DiscordChatBridge discord; private MineOnlineConfig mineOnlineConfig; + String serverUUID; public void launchProxy() throws IOException { ServerSocket serverSocket = new ServerSocket(0); @@ -74,79 +74,6 @@ public class MineOnlineBroadcastPlugin extends JavaPlugin { return complete.digest(); } - public static void listServer( - String ip, - String port, - int users, - int maxUsers, - String name, - boolean onlineMode, - String md5, - boolean whitelisted, - String[] playerNames, - String motd, - boolean dontListPlayers, - boolean useBetaEvolutionsAuth - ) { - HttpURLConnection connection = null; - - try { - URLClassLoader classLoader = new URLClassLoader(new URL[]{MineOnlineBroadcastPlugin.class.getProtectionDomain().getCodeSource().getLocation()}); - - Class jsonObjectClass = classLoader.loadClass("org.json.JSONObject"); - - Constructor jsonObjectConstructor = jsonObjectClass.getConstructor(); - Method jsonObjectPut = jsonObjectClass.getMethod("put", String.class, Object.class); - Method jsonObjectToString = jsonObjectClass.getMethod("toString"); - - Object jsonObject = jsonObjectConstructor.newInstance(); - if (ip != null) - jsonObjectPut.invoke(jsonObject, "ip", ip); - jsonObjectPut.invoke(jsonObject, "port", port); - if (users > -1 && !dontListPlayers) - jsonObjectPut.invoke(jsonObject, "users", users); - jsonObjectPut.invoke(jsonObject, "max", maxUsers); - jsonObjectPut.invoke(jsonObject, "name", name); - jsonObjectPut.invoke(jsonObject, "onlinemode", onlineMode); - jsonObjectPut.invoke(jsonObject, "md5", md5); - jsonObjectPut.invoke(jsonObject, "whitelisted", whitelisted); - if (!dontListPlayers) - jsonObjectPut.invoke(jsonObject, "players", playerNames); - jsonObjectPut.invoke(jsonObject, "motd", motd); - jsonObjectPut.invoke(jsonObject, "dontListPlayers", dontListPlayers); - jsonObjectPut.invoke(jsonObject, "useBetaEvolutionsAuth", useBetaEvolutionsAuth); - - String json = (String) jsonObjectToString.invoke(jsonObject); - - URL url = new URL("https://mineonline.codie.gg/api/servers"); - connection = (HttpURLConnection) url.openConnection(); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setRequestMethod("POST"); - connection.setDoInput(true); - connection.setDoOutput(true); - - connection.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8)); - connection.getOutputStream().flush(); - connection.getOutputStream().close(); - - InputStream is = connection.getInputStream(); - BufferedReader rd = new BufferedReader(new InputStreamReader(is)); - - StringBuilder response = new StringBuilder(); - String line; - while ((line = rd.readLine()) != null) { - response.append(line); - response.append('\r'); - } - rd.close(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (connection != null) - connection.disconnect(); - } - } - @Override public void onEnable() { initialize(); @@ -189,7 +116,27 @@ public class MineOnlineBroadcastPlugin extends JavaPlugin { String[] playerNames = Arrays.stream(getServer().getOnlinePlayers()).map(Player::getName).collect(Collectors.toList()).toArray(new String[users]); - listServer( + String serverIcon = null; + + try { + BufferedImage serverIconBufferedImage = ImageIO.read(new File(System.getProperty("user.dir") + File.separator + "server-icon.png")); + if (serverIconBufferedImage.getHeight() <= 64 && serverIconBufferedImage.getWidth() <= 64) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + ImageIO.write(serverIconBufferedImage, "png", bos); + byte[] bytes = bos.toByteArray(); + Base64.Encoder encoder = Base64.getEncoder(); + serverIcon = encoder.encodeToString(bytes); + serverIcon = serverIcon.replace(System.lineSeparator(), ""); + } catch (IOException e) { + e.printStackTrace(); + } + } + } catch (IIOException ex) { + ex.printStackTrace(); + } + + serverUUID = MineOnlineAPI.listServer( ip, port, users, @@ -201,7 +148,8 @@ public class MineOnlineBroadcastPlugin extends JavaPlugin { playerNames, motd, dontListPlayers, - useBetaEvolutionsAuth + useBetaEvolutionsAuth, + serverIcon ); } catch (IOException ex) { // ignore. @@ -308,14 +256,16 @@ public class MineOnlineBroadcastPlugin extends JavaPlugin { this.getServer().getPluginManager().registerEvent(Event.Type.PLAYER_CHAT, this.listener, Event.Priority.Highest, this); } -// private void unregister() { -// if(registeredListener != null) -// etc.getLoader().removeListener(registeredListener); -// } - @Override public void onDisable() { - //unregister(); + if (serverUUID != null) { + try { + MineOnlineAPI.deleteServerListing(serverUUID); + } catch (Exception ex) { + + } + } + if (discord != null) discord.shutdown(); broadcastThread.interrupt();