Actually make the HTTPD look nice

This commit is contained in:
2022-04-17 16:39:08 -05:00
parent e6e8439a72
commit 2ce0a1e698
14 changed files with 390 additions and 73 deletions
@@ -11,44 +11,24 @@ 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.IOException;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
public class PunishmentsEndpoint extends AbstractServlet
{
private static final String TITLE = "Punishments - Plex HTTPD";
@GetMapping(endpoint = "/api/punishments/")
public String getPunishments(HttpServletRequest request)
{
String ipAddress = request.getRemoteAddr();
if (ipAddress == null)
{
return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4.");
return punishmentsHTML("An IP address could not be detected. Please ensure you are connecting using IPv4.");
}
if (request.getPathInfo() == null)
if (request.getPathInfo() == null || request.getPathInfo().equals("/"))
{
StringBuilder contentBuilder = new StringBuilder();
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(Objects.requireNonNull(this.getClass().getResourceAsStream("/httpd/punishments.html"))));
String str;
while ((str = in.readLine()) != null)
{
contentBuilder.append(str);
}
in.close();
}
catch (IOException ignored)
{
}
return contentBuilder.toString();
return readFile(this.getClass().getResourceAsStream("/httpd/punishments.html"));
}
UUID pathUUID;
String pathPlexPlayer;
@@ -67,16 +47,16 @@ public class PunishmentsEndpoint extends AbstractServlet
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
if (punishedPlayer == null)
{
return createBasicHTML(TITLE, "This player has never joined the server before.");
return punishmentsHTML("This player has never joined the server before.");
}
if (punishedPlayer.getPunishments().isEmpty())
{
return createBasicHTML(TITLE, "This player has been a good boy. They have no punishments!");
return punishmentsGoodHTML("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 createJSONHTML(TITLE, 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().peek(punishment -> punishment.setIp("")).toList());
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{
@@ -84,7 +64,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 createJSONHTML(TITLE, 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().peek(punishment -> punishment.setIp("")).toList());
}
}
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
@@ -94,9 +74,23 @@ 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 createJSONHTML(TITLE, 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().peek(punishment -> punishment.setIp("")).toList());
}
}
return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()));
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList());
}
private String punishmentsHTML(String message)
{
String file = readFile(this.getClass().getResourceAsStream("/httpd/punishments_error.html"));
file = file.replace("${MESSAGE}", message);
return file;
}
private String punishmentsGoodHTML(String message)
{
String file = readFile(this.getClass().getResourceAsStream("/httpd/punishments_good.html"));
file = file.replace("${MESSAGE}", message);
return file;
}
}