diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Game.java | 10 | ||||
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Molehunt.java | 45 |
2 files changed, 44 insertions, 11 deletions
diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index a32bd23..bf0f294 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -95,7 +95,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();
}
@@ -174,7 +180,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..49e775d 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.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.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); }); |
