|
6 | 6 | import net.lax1dude.eaglercraft.backend.server.api.IEaglerPlayer; |
7 | 7 | import net.lax1dude.eaglercraft.backend.server.api.EnumWebSocketHeader; |
8 | 8 | import net.lax1dude.eaglercraft.backend.server.api.event.IEaglercraftInitializePlayerEvent; |
| 9 | +import net.lax1dude.eaglercraft.backend.server.api.event.IEaglercraftMOTDEvent; |
| 10 | +import net.lax1dude.eaglercraft.backend.server.api.query.IMOTDConnection; |
| 11 | + |
| 12 | +import java.io.InputStream; |
9 | 13 | import java.io.OutputStream; |
10 | 14 | import java.net.HttpURLConnection; |
| 15 | +import javax.imageio.ImageIO; |
| 16 | +import java.awt.image.BufferedImage; |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
11 | 19 | import java.net.URL; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.StandardCopyOption; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
12 | 24 |
|
13 | 25 | public class Base { |
14 | 26 | private static LoggerAdapter adapter; |
@@ -61,6 +73,60 @@ public static void handleConnection(IEaglercraftInitializePlayerEvent e) { |
61 | 73 | } |
62 | 74 | } |
63 | 75 |
|
| 76 | + public static void handleMOTD(IEaglercraftMOTDEvent e) { |
| 77 | + if (config.messages.motd.enabled) { |
| 78 | + IMOTDConnection conn = e.getMOTDConnection(); |
| 79 | + String origin = conn.getWebSocketHeader(EnumWebSocketHeader.HEADER_ORIGIN); |
| 80 | + List<String> m = List.of(config.messages.motd.text.split("\n")).stream() |
| 81 | + .map(line -> line |
| 82 | + .replace("%blocktype%", "origin") |
| 83 | + .replace("%easyblocktype%", "website") |
| 84 | + .replace("%blocked%", origin)) |
| 85 | + .map(line -> LegacyComponentSerializer.legacySection() |
| 86 | + .serialize(MiniMessage.miniMessage().deserialize(line))) |
| 87 | + .collect(Collectors.toList()); |
| 88 | + if ((origin != "null" || origin != null) && !config.blacklist.missing_origin) { |
| 89 | + for (String origin1 : config.blacklist.origins) { |
| 90 | + if (matches(origin, origin1)) { |
| 91 | + setMOTD(conn, m); |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | + } else { |
| 96 | + setMOTD(conn, m); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static void setMOTD(IMOTDConnection conn, List<String> m) { |
| 102 | + conn.setServerMOTD(m); |
| 103 | + conn.setPlayerTotal(0); |
| 104 | + conn.setPlayerMax(0); |
| 105 | + conn.setPlayerList(List.of()); |
| 106 | + if (config.messages.motd.icon != null && !config.messages.motd.icon.isEmpty()) |
| 107 | + try { |
| 108 | + BufferedImage img = ImageIO.read(new File(config.messages.motd.icon)); |
| 109 | + if (img.getWidth() != 64 || img.getHeight() != 64) { |
| 110 | + getLogger().warn("Icon must be 64x64"); |
| 111 | + return; |
| 112 | + } |
| 113 | + byte[] bytes = new byte[64 * 64 * 4]; |
| 114 | + for (int y = 0; y < 64; y++) { |
| 115 | + for (int x = 0; x < 64; x++) { |
| 116 | + int pixel = img.getRGB(x, y); |
| 117 | + int i = (y * 64 + x) * 4; |
| 118 | + bytes[i] = (byte) ((pixel >> 16) & 0xFF); |
| 119 | + bytes[i + 1] = (byte) ((pixel >> 8) & 0xFF); |
| 120 | + bytes[i + 2] = (byte) (pixel & 0xFF); |
| 121 | + bytes[i + 3] = (byte) ((pixel >> 24) & 0xFF); |
| 122 | + } |
| 123 | + } |
| 124 | + conn.setServerIcon(bytes); |
| 125 | + } catch (IOException ex) { |
| 126 | + getLogger().error(ex.toString()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
64 | 130 | public static boolean matches(String text1, String text2) { |
65 | 131 | return text1.toLowerCase().matches(text2.replace(".", "\\.").replaceAll("\\*", ".*").toLowerCase()); |
66 | 132 | } |
@@ -119,6 +185,19 @@ public static void webhook(IEaglerPlayer plr, String origin, String brand, Strin |
119 | 185 | } |
120 | 186 | } |
121 | 187 |
|
| 188 | + public static void init() { |
| 189 | + File motdIcon = new File(config.messages.motd.icon); |
| 190 | + if (!motdIcon.exists()) { |
| 191 | + try (InputStream in = ConfigManager.class.getResourceAsStream("/server-blocked.png")) { |
| 192 | + if (in != null) { |
| 193 | + Files.copy(in, motdIcon.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| 194 | + } |
| 195 | + } catch (IOException e) { |
| 196 | + getLogger().warn(e.toString()); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
122 | 201 | public static void reloadConfig() { |
123 | 202 | config = ConfigManager.loadConfig(adapter); |
124 | 203 | } |
|
0 commit comments