From 8a26c888b1a2c566438bfdb14fb22af8d1318b94 Mon Sep 17 00:00:00 2001 From: Taah Date: Mon, 4 Apr 2022 01:47:51 -0700 Subject: [PATCH] add local date time type adapters --- .../dev/plex/request/AbstractServlet.java | 2 +- .../dev/plex/request/impl/AdminsEndpoint.java | 11 ++++--- .../request/impl/PunishmentsEndpoint.java | 33 +++---------------- 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/src/main/java/dev/plex/request/AbstractServlet.java b/src/main/java/dev/plex/request/AbstractServlet.java index 3362b5b..69378d8 100644 --- a/src/main/java/dev/plex/request/AbstractServlet.java +++ b/src/main/java/dev/plex/request/AbstractServlet.java @@ -3,6 +3,7 @@ package dev.plex.request; import com.google.common.collect.Lists; import dev.plex.HTTPDModule; import dev.plex.logging.Log; +import jakarta.servlet.AsyncContext; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; @@ -54,7 +55,6 @@ public class AbstractServlet extends HttpServlet String header = headerz.nextElement(); PlexLog.debug("Header: {0} Value {1}", header, req.getHeader(header)); }*/ - 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) diff --git a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java index f39fe00..79c641d 100644 --- a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java @@ -10,7 +10,10 @@ 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.time.LocalDateTime; import java.util.UUID; import java.util.stream.Collectors; import org.bukkit.Bukkit; @@ -30,7 +33,7 @@ public class AdminsEndpoint extends AbstractServlet if (player == null) { // 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())); + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); } if (Plex.get().getSystem().equalsIgnoreCase("ranks")) { @@ -38,7 +41,7 @@ public class AdminsEndpoint 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().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList())); + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); } } else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) @@ -48,9 +51,9 @@ public class AdminsEndpoint extends AbstractServlet 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())); + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); } } - return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()); + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()); } } diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java index 6e821d7..a4f9ad6 100644 --- a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java @@ -5,20 +5,18 @@ 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 org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import java.time.LocalDateTime; +import java.util.UUID; + public class PunishmentsEndpoint extends AbstractServlet { @GetMapping(endpoint = "/api/punishments/") @@ -36,7 +34,7 @@ public class PunishmentsEndpoint extends AbstractServlet try { UUID uuid = UUID.fromString(request.getPathInfo().replace("/", "")); - final PunishedPlayer punishedPlayer = new PunishedPlayer(uuid); + final PlexPlayer punishedPlayer = DataUtils.getPlayer(uuid); final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (punishedPlayer.getPunishments().isEmpty()) { @@ -73,27 +71,4 @@ public class PunishmentsEndpoint extends AbstractServlet return "Invalid UUID"; } } - - 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; - } }