Skip to content

Commit 5f4f91d

Browse files
committed
Port over to the new API
1 parent 563b450 commit 5f4f91d

18 files changed

Lines changed: 161 additions & 154 deletions

build.gradle.kts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
java
33
`maven-publish`
44
idea
5-
id("dev.plex.module") version "1.0"
5+
id("dev.plex.module") version "1.1"
66
}
77

88
group = "dev.plex"
@@ -20,21 +20,26 @@ repositories {
2020
url = uri("https://nexus.telesphoreo.me/repository/plex/")
2121
}
2222

23-
maven { url = uri("https://maven.enginehub.org/repo/") }
23+
maven {
24+
url = uri("https://maven.enginehub.org/repo/")
25+
}
2426

25-
maven { url = uri("https://repo.codemc.io/repository/maven-public/") }
27+
maven {
28+
url = uri("https://repo.codemc.io/repository/maven-public/")
29+
}
2630
}
2731

2832
dependencies {
2933
implementation("org.projectlombok:lombok:1.18.46")
3034
annotationProcessor("org.projectlombok:lombok:1.18.46")
3135
compileOnly("io.papermc.paper:paper-api:26.1.2.build.+")
32-
implementation("dev.plex:server:2.0-SNAPSHOT")
36+
implementation("dev.plex:api:2.0-SNAPSHOT")
3337
implementation("org.json:json:20251224")
3438
implementation("org.reflections:reflections:0.10.2")
3539
plexLibrary("org.eclipse.jetty:jetty-server:12.1.9")
3640
plexLibrary("org.eclipse.jetty.ee10:jetty-ee10-servlet:12.1.9")
3741
plexLibrary("org.eclipse.jetty:jetty-proxy:12.1.9")
42+
plexLibrary("de.tr7zw:item-nbt-api:2.15.7")
3843
implementation(platform("com.intellectualsites.bom:bom-newest:1.56")) // Ref: https://github.com/IntellectualSites/bom
3944
compileOnly("com.fastasyncworldedit:FastAsyncWorldEdit-Core")
4045
implementation("commons-io:commons-io:2.22.0")

src/main/java/dev/plex/HTTPDModule.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.plex.assets.MinecraftAssetsManager;
44
import dev.plex.authentication.AuthenticationManager;
55
import dev.plex.cache.FileCache;
6+
import dev.plex.api.PlexApi;
67
import dev.plex.config.ModuleConfig;
78
import dev.plex.logging.Log;
89
import dev.plex.module.PlexModule;
@@ -15,7 +16,6 @@
1516
import dev.plex.request.StaffPlayersStreamServlet;
1617
import dev.plex.request.StatsStreamServlet;
1718
import dev.plex.request.impl.*;
18-
import dev.plex.util.PlexLog;
1919
import jakarta.servlet.DispatcherType;
2020
import jakarta.servlet.MultipartConfigElement;
2121
import lombok.Getter;
@@ -38,6 +38,12 @@ public class HTTPDModule extends PlexModule
3838
private AtomicReference<Server> atomicServer = new AtomicReference<>();
3939

4040
public static ModuleConfig moduleConfig;
41+
private static PlexApi plexApi;
42+
43+
public static PlexApi plexApi()
44+
{
45+
return plexApi;
46+
}
4147

4248
public static final FileCache fileCache = new FileCache();
4349

@@ -55,6 +61,7 @@ public class HTTPDModule extends PlexModule
5561
@Override
5662
public void load()
5763
{
64+
plexApi = api();
5865
// Move it from /httpd/config.yml to /plugins/Plex/modules/Plex-HTTPD/config.yml
5966
moduleConfig = new ModuleConfig(this, "httpd/config.yml", "config.yml");
6067
}
@@ -63,7 +70,7 @@ public void load()
6370
public void enable()
6471
{
6572
moduleConfig.load();
66-
PlexLog.debug("HTTPD Module Port: {0}", moduleConfig.getInt("server.port"));
73+
HTTPDModule.plexApi().logging().debug("HTTPD Module Port: {0}", moduleConfig.getInt("server.port"));
6774

6875
accessLogFile = new File(getDataFolder(), moduleConfig.getString("server.logging.file-path", "httpd.log"));
6976

@@ -73,7 +80,7 @@ public void enable()
7380
authenticationManager = new AuthenticationManager();
7481
if (authenticationManager.provider() == null)
7582
{
76-
PlexLog.debug("Authentication is disabled or misconfigured");
83+
HTTPDModule.plexApi().logging().debug("Authentication is disabled or misconfigured");
7784
}
7885

7986

@@ -153,13 +160,13 @@ public void enable()
153160
}
154161
}, "Jetty-Server");
155162
serverThread.start();
156-
PlexLog.log("Starting Jetty server on port " + moduleConfig.getInt("server.port"));
163+
HTTPDModule.plexApi().logging().info("Starting Jetty server on port " + moduleConfig.getInt("server.port"));
157164
}
158165

159166
@Override
160167
public void disable()
161168
{
162-
PlexLog.debug("Stopping Jetty server");
169+
HTTPDModule.plexApi().logging().debug("Stopping Jetty server");
163170
try
164171
{
165172
StatsBroadcaster.get().shutdown();

src/main/java/dev/plex/assets/MinecraftAssetsManager.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.plex.assets;
22

3-
import dev.plex.util.PlexLog;
3+
import dev.plex.HTTPDModule;
44
import org.bukkit.Bukkit;
55
import org.json.JSONArray;
66
import org.json.JSONObject;
@@ -63,7 +63,7 @@ public void refreshAsync()
6363
}
6464
catch (Exception e)
6565
{
66-
PlexLog.log("Unable to download Minecraft assets for HTTPD inventory view: " + e.getMessage());
66+
HTTPDModule.plexApi().logging().info("Unable to download Minecraft assets for HTTPD inventory view: " + e.getMessage());
6767
e.printStackTrace();
6868
}
6969
});
@@ -90,13 +90,13 @@ private void refreshIfNeeded() throws IOException, InterruptedException
9090
String cachedVersion = Files.exists(versionFile) ? Files.readString(versionFile).trim() : "";
9191
if (minecraftVersion.equals(cachedVersion) && hasAssets())
9292
{
93-
PlexLog.debug("HTTPD Minecraft assets are already cached for {0}", minecraftVersion);
93+
HTTPDModule.plexApi().logging().debug("HTTPD Minecraft assets are already cached for {0}", minecraftVersion);
9494
return;
9595
}
9696

9797
if (!cachedVersion.isEmpty() && !minecraftVersion.equals(cachedVersion))
9898
{
99-
PlexLog.log("Minecraft version changed from " + cachedVersion + " to " + minecraftVersion + "; recreating HTTPD asset cache");
99+
HTTPDModule.plexApi().logging().info("Minecraft version changed from " + cachedVersion + " to " + minecraftVersion + "; recreating HTTPD asset cache");
100100
}
101101
recreateCache();
102102
}
@@ -114,7 +114,7 @@ private void recreateCache() throws IOException, InterruptedException
114114
deleteDirectory(root);
115115
Files.createDirectories(root);
116116

117-
PlexLog.log("Downloading Minecraft " + minecraftVersion + " client assets for HTTPD inventory view");
117+
HTTPDModule.plexApi().logging().info("Downloading Minecraft " + minecraftVersion + " client assets for HTTPD inventory view");
118118
JSONObject version = findVersionJson();
119119
String clientUrl = version.getJSONObject("downloads").getJSONObject("client").getString("url");
120120

@@ -142,7 +142,7 @@ private void recreateCache() throws IOException, InterruptedException
142142
}
143143

144144
Files.writeString(versionFile, minecraftVersion + System.lineSeparator());
145-
PlexLog.log("HTTPD Minecraft assets cached for " + minecraftVersion);
145+
HTTPDModule.plexApi().logging().info("HTTPD Minecraft assets cached for " + minecraftVersion);
146146
}
147147

148148
private JSONObject findVersionJson() throws IOException, InterruptedException

src/main/java/dev/plex/authentication/AuthenticationManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import dev.plex.HTTPDModule;
44
import dev.plex.authentication.impl.XenForoOAuth2Provider;
5-
import dev.plex.util.PlexLog;
65

76
public class AuthenticationManager
87
{
@@ -17,7 +16,7 @@ public AuthenticationManager()
1716
return;
1817
}
1918

20-
PlexLog.log("[HTTPD] XenForo OAuth2 authentication is enabled");
19+
HTTPDModule.plexApi().logging().info("[HTTPD] XenForo OAuth2 authentication is enabled");
2120
provider = new XenForoOAuth2Provider();
2221
}
2322

src/main/java/dev/plex/authentication/impl/XenForoOAuth2Provider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import dev.plex.authentication.AuthenticationException;
66
import dev.plex.authentication.OAuth2Provider;
77
import dev.plex.authentication.UserType;
8-
import dev.plex.util.PlexLog;
98
import jakarta.servlet.http.Cookie;
109
import jakarta.servlet.http.HttpServletRequest;
1110
import jakarta.servlet.http.HttpServletResponse;
@@ -62,7 +61,7 @@ public XenForoOAuth2Provider()
6261

6362
if (domain.isEmpty() || clientId.isEmpty() || clientSecret.isEmpty() || redirectUri.isEmpty())
6463
{
65-
PlexLog.error("XenForo OAuth2 misconfigured: domain, clientId, clientSecret, redirectUri are all required.");
64+
HTTPDModule.plexApi().logging().error("XenForo OAuth2 misconfigured: domain, clientId, clientSecret, redirectUri are all required.");
6665
}
6766

6867
String base = "https://" + domain.replaceFirst("^https?://", "").replaceAll("/+$", "");
@@ -286,7 +285,7 @@ private void revokeToken(String accessToken)
286285
}
287286
catch (Exception e)
288287
{
289-
PlexLog.debug("Failed to revoke XenForo token: {0}", e.getMessage());
288+
HTTPDModule.plexApi().logging().debug("Failed to revoke XenForo token: {0}", e.getMessage());
290289
}
291290
}
292291

src/main/java/dev/plex/request/AbstractServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws Servl
6666
/*Enumeration<String> headerz = req.getHeaderNames();
6767
while (headerz.hasMoreElements()) {
6868
String header = headerz.nextElement();
69-
PlexLog.debug("Header: {0} Value {1}", header, req.getHeader(header));
69+
HTTPDModule.plexApi().logging().debug("Header: {0} Value {1}", header, req.getHeader(header));
7070
}*/
7171
GET_MAPPINGS.stream().filter(mapping -> endpointMatchesRequest(mapping.getMapping().endpoint(), requestPath)).forEach(mapping ->
7272
{

src/main/java/dev/plex/request/PlayerActionServlet.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package dev.plex.request;
22

3-
import dev.plex.Plex;
3+
import dev.plex.HTTPDModule;
4+
import dev.plex.api.player.PlexPlayerView;
5+
import dev.plex.api.punishment.PunishmentRequest;
6+
import dev.plex.api.punishment.PunishmentType;
47
import dev.plex.authentication.AuthenticatedUser;
5-
import dev.plex.cache.DataUtils;
68
import dev.plex.logging.Log;
7-
import dev.plex.player.PlexPlayer;
8-
import dev.plex.punishment.Punishment;
9-
import dev.plex.punishment.PunishmentType;
10-
import dev.plex.util.BungeeUtil;
11-
import dev.plex.util.TimeUtils;
129
import jakarta.servlet.ServletException;
1310
import jakarta.servlet.http.HttpServlet;
1411
import jakarta.servlet.http.HttpServletRequest;
1512
import jakarta.servlet.http.HttpServletResponse;
1613
import org.bukkit.Bukkit;
1714
import org.bukkit.entity.Player;
1815
import org.bukkit.inventory.PlayerInventory;
16+
import net.kyori.adventure.text.Component;
1917

2018
import java.io.IOException;
2119
import java.time.ZoneId;
@@ -73,7 +71,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
7371
return;
7472
}
7573

76-
PlexPlayer target = DataUtils.getPlayer(uuid);
74+
PlexPlayerView target = HTTPDModule.plexApi().players().byUuid(uuid).orElse(null);
7775
if (target == null)
7876
{
7977
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
@@ -91,37 +89,41 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
9189
if (safeReason.length() > 500) safeReason = safeReason.substring(0, 500);
9290

9391
PunishmentType type = mapType(action);
94-
ZonedDateTime now = ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE));
92+
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
9593
ZonedDateTime endDate = TEMP_ACTIONS.contains(action)
9694
? now.plusSeconds(parseDurationSeconds(durationStr))
9795
: now.plusDays(FAR_FUTURE_DAYS);
9896

99-
Punishment punishment = new Punishment(uuid, null);
100-
punishment.setType(type);
101-
punishment.setReason(safeReason);
102-
punishment.setPunishedUsername(target.getName());
103-
punishment.setPunisherName("xf:" + staff.username());
104-
punishment.setEndDate(endDate);
105-
punishment.setCustomTime(TEMP_ACTIONS.contains(action));
106-
punishment.setActive(true);
107-
List<String> ips = target.getIps();
108-
if (ips != null && !ips.isEmpty()) punishment.setIp(ips.getLast());
97+
List<String> ips = target.ips();
98+
String ip = ips == null || ips.isEmpty() ? "" : ips.getLast();
99+
PunishmentRequest punishment = new PunishmentRequest(
100+
uuid,
101+
null,
102+
"xf:" + staff.username(),
103+
ip,
104+
target.name(),
105+
type,
106+
safeReason,
107+
TEMP_ACTIONS.contains(action),
108+
true,
109+
endDate
110+
);
109111

110112
String ipAddress = request.getRemoteAddr();
111113
if ("127.0.0.1".equals(ipAddress))
112114
{
113115
String forwarded = request.getHeader("X-FORWARDED-FOR");
114116
if (forwarded != null) ipAddress = forwarded;
115117
}
116-
Log.log(ipAddress + " (xf:" + staff.username() + ") issued " + action + " on " + target.getName() + " (" + uuid + ")");
118+
Log.log(ipAddress + " (xf:" + staff.username() + ") issued " + action + " on " + target.name() + " (" + uuid + ")");
117119

118120
final boolean kick = action.equals("ban") || action.equals("tempban");
119-
final Punishment toApply = punishment;
120-
Bukkit.getScheduler().runTask(Plex.get(), () ->
121+
final PunishmentRequest toApply = punishment;
122+
HTTPDModule.plexApi().scheduler().runSync(() ->
121123
{
122124
try
123125
{
124-
Plex.get().getPunishmentManager().punish(target, toApply);
126+
HTTPDModule.plexApi().punishments().punish(target, toApply);
125127
}
126128
catch (Throwable t)
127129
{
@@ -133,7 +135,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
133135
Player online = Bukkit.getPlayer(uuid);
134136
if (online != null)
135137
{
136-
try { BungeeUtil.kickPlayer(online, Punishment.generateBanMessage(toApply)); }
138+
try { online.kick(Component.text("You have been banned: " + toApply.reason())); }
137139
catch (Throwable t) { t.printStackTrace(); }
138140
}
139141
}
@@ -142,7 +144,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
142144
response.sendRedirect("/player/" + uuid);
143145
}
144146

145-
private static void handleInventoryAction(HttpServletRequest request, HttpServletResponse response, AuthenticatedUser staff, UUID uuid, PlexPlayer target, String action, String slot)
147+
private static void handleInventoryAction(HttpServletRequest request, HttpServletResponse response, AuthenticatedUser staff, UUID uuid, PlexPlayerView target, String action, String slot)
146148
throws IOException
147149
{
148150
String ipAddress = request.getRemoteAddr();
@@ -152,9 +154,9 @@ private static void handleInventoryAction(HttpServletRequest request, HttpServle
152154
if (forwarded != null) ipAddress = forwarded;
153155
}
154156

155-
Log.log(ipAddress + " (xf:" + staff.username() + ") issued " + action + " on " + target.getName() + " (" + uuid + ")" + (slot == null || slot.isBlank() ? "" : " slot " + slot));
157+
Log.log(ipAddress + " (xf:" + staff.username() + ") issued " + action + " on " + target.name() + " (" + uuid + ")" + (slot == null || slot.isBlank() ? "" : " slot " + slot));
156158

157-
Bukkit.getScheduler().runTask(Plex.get(), () ->
159+
HTTPDModule.plexApi().scheduler().runSync(() ->
158160
{
159161
Player online = Bukkit.getPlayer(uuid);
160162
if (online == null) return;

src/main/java/dev/plex/request/SchematicUploadServlet.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import dev.plex.HTTPDModule;
66
import dev.plex.authentication.AuthenticatedUser;
77
import dev.plex.logging.Log;
8-
import dev.plex.util.PlexLog;
98
import jakarta.servlet.ServletException;
109
import jakarta.servlet.http.HttpServlet;
1110
import jakarta.servlet.http.HttpServletRequest;
@@ -68,7 +67,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
6867
ClipboardFormat schematicFormat = ClipboardFormats.findByFile(schematicFile);
6968
if (schematicFormat == null)
7069
{
71-
PlexLog.log(user.username() + " FAILED to upload schematic with filename: " + filename);
70+
HTTPDModule.plexApi().logging().info(user.username() + " FAILED to upload schematic with filename: " + filename);
7271
Log.log("{0} (xf:{1}) FAILED to upload schematic {2}", user.username(), user.userId(), filename);
7372
response.getWriter().println(schematicUploadBadHTML("Schematic is not a valid format."));
7473
FileUtils.deleteQuietly(schematicFile);
@@ -80,15 +79,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
8079
}
8180
catch (IOException e)
8281
{
83-
PlexLog.log(user.username() + " FAILED to upload schematic with filename: " + filename);
82+
HTTPDModule.plexApi().logging().info(user.username() + " FAILED to upload schematic with filename: " + filename);
8483
Log.log("{0} (xf:{1}) FAILED to upload schematic {2}", user.username(), user.userId(), filename);
8584
response.getWriter().println(schematicUploadBadHTML("Schematic is not a valid format."));
8685
FileUtils.deleteQuietly(schematicFile);
8786
return;
8887
}
8988
inputStream.close();
9089
response.getWriter().println(schematicUploadGoodHTML("Successfully uploaded <b>" + filename + "</b>."));
91-
PlexLog.log(user.username() + " uploaded schematic with filename: " + filename);
90+
HTTPDModule.plexApi().logging().info(user.username() + " uploaded schematic with filename: " + filename);
9291
Log.log("{0} (xf:{1}) uploaded schematic {2}", user.username(), user.userId(), filename);
9392
}
9493

src/main/java/dev/plex/request/impl/AuthenticationEndpoint.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import dev.plex.request.AbstractServlet;
88
import dev.plex.request.GetMapping;
99
import dev.plex.request.MappingHeaders;
10-
import dev.plex.util.PlexLog;
1110
import jakarta.servlet.http.Cookie;
1211
import jakarta.servlet.http.HttpServletRequest;
1312
import jakarta.servlet.http.HttpServletResponse;
@@ -64,7 +63,7 @@ public String callback(HttpServletRequest request, HttpServletResponse response)
6463
}
6564
catch (AuthenticationException e)
6665
{
67-
PlexLog.error("OAuth2 callback failed: " + e.getMessage());
66+
HTTPDModule.plexApi().logging().error("OAuth2 callback failed: " + e.getMessage());
6867
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
6968
response.setContentType("text/html; charset=UTF-8");
7069
return "<!doctype html><meta charset=utf-8><title>Sign-in failed</title>"

0 commit comments

Comments
 (0)