add local date time type adapters

This commit is contained in:
Taah 2022-04-04 01:47:51 -07:00
parent 5d6a2deb2e
commit 8a26c888b1
3 changed files with 12 additions and 34 deletions

View File

@ -3,6 +3,7 @@ package dev.plex.request;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import dev.plex.HTTPDModule; import dev.plex.HTTPDModule;
import dev.plex.logging.Log; import dev.plex.logging.Log;
import jakarta.servlet.AsyncContext;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
@ -54,7 +55,6 @@ public class AbstractServlet extends HttpServlet
String header = headerz.nextElement(); String header = headerz.nextElement();
PlexLog.debug("Header: {0} Value {1}", header, req.getHeader(header)); 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 -> 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) if (mapping.headers != null)

View File

@ -10,7 +10,10 @@ import dev.plex.rank.enums.Rank;
import dev.plex.request.AbstractServlet; import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.adapter.LocalDateTimeSerializer;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -30,7 +33,7 @@ public class AdminsEndpoint extends AbstractServlet
if (player == null) if (player == null)
{ {
// This likely means they've never joined the server before. That's okay. We can just not return IPs. // 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")) if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{ {
@ -38,7 +41,7 @@ public class AdminsEndpoint extends AbstractServlet
if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{ {
// Don't return IPs either if the person is not an Admin or above. // 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")) 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 (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access"))
{ {
// If the person doesn't have permission, don't return IPs // 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());
} }
} }

View File

@ -5,20 +5,18 @@ import dev.plex.HTTPDModule;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.cache.DataUtils; import dev.plex.cache.DataUtils;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.player.PunishedPlayer;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.request.AbstractServlet; import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.adapter.LocalDateTimeSerializer; import dev.plex.util.adapter.LocalDateTimeSerializer;
import jakarta.servlet.http.HttpServletRequest; 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.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import java.time.LocalDateTime;
import java.util.UUID;
public class PunishmentsEndpoint extends AbstractServlet public class PunishmentsEndpoint extends AbstractServlet
{ {
@GetMapping(endpoint = "/api/punishments/") @GetMapping(endpoint = "/api/punishments/")
@ -36,7 +34,7 @@ public class PunishmentsEndpoint extends AbstractServlet
try try
{ {
UUID uuid = UUID.fromString(request.getPathInfo().replace("/", "")); 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); final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (punishedPlayer.getPunishments().isEmpty()) if (punishedPlayer.getPunishments().isEmpty())
{ {
@ -73,27 +71,4 @@ public class PunishmentsEndpoint extends AbstractServlet
return "Invalid UUID"; 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;
}
} }