diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-03-17 17:30:22 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-03-17 18:06:18 +0100 |
| commit | 007aa2b47484b0ea2f653ee5fced4f081df918dc (patch) | |
| tree | 5efe2d3fc94249bf0e779e17e7c3d0fecaa77b3d | |
| parent | 8df7794e7413165f972adcaa3516d449265d8aa3 (diff) | |
fix(command): can start game is already launched
| -rw-r--r-- | src/client/resources/assets/molehunt/lang/en_us.json | 1 | ||||
| -rw-r--r-- | src/client/resources/assets/molehunt/lang/fr_fr.json | 1 | ||||
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Molehunt.java | 41 |
3 files changed, 22 insertions, 21 deletions
diff --git a/src/client/resources/assets/molehunt/lang/en_us.json b/src/client/resources/assets/molehunt/lang/en_us.json index 267230a..9c10680 100644 --- a/src/client/resources/assets/molehunt/lang/en_us.json +++ b/src/client/resources/assets/molehunt/lang/en_us.json @@ -1,5 +1,6 @@ { "commands.molehunt.error.game_not_started": "The Molehunt game has not been started yet.", + "commands.molehunt.error.game_already_started": "The Molehunt game has already been started yet.", "commands.molehunt.timer.show": "Showing Molehunt timer.", "commands.molehunt.timer.hide": "Hiding Molehunt timer.", "commands.molehunt.role.mole": "§cYou are a Mole.\nKill all the survivors before the timer runs out.", diff --git a/src/client/resources/assets/molehunt/lang/fr_fr.json b/src/client/resources/assets/molehunt/lang/fr_fr.json index 62c10ca..c481d76 100644 --- a/src/client/resources/assets/molehunt/lang/fr_fr.json +++ b/src/client/resources/assets/molehunt/lang/fr_fr.json @@ -1,5 +1,6 @@ { "commands.molehunt.error.game_not_started": "La partie de Molehunt n'a pas encore commencé.", + "commands.molehunt.error.game_already_started": "La partie de Molehunt a déjà été lancée.", "commands.molehunt.timer.show": "Le timer est maintenant affiché.", "commands.molehunt.timer.hide": "Le timer est maintenant caché.", "commands.molehunt.role.mole": "§cVous êtes une taupe.\nTuez tous les survivants avant la fin de la partie..", diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index 966d6c5..12bd3b9 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -12,7 +12,6 @@ import net.fabricmc.fabric.api.gamerule.v1.GameRuleEvents; import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; @@ -115,6 +114,10 @@ public class Molehunt implements ModInitializer { command.then(literal("start") .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)) .executes(context -> { + if (game != null && game.started()) + throw (new SimpleCommandExceptionType( + Component.translatable("commands.molehunt.error.game_already_started") + )).create(); game = new Game(context.getSource().getServer()); game.start(); return Command.SINGLE_SUCCESS; @@ -150,9 +153,10 @@ public class Molehunt implements ModInitializer { command.then(literal("role") .requires(CommandSourceStack::isPlayer) .executes(context -> { - if (game == null || !game.started()) { - throw (new SimpleCommandExceptionType(Component.translatable("commands.molehunt.error.game_not_started"))).create(); - } + if (game == null || !game.started()) + throw (new SimpleCommandExceptionType( + Component.translatable("commands.molehunt.error.game_not_started") + )).create(); final var source = context.getSource(); final var player = source.getPlayer(); @@ -180,10 +184,11 @@ public class Molehunt implements ModInitializer { })); command.then(literal("stop") .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)) - .executes(context -> { - if (game == null || !game.started()) { - throw (new SimpleCommandExceptionType(Component.translatable("commands.molehunt.error.game_not_started"))).create(); - } + .executes(_ -> { + if (game == null || !game.started()) + throw (new SimpleCommandExceptionType( + Component.translatable("commands.molehunt.error.game_not_started") + )).create(); game.stop(); @@ -192,31 +197,25 @@ public class Molehunt implements ModInitializer { ServerLifecycleEvents.SERVER_STARTED.register(server -> CONFIG = new Config(server)); - CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(command)); + CommandRegistrationCallback.EVENT.register((dispatcher, _, _) -> dispatcher.register(command)); - ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) -> false); + ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((_, _, _) -> false); - ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> { + ServerLivingEntityEvents.AFTER_DEATH.register((entity, _) -> { if (!(entity instanceof ServerPlayer) || game == null) return; if (!game.started()) return; if (game.wonByMoles()) game.end(); }); - ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> { + ServerPlayerEvents.AFTER_RESPAWN.register((_, newPlayer, _) -> { if (game == null) return; if (!game.started()) return; newPlayer.setGameMode(GameType.SPECTATOR); }); - ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { - ServerPlayNetworking.send( - handler.player, - new ConfigPayload(CONFIG.nametagsEnabled(), CONFIG.skinsEnabled(), CONFIG.tabEnabled()) - ); - ServerPlayNetworking.send( - handler.player, - new GamePayload(game != null && game.started()) - ); + ServerPlayConnectionEvents.JOIN.register((_, sender, _) -> { + sender.sendPacket(new ConfigPayload(CONFIG.nametagsEnabled(), CONFIG.skinsEnabled(), CONFIG.tabEnabled())); + sender.sendPacket(new GamePayload(game != null && game.started())); }); PayloadTypeRegistry.clientboundPlay().register(ConfigPayload.ID, ConfigPayload.CODEC); |
