Skip to content

Commit

Permalink
It works! I hope...
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueBurger committed Nov 6, 2023
1 parent 46a7956 commit 1bd1144
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.compile.nullAnalysis.mode": "automatic"
}
1 change: 1 addition & 0 deletions Integrator/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
.gradle
.idea
/bin
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Packet> packets = new HashMap<>();
ArrayList<String> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Player> 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);
}
}
2 changes: 1 addition & 1 deletion Server/src/serverIntegrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1bd1144

Please sign in to comment.