diff --git a/src/main/java/de/strifel/VTools/OnlinePlayerQueryService.java b/src/main/java/de/strifel/VTools/OnlinePlayerQueryService.java new file mode 100644 index 0000000..ff3fe2d --- /dev/null +++ b/src/main/java/de/strifel/VTools/OnlinePlayerQueryService.java @@ -0,0 +1,56 @@ +package de.strifel.VTools; + +import com.google.gson.Gson; +import com.sun.net.httpserver.HttpServer; +import com.velocitypowered.api.proxy.Player; +import org.jetbrains.annotations.TestOnly; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Collection; + +public class OnlinePlayerQueryService { + + + private final VTools plugin; + + public OnlinePlayerQueryService(VTools plugin) { + this.plugin = plugin; + } + + private static final Gson gson = new Gson(); + + public void register() { + try { + HttpServer server = HttpServer.create(new InetSocketAddress(17611), 0); + + server.createContext("/api/getOnlinePlayers", exchange -> { + Collection players = plugin.getServer().getAllPlayers(); + ArrayList playerNames = new ArrayList<>(players.size()); + for (Player player : players) { + playerNames.add(player.getUsername()); + } + exchange.getResponseHeaders().set("Content-Type", "application/json"); + exchange.sendResponseHeaders(200, 0); + + try (OutputStream os = exchange.getResponseBody()) { + String response = gson.toJson(playerNames); + os.write(response.getBytes()); + } + }); + + server.start(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @TestOnly + public static void main(String[] args) { + ArrayList arrayList = new ArrayList<>(); + arrayList.add("jerry"); + System.out.println(gson.toJson(arrayList)); + } +} diff --git a/src/main/java/de/strifel/VTools/VTools.java b/src/main/java/de/strifel/VTools/VTools.java index eaf1296..7a14367 100644 --- a/src/main/java/de/strifel/VTools/VTools.java +++ b/src/main/java/de/strifel/VTools/VTools.java @@ -46,6 +46,7 @@ public class VTools { new PlayerStatus(this).register(); new GlobalChat(this).register(); new ServerCloser(this).register(); + new OnlinePlayerQueryService(this).register(); } public ProxyServer getServer() {