diff options
| author | Léo Kosman <leo.kosman@proton.me> | 2024-08-22 20:34:50 +0200 |
|---|---|---|
| committer | Léo Kosman <leo.kosman@proton.me> | 2024-08-22 20:34:50 +0200 |
| commit | dd30f842917ddc1ac3d907e7c2c4a051ad6bd2dc (patch) | |
| tree | 34398b56334393dee68237dfad85f68abd483209 | |
| parent | 5029f4b7ef38e55d981a03790083167c8fdb3914 (diff) | |
feat: toggleable molehunt timer
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Game.java | 15 | ||||
| -rw-r--r-- | src/main/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<ServerPlayerEntity, Boolean> 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; })); |
