diff options
| author | William Hergès <anhgelus.morhtuuzh@proton.me> | 2024-08-22 23:39:15 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-22 23:39:15 +0200 |
| commit | 1973cd58deb902e6df6a152b38e6585a0bf61a5e (patch) | |
| tree | 38dbcee47e5860a8ff58a9ffd7046b6b27aaf7bd /src/main/java | |
| parent | 7dc2ddcbb808d9dba78f806b0b1138cc9a9c8bbb (diff) | |
| parent | b504d77be3eafe23dbbab6c69ca9f23aca6111fe (diff) | |
Merge pull request #1 from leo-210/feat/hide-timer
Feat: toggleable timer
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Game.java | 13 | ||||
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Molehunt.java | 45 |
2 files changed, 45 insertions, 13 deletions
diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index a32bd23..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;
@@ -95,7 +94,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();
}
@@ -130,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);
}
@@ -174,7 +179,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 4c8c28b..5273b21 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -6,14 +6,15 @@ 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 net.minecraft.server.command.CommandManager.literal; @@ -24,6 +25,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 +41,41 @@ 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.hasStarted()) { + 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.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.hasStarted()) { + context.getSource().sendError(Text.of("Game has not started yet")); + return Command.SINGLE_SUCCESS; + } + game.stop(); + return Command.SINGLE_SUCCESS; })); @@ -63,7 +90,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); }); |
