|
| 1 | +package dev.cayadev.msms.bridge; |
| 2 | + |
| 3 | +import org.bukkit.Bukkit; |
| 4 | +import org.bukkit.Location; |
| 5 | +import org.bukkit.World; |
| 6 | +import org.bukkit.entity.Player; |
| 7 | +import org.bukkit.event.EventHandler; |
| 8 | +import org.bukkit.event.Listener; |
| 9 | +import org.bukkit.event.entity.PlayerDeathEvent; |
| 10 | +import org.bukkit.plugin.java.JavaPlugin; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +/** |
| 15 | + * MSMS Bridge - reports telemetry the console cannot otherwise expose. |
| 16 | + * |
| 17 | + * Transport is the server's own standard output: one marked line per message. |
| 18 | + * That needs no port, no socket, no firewall rule and no credentials, and it |
| 19 | + * works identically for a LAN server, a box behind NAT, and a host with every |
| 20 | + * port closed. See docs/bridge-protocol.md for the wire format. |
| 21 | + * |
| 22 | + * Everything here runs on the main thread and does almost nothing: reading the |
| 23 | + * tick rate and the player list must not itself become a source of lag. |
| 24 | + */ |
| 25 | +public final class MsmsBridge extends JavaPlugin implements Listener { |
| 26 | + |
| 27 | + private static final String MARKER = "[MSMS-BRIDGE]"; |
| 28 | + private static final int PROTOCOL = 1; |
| 29 | + |
| 30 | + private long intervalMs; |
| 31 | + |
| 32 | + @Override |
| 33 | + public void onEnable() { |
| 34 | + saveDefaultConfig(); |
| 35 | + int seconds = Math.max(1, getConfig().getInt("interval-seconds", 5)); |
| 36 | + this.intervalMs = seconds * 1000L; |
| 37 | + |
| 38 | + emitHello(); |
| 39 | + getServer().getPluginManager().registerEvents(this, this); |
| 40 | + |
| 41 | + // Sync, because the player list and their locations are main-thread |
| 42 | + // state. The work is a handful of field reads every few seconds. |
| 43 | + long ticks = seconds * 20L; |
| 44 | + getServer().getScheduler().runTaskTimer(this, this::heartbeat, ticks, ticks); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onDisable() { |
| 49 | + emit("{\"v\":" + PROTOCOL + ",\"t\":\"bye\"}"); |
| 50 | + } |
| 51 | + |
| 52 | + // ---- messages ---- |
| 53 | + |
| 54 | + private void emitHello() { |
| 55 | + StringBuilder b = new StringBuilder(); |
| 56 | + b.append("{\"v\":").append(PROTOCOL).append(",\"t\":\"hello\""); |
| 57 | + Json.field(b, "plugin", getName()); |
| 58 | + Json.field(b, "pluginVersion", getPluginMeta().getVersion()); |
| 59 | + Json.field(b, "server", getServer().getName()); |
| 60 | + Json.field(b, "mc", getServer().getMinecraftVersion()); |
| 61 | + b.append(",\"interval\":").append(intervalMs); |
| 62 | + b.append("}"); |
| 63 | + emit(b.toString()); |
| 64 | + } |
| 65 | + |
| 66 | + private void heartbeat() { |
| 67 | + emitTick(); |
| 68 | + emitPlayers(); |
| 69 | + } |
| 70 | + |
| 71 | + private void emitTick() { |
| 72 | + double[] tps = getServer().getTPS(); |
| 73 | + StringBuilder b = new StringBuilder(); |
| 74 | + b.append("{\"v\":").append(PROTOCOL).append(",\"t\":\"tick\""); |
| 75 | + // The app treats these as reported and does no clamping, so a warming-up |
| 76 | + // server showing 20.4 stays visible rather than being tidied away. |
| 77 | + b.append(",\"tps\":").append(Json.num(tps.length > 0 ? tps[0] : Double.NaN)); |
| 78 | + b.append(",\"tps5\":").append(Json.num(tps.length > 1 ? tps[1] : Double.NaN)); |
| 79 | + b.append(",\"tps15\":").append(Json.num(tps.length > 2 ? tps[2] : Double.NaN)); |
| 80 | + b.append(",\"mspt\":").append(Json.num(getServer().getAverageTickTime())); |
| 81 | + b.append("}"); |
| 82 | + emit(b.toString()); |
| 83 | + } |
| 84 | + |
| 85 | + private void emitPlayers() { |
| 86 | + List<? extends Player> online = List.copyOf(getServer().getOnlinePlayers()); |
| 87 | + StringBuilder b = new StringBuilder(); |
| 88 | + b.append("{\"v\":").append(PROTOCOL).append(",\"t\":\"players\",\"online\":").append(online.size()); |
| 89 | + b.append(",\"list\":["); |
| 90 | + boolean first = true; |
| 91 | + for (Player p : online) { |
| 92 | + if (!first) b.append(','); |
| 93 | + first = false; |
| 94 | + Location loc = p.getLocation(); |
| 95 | + World w = loc.getWorld(); |
| 96 | + b.append('{'); |
| 97 | + b.append("\"name\":").append(Json.str(p.getName())); |
| 98 | + b.append(",\"uuid\":").append(Json.str(p.getUniqueId().toString())); |
| 99 | + if (w != null) { |
| 100 | + b.append(",\"world\":").append(Json.str(w.getName())); |
| 101 | + b.append(",\"dim\":").append(Json.str(dimension(w))); |
| 102 | + } |
| 103 | + b.append(",\"x\":").append(Json.num(loc.getX())); |
| 104 | + b.append(",\"y\":").append(Json.num(loc.getY())); |
| 105 | + b.append(",\"z\":").append(Json.num(loc.getZ())); |
| 106 | + b.append('}'); |
| 107 | + } |
| 108 | + b.append("]}"); |
| 109 | + emit(b.toString()); |
| 110 | + } |
| 111 | + |
| 112 | + @EventHandler |
| 113 | + public void onDeath(PlayerDeathEvent e) { |
| 114 | + // A death is exactly the kind of thing the console cannot report in a |
| 115 | + // structured way - the death message is free text and locale-dependent. |
| 116 | + Player p = e.getEntity(); |
| 117 | + Location loc = p.getLocation(); |
| 118 | + World w = loc.getWorld(); |
| 119 | + StringBuilder b = new StringBuilder(); |
| 120 | + b.append("{\"v\":").append(PROTOCOL).append(",\"t\":\"event\",\"kind\":\"player.death\""); |
| 121 | + Json.field(b, "text", p.getName()); |
| 122 | + b.append(",\"data\":{"); |
| 123 | + b.append("\"player\":").append(Json.str(p.getName())); |
| 124 | + if (w != null) b.append(",\"world\":").append(Json.str(w.getName())); |
| 125 | + b.append(",\"x\":").append(Json.num(loc.getX())); |
| 126 | + b.append(",\"y\":").append(Json.num(loc.getY())); |
| 127 | + b.append(",\"z\":").append(Json.num(loc.getZ())); |
| 128 | + b.append("}}"); |
| 129 | + emit(b.toString()); |
| 130 | + } |
| 131 | + |
| 132 | + // ---- plumbing ---- |
| 133 | + |
| 134 | + private static String dimension(World w) { |
| 135 | + switch (w.getEnvironment()) { |
| 136 | + case NETHER: |
| 137 | + return "nether"; |
| 138 | + case THE_END: |
| 139 | + return "the_end"; |
| 140 | + default: |
| 141 | + return "overworld"; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * One message per line. A newline inside the payload would split the |
| 147 | + * message, so every string is escaped and nothing is pretty-printed. |
| 148 | + */ |
| 149 | + private void emit(String json) { |
| 150 | + System.out.println(MARKER + " " + json); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments