From cc6fd0edb81e4f394abdeb064973951aaf227f36 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Wed, 30 Oct 2024 10:05:08 +0100 Subject: [PATCH] Config refactor (#6) * Refactor config into categories * Bump version to 1.16.2 --- gradle.properties | 2 +- .../server/RccServerConfigModel.java | 125 +++++++++++------- .../server/commands/admin/RccCommand.java | 3 +- .../server/commands/admin/RestartCommand.java | 2 +- .../server/commands/misc/NearCommand.java | 4 +- .../server/commands/tell/TellCommand.java | 8 +- .../reconnected/server/core/AfkTracker.java | 8 +- .../reconnected/server/core/AutoRestart.java | 16 +-- .../server/core/HttpApiServer.java | 4 +- .../cc/reconnected/server/core/TabList.java | 14 +- .../server/core/TeleportTracker.java | 2 +- .../mixin/ServerPlayNetworkManagerMixin.java | 2 +- .../server/mixin/ServerPlayerEntityMixin.java | 4 +- 13 files changed, 115 insertions(+), 79 deletions(-) diff --git a/gradle.properties b/gradle.properties index 344cdc3..98bb9ba 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10 loader_version=0.16.7 # Mod Properties -mod_version=1.16.1 +mod_version=1.16.2 maven_group=cc.reconnected archives_base_name=rcc-server diff --git a/src/main/java/cc/reconnected/server/RccServerConfigModel.java b/src/main/java/cc/reconnected/server/RccServerConfigModel.java index e06c020..fe9547b 100644 --- a/src/main/java/cc/reconnected/server/RccServerConfigModel.java +++ b/src/main/java/cc/reconnected/server/RccServerConfigModel.java @@ -1,67 +1,102 @@ package cc.reconnected.server; import io.wispforest.owo.config.annotation.Config; +import io.wispforest.owo.config.annotation.Nest; import java.util.ArrayList; import java.util.List; @Config(name = "rcc-server-config", wrapperName = "RccServerConfig") public class RccServerConfigModel { - public boolean enableHttpApi = true; - public int httpPort = 25581; + @Nest + public HttpApi httpApi = new HttpApi(); - public int afkTimeTrigger = 300; + @Nest + public Afk afk = new Afk(); - public String afkMessage = " is now AFK"; - public String afkReturnMessage = " is no longer AFK"; - public String afkTag = "[AFK] "; + @Nest + public DirectMessages directMessages = new DirectMessages(); - public String tellMessage = "[ ] "; - public String tellMessageSpy = "\uD83D\uDC41 [] "; + @Nest + public TeleportRequests teleportRequests = new TeleportRequests(); - public int teleportRequestTimeout = 120; + @Nest + public CustomTabList customTabList = new CustomTabList(); - public boolean enableTabList = true; - public int tabListTickDelay = 5; - public double tabPhasePeriod = 300; - public ArrayList tabHeader = new ArrayList<>(List.of( - " " - )); + @Nest + public NearCommand nearCommand = new NearCommand(); - public ArrayList tabFooter = new ArrayList<>(List.of( - " " - )); + @Nest + public AutoRestart autoRestart = new AutoRestart(); - public String playerTabName = "%rcc-server:afk%%player:displayname_visual%"; + public static class HttpApi { + public boolean enableHttpApi = true; + public int httpPort = 25581; + } - public int nearCommandMaxRange = 48; - public int nearCommandDefaultRange = 32; + public static class Afk { + public int afkTimeTrigger = 300; + public String afkMessage = " is now AFK"; + public String afkReturnMessage = " is no longer AFK"; + public String afkTag = "[AFK] "; + } - public boolean enableAutoRestart = true; - public String restartBarLabel = "Server restarting in "; - public String restartKickMessage = "The server is restarting!"; - public String restartChatMessage = "The server is restarting in "; + public static class DirectMessages { + public String tellMessage = "[ ] "; + public String tellMessageSpy = "\uD83D\uDC41 [] "; + } - public ArrayList restartAt = new ArrayList<>(List.of( - "06:00", - "18:00" - )); + public static class TeleportRequests { + public int teleportRequestTimeout = 120; + } - public String restartSound = "minecraft:block.note_block.bell"; - public float restartSoundPitch = 0.9f; + public static class CustomTabList { + public boolean enableTabList = true; + public int tabListTickDelay = 5; + public double tabPhasePeriod = 300; + public ArrayList tabHeader = new ArrayList<>(List.of( + " " + )); - public ArrayList restartNotifications = new ArrayList<>(List.of( - 600, - 300, - 120, - 60, - 30, - 15, - 10, - 5, - 4, - 3, - 2, - 1 - )); + public ArrayList tabFooter = new ArrayList<>(List.of( + " " + )); + + public String playerTabName = "%rcc-server:afk%%player:displayname_visual%"; + } + + public static class NearCommand { + public int nearCommandMaxRange = 48; + public int nearCommandDefaultRange = 32; + } + + public static class AutoRestart { + public boolean enableAutoRestart = true; + public String restartBarLabel = "Server restarting in "; + public String restartKickMessage = "The server is restarting!"; + public String restartChatMessage = "The server is restarting in "; + + public ArrayList restartAt = new ArrayList<>(List.of( + "06:00", + "18:00" + )); + + public String restartSound = "minecraft:block.note_block.bell"; + public float restartSoundPitch = 0.9f; + + public ArrayList restartNotifications = new ArrayList<>(List.of( + 600, + 300, + 120, + 60, + 30, + 15, + 10, + 5, + 4, + 3, + 2, + 1 + )); + } } 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 a85a65a..8dff08f 100644 --- a/src/main/java/cc/reconnected/server/commands/admin/RccCommand.java +++ b/src/main/java/cc/reconnected/server/commands/admin/RccCommand.java @@ -14,8 +14,9 @@ import static net.minecraft.server.command.CommandManager.literal; public class RccCommand { public static void register(CommandDispatcher dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) { var rootCommand = literal("rcc") - .requires(Permissions.require("rcc.command.afk", 3)) + .requires(Permissions.require("rcc.command.rcc", 3)) .then(literal("reload") + .requires(Permissions.require("rcc.command.rcc.reload", 3)) .executes(context -> { context.getSource().sendFeedback(() -> Text.of("Reloading RCC config..."), true); diff --git a/src/main/java/cc/reconnected/server/commands/admin/RestartCommand.java b/src/main/java/cc/reconnected/server/commands/admin/RestartCommand.java index f893260..9ee012c 100644 --- a/src/main/java/cc/reconnected/server/commands/admin/RestartCommand.java +++ b/src/main/java/cc/reconnected/server/commands/admin/RestartCommand.java @@ -37,7 +37,7 @@ public class RestartCommand { private static int schedule(CommandContext context, int seconds, @Nullable String message) { if (message == null) { - message = RccServer.CONFIG.restartBarLabel(); + message = RccServer.CONFIG.autoRestart.restartBarLabel(); } AutoRestart.schedule(seconds, message); 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 cfabbc3..0dc1a07 100644 --- a/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java +++ b/src/main/java/cc/reconnected/server/commands/misc/NearCommand.java @@ -26,9 +26,9 @@ public class NearCommand { context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false); return 1; } - return execute(context, RccServer.CONFIG.nearCommandDefaultRange(), context.getSource().getPlayer()); + return execute(context, RccServer.CONFIG.nearCommand.nearCommandDefaultRange(), context.getSource().getPlayer()); }) - .then(argument("radius", IntegerArgumentType.integer(0, RccServer.CONFIG.nearCommandMaxRange())) + .then(argument("radius", IntegerArgumentType.integer(0, RccServer.CONFIG.nearCommand.nearCommandMaxRange())) .executes(context -> { if (!context.getSource().isExecutedByPlayer()) { context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), 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 9776127..5db53b4 100644 --- a/src/main/java/cc/reconnected/server/commands/tell/TellCommand.java +++ b/src/main/java/cc/reconnected/server/commands/tell/TellCommand.java @@ -71,17 +71,17 @@ public class TellCommand { var parsedMessage = MarkdownParser.defaultParser.parseNode(message); var you = Component.text("You", NamedTextColor.GRAY, TextDecoration.ITALIC); - var sourceText = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.tellMessage(), + var sourceText = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.directMessages.tellMessage(), Placeholder.component("source", you), Placeholder.component("target", targetDisplayName), Placeholder.component("message", parsedMessage.toText())); - var targetText = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.tellMessage(), + var targetText = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.directMessages.tellMessage(), Placeholder.component("source", source.getDisplayName()), Placeholder.component("target", you), Placeholder.component("message", parsedMessage.toText())); - var text = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.tellMessage(), + var text = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.directMessages.tellMessage(), Placeholder.component("source", source.getDisplayName()), Placeholder.component("target", targetDisplayName), Placeholder.component("message", parsedMessage.toText())); @@ -104,7 +104,7 @@ public class TellCommand { var lp = RccServer.getInstance().luckPerms(); var playerAdapter = lp.getPlayerAdapter(ServerPlayerEntity.class); - var spyText = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.tellMessageSpy(), + var spyText = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.directMessages.tellMessageSpy(), Placeholder.component("source", source.getDisplayName()), Placeholder.component("target", targetDisplayName), Placeholder.component("message", parsedMessage.toText())); diff --git a/src/main/java/cc/reconnected/server/core/AfkTracker.java b/src/main/java/cc/reconnected/server/core/AfkTracker.java index 9928430..a9919c7 100644 --- a/src/main/java/cc/reconnected/server/core/AfkTracker.java +++ b/src/main/java/cc/reconnected/server/core/AfkTracker.java @@ -25,7 +25,7 @@ import java.util.concurrent.ConcurrentHashMap; public class AfkTracker { private static final int cycleDelay = 1; - private static final int absentTimeTrigger = RccServer.CONFIG.afkTimeTrigger() * 20; // seconds * 20 ticks + private static final int absentTimeTrigger = RccServer.CONFIG.afk.afkTimeTrigger() * 20; // seconds * 20 ticks private final ConcurrentHashMap playerActivityStates = new ConcurrentHashMap<>(); @@ -57,7 +57,7 @@ public class AfkTracker { var displayNameJson = Text.Serializer.toJson(player.getDisplayName()); var displayName = JSONComponentSerializer.json().deserialize(displayNameJson); - var message = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.afkMessage(), + var message = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.afk.afkMessage(), Placeholder.component("displayname", displayName), Placeholder.unparsed("username", player.getGameProfile().getName()), Placeholder.unparsed("uuid", player.getUuid().toString()) @@ -72,7 +72,7 @@ public class AfkTracker { var displayNameJson = Text.Serializer.toJson(player.getDisplayName()); var displayName = JSONComponentSerializer.json().deserialize(displayNameJson); - var message = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.afkReturnMessage(), + var message = MiniMessage.miniMessage().deserialize(RccServer.CONFIG.afk.afkReturnMessage(), Placeholder.component("displayname", displayName), Placeholder.unparsed("username", player.getGameProfile().getName()), Placeholder.unparsed("uuid", player.getUuid().toString()) @@ -85,7 +85,7 @@ public class AfkTracker { } private static void loadAfkTag() { - afkTag = Components.toText(MiniMessage.miniMessage().deserialize(RccServer.CONFIG.afkTag())); + afkTag = Components.toText(MiniMessage.miniMessage().deserialize(RccServer.CONFIG.afk.afkTag())); } private AfkTracker() { diff --git a/src/main/java/cc/reconnected/server/core/AutoRestart.java b/src/main/java/cc/reconnected/server/core/AutoRestart.java index a5c703b..1449967 100644 --- a/src/main/java/cc/reconnected/server/core/AutoRestart.java +++ b/src/main/java/cc/reconnected/server/core/AutoRestart.java @@ -33,7 +33,7 @@ public class AutoRestart { var miniMessage = MiniMessage.miniMessage(); RccEvents.READY.register((server, luckPerms) -> { - if (RccServer.CONFIG.enableAutoRestart()) { + if (RccServer.CONFIG.autoRestart.enableAutoRestart()) { scheduleNextRestart(); } }); @@ -42,7 +42,7 @@ public class AutoRestart { if (restartBar == null || !timeBar.getUuid().equals(restartBar.getUuid())) return; - var notificationTimes = RccServer.CONFIG.restartNotifications(); + var notificationTimes = RccServer.CONFIG.autoRestart.restartNotifications(); var remainingSeconds = restartBar.getRemainingSeconds(); if (notificationTimes.contains(remainingSeconds)) { @@ -57,7 +57,7 @@ public class AutoRestart { return; final var text = Components.toText( - miniMessage.deserialize(RccServer.CONFIG.restartKickMessage()) + miniMessage.deserialize(RccServer.CONFIG.autoRestart.restartKickMessage()) ); server.getPlayerManager().getPlayerList().forEach(player -> { player.networkHandler.disconnect(text); @@ -74,7 +74,7 @@ public class AutoRestart { } private static void setup() { - var soundName = RccServer.CONFIG.restartSound(); + var soundName = RccServer.CONFIG.autoRestart.restartSound(); try { notificationKey = Key.key(soundName); } catch (InvalidKeyException e) { @@ -115,10 +115,10 @@ public class AutoRestart { private static void notifyRestart(MinecraftServer server, BossBarManager.TimeBar bar) { var rcc = RccServer.getInstance(); var audience = rcc.adventure().players(); - var sound = Sound.sound(notificationKey, Sound.Source.MASTER, 10f, RccServer.CONFIG.restartSoundPitch()); + var sound = Sound.sound(notificationKey, Sound.Source.MASTER, 10f, RccServer.CONFIG.autoRestart.restartSoundPitch()); audience.playSound(sound, Sound.Emitter.self()); - var comp = bar.parseLabel(RccServer.CONFIG.restartChatMessage()); + var comp = bar.parseLabel(RccServer.CONFIG.autoRestart.restartChatMessage()); rcc.broadcastMessage(server, comp); } @@ -133,7 +133,7 @@ public class AutoRestart { var barStartTime = delay - barTime; currentSchedule = scheduler.schedule(() -> { - schedule(barTime, RccServer.CONFIG.restartBarLabel()); + schedule(barTime, RccServer.CONFIG.autoRestart.restartBarLabel()); }, barStartTime, TimeUnit.SECONDS); RccServer.LOGGER.info("Restart scheduled for in {} seconds", delay); @@ -142,7 +142,7 @@ public class AutoRestart { @Nullable private static Long getNextDelay() { - var restartTimeStrings = RccServer.CONFIG.restartAt(); + var restartTimeStrings = RccServer.CONFIG.autoRestart.restartAt(); LocalDateTime now = LocalDateTime.now(); LocalDateTime nextRunTime = null; long shortestDelay = Long.MAX_VALUE; diff --git a/src/main/java/cc/reconnected/server/core/HttpApiServer.java b/src/main/java/cc/reconnected/server/core/HttpApiServer.java index d67ca15..3c26509 100644 --- a/src/main/java/cc/reconnected/server/core/HttpApiServer.java +++ b/src/main/java/cc/reconnected/server/core/HttpApiServer.java @@ -23,7 +23,7 @@ public class HttpApiServer { private static int currentPlayerCount = 0; public static void register() { - if (!RccServer.CONFIG.enableHttpApi()) + if (!RccServer.CONFIG.httpApi.enableHttpApi()) return; try { @@ -62,7 +62,7 @@ public class HttpApiServer { } private HttpApiServer() throws IOException { - server = HttpServer.create(new InetSocketAddress(RccServer.CONFIG.httpPort()), 0); + 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()); diff --git a/src/main/java/cc/reconnected/server/core/TabList.java b/src/main/java/cc/reconnected/server/core/TabList.java index d4e84cd..2f83982 100644 --- a/src/main/java/cc/reconnected/server/core/TabList.java +++ b/src/main/java/cc/reconnected/server/core/TabList.java @@ -10,23 +10,23 @@ import net.kyori.adventure.text.minimessage.MiniMessage; public class TabList { public static void register() { - if (!RccServer.CONFIG.enableTabList()) + if (!RccServer.CONFIG.customTabList.enableTabList()) return; var minimessage = MiniMessage.miniMessage(); ServerTickEvents.END_SERVER_TICK.register(server -> { - var delay = Math.max(RccServer.CONFIG.tabListTickDelay(), 1); + var delay = Math.max(RccServer.CONFIG.customTabList.tabListTickDelay(), 1); if(server.getTicks() % delay == 0) { - var period = Math.max(RccServer.CONFIG.tabPhasePeriod(), 1); + var period = Math.max(RccServer.CONFIG.customTabList.tabPhasePeriod(), 1); var phase = (Math.sin((server.getTicks() * Math.PI * 2) / period) + 1) / 2d; server.getPlayerManager().getPlayerList().forEach(player -> { var playerContext = PlaceholderContext.of(player); Component headerComponent = Component.empty(); - for (int i = 0; i < RccServer.CONFIG.tabHeader().size(); i++) { - var line = RccServer.CONFIG.tabHeader().get(i); + for (int i = 0; i < RccServer.CONFIG.customTabList.tabHeader().size(); i++) { + var line = RccServer.CONFIG.customTabList.tabHeader().get(i); line = line.replace("{phase}", String.valueOf(phase)); if (i > 0) { headerComponent = headerComponent.appendNewline(); @@ -36,8 +36,8 @@ public class TabList { } Component footerComponent = Component.empty(); - for (int i = 0; i < RccServer.CONFIG.tabFooter().size(); i++) { - var line = RccServer.CONFIG.tabFooter().get(i); + for (int i = 0; i < RccServer.CONFIG.customTabList.tabFooter().size(); i++) { + var line = RccServer.CONFIG.customTabList.tabFooter().get(i); line = line.replace("{phase}", String.valueOf(phase)); if (i > 0) { footerComponent = footerComponent.appendNewline(); diff --git a/src/main/java/cc/reconnected/server/core/TeleportTracker.java b/src/main/java/cc/reconnected/server/core/TeleportTracker.java index 59c512c..fd7fb25 100644 --- a/src/main/java/cc/reconnected/server/core/TeleportTracker.java +++ b/src/main/java/cc/reconnected/server/core/TeleportTracker.java @@ -37,7 +37,7 @@ public class TeleportTracker { this.player = player; this.target = target; // Seconds in config per 20 ticks - this.remainingTicks = RccServer.CONFIG.teleportRequestTimeout() * 20; + this.remainingTicks = RccServer.CONFIG.teleportRequests.teleportRequestTimeout() * 20; } public void expire() { diff --git a/src/main/java/cc/reconnected/server/mixin/ServerPlayNetworkManagerMixin.java b/src/main/java/cc/reconnected/server/mixin/ServerPlayNetworkManagerMixin.java index 33d1d5b..dcf140a 100644 --- a/src/main/java/cc/reconnected/server/mixin/ServerPlayNetworkManagerMixin.java +++ b/src/main/java/cc/reconnected/server/mixin/ServerPlayNetworkManagerMixin.java @@ -29,7 +29,7 @@ public abstract class ServerPlayNetworkManagerMixin { @Inject(method = "tick", at = @At("TAIL")) private void rccServer$updatePlayerList(CallbackInfo ci) { - if(RccServer.CONFIG.enableTabList()) { + if(RccServer.CONFIG.customTabList.enableTabList()) { var packet = new PlayerListS2CPacket(EnumSet.of(PlayerListS2CPacket.Action.UPDATE_DISPLAY_NAME, PlayerListS2CPacket.Action.UPDATE_LISTED), List.of(this.player)); this.server.getPlayerManager().sendToAll(packet); } diff --git a/src/main/java/cc/reconnected/server/mixin/ServerPlayerEntityMixin.java b/src/main/java/cc/reconnected/server/mixin/ServerPlayerEntityMixin.java index 1edb20e..c798bf0 100644 --- a/src/main/java/cc/reconnected/server/mixin/ServerPlayerEntityMixin.java +++ b/src/main/java/cc/reconnected/server/mixin/ServerPlayerEntityMixin.java @@ -20,10 +20,10 @@ public class ServerPlayerEntityMixin { @Inject(method = "getPlayerListName", at = @At("HEAD"), cancellable = true) private void rccServer$customizePlayerListName(CallbackInfoReturnable callback) { - if (RccServer.CONFIG.enableTabList()) { + if (RccServer.CONFIG.customTabList.enableTabList()) { var player = (ServerPlayerEntity) (Object) this; var playerContext = PlaceholderContext.of(player); - var text = Placeholders.parseText(parser.parseNode(RccServer.CONFIG.playerTabName()), playerContext); + var text = Placeholders.parseText(parser.parseNode(RccServer.CONFIG.customTabList.playerTabName()), playerContext); callback.setReturnValue(text); } }