From 8dcd83144370774fe3ae29e87130931545060162 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Wed, 20 Nov 2024 22:53:20 +0100 Subject: [PATCH] Splitting into different mods --- build.gradle | 3 + gradle.properties | 2 + .../java/cc/reconnected/server/RccServer.java | 9 +- .../Config.java => RccServerConfig.java} | 16 +-- .../server/commands/admin/RccCommand.java | 8 +- .../commands/home/DeleteHomeCommand.java | 5 +- .../server/commands/home/HomeCommand.java | 5 +- .../server/commands/home/SetHomeCommand.java | 7 +- .../server/commands/misc/NearCommand.java | 9 +- .../server/commands/spawn/SpawnCommand.java | 3 +- .../server/commands/teleport/BackCommand.java | 5 +- .../teleport/TeleportAcceptCommand.java | 15 +-- .../commands/teleport/TeleportAskCommand.java | 7 +- .../teleport/TeleportAskHereCommand.java | 7 +- .../teleport/TeleportDenyCommand.java | 9 +- .../server/commands/tell/ReplyCommand.java | 3 +- .../server/commands/tell/TellCommand.java | 13 ++- .../server/commands/warp/WarpCommand.java | 5 +- .../server/config/ConfigManager.java | 45 -------- .../reconnected/server/core/AfkTracker.java | 5 +- .../server/core/HttpApiServer.java | 104 ------------------ .../core/customChat/CustomChatMessage.java | 3 +- .../customChat/CustomConnectionMessage.java | 7 +- .../core/customChat/CustomDeathMessage.java | 3 +- .../core/customChat/CustomEmoteMessage.java | 3 +- .../reconnected/server/parser/LinkParser.java | 99 ----------------- .../parser/MarkdownComponentParser.java | 52 --------- .../server/parser/MarkdownParser.java | 32 ------ .../reconnected/server/util/Components.java | 31 +----- 29 files changed, 85 insertions(+), 430 deletions(-) rename src/main/java/cc/reconnected/server/{config/Config.java => RccServerConfig.java} (97%) delete mode 100644 src/main/java/cc/reconnected/server/config/ConfigManager.java delete mode 100644 src/main/java/cc/reconnected/server/core/HttpApiServer.java delete mode 100644 src/main/java/cc/reconnected/server/parser/LinkParser.java delete mode 100644 src/main/java/cc/reconnected/server/parser/MarkdownComponentParser.java delete mode 100644 src/main/java/cc/reconnected/server/parser/MarkdownParser.java diff --git a/build.gradle b/build.gradle index b374b75..20e118d 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ repositories { maven { url 'https://maven.nucleoid.xyz' } maven { url 'https://maven.reconnected.cc/releases' } + mavenLocal() } loom { @@ -41,6 +42,8 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + modImplementation "cc.reconnected:rcc-lib:${project.rcclib_version}" + compileOnly "net.luckperms:api:${project.luckpermsapi_version}" include modImplementation("me.lucko:fabric-permissions-api:${project.permissions_api_version}") diff --git a/gradle.properties b/gradle.properties index f379c8d..0ebfd05 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,6 +16,8 @@ archives_base_name=rcc-server # Dependencies fabric_version=0.92.2+1.20.1 +rcclib_version=1.0.0 + luckpermsapi_version=5.4 permissions_api_version=0.2-SNAPSHOT diff --git a/src/main/java/cc/reconnected/server/RccServer.java b/src/main/java/cc/reconnected/server/RccServer.java index c13a353..d89f91c 100644 --- a/src/main/java/cc/reconnected/server/RccServer.java +++ b/src/main/java/cc/reconnected/server/RccServer.java @@ -1,5 +1,6 @@ package cc.reconnected.server; +import cc.reconnected.library.config.ConfigManager; import cc.reconnected.server.api.events.RccEvents; import cc.reconnected.server.commands.admin.*; import cc.reconnected.server.commands.home.DeleteHomeCommand; @@ -19,8 +20,6 @@ import cc.reconnected.server.commands.tell.TellCommand; import cc.reconnected.server.commands.warp.DeleteWarpCommand; import cc.reconnected.server.commands.warp.SetWarpCommand; import cc.reconnected.server.commands.warp.WarpCommand; -import cc.reconnected.server.config.Config; -import cc.reconnected.server.config.ConfigManager; import cc.reconnected.server.core.*; import cc.reconnected.server.core.customChat.CustomChatMessage; import cc.reconnected.server.data.StateManager; @@ -45,7 +44,6 @@ import net.minecraft.util.WorldSavePath; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; import java.util.Date; @@ -53,7 +51,7 @@ public class RccServer implements ModInitializer { public static final String MOD_ID = "rcc-server"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); - public static Config CONFIG; + public static RccServerConfig CONFIG; public static final StateManager state = new StateManager(); @@ -93,7 +91,7 @@ public class RccServer implements ModInitializer { LOGGER.info("Starting rcc-server"); try { - CONFIG = ConfigManager.load(); + CONFIG = ConfigManager.load(RccServerConfig.class); } catch (Exception e) { LOGGER.error("Failed to load config. Refusing to continue.", e); return; @@ -144,7 +142,6 @@ public class RccServer implements ModInitializer { TeleportTracker.register(); BackTracker.register(); TabList.register(); - HttpApiServer.register(); BossBarManager.register(); AutoRestart.register(); diff --git a/src/main/java/cc/reconnected/server/config/Config.java b/src/main/java/cc/reconnected/server/RccServerConfig.java similarity index 97% rename from src/main/java/cc/reconnected/server/config/Config.java rename to src/main/java/cc/reconnected/server/RccServerConfig.java index c89b6b5..b477d45 100644 --- a/src/main/java/cc/reconnected/server/config/Config.java +++ b/src/main/java/cc/reconnected/server/RccServerConfig.java @@ -1,15 +1,14 @@ -package cc.reconnected.server.config; +package cc.reconnected.server; + +import cc.reconnected.library.config.Config; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -public class Config { - Config() { - } - - public HttpApi httpApi = new HttpApi(); +@Config(RccServer.MOD_ID) +public class RccServerConfig { public Afk afk = new Afk(); public TeleportRequests teleportRequests = new TeleportRequests(); public Homes homes = new Homes(); @@ -19,11 +18,6 @@ public class Config { public Chat chat = new Chat(); public TextFormats textFormats = new TextFormats(); - public static class HttpApi { - public boolean enableHttpApi = true; - public int httpPort = 25581; - } - public static class Afk { public int afkTimeTrigger = 300; } diff --git a/src/main/java/cc/reconnected/server/commands/admin/RccCommand.java b/src/main/java/cc/reconnected/server/commands/admin/RccCommand.java index e4ec371..39df7fd 100644 --- a/src/main/java/cc/reconnected/server/commands/admin/RccCommand.java +++ b/src/main/java/cc/reconnected/server/commands/admin/RccCommand.java @@ -1,8 +1,10 @@ package cc.reconnected.server.commands.admin; +import cc.reconnected.library.config.ConfigManager; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; +import cc.reconnected.server.RccServerConfig; import cc.reconnected.server.api.events.RccEvents; -import cc.reconnected.server.config.ConfigManager; import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; import me.lucko.fabric.api.permissions.v0.Permissions; @@ -31,7 +33,7 @@ public class RccCommand { "version", Text.of(metadata.getVersion().getFriendlyString()) ); - var text = Components.parse( + var text = Placeholder.parse( "${name} v${version}", placeholders); context.getSource().sendFeedback(() -> text, false); @@ -44,7 +46,7 @@ public class RccCommand { context.getSource().sendFeedback(() -> Text.of("Reloading RCC config..."), true); try { - RccServer.CONFIG = ConfigManager.load(); + RccServer.CONFIG = ConfigManager.load(RccServerConfig.class); } catch (Exception e) { RccServer.LOGGER.error("Failed to load RCC config", e); context.getSource().sendFeedback(() -> Text.of("Failed to load RCC config. Check console for more info."), true); diff --git a/src/main/java/cc/reconnected/server/commands/home/DeleteHomeCommand.java b/src/main/java/cc/reconnected/server/commands/home/DeleteHomeCommand.java index dbf23c6..677700b 100644 --- a/src/main/java/cc/reconnected/server/commands/home/DeleteHomeCommand.java +++ b/src/main/java/cc/reconnected/server/commands/home/DeleteHomeCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.home; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; @@ -46,7 +47,7 @@ public class DeleteHomeCommand { ); if (!homes.containsKey(name)) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.home.homeNotFound, playerContext, placeholders @@ -57,7 +58,7 @@ public class DeleteHomeCommand { homes.remove(name); RccServer.state.savePlayerState(player.getUuid(), playerState); - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.home.homeDeleted, playerContext, placeholders diff --git a/src/main/java/cc/reconnected/server/commands/home/HomeCommand.java b/src/main/java/cc/reconnected/server/commands/home/HomeCommand.java index 65095c1..c385106 100644 --- a/src/main/java/cc/reconnected/server/commands/home/HomeCommand.java +++ b/src/main/java/cc/reconnected/server/commands/home/HomeCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.home; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; @@ -47,7 +48,7 @@ public class HomeCommand { if (!homes.containsKey(name)) { context.getSource().sendFeedback(() -> - Components.parse( + Placeholder.parse( RccServer.CONFIG.textFormats.commands.home.homeNotFound, playerContext, placeholders @@ -57,7 +58,7 @@ public class HomeCommand { } context.getSource().sendFeedback(() -> - Components.parse( + Placeholder.parse( RccServer.CONFIG.textFormats.commands.home.teleporting, playerContext, placeholders diff --git a/src/main/java/cc/reconnected/server/commands/home/SetHomeCommand.java b/src/main/java/cc/reconnected/server/commands/home/SetHomeCommand.java index 61cff48..1a0999c 100644 --- a/src/main/java/cc/reconnected/server/commands/home/SetHomeCommand.java +++ b/src/main/java/cc/reconnected/server/commands/home/SetHomeCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.home; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.struct.ServerPosition; import cc.reconnected.server.util.Components; @@ -54,7 +55,7 @@ public class SetHomeCommand { var exists = homes.containsKey(name); if (exists && !forced) { - var text = Components.parse( + var text = Placeholder.parse( RccServer.CONFIG.textFormats.commands.home.homeExists, playerContext, placeholders @@ -67,7 +68,7 @@ public class SetHomeCommand { var maxHomes = RccServer.CONFIG.homes.maxHomes; if(maxHomes >= 0 && homes.size() >= maxHomes && !exists) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.home.maxHomesReached, playerContext, placeholders @@ -80,7 +81,7 @@ public class SetHomeCommand { RccServer.state.savePlayerState(player.getUuid(), playerState); - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.home.homeSetSuccess, playerContext, placeholders diff --git a/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java b/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java index 2504f91..9db7c6c 100644 --- a/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java +++ b/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.misc; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; @@ -43,7 +44,7 @@ public class NearCommand { }); if (list.isEmpty()) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.near.noOne, playerContext ), false); @@ -53,7 +54,7 @@ public class NearCommand { list.sort(Comparator.comparingDouble(ClosePlayers::distance)); var listText = Text.empty(); - var comma = Components.parse(RccServer.CONFIG.textFormats.commands.near.comma); + var comma = Placeholder.parse(RccServer.CONFIG.textFormats.commands.near.comma); for (int i = 0; i < list.size(); i++) { var player = list.get(i); if (i > 0) { @@ -66,7 +67,7 @@ public class NearCommand { var targetContext = PlaceholderContext.of(sourcePlayer); - listText = listText.append(Components.parse( + listText = listText.append(Placeholder.parse( RccServer.CONFIG.textFormats.commands.near.format, targetContext, placeholders @@ -76,7 +77,7 @@ public class NearCommand { var placeholders = Map.of( "playerList", (Text) listText ); - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.near.nearestPlayers, playerContext, placeholders diff --git a/src/main/java/cc/reconnected/server/commands/spawn/SpawnCommand.java b/src/main/java/cc/reconnected/server/commands/spawn/SpawnCommand.java index 9fe3df2..8dc7ffc 100644 --- a/src/main/java/cc/reconnected/server/commands/spawn/SpawnCommand.java +++ b/src/main/java/cc/reconnected/server/commands/spawn/SpawnCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.spawn; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.struct.ServerPosition; import cc.reconnected.server.util.Components; @@ -26,7 +27,7 @@ public class SpawnCommand { spawnPosition = new ServerPosition(spawnPos.getX(), spawnPos.getY(), spawnPos.getZ(), 0, 0, server.getOverworld()); } - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.spawn.teleporting, playerContext ), false); diff --git a/src/main/java/cc/reconnected/server/commands/teleport/BackCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/BackCommand.java index 13912e7..b60841c 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/BackCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/BackCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.teleport; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.core.BackTracker; import cc.reconnected.server.util.Components; @@ -21,14 +22,14 @@ public class BackCommand { var lastPosition = BackTracker.lastPlayerPositions.get(player.getUuid()); if (lastPosition == null) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.back.noPosition, playerContext ), false); return 1; } - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.back.teleporting, playerContext ), false); diff --git a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAcceptCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAcceptCommand.java index 2aa30b4..21ba783 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAcceptCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAcceptCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.teleport; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.core.TeleportTracker; import cc.reconnected.server.struct.ServerPosition; @@ -28,7 +29,7 @@ public class TeleportAcceptCommand { var request = playerRequests.pollLast(); if (request == null) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.noPending, playerContext ), false); @@ -54,7 +55,7 @@ public class TeleportAcceptCommand { var request = playerRequests.stream().filter(req -> req.requestId.equals(uuid)).findFirst().orElse(null); if (request == null) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.unavailable, playerContext ), false); @@ -83,7 +84,7 @@ public class TeleportAcceptCommand { var playerContext = PlaceholderContext.of(player); if (sourcePlayer == null || targetPlayer == null) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.playerUnavailable, playerContext ), false); @@ -93,23 +94,23 @@ public class TeleportAcceptCommand { if (player.getUuid().equals(request.target)) { var sourceContext = PlaceholderContext.of(sourcePlayer); // accepted a tpa from other to self - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.requestAcceptedResult, playerContext ), false); - sourcePlayer.sendMessage(Components.parse( + sourcePlayer.sendMessage(Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.teleporting, sourceContext ), false); } else { var targetContext = PlaceholderContext.of(targetPlayer); // accepted a tpa from self to other - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.teleporting, playerContext ), false); - targetPlayer.sendMessage(Components.parse( + targetPlayer.sendMessage(Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.requestAccepted, targetContext, Map.of("player", sourcePlayer.getDisplayName()) diff --git a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskCommand.java index 3ef0902..0e2de94 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.teleport; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.core.TeleportTracker; import cc.reconnected.server.util.Components; @@ -48,7 +49,7 @@ public class TeleportAskCommand { var placeholders = Map.of( "targetPlayer", Text.of(targetName) ); - source.sendFeedback(() -> Components.parse( + source.sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.playerNotFound, playerContext, placeholders @@ -72,13 +73,13 @@ public class TeleportAskCommand { "/tpdeny " + request.requestId) ); - target.sendMessage(Components.parse( + target.sendMessage(Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.pendingTeleport, targetContext, placeholders )); - source.sendFeedback(() -> Components.parse( + source.sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.requestSent, playerContext ), false); diff --git a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskHereCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskHereCommand.java index 039e992..52d106b 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskHereCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/TeleportAskHereCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.teleport; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.core.TeleportTracker; import cc.reconnected.server.util.Components; @@ -47,7 +48,7 @@ public class TeleportAskHereCommand { var placeholders = Map.of( "targetPlayer", Text.of(targetName) ); - source.sendFeedback(() -> Components.parse( + source.sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.playerNotFound, playerContext, placeholders @@ -72,13 +73,13 @@ public class TeleportAskHereCommand { "/tpdeny " + request.requestId) ); - target.sendMessage(Components.parse( + target.sendMessage(Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.pendingTeleportHere, targetContext, placeholders )); - source.sendFeedback(() -> Components.parse( + source.sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.requestSent, playerContext ), false); diff --git a/src/main/java/cc/reconnected/server/commands/teleport/TeleportDenyCommand.java b/src/main/java/cc/reconnected/server/commands/teleport/TeleportDenyCommand.java index 9bbf760..bdb4de8 100644 --- a/src/main/java/cc/reconnected/server/commands/teleport/TeleportDenyCommand.java +++ b/src/main/java/cc/reconnected/server/commands/teleport/TeleportDenyCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.teleport; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.core.TeleportTracker; import cc.reconnected.server.util.Components; @@ -33,7 +34,7 @@ public class TeleportDenyCommand { var request = playerRequests.pollLast(); if (request == null) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.noPending, playerContext ), false); @@ -54,7 +55,7 @@ public class TeleportDenyCommand { var request = playerRequests.stream().filter(req -> req.requestId.equals(uuid)).findFirst().orElse(null); if (request == null) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.unavailable, playerContext ), false); @@ -87,14 +88,14 @@ public class TeleportDenyCommand { if (otherPlayer != null) { var otherContext = PlaceholderContext.of(otherPlayer); - otherPlayer.sendMessage(Components.parse( + otherPlayer.sendMessage(Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.requestRefused, otherContext, Map.of("player", player.getDisplayName()) ), false); } - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.teleportRequest.requestRefusedResult, playerContext ), false); diff --git a/src/main/java/cc/reconnected/server/commands/tell/ReplyCommand.java b/src/main/java/cc/reconnected/server/commands/tell/ReplyCommand.java index 277f9bb..67fdb14 100644 --- a/src/main/java/cc/reconnected/server/commands/tell/ReplyCommand.java +++ b/src/main/java/cc/reconnected/server/commands/tell/ReplyCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.tell; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; @@ -30,7 +31,7 @@ public class ReplyCommand { if (!TellCommand.lastSender.containsKey(senderName)) { var playerContext = PlaceholderContext.of(context.getSource()); - source.sendFeedback(() -> Components.parse( + source.sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.tell.noLastSenderReply, playerContext ), false); diff --git a/src/main/java/cc/reconnected/server/commands/tell/TellCommand.java b/src/main/java/cc/reconnected/server/commands/tell/TellCommand.java index 401eea0..4a59dce 100644 --- a/src/main/java/cc/reconnected/server/commands/tell/TellCommand.java +++ b/src/main/java/cc/reconnected/server/commands/tell/TellCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.tell; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; @@ -62,7 +63,7 @@ public class TellCommand { ); var sourceContext = PlaceholderContext.of(source); - source.sendFeedback(() -> Components.parse( + source.sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.tell.playerNotFound, sourceContext, placeholders @@ -84,7 +85,7 @@ public class TellCommand { } - var you = Components.parse(RccServer.CONFIG.textFormats.commands.tell.you); + var you = Placeholder.parse(RccServer.CONFIG.textFormats.commands.tell.you); var placeholdersToSource = Map.of( "sourcePlayer", you, @@ -104,22 +105,22 @@ public class TellCommand { "message", parsedMessage ); - var sourceText = Components.parse( + var sourceText = Placeholder.parse( RccServer.CONFIG.textFormats.commands.tell.message, sourceContext, placeholdersToSource ); - var targetText = Components.parse( + var targetText = Placeholder.parse( RccServer.CONFIG.textFormats.commands.tell.message, targetContext, placeholdersToTarget ); - var genericText = Components.parse( + var genericText = Placeholder.parse( RccServer.CONFIG.textFormats.commands.tell.message, serverContext, placeholders ); - var spyText = Components.parse( + var spyText = Placeholder.parse( RccServer.CONFIG.textFormats.commands.tell.messageSpy, serverContext, placeholders diff --git a/src/main/java/cc/reconnected/server/commands/warp/WarpCommand.java b/src/main/java/cc/reconnected/server/commands/warp/WarpCommand.java index 8403b46..b5c1122 100644 --- a/src/main/java/cc/reconnected/server/commands/warp/WarpCommand.java +++ b/src/main/java/cc/reconnected/server/commands/warp/WarpCommand.java @@ -1,5 +1,6 @@ package cc.reconnected.server.commands.warp; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import com.mojang.brigadier.CommandDispatcher; @@ -41,14 +42,14 @@ public class WarpCommand { var playerContext = PlaceholderContext.of(player); if (!warps.containsKey(name)) { - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.warp.warpNotFound, playerContext ), false); return 1; } - context.getSource().sendFeedback(() -> Components.parse( + context.getSource().sendFeedback(() -> Placeholder.parse( RccServer.CONFIG.textFormats.commands.warp.teleporting, playerContext, Map.of( diff --git a/src/main/java/cc/reconnected/server/config/ConfigManager.java b/src/main/java/cc/reconnected/server/config/ConfigManager.java deleted file mode 100644 index 7f1e29b..0000000 --- a/src/main/java/cc/reconnected/server/config/ConfigManager.java +++ /dev/null @@ -1,45 +0,0 @@ -package cc.reconnected.server.config; - -import cc.reconnected.server.RccServer; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import net.fabricmc.loader.api.FabricLoader; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Path; - -public class ConfigManager { - private static final Gson gson = new GsonBuilder() - .disableHtmlEscaping() - .setPrettyPrinting() - .setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX") - .create(); - private static final Path configFilePath = FabricLoader.getInstance().getConfigDir().resolve(RccServer.MOD_ID + ".json"); - private static Config config = null; - - public static Config load() throws IOException { - if (!configFilePath.toFile().exists()) { - config = new Config(); - save(); - return config; - } - var bf = new BufferedReader(new FileReader(configFilePath.toFile(), StandardCharsets.UTF_8)); - config = gson.fromJson(bf, Config.class); - bf.close(); - save(); - return config; - } - - public static void save() { - var json = gson.toJson(config); - try (var fw = new FileWriter(configFilePath.toFile(), StandardCharsets.UTF_8)) { - fw.write(json); - } catch (Exception e) { - RccServer.LOGGER.error("Error saving the RccServer config file.", e); - } - } -} diff --git a/src/main/java/cc/reconnected/server/core/AfkTracker.java b/src/main/java/cc/reconnected/server/core/AfkTracker.java index 4f7874d..8fb43e2 100644 --- a/src/main/java/cc/reconnected/server/core/AfkTracker.java +++ b/src/main/java/cc/reconnected/server/core/AfkTracker.java @@ -1,5 +1,6 @@ package cc.reconnected.server.core; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.api.events.PlayerActivityEvents; import cc.reconnected.server.api.events.RccEvents; @@ -55,7 +56,7 @@ public class AfkTracker { var playerContext = PlaceholderContext.of(player); - RccServer.getInstance().broadcast(Components.parse( + RccServer.getInstance().broadcast(Placeholder.parse( RccServer.CONFIG.textFormats.commands.afk.goneAfk, playerContext )); @@ -66,7 +67,7 @@ public class AfkTracker { var playerContext = PlaceholderContext.of(player); - RccServer.getInstance().broadcast(Components.parse( + RccServer.getInstance().broadcast(Placeholder.parse( RccServer.CONFIG.textFormats.commands.afk.returnAfk, playerContext )); diff --git a/src/main/java/cc/reconnected/server/core/HttpApiServer.java b/src/main/java/cc/reconnected/server/core/HttpApiServer.java deleted file mode 100644 index fdb4d91..0000000 --- a/src/main/java/cc/reconnected/server/core/HttpApiServer.java +++ /dev/null @@ -1,104 +0,0 @@ -package cc.reconnected.server.core; - -import cc.reconnected.server.RccServer; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; -import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; - -import java.io.IOException; -import java.net.InetSocketAddress; - -public class HttpApiServer { - private static HttpApiServer instance; - - public static HttpApiServer getInstance() { - return instance; - } - - private static float currentTps = 0; - private static float currentMspt = 0; - private static int currentPlayerCount = 0; - - public static void register() { - if (!RccServer.CONFIG.httpApi.enableHttpApi) - return; - - try { - instance = new HttpApiServer(); - } catch (IOException e) { - RccServer.LOGGER.error("Could not start HTTP API server", e); - return; - } - new Thread(() -> instance.httpServer().start()); - - ServerLifecycleEvents.SERVER_STOPPING.register(server -> { - RccServer.LOGGER.info("Stopping HTTP services"); - instance.httpServer().stop(0); - }); - - ServerTickEvents.END_SERVER_TICK.register(server -> { - currentMspt = server.getTickTime(); - if (currentMspt != 0) { - currentTps = Math.min(20, 1000 / currentMspt); - } - }); - - ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { - currentPlayerCount = server.getCurrentPlayerCount() + 1; - }); - - ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { - currentPlayerCount = server.getCurrentPlayerCount() - 1; - }); - } - - private final HttpServer server; - - public HttpServer httpServer() { - return this.server; - } - - private HttpApiServer() throws IOException { - server = HttpServer.create(new InetSocketAddress(RccServer.CONFIG.httpApi.httpPort), 0); - server.createContext("/tps", new TPSHandler()); - server.createContext("/mspt", new MSPTHandler()); - server.createContext("/player", new PlayerCountHandler()); - server.setExecutor(null); - } - - static class TPSHandler implements HttpHandler { - @Override - public void handle(HttpExchange t) throws IOException { - var tps = String.valueOf(currentTps); - t.sendResponseHeaders(200, tps.length()); - var body = t.getResponseBody(); - body.write(tps.getBytes()); - body.close(); - } - } - - static class MSPTHandler implements HttpHandler { - @Override - public void handle(HttpExchange t) throws IOException { - var tps = String.valueOf(currentTps); - t.sendResponseHeaders(200, tps.length()); - var body = t.getResponseBody(); - body.write(tps.getBytes()); - body.close(); - } - } - - static class PlayerCountHandler implements HttpHandler { - @Override - public void handle(HttpExchange t) throws IOException { - var tps = String.valueOf(currentPlayerCount); - t.sendResponseHeaders(200, tps.length()); - var body = t.getResponseBody(); - body.write(tps.getBytes()); - body.close(); - } - } -} diff --git a/src/main/java/cc/reconnected/server/core/customChat/CustomChatMessage.java b/src/main/java/cc/reconnected/server/core/customChat/CustomChatMessage.java index aef77d8..71824dc 100644 --- a/src/main/java/cc/reconnected/server/core/customChat/CustomChatMessage.java +++ b/src/main/java/cc/reconnected/server/core/customChat/CustomChatMessage.java @@ -1,5 +1,6 @@ package cc.reconnected.server.core.customChat; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import eu.pb4.placeholders.api.PlaceholderContext; @@ -28,7 +29,7 @@ public class CustomChatMessage { Text messageText = Components.chat(message, player); var playerContext = PlaceholderContext.of(player); - var text = Components.parse( + var text = Placeholder.parse( RccServer.CONFIG.textFormats.chatFormat, playerContext, Map.of( diff --git a/src/main/java/cc/reconnected/server/core/customChat/CustomConnectionMessage.java b/src/main/java/cc/reconnected/server/core/customChat/CustomConnectionMessage.java index 7578bf1..9d337e1 100644 --- a/src/main/java/cc/reconnected/server/core/customChat/CustomConnectionMessage.java +++ b/src/main/java/cc/reconnected/server/core/customChat/CustomConnectionMessage.java @@ -1,5 +1,6 @@ package cc.reconnected.server.core.customChat; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import eu.pb4.placeholders.api.PlaceholderContext; @@ -11,7 +12,7 @@ import java.util.Map; public class CustomConnectionMessage { public static Text onJoin(ServerPlayerEntity player) { var playerContext = PlaceholderContext.of(player); - return Components.parse( + return Placeholder.parse( RccServer.CONFIG.textFormats.joinFormat, playerContext ); @@ -19,7 +20,7 @@ public class CustomConnectionMessage { public static Text onJoinRenamed(ServerPlayerEntity player, String previousName) { var playerContext = PlaceholderContext.of(player); - return Components.parse( + return Placeholder.parse( RccServer.CONFIG.textFormats.joinRenamedFormat, playerContext, Map.of("previousName", Text.of(previousName)) @@ -28,7 +29,7 @@ public class CustomConnectionMessage { public static Text onLeave(ServerPlayerEntity player) { var playerContext = PlaceholderContext.of(player); - return Components.parse( + return Placeholder.parse( RccServer.CONFIG.textFormats.leaveFormat, playerContext ); diff --git a/src/main/java/cc/reconnected/server/core/customChat/CustomDeathMessage.java b/src/main/java/cc/reconnected/server/core/customChat/CustomDeathMessage.java index 7807c7a..7ab6bb9 100644 --- a/src/main/java/cc/reconnected/server/core/customChat/CustomDeathMessage.java +++ b/src/main/java/cc/reconnected/server/core/customChat/CustomDeathMessage.java @@ -1,5 +1,6 @@ package cc.reconnected.server.core.customChat; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import eu.pb4.placeholders.api.PlaceholderContext; @@ -14,7 +15,7 @@ public class CustomDeathMessage { var deathMessage = instance.getDeathMessage(); var playerContext = PlaceholderContext.of(player); - return Components.parse( + return Placeholder.parse( RccServer.CONFIG.textFormats.deathFormat, playerContext, Map.of("message", deathMessage) diff --git a/src/main/java/cc/reconnected/server/core/customChat/CustomEmoteMessage.java b/src/main/java/cc/reconnected/server/core/customChat/CustomEmoteMessage.java index 51d6572..5a497bb 100644 --- a/src/main/java/cc/reconnected/server/core/customChat/CustomEmoteMessage.java +++ b/src/main/java/cc/reconnected/server/core/customChat/CustomEmoteMessage.java @@ -1,5 +1,6 @@ package cc.reconnected.server.core.customChat; +import cc.reconnected.library.text.Placeholder; import cc.reconnected.server.RccServer; import cc.reconnected.server.util.Components; import eu.pb4.placeholders.api.PlaceholderContext; @@ -19,7 +20,7 @@ public class CustomEmoteMessage { Text messageText = Components.chat(message, player); - var text = Components.parse( + var text = Placeholder.parse( RccServer.CONFIG.textFormats.emoteFormat, playerContext, Map.of( diff --git a/src/main/java/cc/reconnected/server/parser/LinkParser.java b/src/main/java/cc/reconnected/server/parser/LinkParser.java deleted file mode 100644 index 7e18c7b..0000000 --- a/src/main/java/cc/reconnected/server/parser/LinkParser.java +++ /dev/null @@ -1,99 +0,0 @@ -package cc.reconnected.server.parser; - -import cc.reconnected.server.RccServer; -import cc.reconnected.server.util.Components; -import eu.pb4.placeholders.api.node.DirectTextNode; -import eu.pb4.placeholders.api.node.LiteralNode; -import eu.pb4.placeholders.api.node.TextNode; -import eu.pb4.placeholders.api.node.parent.ParentNode; -import eu.pb4.placeholders.api.parsers.NodeParser; -import net.minecraft.text.ClickEvent; -import net.minecraft.text.HoverEvent; -import net.minecraft.text.Style; -import net.minecraft.text.Text; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; - -public class LinkParser implements NodeParser { - public static final Pattern URL_REGEX = Pattern.compile("https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)"); - - @Override - public TextNode[] parseNodes(TextNode node) { - if (node instanceof LiteralNode literalNode) { - var input = literalNode.value(); - var list = new ArrayList(); - var inputLength = input.length(); - - var matcher = URL_REGEX.matcher(input); - int pos = 0; - - while (matcher.find()) { - if (inputLength <= matcher.start()) { - break; - } - - String betweenText = input.substring(pos, matcher.start()); - - if (!betweenText.isEmpty()) { - list.add(new LiteralNode(betweenText)); - } - - var link = matcher.group(); - - var url = Text.of(link); - - var placeholders = Map.of( - "url", url, - "label", url - ); - - var display = Components.parse( - RccServer.CONFIG.textFormats.link, - placeholders - ); - - var hover = Components.parse( - RccServer.CONFIG.textFormats.linkHover, - placeholders - ); - - var text = Text.empty() - .append(display) - .setStyle(Style.EMPTY - .withHoverEvent( - new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover) - ) - .withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link)) - ); - - list.add(new DirectTextNode(text)); - - pos = matcher.end(); - } - - if (pos < inputLength) { - var text = input.substring(pos, inputLength); - if (!text.isEmpty()) { - list.add(new LiteralNode(text)); - } - } - - return list.toArray(TextNode[]::new); - } else if (node instanceof ParentNode parentNode) { - var list = new ArrayList(); - - for (var child : parentNode.getChildren()) { - list.addAll(List.of(this.parseNodes(child))); - } - - return new TextNode[]{ - parentNode.copyWith(list.toArray(TextNode[]::new)) - }; - } - - return TextNode.array(node); - } -} \ No newline at end of file diff --git a/src/main/java/cc/reconnected/server/parser/MarkdownComponentParser.java b/src/main/java/cc/reconnected/server/parser/MarkdownComponentParser.java deleted file mode 100644 index 64a8f51..0000000 --- a/src/main/java/cc/reconnected/server/parser/MarkdownComponentParser.java +++ /dev/null @@ -1,52 +0,0 @@ -package cc.reconnected.server.parser; - -import cc.reconnected.server.RccServer; -import cc.reconnected.server.util.Components; -import eu.pb4.placeholders.api.node.TextNode; -import eu.pb4.placeholders.api.node.parent.ClickActionNode; -import eu.pb4.placeholders.api.node.parent.FormattingNode; -import eu.pb4.placeholders.api.node.parent.HoverNode; -import net.minecraft.text.ClickEvent; -import net.minecraft.util.Formatting; - -import java.util.Map; - -public class MarkdownComponentParser { - public static TextNode spoilerFormatting(TextNode[] textNodes) { - var text = TextNode.asSingle(textNodes); - return new HoverNode<>( - TextNode.array( - new FormattingNode(TextNode.array(TextNode.of("\u258C".repeat(text.toText().getString().length()))), Formatting.DARK_GRAY) - ), - HoverNode.Action.TEXT, text); - } - - public static TextNode quoteFormatting(TextNode[] textNodes) { - return new ClickActionNode( - TextNode.array( - new HoverNode<>( - TextNode.array(new FormattingNode(textNodes, Formatting.GRAY)), - HoverNode.Action.TEXT, TextNode.of("Click to copy")) - ), - ClickEvent.Action.COPY_TO_CLIPBOARD, TextNode.asSingle(textNodes) - ); - } - - public static TextNode urlFormatting(TextNode[] textNodes, TextNode url) { - var placeholders = Map.of( - "label", TextNode.wrap(textNodes).toText(), - "url", url.toText() - ); - var text = Components.parse(RccServer.CONFIG.textFormats.link, placeholders); - var hover = Components.parse(RccServer.CONFIG.textFormats.linkHover, placeholders); - - return new HoverNode<>(TextNode.array( - new ClickActionNode( - TextNode.array( - TextNode.convert(text) - ), - ClickEvent.Action.OPEN_URL, url)), - HoverNode.Action.TEXT, TextNode.convert(hover) - ); - } -} diff --git a/src/main/java/cc/reconnected/server/parser/MarkdownParser.java b/src/main/java/cc/reconnected/server/parser/MarkdownParser.java deleted file mode 100644 index 6cd61ef..0000000 --- a/src/main/java/cc/reconnected/server/parser/MarkdownParser.java +++ /dev/null @@ -1,32 +0,0 @@ -package cc.reconnected.server.parser; - -import eu.pb4.placeholders.api.parsers.MarkdownLiteParserV1; -import eu.pb4.placeholders.api.parsers.NodeParser; - -import static eu.pb4.placeholders.api.parsers.MarkdownLiteParserV1.MarkdownFormat; - - -public class MarkdownParser { - public static final MarkdownFormat[] ALL = new MarkdownFormat[]{ - MarkdownFormat.QUOTE, - MarkdownFormat.BOLD, - MarkdownFormat.ITALIC, - MarkdownFormat.UNDERLINE, - MarkdownFormat.STRIKETHROUGH, - MarkdownFormat.SPOILER, - MarkdownFormat.URL - }; - - public static final NodeParser defaultParser = createParser(ALL); - - public static NodeParser createParser(MarkdownFormat[] capabilities) { - var mdParser = new MarkdownLiteParserV1( - MarkdownComponentParser::spoilerFormatting, - MarkdownComponentParser::quoteFormatting, - MarkdownComponentParser::urlFormatting, - capabilities - ); - - return NodeParser.merge(mdParser, new LinkParser()); - } -} diff --git a/src/main/java/cc/reconnected/server/util/Components.java b/src/main/java/cc/reconnected/server/util/Components.java index 5c023d3..c7346af 100644 --- a/src/main/java/cc/reconnected/server/util/Components.java +++ b/src/main/java/cc/reconnected/server/util/Components.java @@ -1,16 +1,12 @@ package cc.reconnected.server.util; +import cc.reconnected.library.text.parser.MarkdownParser; import cc.reconnected.server.RccServer; -import cc.reconnected.server.parser.MarkdownParser; -import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.Placeholders; import eu.pb4.placeholders.api.TextParserUtils; -import eu.pb4.placeholders.api.node.TextNode; import eu.pb4.placeholders.api.parsers.NodeParser; import eu.pb4.placeholders.api.parsers.PatternPlaceholderParser; import eu.pb4.placeholders.api.parsers.TextParserV1; -import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; -import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.event.ClickEvent; @@ -65,31 +61,6 @@ public class Components { return Text.Serializer.fromJson(json); } - public static Text parse(String text) { - return TextParserUtils.formatText(text); - } - - public static Text parse(TextNode textNode, PlaceholderContext context, Map placeholders) { - var predefinedNode = Placeholders.parseNodes(textNode, PatternPlaceholderParser.PREDEFINED_PLACEHOLDER_PATTERN, placeholders); - return Placeholders.parseText(predefinedNode, context); - } - - public static Text parse(Text text, PlaceholderContext context, Map placeholders) { - return parse(TextNode.convert(text), context, placeholders); - } - - public static Text parse(String text, PlaceholderContext context, Map placeholders) { - return parse(parse(text), context, placeholders); - } - - public static Text parse(String text, PlaceholderContext context) { - return parse(parse(text), context, Map.of()); - } - - public static Text parse(String text, Map placeholders) { - return Placeholders.parseText(parse(text), PatternPlaceholderParser.PREDEFINED_PLACEHOLDER_PATTERN, placeholders); - } - public static Text chat(SignedMessage message, ServerPlayerEntity player) { var luckperms = RccServer.getInstance().luckPerms();