From afae76d9dd52255098c9459fe92d51fa7af737b3 Mon Sep 17 00:00:00 2001 From: Sodium-Aluminate Date: Wed, 15 Nov 2023 22:18:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99mention=20bot=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=8E=A9=E5=AE=B6=E5=88=97=E8=A1=A8=E7=9A=84?= =?UTF-8?q?http=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VTools/OnlinePlayerQueryService.java | 56 +++++++++++++++++++ src/main/java/de/strifel/VTools/VTools.java | 1 + 2 files changed, 57 insertions(+) create mode 100644 src/main/java/de/strifel/VTools/OnlinePlayerQueryService.java 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() {