From dd30f842917ddc1ac3d907e7c2c4a051ad6bd2dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Thu, 22 Aug 2024 20:34:50 +0200 Subject: feat: toggleable molehunt timer --- src/main/java/world/anhgelus/molehunt/Game.java | 15 +++++--- .../java/world/anhgelus/molehunt/Molehunt.java | 43 +++++++++++++++++----- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index 68ac511..c2cb221 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -1,6 +1,5 @@ package world.anhgelus.molehunt; -import net.minecraft.entity.LivingEntity; import net.minecraft.network.packet.s2c.play.OverlayMessageS2CPacket; import net.minecraft.network.packet.s2c.play.SubtitleS2CPacket; import net.minecraft.network.packet.s2c.play.TitleFadeS2CPacket; @@ -71,7 +70,7 @@ public class Game { p.networkHandler.sendPacket(timing); if (moles.contains(p)) { p.networkHandler.sendPacket(new TitleS2CPacket(Text.of("The Mole!"))); - p.networkHandler.sendPacket(new SubtitleS2CPacket(Text.of("get the list of moles with /molehunt moles"))); + p.networkHandler.sendPacket(new SubtitleS2CPacket(Text.of("Get the list of moles with /molehunt moles"))); } else { p.networkHandler.sendPacket(new TitleS2CPacket(Text.of("Not the Mole!"))); } @@ -94,7 +93,13 @@ public class Game { public void run() { remaining--; playerManager.sendToAll(timing); - playerManager.sendToAll(new OverlayMessageS2CPacket(Text.of(getShortRemainingText()))); + + playerManager.getPlayerList().forEach(player -> { + if (Molehunt.timerVisibility.getOrDefault(player, true)) { + player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of(getShortRemainingText()))); + } + }); + if (remaining == 0) { end(); } @@ -127,9 +132,9 @@ public class Game { if (gameWonByMoles()) { winner = new TitleS2CPacket(Text.of("The Moles!")); } else { - winner = new TitleS2CPacket(Text.of("Not the Mole!")); + winner = new TitleS2CPacket(Text.of("The survivors!")); } - pm.sendToAll(new SubtitleS2CPacket(Text.of("Moles were " + getMolesAsString()))); + pm.sendToAll(new SubtitleS2CPacket(Text.of("The moles were " + getMolesAsString()))); pm.sendToAll(winner); pm.sendToAll(timing); } diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index 4c8c28b..b5bb92d 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -6,15 +6,18 @@ 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.message.v1.ServerMessageEvents; +import net.minecraft.network.packet.s2c.play.OverlayMessageS2CPacket; +import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.world.GameMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Objects; -import java.util.stream.Collectors; +import java.util.HashMap; +import static com.mojang.brigadier.arguments.BoolArgumentType.bool; +import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; @@ -24,6 +27,8 @@ public class Molehunt implements ModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); public Game game; + public static HashMap timerVisibility = new HashMap<>(); + @Override public void onInitialize() { LOGGER.info("Initializing Molehunt"); @@ -38,17 +43,37 @@ public class Molehunt implements ModInitializer { // context.getSource().sendFeedback(() -> game.getRemainingText(), false); // return Command.SINGLE_SUCCESS; // })); - command.then(literal("moles").requires(source -> { - if (game == null) { - return false; - } - return game.isAMole(source.getPlayer()); - }).executes(context -> { + command.then(literal("timer").requires(ServerCommandSource::isExecutedByPlayer).then( + literal("show").executes(context -> { + timerVisibility.put(context.getSource().getPlayer(), true); + context.getSource().sendFeedback(() -> Text.of("Showing molehunt timer"), false); + + var player = context.getSource().getPlayer(); + assert player != null; + + if (game == null || !game.isStarted()) { + player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of("§cGame has not started yet"))); + } else { + player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of(game.getShortRemainingText()))); + } + + return Command.SINGLE_SUCCESS; + }) + ).then( + literal("hide").executes(context -> { + timerVisibility.put(context.getSource().getPlayer(), false); + context.getSource().sendFeedback(() -> Text.of("Hiding molehunt timer"), false); + return Command.SINGLE_SUCCESS; + }) + )); + command.then(literal("moles").requires(source -> (game != null) && game.isStarted() && game.isAMole(source.getPlayer())).executes(context -> { context.getSource().sendFeedback(() -> Text.literal("List of moles: " + game.getMolesAsString()),false); return Command.SINGLE_SUCCESS; })); command.then(literal("stop").requires(source -> source.hasPermissionLevel(1)).executes(context -> { - game.stop(); + if (game != null) { + game.stop(); + } return Command.SINGLE_SUCCESS; })); -- cgit v1.2.3 From 2736ce66b5c6344422dfcdc613cd6cc0be55cc64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Thu, 22 Aug 2024 20:35:40 +0200 Subject: style: remove unnecessary imports --- src/main/java/world/anhgelus/molehunt/Molehunt.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index b5bb92d..7140512 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -16,8 +16,6 @@ import org.slf4j.LoggerFactory; import java.util.HashMap; -import static com.mojang.brigadier.arguments.BoolArgumentType.bool; -import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; -- cgit v1.2.3 From 64977f4392c79871ad252c6071d397c05c2f97ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Thu, 22 Aug 2024 20:44:23 +0200 Subject: fix: check if game has started before stopping --- src/main/java/world/anhgelus/molehunt/Molehunt.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index 7140512..cbb415d 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -69,9 +69,12 @@ public class Molehunt implements ModInitializer { return Command.SINGLE_SUCCESS; })); command.then(literal("stop").requires(source -> source.hasPermissionLevel(1)).executes(context -> { - if (game != null) { - game.stop(); + if (game == null || !game.isStarted()) { + context.getSource().sendError(Text.of("Game has not started yet")); } + + game.stop(); + return Command.SINGLE_SUCCESS; })); -- cgit v1.2.3 From 809bcecbdccf29a3c6a710590d22758431033ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Thu, 22 Aug 2024 20:45:38 +0200 Subject: refactor: change `isStarted` to `hasStarted --- src/main/java/world/anhgelus/molehunt/Game.java | 2 +- src/main/java/world/anhgelus/molehunt/Molehunt.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index c2cb221..9c3910b 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -174,7 +174,7 @@ public class Game { moles.add(newPlayer); } - public boolean isStarted() { + public boolean hasStarted() { return started; } } diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index cbb415d..ad1fedd 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -49,7 +49,7 @@ public class Molehunt implements ModInitializer { var player = context.getSource().getPlayer(); assert player != null; - if (game == null || !game.isStarted()) { + if (game == null || !game.hasStarted()) { player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of("§cGame has not started yet"))); } else { player.networkHandler.sendPacket(new OverlayMessageS2CPacket(Text.of(game.getShortRemainingText()))); @@ -64,12 +64,12 @@ public class Molehunt implements ModInitializer { return Command.SINGLE_SUCCESS; }) )); - command.then(literal("moles").requires(source -> (game != null) && game.isStarted() && game.isAMole(source.getPlayer())).executes(context -> { + command.then(literal("moles").requires(source -> (game != null) && game.hasStarted() && game.isAMole(source.getPlayer())).executes(context -> { context.getSource().sendFeedback(() -> Text.literal("List of moles: " + game.getMolesAsString()),false); return Command.SINGLE_SUCCESS; })); command.then(literal("stop").requires(source -> source.hasPermissionLevel(1)).executes(context -> { - if (game == null || !game.isStarted()) { + if (game == null || !game.hasStarted()) { context.getSource().sendError(Text.of("Game has not started yet")); } @@ -89,7 +89,7 @@ public class Molehunt implements ModInitializer { ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> { if (game == null) return; - if (!game.isStarted()) return; + if (!game.hasStarted()) return; if (game.getMoles().contains(oldPlayer)) game.updateMole(oldPlayer, newPlayer); newPlayer.changeGameMode(GameMode.SPECTATOR); }); -- cgit v1.2.3 From dc17a97ff6a6bab748d7bd3062481bb5dac6d85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Thu, 22 Aug 2024 20:47:57 +0200 Subject: feat: add error message when stopping unstarted game --- src/main/java/world/anhgelus/molehunt/Molehunt.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index ad1fedd..49e775d 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -71,6 +71,7 @@ public class Molehunt implements ModInitializer { command.then(literal("stop").requires(source -> source.hasPermissionLevel(1)).executes(context -> { if (game == null || !game.hasStarted()) { context.getSource().sendError(Text.of("Game has not started yet")); + return Command.SINGLE_SUCCESS; } game.stop(); -- cgit v1.2.3 From b1b716f8265b730dec82eafea33bfc1cfc33b0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Thu, 22 Aug 2024 23:13:25 +0200 Subject: fix: moles command suggestion --- src/main/java/world/anhgelus/molehunt/Molehunt.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index 49e775d..5273b21 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -64,7 +64,7 @@ public class Molehunt implements ModInitializer { return Command.SINGLE_SUCCESS; }) )); - command.then(literal("moles").requires(source -> (game != null) && game.hasStarted() && game.isAMole(source.getPlayer())).executes(context -> { + command.then(literal("moles").requires(source -> (game != null) && game.isAMole(source.getPlayer())).executes(context -> { context.getSource().sendFeedback(() -> Text.literal("List of moles: " + game.getMolesAsString()),false); return Command.SINGLE_SUCCESS; })); -- cgit v1.2.3 From b504d77be3eafe23dbbab6c69ca9f23aca6111fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Kosman?= Date: Thu, 22 Aug 2024 23:35:37 +0200 Subject: style: revert subtitle back to *The* moles --- src/main/java/world/anhgelus/molehunt/Game.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index bf0f294..b116b72 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -7,7 +7,6 @@ import net.minecraft.network.packet.s2c.play.TitleS2CPacket; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import net.minecraft.text.TextContent; import net.minecraft.world.GameMode; import net.minecraft.world.GameRules; import world.anhgelus.molehunt.utils.TimeUtils; @@ -136,7 +135,7 @@ public class Game { } else { winner = new TitleS2CPacket(Text.of("§aNot the Mole!")); } - pm.sendToAll(new SubtitleS2CPacket(Text.of("§6Moles were " + getMolesAsString()))); + pm.sendToAll(new SubtitleS2CPacket(Text.of("§6The Moles were " + getMolesAsString()))); pm.sendToAll(winner); pm.sendToAll(timing); } -- cgit v1.2.3