Enhance death handling and round logic: suppress death messages, clear player inventories, reset health and food levels, and teleport players to spectator area or prepare them for countdown phase. Integrate PlayerTeleporterService into RoundService.

This commit is contained in:
akastijn 2026-06-27 22:02:05 +02:00
parent 4bf6ca9dc4
commit ca7b6f1152
2 changed files with 24 additions and 6 deletions

View File

@ -42,7 +42,10 @@ public class PlayerDamageListener implements Listener {
@EventHandler @EventHandler
public void onPlayerDeath(PlayerDeathEvent event) { public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
event.setShowDeathMessages(false);
event.deathMessage(null); event.deathMessage(null);
event.getDrops().clear();
player.getInventory().clear();
playerService.handlePlayerDeath(player); playerService.handlePlayerDeath(player);
statService.addDeath(player.getUniqueId()); statService.addDeath(player.getUniqueId());

View File

@ -2,14 +2,17 @@ package com.alttd.hunger_games.services;
import com.alttd.hunger_games.Main; import com.alttd.hunger_games.Main;
import com.alttd.hunger_games.config.Messages; import com.alttd.hunger_games.config.Messages;
import com.alttd.hunger_games.data_objects.DESTINATION;
import com.alttd.hunger_games.data_objects.PLAYER_STATE; import com.alttd.hunger_games.data_objects.PLAYER_STATE;
import com.alttd.hunger_games.data_objects.ROUND_STATE; import com.alttd.hunger_games.data_objects.ROUND_STATE;
import com.alttd.hunger_games.event_listeners.PlayerMovementListener; import com.alttd.hunger_games.event_listeners.PlayerMovementListener;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -21,6 +24,7 @@ public class RoundService implements RoundListener {
private static RoundService instance = null; private static RoundService instance = null;
private PlayerMovementListener playerMovementListener; private PlayerMovementListener playerMovementListener;
private final PlayerTeleporterService playerTeleporterService;
private final Main main; private final Main main;
private final LootService lootService; private final LootService lootService;
private final StatService statService; private final StatService statService;
@ -30,22 +34,24 @@ public class RoundService implements RoundListener {
private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>(); private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>();
private final Round round; private final Round round;
@Setter
private BossBarService bossBarService; private BossBarService bossBarService;
public static RoundService createSingletonInstance(Round round, Main main, LootService lootService, StatService statService) { public static RoundService createSingletonInstance(Round round, Main main, LootService lootService, StatService statService, PlayerTeleporterService playerTeleporterService) {
if (instance != null) { if (instance != null) {
throw new IllegalStateException("RoundService is already initialized."); throw new IllegalStateException("RoundService is already initialized.");
} }
instance = new RoundService(round, main, lootService, statService); instance = new RoundService(round, main, lootService, statService, playerTeleporterService);
return instance; return instance;
} }
private RoundService(Round round, Main main, LootService lootService, StatService statService) { private RoundService(Round round, Main main, LootService lootService, StatService statService, PlayerTeleporterService playerTeleporterService) {
this.round = round; this.round = round;
this.roundState = round.register(this); this.roundState = round.register(this);
this.main = main; this.main = main;
this.lootService = lootService; this.lootService = lootService;
this.statService = statService; this.statService = statService;
this.playerTeleporterService = playerTeleporterService;
} }
public Set<UUID> getPlayers(PLAYER_STATE playerState) { public Set<UUID> getPlayers(PLAYER_STATE playerState) {
@ -87,6 +93,10 @@ public class RoundService implements RoundListener {
statService.setWon(winnerUUID, true); statService.setWon(winnerUUID, true);
Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.GAME.WINNER, Placeholder.component("player", player.name()))); Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.GAME.WINNER, Placeholder.component("player", player.name())));
playerTeleporterService.teleportPlayer(player, DESTINATION.SPECTATOR_AREA);
player.setHealth(20);
player.setFoodLevel(20);
player.getInventory().clear();
round.endRound(); round.endRound();
} }
@ -107,6 +117,14 @@ public class RoundService implements RoundListener {
lootService.clear(); lootService.clear();
} }
if (roundState.equals(ROUND_STATE.COUNTDOWN)) { if (roundState.equals(ROUND_STATE.COUNTDOWN)) {
Bukkit.getOnlinePlayers().stream()
.filter(player -> PLAYER_STATE.REGISTERED.equals(getPlayerState(player.getUniqueId()).orElse(null)))
.forEach(player -> {
player.setHealth(20);
player.setFoodLevel(20);
player.setGameMode(GameMode.SURVIVAL);
player.getInventory().clear();
});
startFreeze(); startFreeze();
} }
Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.GAME.NEXT_STATE, Placeholder.parsed("state", roundState.getHumandReadableName()))); Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.GAME.NEXT_STATE, Placeholder.parsed("state", roundState.getHumandReadableName())));
@ -127,7 +145,4 @@ public class RoundService implements RoundListener {
main.getServer().getPluginManager().registerEvents(playerMovementListener, main); main.getServer().getPluginManager().registerEvents(playerMovementListener, main);
} }
public void setBossBarService(BossBarService bossBarService) {
this.bossBarService = bossBarService;
}
} }