From f41d09d7bf4d3fddc6709ae8768851e3561284ad Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sat, 2 Apr 2022 15:51:14 -0500 Subject: [PATCH 1/6] Organize a few things --- src/main/java/dev/plex/HTTPDModule.java | 11 ++-- src/main/java/dev/plex/logging/Log.java | 25 +++++++++ .../dev/plex/request/AbstractServlet.java | 13 ++--- ...{GetEndpoints.java => AdminsEndpoint.java} | 44 ++-------------- .../plex/request/impl/IndefBansEndpoint.java | 51 +++++++++++++++++++ src/main/resources/settings.yml | 3 +- 6 files changed, 92 insertions(+), 55 deletions(-) create mode 100644 src/main/java/dev/plex/logging/Log.java rename src/main/java/dev/plex/request/impl/{GetEndpoints.java => AdminsEndpoint.java} (57%) create mode 100644 src/main/java/dev/plex/request/impl/IndefBansEndpoint.java diff --git a/src/main/java/dev/plex/HTTPDModule.java b/src/main/java/dev/plex/HTTPDModule.java index 4517e8e..e36be84 100644 --- a/src/main/java/dev/plex/HTTPDModule.java +++ b/src/main/java/dev/plex/HTTPDModule.java @@ -2,7 +2,8 @@ package dev.plex; import dev.plex.config.ModuleConfig; import dev.plex.module.PlexModule; -import dev.plex.request.impl.GetEndpoints; +import dev.plex.request.impl.AdminsEndpoint; +import dev.plex.request.impl.IndefBansEndpoint; import dev.plex.util.PlexLog; import java.util.concurrent.atomic.AtomicReference; import lombok.Getter; @@ -27,12 +28,11 @@ public class HTTPDModule extends PlexModule @Getter private static Permission permissions = null; - private ModuleConfig moduleConfig; + public static ModuleConfig moduleConfig; @Override public void load() { - moduleConfig = new ModuleConfig(this, "settings.yml"); } @@ -58,13 +58,13 @@ public class HTTPDModule extends PlexModule connector.setHost(moduleConfig.getString("server.bind-address")); connector.setPort(moduleConfig.getInt("server.port")); - new GetEndpoints(); + new AdminsEndpoint(); + new IndefBansEndpoint(); server.setConnectors(new Connector[]{connector}); server.setHandler(context); atomicServer.set(server); - PlexLog.debug("Set atomicServer value? {0}", atomicServer.get() != null); try { server.start(); @@ -76,6 +76,7 @@ public class HTTPDModule extends PlexModule } }, "Jetty-Server"); serverThread.start(); + PlexLog.log("Starting Jetty server on port " + moduleConfig.getInt("server.port")); } @Override diff --git a/src/main/java/dev/plex/logging/Log.java b/src/main/java/dev/plex/logging/Log.java new file mode 100644 index 0000000..99d5360 --- /dev/null +++ b/src/main/java/dev/plex/logging/Log.java @@ -0,0 +1,25 @@ +package dev.plex.logging; + +import dev.plex.HTTPDModule; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; + + +public class Log +{ + public static void log(String message, Object... strings) + { + for (int i = 0; i < strings.length; i++) + { + if (message.contains("{" + i + "}")) + { + message = message.replace("{" + i + "}", strings[i].toString()); + } + } + + if (HTTPDModule.moduleConfig.getBoolean("server.logging")) + { + Bukkit.getConsoleSender().sendMessage(String.format(ChatColor.DARK_AQUA + "[Plex HTTPD] " + ChatColor.GRAY + "%s", message)); + } + } +} diff --git a/src/main/java/dev/plex/request/AbstractServlet.java b/src/main/java/dev/plex/request/AbstractServlet.java index 4db509e..3beebb4 100644 --- a/src/main/java/dev/plex/request/AbstractServlet.java +++ b/src/main/java/dev/plex/request/AbstractServlet.java @@ -2,7 +2,7 @@ package dev.plex.request; import com.google.common.collect.Lists; import dev.plex.HTTPDModule; -import dev.plex.util.PlexLog; +import dev.plex.logging.Log; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; @@ -16,7 +16,6 @@ import org.eclipse.jetty.servlet.ServletHolder; public class AbstractServlet extends HttpServlet { - private final List GET_MAPPINGS = Lists.newArrayList(); public AbstractServlet() @@ -42,16 +41,13 @@ public class AbstractServlet extends HttpServlet @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - PlexLog.debug("Context Path: " + req.getHttpServletMapping().getMatchValue()); - String ipAddress = req.getHeader("X-FORWARDED-FOR"); if (ipAddress == null) { ipAddress = req.getRemoteAddr(); } - PlexLog.debug("HTTP Remote IP: " + ipAddress); - PlexLog.debug("HTTP Local IP: " + req.getLocalAddr()); + + Log.log(ipAddress + " visited endpoint " + req.getHttpServletMapping().getMatchValue()); /*Enumeration headerz = req.getHeaderNames(); while (headerz.hasMoreElements()) { @@ -59,7 +55,6 @@ public class AbstractServlet extends HttpServlet PlexLog.debug("Header: {0} Value {1}", header, req.getHeader(header)); }*/ - PlexLog.debug("-------------------------"); GET_MAPPINGS.stream().filter(mapping -> mapping.getMapping().endpoint().substring(1, mapping.getMapping().endpoint().length() - 1).equalsIgnoreCase(req.getHttpServletMapping().getMatchValue())).forEach(mapping -> { if (mapping.headers != null) @@ -86,7 +81,7 @@ public class AbstractServlet extends HttpServlet @Data - public class Mapping + public static class Mapping { private final Method method; private final GetMapping mapping; diff --git a/src/main/java/dev/plex/request/impl/GetEndpoints.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java similarity index 57% rename from src/main/java/dev/plex/request/impl/GetEndpoints.java rename to src/main/java/dev/plex/request/impl/AdminsEndpoint.java index b150129..d2cd14b 100644 --- a/src/main/java/dev/plex/request/impl/GetEndpoints.java +++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java @@ -11,15 +11,12 @@ import dev.plex.request.AbstractServlet; import dev.plex.request.GetMapping; import dev.plex.util.PlexLog; import jakarta.servlet.http.HttpServletRequest; +import java.util.UUID; +import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import java.util.UUID; -import java.util.stream.Collectors; - -//@RestController -//@RequestMapping("/api/admins") -public class GetEndpoints extends AbstractServlet +public class AdminsEndpoint extends AbstractServlet { @GetMapping(endpoint = "/api/admins/") public String getAdmins(HttpServletRequest request) @@ -32,7 +29,7 @@ public class GetEndpoints extends AbstractServlet final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (player == null) { - return "Couldn't load your IP Address: " + ipAddress + ". Check if your SSL settings are setup correctly."; + return "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?"; } if (Plex.get().getSystem().equalsIgnoreCase("ranks")) { @@ -53,37 +50,4 @@ public class GetEndpoints extends AbstractServlet } return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()); } - - @GetMapping(endpoint = "/api/indefbans/") - public String getBans(HttpServletRequest request) - { - String ipAddress = request.getHeader("X-FORWARDED-FOR"); - if (ipAddress == null) - { - ipAddress = request.getRemoteAddr(); - } - final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); - if (player == null) - { - return "Couldn't load your IP Address: " + ipAddress + ". Check if your SSL settings are setup correctly."; - } - if (Plex.get().getSystem().equalsIgnoreCase("ranks")) - { - PlexLog.debug("Plex-HTTPD using ranks check"); - if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) - { - return "Not a high enough rank to view this page."; - } - } - else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) - { - PlexLog.debug("Plex-HTTPD using permissions check"); - final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(player.getUuid())); - if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.indefbans.access")) - { - return "Not enough permissions to view this page."; - } - } - return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList()); - } } diff --git a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java new file mode 100644 index 0000000..448e90d --- /dev/null +++ b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java @@ -0,0 +1,51 @@ +package dev.plex.request.impl; + +import com.google.gson.GsonBuilder; +import dev.plex.HTTPDModule; +import dev.plex.Plex; +import dev.plex.cache.DataUtils; +import dev.plex.player.PlexPlayer; +import dev.plex.rank.enums.Rank; +import dev.plex.request.AbstractServlet; +import dev.plex.request.GetMapping; +import dev.plex.util.PlexLog; +import jakarta.servlet.http.HttpServletRequest; +import java.util.UUID; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +public class IndefBansEndpoint extends AbstractServlet +{ + @GetMapping(endpoint = "/api/indefbans/") + public String getBans(HttpServletRequest request) + { + String ipAddress = request.getHeader("X-FORWARDED-FOR"); + if (ipAddress == null) + { + ipAddress = request.getRemoteAddr(); + } + final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); + if (player == null) + { + return "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?"; + } + if (Plex.get().getSystem().equalsIgnoreCase("ranks")) + { + PlexLog.debug("Plex-HTTPD using ranks check"); + if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) + { + return "Not a high enough rank to view this page."; + } + } + else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) + { + PlexLog.debug("Plex-HTTPD using permissions check"); + final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(player.getUuid())); + if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.indefbans.access")) + { + return "Not enough permissions to view this page."; + } + } + return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList()); + } +} diff --git a/src/main/resources/settings.yml b/src/main/resources/settings.yml index 51742e3..3dae029 100644 --- a/src/main/resources/settings.yml +++ b/src/main/resources/settings.yml @@ -1,3 +1,4 @@ server: bind-address: 0.0.0.0 - port: 27192 \ No newline at end of file + port: 27192 + logging: false \ No newline at end of file From 98f974b63477419692397a5d193dffeb499b85e5 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sat, 2 Apr 2022 16:34:29 -0500 Subject: [PATCH 2/6] 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()); + } +} From 1096ed1e09d33f4ad22847e096b7476dfe7374bf Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sat, 2 Apr 2022 16:50:15 -0500 Subject: [PATCH 3/6] Just say fuck you if an IP wasn't detected --- src/main/java/dev/plex/request/impl/AdminsEndpoint.java | 4 ++-- src/main/java/dev/plex/request/impl/IndefBansEndpoint.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java index d2cd14b..ad732d9 100644 --- a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java @@ -21,10 +21,10 @@ public class AdminsEndpoint extends AbstractServlet @GetMapping(endpoint = "/api/admins/") public String getAdmins(HttpServletRequest request) { - String ipAddress = request.getHeader("X-FORWARDED-FOR"); + String ipAddress = request.getRemoteAddr(); if (ipAddress == null) { - ipAddress = request.getRemoteAddr(); + return "An IP address could not be detected. Please ensure you are connecting using IPv4."; } final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (player == null) diff --git a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java index 448e90d..20f5932 100644 --- a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java +++ b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java @@ -19,10 +19,10 @@ public class IndefBansEndpoint extends AbstractServlet @GetMapping(endpoint = "/api/indefbans/") public String getBans(HttpServletRequest request) { - String ipAddress = request.getHeader("X-FORWARDED-FOR"); + String ipAddress = request.getRemoteAddr(); if (ipAddress == null) { - ipAddress = request.getRemoteAddr(); + return "An IP address could not be detected. Please ensure you are connecting using IPv4."; } final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (player == null) From 3dbc5e78bde6edf767388ec28327e19c7441b68f Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sat, 2 Apr 2022 18:34:19 -0500 Subject: [PATCH 4/6] Punishments somewhat working --- src/main/java/dev/plex/HTTPDModule.java | 2 + .../dev/plex/request/AbstractServlet.java | 5 +- .../dev/plex/request/impl/AdminsEndpoint.java | 5 +- .../request/impl/PunishmentsEndpoint.java | 97 +++++++++++++++++++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java diff --git a/src/main/java/dev/plex/HTTPDModule.java b/src/main/java/dev/plex/HTTPDModule.java index 8805058..1fd3e70 100644 --- a/src/main/java/dev/plex/HTTPDModule.java +++ b/src/main/java/dev/plex/HTTPDModule.java @@ -5,6 +5,7 @@ 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.request.impl.PunishmentsEndpoint; import dev.plex.util.PlexLog; import java.util.concurrent.atomic.AtomicReference; import lombok.Getter; @@ -62,6 +63,7 @@ public class HTTPDModule extends PlexModule new AdminsEndpoint(); new IndefBansEndpoint(); new ListEndpoint(); + new PunishmentsEndpoint(); server.setConnectors(new Connector[]{connector}); server.setHandler(context); diff --git a/src/main/java/dev/plex/request/AbstractServlet.java b/src/main/java/dev/plex/request/AbstractServlet.java index 3beebb4..3362b5b 100644 --- a/src/main/java/dev/plex/request/AbstractServlet.java +++ b/src/main/java/dev/plex/request/AbstractServlet.java @@ -41,10 +41,10 @@ public class AbstractServlet extends HttpServlet @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - String ipAddress = req.getHeader("X-FORWARDED-FOR"); + String ipAddress = req.getRemoteAddr(); if (ipAddress == null) { - ipAddress = req.getRemoteAddr(); + ipAddress = req.getHeader("X-FORWARDED-FOR"); } Log.log(ipAddress + " visited endpoint " + req.getHttpServletMapping().getMatchValue()); @@ -79,7 +79,6 @@ public class AbstractServlet extends HttpServlet }); } - @Data public static class Mapping { diff --git a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java index ad732d9..f39fe00 100644 --- a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java @@ -29,13 +29,15 @@ public class AdminsEndpoint extends AbstractServlet final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (player == null) { - return "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?"; + // This likely means they've never joined the server before. That's okay. We can just not return IPs. + return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.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().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList())); } } @@ -45,6 +47,7 @@ public class AdminsEndpoint extends AbstractServlet final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(player.getUuid())); if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access")) { + // If the person doesn't have permission, don't return IPs return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList())); } } diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java new file mode 100644 index 0000000..81b1932 --- /dev/null +++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java @@ -0,0 +1,97 @@ +package dev.plex.request.impl; + +import com.google.common.collect.Lists; +import com.google.gson.GsonBuilder; +import dev.plex.HTTPDModule; +import dev.plex.Plex; +import dev.plex.cache.DataUtils; +import dev.plex.player.PlexPlayer; +import dev.plex.player.PunishedPlayer; +import dev.plex.rank.enums.Rank; +import dev.plex.request.AbstractServlet; +import dev.plex.request.GetMapping; +import dev.plex.util.PlexLog; +import dev.plex.util.adapter.LocalDateTimeSerializer; +import jakarta.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.UUID; +import java.util.stream.Collectors; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +public class PunishmentsEndpoint extends AbstractServlet +{ + @GetMapping(endpoint = "/api/punishments/") + public String getPunishments(HttpServletRequest request) + { + String ipAddress = request.getRemoteAddr(); + if (ipAddress == null) + { + return "An IP address could not be detected. Please ensure you are connecting using IPv4."; + } + if (request.getPathInfo() == null) + { + return "Please specify the UUID of the player you would like to check."; + } + try + { + UUID uuid = UUID.fromString(request.getPathInfo().replace("/", "")); + final PunishedPlayer punishedPlayer = new PunishedPlayer(uuid); + final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); + 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(""))); + } + 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(""))); + } + } + else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) + { + PlexLog.debug("Plex-HTTPD using permissions check"); + final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(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(""))); + } + } + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()); + } + catch (java.lang.IllegalArgumentException ignored) + { + return "Invalid UUID string"; + } + } + + public File getPunishmentsFile(UUID uuid) + { + File folder = new File(Plex.get().getDataFolder() + File.separator + "punishments"); + if (!folder.exists()) + { + folder.mkdir(); + } + + File file = new File(folder, "" + uuid.toString() + ".json"); + if (!file.exists()) + { + try + { + file.createNewFile(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return file; + } +} From eea17dff800131fc2318170e84e6ba52ea26af18 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sat, 2 Apr 2022 18:50:27 -0500 Subject: [PATCH 5/6] Everything works now --- .../dev/plex/request/impl/PunishmentsEndpoint.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java index 81b1932..9610f5d 100644 --- a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java @@ -1,6 +1,5 @@ package dev.plex.request.impl; -import com.google.common.collect.Lists; import com.google.gson.GsonBuilder; import dev.plex.HTTPDModule; import dev.plex.Plex; @@ -17,7 +16,6 @@ import java.io.File; import java.io.IOException; import java.time.LocalDateTime; import java.util.UUID; -import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -40,10 +38,14 @@ public class PunishmentsEndpoint extends AbstractServlet UUID uuid = UUID.fromString(request.getPathInfo().replace("/", "")); final PunishedPlayer punishedPlayer = new PunishedPlayer(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(""))); + 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")) { @@ -51,7 +53,7 @@ public class PunishmentsEndpoint extends AbstractServlet 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(""))); + 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")) @@ -61,14 +63,14 @@ public class PunishmentsEndpoint extends AbstractServlet 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(""))); + 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()); } catch (java.lang.IllegalArgumentException ignored) { - return "Invalid UUID string"; + return "Invalid UUID"; } } From c55a184fb13a0d70547e5e35ffa6365f4488f2fa Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sun, 3 Apr 2022 16:52:51 -0500 Subject: [PATCH 6/6] Clarification --- src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java index 9610f5d..6e821d7 100644 --- a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java @@ -31,7 +31,7 @@ public class PunishmentsEndpoint extends AbstractServlet } if (request.getPathInfo() == null) { - return "Please specify the UUID of the player you would like to check."; + return "Please specify the UUID of the player you would like to check.\nExample: /api/punishments/"; } try {