diff options
Diffstat (limited to 'src')
4 files changed, 122 insertions, 37 deletions
diff --git a/src/client/resources/assets/molehunt/lang/en_us.json b/src/client/resources/assets/molehunt/lang/en_us.json index 63ac06d..33aef84 100644 --- a/src/client/resources/assets/molehunt/lang/en_us.json +++ b/src/client/resources/assets/molehunt/lang/en_us.json @@ -11,5 +11,11 @@ "molehunt.game.start.suspense": "§eYou are...", "molehunt.game.start.mole.title": "§cThe Mole!", "molehunt.game.start.mole.subtitle": "§eGet the list of moles with §6/molehunt moles", - "molehunt.game.start.survivor": "§aNot the Mole!" + "molehunt.game.start.survivor": "§aNot the Mole!", + "gamerule.gameDuration": "Duration of a game", + "gamerule.molePercentage": "Percentage of mole", + "gamerule.moleCount": "Number of mole", + "gamerule.showNametags": "Show players' nametag", + "gamerule.showTab": "Enable the tab", + "gamerule.showSkins": "Show players' skin" }
\ No newline at end of file diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index a50598e..2f94a8b 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -18,8 +18,8 @@ import java.util.stream.Collectors; public class Game {
private Timer timer = new Timer();
- public final static int DEFAULT_TIME = Molehunt.CONFIG.gameDuration;
- private int remaining = DEFAULT_TIME;
+ public final int defaultTime = Molehunt.CONFIG.getGameDuration();
+ private int remaining = defaultTime;
private final MinecraftServer server;
@@ -34,12 +34,9 @@ public class Game { }
public void start() {
- final int n;
- if (Molehunt.CONFIG.moleCount < 0) {
- n = Math.floorDiv(server.getCurrentPlayerCount(), Math.floorDiv(100, (int) Molehunt.CONFIG.molePercentage));
- } else {
- n = Molehunt.CONFIG.moleCount;
- }
+ final int n = Molehunt.CONFIG.getMoleCount() < 0
+ ? Math.floorDiv(server.getCurrentPlayerCount(), Math.floorDiv(100, Molehunt.CONFIG.getMolePercentage()))
+ : Molehunt.CONFIG.getMoleCount();
final var playerManager = server.getPlayerManager();
diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index 4a728a0..0ec3c3b 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -6,6 +6,9 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory; +import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry; import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; @@ -15,6 +18,7 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.*; import net.minecraft.world.GameMode; +import net.minecraft.world.GameRules; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import world.anhgelus.molehunt.config.Config; @@ -29,7 +33,41 @@ public class Molehunt implements ModInitializer { public static final String MOD_ID = "molehunt"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); - public static Config CONFIG = new Config(MOD_ID); + public static Config CONFIG; + + public static final GameRules.Key<GameRules.IntRule> GAME_DURATION = GameRuleRegistry.register( + "gameDuration", GameRules.Category.MISC, GameRuleFactory.createIntRule(90) + ); + public static final GameRules.Key<GameRules.IntRule> MOLE_PERCENTAGE = GameRuleRegistry.register( + "molePercentage", GameRules.Category.MISC, GameRuleFactory.createIntRule(25) + ); + public static final GameRules.Key<GameRules.IntRule> MOLE_COUNT = GameRuleRegistry.register( + "moleCount", GameRules.Category.MISC, GameRuleFactory.createIntRule(-1) + ); + public static final GameRules.Key<GameRules.BooleanRule> SHOW_NAMETAGS = GameRuleRegistry.register( + "showNametags", + GameRules.Category.MISC, + GameRuleFactory.createBooleanRule(false, (server, val) -> { + if (CONFIG == null) return; + CONFIG.sendConfigPayload(); + }) + ); + public static final GameRules.Key<GameRules.BooleanRule> SHOW_TAB = GameRuleRegistry.register( + "showTab" + , GameRules.Category.MISC, + GameRuleFactory.createBooleanRule(false, (server, val) -> { + if (CONFIG == null) return; + CONFIG.sendConfigPayload(); + }) + ); + public static final GameRules.Key<GameRules.BooleanRule> SHOW_SKINS = GameRuleRegistry.register( + "showSkins", + GameRules.Category.MISC, + GameRuleFactory.createBooleanRule(false, (server, val) -> { + if (CONFIG == null) return; + CONFIG.sendConfigPayload(); + }) + ); public Game game; @@ -54,7 +92,9 @@ public class Molehunt implements ModInitializer { assert player != null; if (game == null || !game.hasStarted()) { - player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.translatable("commands.molehunt.stop.failed").setStyle(Style.EMPTY.withColor(16733525)))); + player.networkHandler.sendPacket(new OverlayMessageS2CPacket( + Text.translatable("commands.molehunt.stop.failed").setStyle(Style.EMPTY.withColor(16733525)) + )); } else { player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of(game.getShortRemainingText()))); } @@ -81,17 +121,8 @@ public class Molehunt implements ModInitializer { return Command.SINGLE_SUCCESS; })); - command.then(literal("reload").requires(source -> source.hasPermissionLevel(1)).executes(context -> { - if (game != null && game.hasStarted()) { - game.end(); - game = null; - } - CONFIG = new Config(MOD_ID); - context.getSource().getServer().getPlayerManager().getPlayerList().forEach(p -> { - ServerPlayNetworking.send(p, new ConfigPayload(CONFIG.showNametags, CONFIG.showSkins, CONFIG.showTab)); - }); - return Command.SINGLE_SUCCESS; - })); + + ServerLifecycleEvents.SERVER_STARTED.register(server -> CONFIG = new Config(MOD_ID, server)); CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(command)); @@ -110,7 +141,10 @@ public class Molehunt implements ModInitializer { }); ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { - ServerPlayNetworking.send(handler.player, new ConfigPayload(CONFIG.showNametags, CONFIG.showSkins, CONFIG.showTab)); + 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 9edfc1b..03ca86e 100644 --- a/src/main/java/world/anhgelus/molehunt/config/Config.java +++ b/src/main/java/world/anhgelus/molehunt/config/Config.java @@ -1,24 +1,72 @@ package world.anhgelus.molehunt.config; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.server.MinecraftServer; +import world.anhgelus.molehunt.Molehunt; + public class Config { - public final int gameDuration; - public final double molePercentage; - public final int moleCount; - public final boolean showNametags; - public final boolean showTab; - public final boolean showSkins; - - public Config(String fileName) { + + private final MinecraftServer server; + + public Config(String fileName, MinecraftServer server) { final SimpleConfig CONFIG = SimpleConfig.of(fileName).provider(Config::defaultConfig).request(); + this.server = server; + + final var rules = server.getGameRules(); + // In seconds - gameDuration = CONFIG.getOrDefault("game_duration", 90) * 60; - molePercentage = CONFIG.getOrDefault("mole_percentage", 25); - moleCount = CONFIG.getOrDefault("mole_count", -1); + 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 - showNametags = CONFIG.getOrDefault("show_nametags", false); - showSkins = CONFIG.getOrDefault("show_skins", false); - showTab = CONFIG.getOrDefault("show_tab", false); + 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); + } + + public void sendConfigPayload() { + final var payload = new ConfigPayload(areNametagsEnabled(), areSkinsEnabled(), isTabEnabled()); + server.getPlayerManager().getPlayerList().forEach(p -> { + ServerPlayNetworking.send(p, payload); + }); + } + + public void sendConfigPayload(boolean showNametags, boolean showSkins, boolean showTab) { + final var payload = new ConfigPayload(showNametags, showSkins, showTab); + server.getPlayerManager().getPlayerList().forEach(p -> ServerPlayNetworking.send(p, payload)); + } + + public int getGameDuration() { + return server.getGameRules().getInt(Molehunt.GAME_DURATION); + } + + public int getMolePercentage() { + return server.getGameRules().getInt(Molehunt.MOLE_PERCENTAGE); + } + + public int getMoleCount() { + return server.getGameRules().getInt(Molehunt.MOLE_COUNT); + } + + public boolean areNametagsEnabled() { + return server.getGameRules().getBoolean(Molehunt.SHOW_NAMETAGS); + } + + public boolean areSkinsEnabled() { + return server.getGameRules().getBoolean(Molehunt.SHOW_SKINS); + } + + public boolean isTabEnabled() { + return server.getGameRules().getBoolean(Molehunt.SHOW_TAB); } private static String defaultConfig(String s) { |
