diff options
| author | anhgelus <anhgelus.morhtuuzh@proton.me> | 2024-08-22 14:07:43 +0000 |
|---|---|---|
| committer | anhgelus <anhgelus.morhtuuzh@proton.me> | 2024-08-22 14:07:43 +0000 |
| commit | d4f96c80e550b261534e0ddd202fe10d6a098e6b (patch) | |
| tree | 7f1253f95e9bb471b66fff756e957c30b70d0dee /src/main/java/world | |
| parent | ee4512e9fb41671b26940f2e994d6e49cdd304a9 (diff) | |
feat(game): showing winners on end
Diffstat (limited to 'src/main/java/world')
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Game.java | 54 | ||||
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Molehunt.java | 13 |
2 files changed, 50 insertions, 17 deletions
diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/Game.java index fb39199..585cbd5 100644 --- a/src/main/java/world/anhgelus/molehunt/Game.java +++ b/src/main/java/world/anhgelus/molehunt/Game.java @@ -14,16 +14,21 @@ import world.anhgelus.molehunt.utils.TimeUtils; import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
public class Game {
- final private Timer timer = new Timer();
- final public static int DEFAULT_TIME = 90*60; // 1:30
+ private Timer timer = new Timer();
+ public final static int DEFAULT_TIME = 90*60; // 1:30
private int remaining = DEFAULT_TIME;
- final private MinecraftServer server;
+ private final MinecraftServer server;
- final private List<ServerPlayerEntity> moles = new ArrayList<>();
+ private final List<ServerPlayerEntity> moles = new ArrayList<>();
+
+ private final TitleFadeS2CPacket timing = new TitleFadeS2CPacket(20, 40, 20);
+
+ private boolean started = false;
public Game(MinecraftServer server) {
this.server = server;
@@ -48,11 +53,10 @@ public class Game { // gamerules for the start
gamerules.get(GameRules.DO_IMMEDIATE_RESPAWN).set(true, server);
gamerules.get(GameRules.DO_ENTITY_DROPS).set(false, server);
- playerManager.getPlayerList().forEach(LivingEntity::kill);
final var title = new TitleS2CPacket(Text.of("You are..."));
- final var timing = new TitleFadeS2CPacket(20, 40, 20);
playerManager.getPlayerList().forEach(p -> {
+ p.kill();
p.networkHandler.sendPacket(timing);
p.networkHandler.sendPacket(title);
p.changeGameMode(GameMode.SURVIVAL);
@@ -83,6 +87,8 @@ public class Game { server.getOverworld().setTimeOfDay(0);
server.getOverworld().resetWeather();
+ started = true;
+
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
@@ -93,7 +99,7 @@ public class Game { end();
}
}
- }, 4*1000L, 1000L);
+ }, 4*1000, 1000);
}
}, 4*1000);
}
@@ -105,7 +111,29 @@ public class Game { public void end() {
timer.cancel();
- // affiche les gagnants
+ timer = new Timer();
+ started = false;
+ final var pm = server.getPlayerManager();
+ final var winnerSuspense = new TitleS2CPacket(Text.of("And the winners are..."));
+ pm.getPlayerList().forEach(p -> {
+ p.networkHandler.sendPacket(timing);
+ p.networkHandler.sendPacket(winnerSuspense);
+ p.changeGameMode(GameMode.CREATIVE);
+ });
+ timer.schedule(new TimerTask() {
+ @Override
+ public void run() {
+ TitleS2CPacket winner;
+ if (gameWonByMoles()) {
+ winner = new TitleS2CPacket(Text.of("The Moles!"));
+ } else {
+ winner = new TitleS2CPacket(Text.of("Not the Mole!"));
+ }
+ pm.sendToAll(new SubtitleS2CPacket(Text.of("Moles were " + getMolesAsString())));
+ pm.sendToAll(winner);
+ pm.sendToAll(timing);
+ }
+ }, 4*1000);
}
public int getRemaining() {
@@ -124,11 +152,19 @@ public class Game { return moles;
}
+ public String getMolesAsString() {
+ return moles.stream().map(ServerPlayerEntity::getDisplayName).filter(Objects::nonNull).map(Text::toString).collect(Collectors.joining(", "));
+ }
+
public boolean isAMole(ServerPlayerEntity player) {
return moles.contains(player);
}
- public boolean gameFinished() {
+ public boolean gameWonByMoles() {
return new HashSet<>(moles).containsAll(server.getPlayerManager().getPlayerList());
}
+
+ public boolean isStarted() {
+ return started;
+ }
}
diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index 83f5cbd..9132402 100644 --- a/src/main/java/world/anhgelus/molehunt/Molehunt.java +++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java @@ -1,13 +1,11 @@ package world.anhgelus.molehunt; import com.mojang.brigadier.Command; -import com.mojang.brigadier.arguments.StringArgumentType; import net.fabricmc.api.ModInitializer; 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.message.MessageType; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.world.GameMode; @@ -46,9 +44,7 @@ public class Molehunt implements ModInitializer { } return game.isAMole(source.getPlayer()); }).executes(context -> { - context.getSource().sendFeedback(() -> Text.literal( - "List of moles: " + game.getMoles().stream().map(ServerPlayerEntity::getDisplayName).filter(Objects::nonNull).map(Text::toString).collect(Collectors.joining(", "))), - false); + 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 -> { @@ -61,12 +57,13 @@ public class Molehunt implements ModInitializer { ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) -> false); ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> { - if (!(entity instanceof ServerPlayerEntity)) return; - if (game == null) return; - if (game.gameFinished()) game.end(); + if (!(entity instanceof ServerPlayerEntity) || game == null) return; + if (game.gameWonByMoles()) game.end(); }); ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> { + if (game == null) return; + if (!game.isStarted()) return; newPlayer.changeGameMode(GameMode.SPECTATOR); }); } |
