From a229e8f4705fcef5e5037b45b47ade8d7a21205c Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Tue, 12 Apr 2022 17:39:16 -0500 Subject: [PATCH] Add UI to punishments page --- src/main/java/dev/plex/HTTPDModule.java | 1 + .../request/impl/PunishmentsEndpoint.java | 112 ++++++++++++------ src/main/resources/punishments.html | 16 +++ 3 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 src/main/resources/punishments.html diff --git a/src/main/java/dev/plex/HTTPDModule.java b/src/main/java/dev/plex/HTTPDModule.java index 1fd3e70..16c5e18 100644 --- a/src/main/java/dev/plex/HTTPDModule.java +++ b/src/main/java/dev/plex/HTTPDModule.java @@ -35,6 +35,7 @@ public class HTTPDModule extends PlexModule @Override public void load() { + //moduleConfig = new ModuleConfig(this, this.getClass().getClassLoader().getResource("settings.yml").getFile()); moduleConfig = new ModuleConfig(this, "settings.yml"); } diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java index a0e1dc7..8f50b63 100644 --- a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java @@ -11,6 +11,9 @@ import dev.plex.request.GetMapping; import dev.plex.util.PlexLog; import dev.plex.util.adapter.LocalDateTimeSerializer; import jakarta.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -29,46 +32,87 @@ public class PunishmentsEndpoint extends AbstractServlet } if (request.getPathInfo() == null) { - return "Please specify the UUID of the player you would like to check.\nExample: /api/punishments/"; + /*StringBuilder contentBuilder = new StringBuilder(); + PlexLog.log(this.getClass().getClassLoader().getResource("punishments.html").getPath()); + try + { + BufferedReader in = new BufferedReader(new FileReader(this.getClass().getClassLoader().getResource("punishments.html").getFile().replace("!", ""))); + String str; + while ((str = in.readLine()) != null) + { + contentBuilder.append(str); + } + in.close(); + } + catch (IOException ignored) + { + } + return contentBuilder.toString();*/ + return """ + + +
+

Enter the UUID or username of the player you want to check

+ + + + +
+ + """; } + UUID pathUUID; + String pathPlexPlayer; + PlexPlayer punishedPlayer; try { - UUID uuid = UUID.fromString(request.getPathInfo().replace("/", "")); - final PlexPlayer punishedPlayer = DataUtils.getPlayer(uuid); - final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); - if (punishedPlayer.getPunishments().isEmpty()) - { - return "This player has been a good boy. They have no punishments! Or they've never been on the server before. Take your pick."; - } - if (player == null) - { - // If the player is null, give it to them without the IPs - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); - } - if (Plex.get().getSystem().equalsIgnoreCase("ranks")) - { - PlexLog.debug("Plex-HTTPD using ranks check"); - if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) - { - // Don't return IPs either if the person is not an Admin or above. - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); - } - } - else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) - { - PlexLog.debug("Plex-HTTPD using permissions check"); - final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid()); - if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.punishments.access")) - { - // If the person doesn't have permission, don't return IPs - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); - } - } - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()); + pathUUID = UUID.fromString(request.getPathInfo().replace("/", "")); + punishedPlayer = DataUtils.getPlayer(pathUUID); } catch (java.lang.IllegalArgumentException ignored) { - return "Invalid UUID"; + pathPlexPlayer = request.getPathInfo().replace("/", ""); + punishedPlayer = DataUtils.getPlayer(pathPlexPlayer); } + + final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); + if (punishedPlayer == null) + { + return "This player has never joined the server before."; + } + if (punishedPlayer.getPunishments().isEmpty()) + { + return "This player has been a good boy. They have no punishments!"; + } + if (player == null) + { + // If the player is null, give it to them without the IPs + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); + } + if (Plex.get().getSystem().equalsIgnoreCase("ranks")) + { + PlexLog.debug("Plex-HTTPD using ranks check"); + if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) + { + // Don't return IPs either if the person is not an Admin or above. + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); + } + } + else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) + { + PlexLog.debug("Plex-HTTPD using permissions check"); + final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid()); + if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.punishments.access")) + { + // If the person doesn't have permission, don't return IPs + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); + } + } + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()); } } diff --git a/src/main/resources/punishments.html b/src/main/resources/punishments.html new file mode 100644 index 0000000..73309ae --- /dev/null +++ b/src/main/resources/punishments.html @@ -0,0 +1,16 @@ + + +
+

Enter the UUID or username of the player you want to check

+ + + + +
+ + \ No newline at end of file