diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7b016a8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.compile.nullAnalysis.mode": "automatic" +} \ No newline at end of file diff --git a/Integrator/.gitignore b/Integrator/.gitignore index 12ddda9..fbc77ab 100644 --- a/Integrator/.gitignore +++ b/Integrator/.gitignore @@ -1,3 +1,4 @@ build .gradle .idea +/bin \ No newline at end of file diff --git a/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/BurgerPanelIntegrator.java b/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/BurgerPanelIntegrator.java index 3506e6d..8d93ee0 100644 --- a/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/BurgerPanelIntegrator.java +++ b/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/BurgerPanelIntegrator.java @@ -28,7 +28,7 @@ public final class BurgerPanelIntegrator extends JavaPlugin { @Override public void onEnable() { packetHandler = new PacketHandler(); - packetHandler.addPacket("status", new StatusPacket()); + packetHandler.addPacket("status", new StatusPacket(), true); logger = this.getSLF4JLogger(); burgerpanelSocketPath = System.getenv("BURGERPANEL_INTEGRATOR_PATH"); burgerpanelID = System.getenv("BURGERPANEL_INTEGRATOR_SERVER_ID"); @@ -53,6 +53,7 @@ public void onEnable() { authObj.put("dataType", "request"); authObj.put("type", "setID"); authObj.put("id", burgerpanelID); + Plugin plugin = this; try { write(authObj); } catch (IOException e) { @@ -77,7 +78,20 @@ public void run() { String bbString = new String(bb.array(), Charset.defaultCharset()).substring(0, byteCount); logger.info(bbString); JSONObject obj = (JSONObject) parser.parse(bbString); - pendingPackets.add(obj); + if(packetHandler.canRunAsync(obj)) { + new BukkitRunnable() { + @Override + public void run() { + try { + packetHandler.execute(obj); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + }.runTaskAsynchronously(plugin); + } else pendingPackets.add(obj); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/PacketHandler.java b/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/PacketHandler.java index 948c54a..b3fc4ac 100644 --- a/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/PacketHandler.java +++ b/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/PacketHandler.java @@ -3,13 +3,27 @@ import org.json.simple.JSONObject; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; public class PacketHandler { HashMap packets = new HashMap<>(); + ArrayList allowedAsyncPackets = new ArrayList<>(); void addPacket(String name, Packet packet) { packets.put(name, packet); } + void addPacket(String name, Packet packet, boolean allowAsync) { + addPacket(name, packet); + if(allowAsync) allowedAsyncPackets.add(name); + } + boolean canRunAsync(JSONObject data) { + Object packetNameProbablyString = data.get("packet"); + if(!(packetNameProbablyString instanceof String packetName)) { + BurgerPanelIntegrator.logger.error("BurgerPanel backend sent a invalid packet, .packet isn't a string!"); + return false; + } + return allowedAsyncPackets.contains(packetName); + } void execute(JSONObject data) throws IOException { Object packetNameProbablyString = data.get("packet"); if(!(packetNameProbablyString instanceof String packetName)) { diff --git a/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/packets/StatusPacket.java b/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/packets/StatusPacket.java index 20242bf..e2fa4a3 100644 --- a/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/packets/StatusPacket.java +++ b/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/packets/StatusPacket.java @@ -3,15 +3,38 @@ import io.github.theblueburger.burgerpanelintegrator.BurgerPanelIntegrator; import io.github.theblueburger.burgerpanelintegrator.Packet; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.io.IOException; +import java.util.Collection; public class StatusPacket extends Packet { @Override protected void execute(JSONObject data, String id) throws IOException { JSONObject responseObj = new JSONObject(); - responseObj.put("tps", Bukkit.getServer().getTPS()[0]); + Server server = Bukkit.getServer(); + responseObj.put("tps", server.getTPS()[0]); + Collection players = server.getOnlinePlayers(); + JSONArray playerArray = new JSONArray(); + for(Player player : players) { + JSONObject playerObject = new JSONObject(); + playerObject.put("name", player.getName()); + playerObject.put("uuid", player.getUniqueId().toString()); + Location location = player.getLocation(); + JSONObject locationObj = new JSONObject(); + locationObj.put("x", location.getX()); + locationObj.put("y", location.getY()); + locationObj.put("z", location.getZ()); + locationObj.put("world", location.getWorld().getName()); + playerObject.put("position", locationObj); + playerArray.add(playerObject); + } + responseObj.put("players", playerArray); BurgerPanelIntegrator.respond(id, responseObj); } } diff --git a/Server/src/serverIntegrator.ts b/Server/src/serverIntegrator.ts index 2cea996..1eedd8e 100644 --- a/Server/src/serverIntegrator.ts +++ b/Server/src/serverIntegrator.ts @@ -51,7 +51,7 @@ export default new class ServerIntegrator { if(Array.isArray(json)) return; if(!["request", "response"].includes(json.dataType)) return; if(json.dataType == "response") { - console.log(json); + console.log(JSON.stringify(json, null, 2)); if(!this.requestCallbacks[json.id]) return; this.requestCallbacks[json.id](json.data); return;