From 98f974b63477419692397a5d193dffeb499b85e5 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sat, 2 Apr 2022 16:34:29 -0500 Subject: [PATCH] Add a list module --- src/main/java/dev/plex/HTTPDModule.java | 2 ++ .../dev/plex/request/impl/ListEndpoint.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/main/java/dev/plex/request/impl/ListEndpoint.java diff --git a/src/main/java/dev/plex/HTTPDModule.java b/src/main/java/dev/plex/HTTPDModule.java index e36be84..8805058 100644 --- a/src/main/java/dev/plex/HTTPDModule.java +++ b/src/main/java/dev/plex/HTTPDModule.java @@ -4,6 +4,7 @@ import dev.plex.config.ModuleConfig; import dev.plex.module.PlexModule; import dev.plex.request.impl.AdminsEndpoint; import dev.plex.request.impl.IndefBansEndpoint; +import dev.plex.request.impl.ListEndpoint; import dev.plex.util.PlexLog; import java.util.concurrent.atomic.AtomicReference; import lombok.Getter; @@ -60,6 +61,7 @@ public class HTTPDModule extends PlexModule new AdminsEndpoint(); new IndefBansEndpoint(); + new ListEndpoint(); server.setConnectors(new Connector[]{connector}); server.setHandler(context); diff --git a/src/main/java/dev/plex/request/impl/ListEndpoint.java b/src/main/java/dev/plex/request/impl/ListEndpoint.java new file mode 100644 index 0000000..8b6fcf5 --- /dev/null +++ b/src/main/java/dev/plex/request/impl/ListEndpoint.java @@ -0,0 +1,25 @@ +package dev.plex.request.impl; + +import com.google.gson.GsonBuilder; +import dev.plex.request.AbstractServlet; +import dev.plex.request.GetMapping; +import jakarta.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.List; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +public class ListEndpoint extends AbstractServlet +{ + List players = new ArrayList<>(); + + @GetMapping(endpoint = "/api/list/") + public String getOnlinePlayers(HttpServletRequest request) + { + for (Player player : Bukkit.getOnlinePlayers()) + { + players.add(player.getName()); + } + return new GsonBuilder().setPrettyPrinting().create().toJson(players.stream().toList()); + } +}