From a26e8d4b279a55af41c665a0a95fd733ebd76471 Mon Sep 17 00:00:00 2001 From: anhgelus Date: Fri, 23 Aug 2024 20:59:54 +0000 Subject: fix(game): gamerules reset after restart (or leave/reconnect in the world) --- .../java/world/anhgelus/molehunt/Molehunt.java | 33 +++++++++++++--------- .../world/anhgelus/molehunt/config/Config.java | 27 ++++-------------- 2 files changed, 26 insertions(+), 34 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index a07cb18..aba2ad4 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -23,6 +23,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import world.anhgelus.molehunt.config.Config; import world.anhgelus.molehunt.config.ConfigPayload; +import world.anhgelus.molehunt.config.SimpleConfig; import java.util.HashMap; @@ -35,19 +36,27 @@ public class Molehunt implements ModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); public static Config CONFIG; + public static final SimpleConfig CONFIG_FILE = Config.configFile(MOD_ID); + public static final GameRules.Key GAME_DURATION = GameRuleRegistry.register( - "gameDuration", GameRules.Category.MISC, GameRuleFactory.createIntRule(90) + "gameDuration", + GameRules.Category.MISC, + GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("game_duration", 90)) ); public static final GameRules.Key MOLE_PERCENTAGE = GameRuleRegistry.register( - "molePercentage", GameRules.Category.MISC, GameRuleFactory.createIntRule(25) + "molePercentage", + GameRules.Category.MISC, + GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("mole_percentage", 25)) ); public static final GameRules.Key MOLE_COUNT = GameRuleRegistry.register( - "moleCount", GameRules.Category.MISC, GameRuleFactory.createIntRule(-1) + "moleCount", + GameRules.Category.MISC, + GameRuleFactory.createIntRule(CONFIG_FILE.getOrDefault("mole_count", -1)) ); public static final GameRules.Key SHOW_NAMETAGS = GameRuleRegistry.register( "showNametags", GameRules.Category.MISC, - GameRuleFactory.createBooleanRule(false, (server, val) -> { + GameRuleFactory.createBooleanRule(CONFIG_FILE.getOrDefault("show_nametags", false), (server, val) -> { if (CONFIG == null) return; CONFIG.sendConfigPayload(); }) @@ -55,7 +64,7 @@ public class Molehunt implements ModInitializer { public static final GameRules.Key SHOW_TAB = GameRuleRegistry.register( "showTab" , GameRules.Category.MISC, - GameRuleFactory.createBooleanRule(false, (server, val) -> { + GameRuleFactory.createBooleanRule(CONFIG_FILE.getOrDefault("show_tab", false), (server, val) -> { if (CONFIG == null) return; CONFIG.sendConfigPayload(); }) @@ -63,7 +72,7 @@ public class Molehunt implements ModInitializer { public static final GameRules.Key SHOW_SKINS = GameRuleRegistry.register( "showSkins", GameRules.Category.MISC, - GameRuleFactory.createBooleanRule(false, (server, val) -> { + GameRuleFactory.createBooleanRule(CONFIG_FILE.getOrDefault("show_skins", false), (server, val) -> { if (CONFIG == null) return; CONFIG.sendConfigPayload(); }) @@ -127,7 +136,7 @@ public class Molehunt implements ModInitializer { return Command.SINGLE_SUCCESS; })); - ServerLifecycleEvents.SERVER_STARTED.register(server -> CONFIG = new Config(MOD_ID, server)); + ServerLifecycleEvents.SERVER_STARTED.register(server -> CONFIG = new Config(server)); CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(command)); @@ -145,12 +154,10 @@ public class Molehunt implements ModInitializer { newPlayer.changeGameMode(GameMode.SPECTATOR); }); - ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { - ServerPlayNetworking.send( - handler.player, - new ConfigPayload(CONFIG.areNametagsEnabled(), CONFIG.areSkinsEnabled(), CONFIG.isTabEnabled()) - ); - }); + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> ServerPlayNetworking.send( + handler.player, + new ConfigPayload(CONFIG.areNametagsEnabled(), CONFIG.areSkinsEnabled(), CONFIG.isTabEnabled()) + )); PayloadTypeRegistry.playS2C().register(ConfigPayload.ID, ConfigPayload.CODEC); } diff --git a/src/main/java/world/anhgelus/molehunt/config/Config.java b/src/main/java/world/anhgelus/molehunt/config/Config.java index 03ca86e..dbaebcf 100644 --- a/src/main/java/world/anhgelus/molehunt/config/Config.java +++ b/src/main/java/world/anhgelus/molehunt/config/Config.java @@ -8,29 +8,10 @@ public class Config { private final MinecraftServer server; - public Config(String fileName, MinecraftServer server) { - final SimpleConfig CONFIG = SimpleConfig.of(fileName).provider(Config::defaultConfig).request(); - + public Config(MinecraftServer server) { this.server = server; - final var rules = server.getGameRules(); - - // In seconds - final var gameDuration = CONFIG.getOrDefault("game_duration", 90) * 60; - rules.get(Molehunt.GAME_DURATION).set(gameDuration/60, server); - final var molePercentage = CONFIG.getOrDefault("mole_percentage", 25); - rules.get(Molehunt.MOLE_PERCENTAGE).set(molePercentage, server); - final var moleCount = CONFIG.getOrDefault("mole_count", -1); - rules.get(Molehunt.MOLE_COUNT).set(moleCount, server); - // bool - final var showNametags = CONFIG.getOrDefault("show_nametags", false); - rules.get(Molehunt.SHOW_NAMETAGS).set(showNametags, server); - final var showSkins = CONFIG.getOrDefault("show_skins", false); - rules.get(Molehunt.SHOW_SKINS).set(showSkins, server); - final var showTab = CONFIG.getOrDefault("show_tab", false); - rules.get(Molehunt.SHOW_TAB).set(showTab, server); - - sendConfigPayload(showNametags, showSkins, showTab); + sendConfigPayload(areNametagsEnabled(), areSkinsEnabled(), isTabEnabled()); } public void sendConfigPayload() { @@ -69,6 +50,10 @@ public class Config { return server.getGameRules().getBoolean(Molehunt.SHOW_TAB); } + public static SimpleConfig configFile(String fileName) { + return SimpleConfig.of(fileName).provider(Config::defaultConfig).request(); + } + private static String defaultConfig(String s) { return """ # Molehunt mod configuration file -- cgit v1.2.3