Config refactor (#6)

* Refactor config into categories

* Bump version to 1.16.2
This commit is contained in:
Alessandro Proto 2024-10-30 10:05:08 +01:00 committed by GitHub
parent 82f4e24404
commit cc6fd0edb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 115 additions and 79 deletions

View file

@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.16.7 loader_version=0.16.7
# Mod Properties # Mod Properties
mod_version=1.16.1 mod_version=1.16.2
maven_group=cc.reconnected maven_group=cc.reconnected
archives_base_name=rcc-server archives_base_name=rcc-server

View file

@ -1,67 +1,102 @@
package cc.reconnected.server; package cc.reconnected.server;
import io.wispforest.owo.config.annotation.Config; import io.wispforest.owo.config.annotation.Config;
import io.wispforest.owo.config.annotation.Nest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Config(name = "rcc-server-config", wrapperName = "RccServerConfig") @Config(name = "rcc-server-config", wrapperName = "RccServerConfig")
public class RccServerConfigModel { public class RccServerConfigModel {
public boolean enableHttpApi = true; @Nest
public int httpPort = 25581; public HttpApi httpApi = new HttpApi();
public int afkTimeTrigger = 300; @Nest
public Afk afk = new Afk();
public String afkMessage = "<gray><displayname> is now AFK</gray>"; @Nest
public String afkReturnMessage = "<gray><displayname> is no longer AFK</gray>"; public DirectMessages directMessages = new DirectMessages();
public String afkTag = "<gray>[AFK]</gray> ";
public String tellMessage = "<gold>[</gold><source> <gray>→</gray> <target><gold>]</gold> <message>"; @Nest
public String tellMessageSpy = "\uD83D\uDC41 <gray>[<source> → <target>] <message></gray>"; public TeleportRequests teleportRequests = new TeleportRequests();
public int teleportRequestTimeout = 120; @Nest
public CustomTabList customTabList = new CustomTabList();
public boolean enableTabList = true; @Nest
public int tabListTickDelay = 5; public NearCommand nearCommand = new NearCommand();
public double tabPhasePeriod = 300;
public ArrayList<String> tabHeader = new ArrayList<>(List.of(
"<gradient:#DEDE6C:#CC4C4C:{phase}><st> </st></gradient>"
));
public ArrayList<String> tabFooter = new ArrayList<>(List.of( @Nest
"<gradient:#DEDE6C:#CC4C4C:{phase}><st> </st></gradient>" 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 static class Afk {
public int nearCommandDefaultRange = 32; public int afkTimeTrigger = 300;
public String afkMessage = "<gray><displayname> is now AFK</gray>";
public String afkReturnMessage = "<gray><displayname> is no longer AFK</gray>";
public String afkTag = "<gray>[AFK]</gray> ";
}
public boolean enableAutoRestart = true; public static class DirectMessages {
public String restartBarLabel = "Server restarting in <remaining_time>"; public String tellMessage = "<gold>[</gold><source> <gray>→</gray> <target><gold>]</gold> <message>";
public String restartKickMessage = "The server is restarting!"; public String tellMessageSpy = "\uD83D\uDC41 <gray>[<source> → <target>] <message></gray>";
public String restartChatMessage = "<red>The server is restarting in </red><gold><remaining_time></gold>"; }
public ArrayList<String> restartAt = new ArrayList<>(List.of( public static class TeleportRequests {
"06:00", public int teleportRequestTimeout = 120;
"18:00" }
));
public String restartSound = "minecraft:block.note_block.bell"; public static class CustomTabList {
public float restartSoundPitch = 0.9f; public boolean enableTabList = true;
public int tabListTickDelay = 5;
public double tabPhasePeriod = 300;
public ArrayList<String> tabHeader = new ArrayList<>(List.of(
"<gradient:#DEDE6C:#CC4C4C:{phase}><st> </st></gradient>"
));
public ArrayList<Integer> restartNotifications = new ArrayList<>(List.of( public ArrayList<String> tabFooter = new ArrayList<>(List.of(
600, "<gradient:#DEDE6C:#CC4C4C:{phase}><st> </st></gradient>"
300, ));
120,
60, public String playerTabName = "%rcc-server:afk%%player:displayname_visual%";
30, }
15,
10, public static class NearCommand {
5, public int nearCommandMaxRange = 48;
4, public int nearCommandDefaultRange = 32;
3, }
2,
1 public static class AutoRestart {
)); public boolean enableAutoRestart = true;
public String restartBarLabel = "Server restarting in <remaining_time>";
public String restartKickMessage = "The server is restarting!";
public String restartChatMessage = "<red>The server is restarting in </red><gold><remaining_time></gold>";
public ArrayList<String> restartAt = new ArrayList<>(List.of(
"06:00",
"18:00"
));
public String restartSound = "minecraft:block.note_block.bell";
public float restartSoundPitch = 0.9f;
public ArrayList<Integer> restartNotifications = new ArrayList<>(List.of(
600,
300,
120,
60,
30,
15,
10,
5,
4,
3,
2,
1
));
}
} }

View file

@ -14,8 +14,9 @@ import static net.minecraft.server.command.CommandManager.literal;
public class RccCommand { public class RccCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) { public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
var rootCommand = literal("rcc") var rootCommand = literal("rcc")
.requires(Permissions.require("rcc.command.afk", 3)) .requires(Permissions.require("rcc.command.rcc", 3))
.then(literal("reload") .then(literal("reload")
.requires(Permissions.require("rcc.command.rcc.reload", 3))
.executes(context -> { .executes(context -> {
context.getSource().sendFeedback(() -> Text.of("Reloading RCC config..."), true); context.getSource().sendFeedback(() -> Text.of("Reloading RCC config..."), true);

View file

@ -37,7 +37,7 @@ public class RestartCommand {
private static int schedule(CommandContext<ServerCommandSource> context, int seconds, @Nullable String message) { private static int schedule(CommandContext<ServerCommandSource> context, int seconds, @Nullable String message) {
if (message == null) { if (message == null) {
message = RccServer.CONFIG.restartBarLabel(); message = RccServer.CONFIG.autoRestart.restartBarLabel();
} }
AutoRestart.schedule(seconds, message); AutoRestart.schedule(seconds, message);

View file

@ -26,9 +26,9 @@ public class NearCommand {
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false); context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);
return 1; 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 -> { .executes(context -> {
if (!context.getSource().isExecutedByPlayer()) { if (!context.getSource().isExecutedByPlayer()) {
context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false); context.getSource().sendFeedback(() -> Text.of("This command can only be executed by players!"), false);

View file

@ -71,17 +71,17 @@ public class TellCommand {
var parsedMessage = MarkdownParser.defaultParser.parseNode(message); var parsedMessage = MarkdownParser.defaultParser.parseNode(message);
var you = Component.text("You", NamedTextColor.GRAY, TextDecoration.ITALIC); 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("source", you),
Placeholder.component("target", targetDisplayName), Placeholder.component("target", targetDisplayName),
Placeholder.component("message", parsedMessage.toText())); 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("source", source.getDisplayName()),
Placeholder.component("target", you), Placeholder.component("target", you),
Placeholder.component("message", parsedMessage.toText())); 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("source", source.getDisplayName()),
Placeholder.component("target", targetDisplayName), Placeholder.component("target", targetDisplayName),
Placeholder.component("message", parsedMessage.toText())); Placeholder.component("message", parsedMessage.toText()));
@ -104,7 +104,7 @@ public class TellCommand {
var lp = RccServer.getInstance().luckPerms(); var lp = RccServer.getInstance().luckPerms();
var playerAdapter = lp.getPlayerAdapter(ServerPlayerEntity.class); 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("source", source.getDisplayName()),
Placeholder.component("target", targetDisplayName), Placeholder.component("target", targetDisplayName),
Placeholder.component("message", parsedMessage.toText())); Placeholder.component("message", parsedMessage.toText()));

View file

@ -25,7 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class AfkTracker { public class AfkTracker {
private static final int cycleDelay = 1; 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<UUID, PlayerActivityState> playerActivityStates = new ConcurrentHashMap<>(); private final ConcurrentHashMap<UUID, PlayerActivityState> playerActivityStates = new ConcurrentHashMap<>();
@ -57,7 +57,7 @@ public class AfkTracker {
var displayNameJson = Text.Serializer.toJson(player.getDisplayName()); var displayNameJson = Text.Serializer.toJson(player.getDisplayName());
var displayName = JSONComponentSerializer.json().deserialize(displayNameJson); 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.component("displayname", displayName),
Placeholder.unparsed("username", player.getGameProfile().getName()), Placeholder.unparsed("username", player.getGameProfile().getName()),
Placeholder.unparsed("uuid", player.getUuid().toString()) Placeholder.unparsed("uuid", player.getUuid().toString())
@ -72,7 +72,7 @@ public class AfkTracker {
var displayNameJson = Text.Serializer.toJson(player.getDisplayName()); var displayNameJson = Text.Serializer.toJson(player.getDisplayName());
var displayName = JSONComponentSerializer.json().deserialize(displayNameJson); 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.component("displayname", displayName),
Placeholder.unparsed("username", player.getGameProfile().getName()), Placeholder.unparsed("username", player.getGameProfile().getName()),
Placeholder.unparsed("uuid", player.getUuid().toString()) Placeholder.unparsed("uuid", player.getUuid().toString())
@ -85,7 +85,7 @@ public class AfkTracker {
} }
private static void loadAfkTag() { 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() { private AfkTracker() {

View file

@ -33,7 +33,7 @@ public class AutoRestart {
var miniMessage = MiniMessage.miniMessage(); var miniMessage = MiniMessage.miniMessage();
RccEvents.READY.register((server, luckPerms) -> { RccEvents.READY.register((server, luckPerms) -> {
if (RccServer.CONFIG.enableAutoRestart()) { if (RccServer.CONFIG.autoRestart.enableAutoRestart()) {
scheduleNextRestart(); scheduleNextRestart();
} }
}); });
@ -42,7 +42,7 @@ public class AutoRestart {
if (restartBar == null || !timeBar.getUuid().equals(restartBar.getUuid())) if (restartBar == null || !timeBar.getUuid().equals(restartBar.getUuid()))
return; return;
var notificationTimes = RccServer.CONFIG.restartNotifications(); var notificationTimes = RccServer.CONFIG.autoRestart.restartNotifications();
var remainingSeconds = restartBar.getRemainingSeconds(); var remainingSeconds = restartBar.getRemainingSeconds();
if (notificationTimes.contains(remainingSeconds)) { if (notificationTimes.contains(remainingSeconds)) {
@ -57,7 +57,7 @@ public class AutoRestart {
return; return;
final var text = Components.toText( final var text = Components.toText(
miniMessage.deserialize(RccServer.CONFIG.restartKickMessage()) miniMessage.deserialize(RccServer.CONFIG.autoRestart.restartKickMessage())
); );
server.getPlayerManager().getPlayerList().forEach(player -> { server.getPlayerManager().getPlayerList().forEach(player -> {
player.networkHandler.disconnect(text); player.networkHandler.disconnect(text);
@ -74,7 +74,7 @@ public class AutoRestart {
} }
private static void setup() { private static void setup() {
var soundName = RccServer.CONFIG.restartSound(); var soundName = RccServer.CONFIG.autoRestart.restartSound();
try { try {
notificationKey = Key.key(soundName); notificationKey = Key.key(soundName);
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
@ -115,10 +115,10 @@ public class AutoRestart {
private static void notifyRestart(MinecraftServer server, BossBarManager.TimeBar bar) { private static void notifyRestart(MinecraftServer server, BossBarManager.TimeBar bar) {
var rcc = RccServer.getInstance(); var rcc = RccServer.getInstance();
var audience = rcc.adventure().players(); 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()); 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); rcc.broadcastMessage(server, comp);
} }
@ -133,7 +133,7 @@ public class AutoRestart {
var barStartTime = delay - barTime; var barStartTime = delay - barTime;
currentSchedule = scheduler.schedule(() -> { currentSchedule = scheduler.schedule(() -> {
schedule(barTime, RccServer.CONFIG.restartBarLabel()); schedule(barTime, RccServer.CONFIG.autoRestart.restartBarLabel());
}, barStartTime, TimeUnit.SECONDS); }, barStartTime, TimeUnit.SECONDS);
RccServer.LOGGER.info("Restart scheduled for in {} seconds", delay); RccServer.LOGGER.info("Restart scheduled for in {} seconds", delay);
@ -142,7 +142,7 @@ public class AutoRestart {
@Nullable @Nullable
private static Long getNextDelay() { private static Long getNextDelay() {
var restartTimeStrings = RccServer.CONFIG.restartAt(); var restartTimeStrings = RccServer.CONFIG.autoRestart.restartAt();
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
LocalDateTime nextRunTime = null; LocalDateTime nextRunTime = null;
long shortestDelay = Long.MAX_VALUE; long shortestDelay = Long.MAX_VALUE;

View file

@ -23,7 +23,7 @@ public class HttpApiServer {
private static int currentPlayerCount = 0; private static int currentPlayerCount = 0;
public static void register() { public static void register() {
if (!RccServer.CONFIG.enableHttpApi()) if (!RccServer.CONFIG.httpApi.enableHttpApi())
return; return;
try { try {
@ -62,7 +62,7 @@ public class HttpApiServer {
} }
private HttpApiServer() throws IOException { 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("/tps", new TPSHandler());
server.createContext("/mspt", new MSPTHandler()); server.createContext("/mspt", new MSPTHandler());
server.createContext("/player", new PlayerCountHandler()); server.createContext("/player", new PlayerCountHandler());

View file

@ -10,23 +10,23 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
public class TabList { public class TabList {
public static void register() { public static void register() {
if (!RccServer.CONFIG.enableTabList()) if (!RccServer.CONFIG.customTabList.enableTabList())
return; return;
var minimessage = MiniMessage.miniMessage(); var minimessage = MiniMessage.miniMessage();
ServerTickEvents.END_SERVER_TICK.register(server -> { 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) { 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; var phase = (Math.sin((server.getTicks() * Math.PI * 2) / period) + 1) / 2d;
server.getPlayerManager().getPlayerList().forEach(player -> { server.getPlayerManager().getPlayerList().forEach(player -> {
var playerContext = PlaceholderContext.of(player); var playerContext = PlaceholderContext.of(player);
Component headerComponent = Component.empty(); Component headerComponent = Component.empty();
for (int i = 0; i < RccServer.CONFIG.tabHeader().size(); i++) { for (int i = 0; i < RccServer.CONFIG.customTabList.tabHeader().size(); i++) {
var line = RccServer.CONFIG.tabHeader().get(i); var line = RccServer.CONFIG.customTabList.tabHeader().get(i);
line = line.replace("{phase}", String.valueOf(phase)); line = line.replace("{phase}", String.valueOf(phase));
if (i > 0) { if (i > 0) {
headerComponent = headerComponent.appendNewline(); headerComponent = headerComponent.appendNewline();
@ -36,8 +36,8 @@ public class TabList {
} }
Component footerComponent = Component.empty(); Component footerComponent = Component.empty();
for (int i = 0; i < RccServer.CONFIG.tabFooter().size(); i++) { for (int i = 0; i < RccServer.CONFIG.customTabList.tabFooter().size(); i++) {
var line = RccServer.CONFIG.tabFooter().get(i); var line = RccServer.CONFIG.customTabList.tabFooter().get(i);
line = line.replace("{phase}", String.valueOf(phase)); line = line.replace("{phase}", String.valueOf(phase));
if (i > 0) { if (i > 0) {
footerComponent = footerComponent.appendNewline(); footerComponent = footerComponent.appendNewline();

View file

@ -37,7 +37,7 @@ public class TeleportTracker {
this.player = player; this.player = player;
this.target = target; this.target = target;
// Seconds in config per 20 ticks // Seconds in config per 20 ticks
this.remainingTicks = RccServer.CONFIG.teleportRequestTimeout() * 20; this.remainingTicks = RccServer.CONFIG.teleportRequests.teleportRequestTimeout() * 20;
} }
public void expire() { public void expire() {

View file

@ -29,7 +29,7 @@ public abstract class ServerPlayNetworkManagerMixin {
@Inject(method = "tick", at = @At("TAIL")) @Inject(method = "tick", at = @At("TAIL"))
private void rccServer$updatePlayerList(CallbackInfo ci) { 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)); var packet = new PlayerListS2CPacket(EnumSet.of(PlayerListS2CPacket.Action.UPDATE_DISPLAY_NAME, PlayerListS2CPacket.Action.UPDATE_LISTED), List.of(this.player));
this.server.getPlayerManager().sendToAll(packet); this.server.getPlayerManager().sendToAll(packet);
} }

View file

@ -20,10 +20,10 @@ public class ServerPlayerEntityMixin {
@Inject(method = "getPlayerListName", at = @At("HEAD"), cancellable = true) @Inject(method = "getPlayerListName", at = @At("HEAD"), cancellable = true)
private void rccServer$customizePlayerListName(CallbackInfoReturnable<Text> callback) { private void rccServer$customizePlayerListName(CallbackInfoReturnable<Text> callback) {
if (RccServer.CONFIG.enableTabList()) { if (RccServer.CONFIG.customTabList.enableTabList()) {
var player = (ServerPlayerEntity) (Object) this; var player = (ServerPlayerEntity) (Object) this;
var playerContext = PlaceholderContext.of(player); 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); callback.setReturnValue(text);
} }
} }