Skip to content

Commit b5dcd03

Browse files
committed
1.2.6
1 parent d1a4098 commit b5dcd03

8 files changed

Lines changed: 183 additions & 8 deletions

File tree

build.gradle

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
plugins {
22
id 'java'
3-
id 'io.papermc.paperweight.userdev' version "2.0.0-beta.19"
3+
id "io.canvasmc.weaver.userdev" version "2.3.12"
44
}
55

66
group = 'com.unfamoussoul'
77

88
repositories {
99
mavenCentral()
10+
maven { url = "https://maven.canvasmc.io/releases" }
1011
maven {
11-
url = 'https://repo.papermc.io/repository/maven-public/'
12-
}
13-
maven {
14-
url = 'https://jitpack.io'
12+
url = "https://maven.canvasmc.io/snapshots"
13+
mavenContent { snapshotsOnly() }
1514
}
15+
maven { url = "https://jitpack.io" }
16+
maven { url = "https://repo.papermc.io/repository/maven-public/" }
1617
}
1718

1819
dependencies {
19-
paperweight.foliaDevBundle("${folia_version}")
20+
paperweight.canvasDevBundle("${canvas_version}")
21+
implementation("io.javalin:javalin:${javalin_version}")
2022
compileOnly("com.github.SoulAPI:SAPI:${sapi_version}")
2123
}
2224

gradle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
folia_version=1.21.11-R0.1-SNAPSHOT
2-
sapi_version=1.2.5
1+
canvas_version=1.21.11-R0.1-SNAPSHOT
2+
javalin_version=7.0.1
3+
sapi_version=1.2.6
34
version=1

settings.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
pluginManagement {
2+
repositories {
3+
gradlePluginPortal()
4+
maven {
5+
url = uri("https://maven.canvasmc.io/releases")
6+
}
7+
}
8+
}
9+
110
rootProject.name = 'Test'

src/main/java/com/unfamoussoul/test/Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.unfamoussoul.test.command.TestCommand;
99
import com.unfamoussoul.test.config.Config;
1010
import com.unfamoussoul.test.listener.EventListener;
11+
import com.unfamoussoul.test.web.Web;
1112

1213
import java.util.*;
1314

@@ -34,6 +35,7 @@ public void onEnable() {
3435
addCommand(new RememberCommand(this));
3536
addCommand(new RemindCommand(this));
3637
addListener(new EventListener(this));
38+
addWebListener(new Web(this), config.webPort);
3739
}
3840

3941
@Override
@@ -49,6 +51,12 @@ public List<String> getPhrases(UUID playerId) {
4951
return playerPhrases.getOrDefault(playerId, Collections.emptyList());
5052
}
5153

54+
public Map<UUID, List<String>> getPlayerPhrasesSnapshot() {
55+
Map<UUID, List<String>> copy = new LinkedHashMap<>();
56+
playerPhrases.forEach((id, list) -> copy.put(id, List.copyOf(list)));
57+
return Collections.unmodifiableMap(copy);
58+
}
59+
5260
public Config getConfig() {
5361
return config;
5462
}

src/main/java/com/unfamoussoul/test/config/Config.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,25 @@ public class Config {
88

99
public final boolean welcomeMessage;
1010

11+
public final int webPort;
12+
13+
public final int webThreadsMin;
14+
public final int webThreadsMax;
15+
public final int webThreadsQueued;
16+
public final int webThreadsIdle;
17+
public final int webThreadsEvict;
18+
1119
public Config(@NotNull ConfigHandler configHandler) {
1220
FileConfiguration cfg = configHandler.getConfig();
1321

1422
welcomeMessage = cfg.getBoolean("welcome_message");
23+
24+
webPort = cfg.getInt("web.port");
25+
26+
webThreadsMin = cfg.getInt("web.threads.min");
27+
webThreadsMax = cfg.getInt("web.threads.max");
28+
webThreadsQueued = cfg.getInt("web.threads.queued");
29+
webThreadsIdle = cfg.getInt("web.threads.idle");
30+
webThreadsEvict = cfg.getInt("web.threads.evict");
1531
}
1632
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.unfamoussoul.test.web;
2+
3+
import com.unfamoussoul.sapi.api.web.WebListener;
4+
import com.unfamoussoul.test.Test;
5+
import com.unfamoussoul.test.web.route.RemindersRoute;
6+
import com.unfamoussoul.test.web.route.ServerRoute;
7+
import org.eclipse.jetty.server.Server;
8+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
9+
10+
import java.util.logging.Level;
11+
12+
import static io.javalin.apibuilder.ApiBuilder.get;
13+
import static io.javalin.apibuilder.ApiBuilder.path;
14+
15+
public class Web extends WebListener {
16+
17+
private final Test module;
18+
private final ServerRoute serverRoute;
19+
private final RemindersRoute remindersRoute;
20+
21+
public Web(Test module) {
22+
this.module = module;
23+
this.serverRoute = new ServerRoute(module);
24+
this.remindersRoute = new RemindersRoute(module);
25+
createInstance();
26+
}
27+
28+
private void createInstance() {
29+
QueuedThreadPool threadPool = getThreadPool();
30+
31+
threadPool.setMinThreads(module.getConfig().webThreadsMin);
32+
threadPool.setMaxThreads(module.getConfig().webThreadsMax);
33+
threadPool.setReservedThreads(module.getConfig().webThreadsQueued);
34+
threadPool.setIdleTimeout(module.getConfig().webThreadsIdle);
35+
threadPool.setMaxEvictCount(module.getConfig().webThreadsEvict);
36+
threadPool.setDetailedDump(true);
37+
38+
create(config -> {
39+
config.jetty.modifyServer(server -> server.setHandler(new Server(threadPool)));
40+
config.requestLogger.http((ctx, ms) -> module.getLogger().log(
41+
Level.CONFIG, "[TYPE: {}], [CONTEXT: {}], [IP: {}], took {}ms", new Object[]{ctx.method(), ctx.path(), ctx.ip(), String.format("%.2f", ms / 1000)}
42+
));
43+
});
44+
45+
getApp().unsafe.routes.apiBuilder(() -> {
46+
path("/server", () -> get("online", serverRoute::getOnline));
47+
path("/reminders", () -> {
48+
get("", remindersRoute::getAll);
49+
get("{nickname}", remindersRoute::getByNickname);
50+
});
51+
});
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.unfamoussoul.test.web.route;
2+
3+
import com.unfamoussoul.test.Test;
4+
import io.javalin.http.Context;
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.OfflinePlayer;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.util.LinkedHashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.UUID;
13+
14+
public class RemindersRoute {
15+
16+
private final Test module;
17+
18+
public RemindersRoute(Test module) {
19+
this.module = module;
20+
}
21+
22+
public void getAll(@NotNull Context ctx) {
23+
Map<String, List<String>> phrases = new LinkedHashMap<>();
24+
module.getPlayerPhrasesSnapshot().forEach((uuid, list) -> phrases.put(uuid.toString(), list));
25+
26+
Map<String, Object> payload = new LinkedHashMap<>();
27+
payload.put("count", phrases.size());
28+
payload.put("data", phrases);
29+
30+
ctx.json(payload);
31+
}
32+
33+
public void getByNickname(@NotNull Context ctx) {
34+
String nickname = ctx.pathParam("nickname");
35+
UUID uuid = resolveUuid(nickname);
36+
List<String> phrases = module.getPhrases(uuid);
37+
38+
Map<String, Object> payload = new LinkedHashMap<>();
39+
payload.put("nickname", nickname);
40+
payload.put("uuid", uuid.toString());
41+
payload.put("count", phrases.size());
42+
payload.put("data", phrases);
43+
44+
ctx.json(payload);
45+
}
46+
47+
private @NotNull UUID resolveUuid(String nickname) {
48+
OfflinePlayer player = Bukkit.getOfflinePlayer(nickname);
49+
return player.getUniqueId();
50+
}
51+
}
52+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.unfamoussoul.test.web.route;
2+
3+
import com.unfamoussoul.test.Test;
4+
import io.javalin.http.Context;
5+
import org.bukkit.entity.Player;
6+
7+
import java.util.ArrayList;
8+
import java.util.LinkedHashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class ServerRoute {
13+
14+
private final Test module;
15+
16+
public ServerRoute(Test module) {
17+
this.module = module;
18+
}
19+
20+
public void getOnline(Context ctx) {
21+
List<String> names = new ArrayList<>();
22+
23+
for (Player player : module.getPlugin().getServer().getOnlinePlayers()) {
24+
names.add(player.getName());
25+
}
26+
27+
Map<String, Object> payload = new LinkedHashMap<>();
28+
payload.put("online", names.size());
29+
payload.put("max", module.getPlugin().getServer().getMaxPlayers());
30+
payload.put("names", names);
31+
32+
ctx.json(payload);
33+
}
34+
}

0 commit comments

Comments
 (0)