Refactor respawn handling: streamline spectator teleportation, reset player experience, adjust finale border size, and improve game stage countdown logic.

This commit is contained in:
akastijn 2026-06-27 23:57:07 +02:00
parent 99bff5446d
commit 1a9f4cbd2d
5 changed files with 27 additions and 9 deletions

View File

@ -57,6 +57,6 @@ public class PlayerDamageListener implements Listener {
@EventHandler @EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) { public void onPlayerRespawn(PlayerRespawnEvent event) {
playerService.handlePlayerRespawn(event.getPlayer()); playerService.handlePlayerRespawn(event);
} }
} }

View File

@ -7,7 +7,9 @@ import com.alttd.hunger_games.data_objects.ROUND_STATE;
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.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerRespawnEvent;
import java.util.*; import java.util.*;
@ -36,6 +38,7 @@ public class PlayerService implements RoundListener {
@Override @Override
public void stateChange(ROUND_STATE roundState) { public void stateChange(ROUND_STATE roundState) {
this.roundState = roundState; this.roundState = roundState;
playerTeleporterService.reset();
switch (roundState) { switch (roundState) {
case PLAYER_REGISTRATION, KILL_PHASE, SAFE_PHASE -> { case PLAYER_REGISTRATION, KILL_PHASE, SAFE_PHASE -> {
//Nothing //Nothing
@ -113,10 +116,9 @@ public class PlayerService implements RoundListener {
Placeholder.unparsed("remaining", String.valueOf(remaining)))); Placeholder.unparsed("remaining", String.valueOf(remaining))));
} }
public void handlePlayerRespawn(Player player) { public void handlePlayerRespawn(PlayerRespawnEvent event) {
roundService.getPlayerState(player.getUniqueId()) Location spectatorArea = playerTeleporterService.getLocationFromDestination(DESTINATION.SPECTATOR_AREA);
.filter(state -> state == PLAYER_STATE.SPECTATING) event.setRespawnLocation(spectatorArea);
.ifPresent(state -> playerTeleporterService.teleportPlayer(player, DESTINATION.SPECTATOR_AREA));
} }
private Optional<PLAYER_STATE> determinePlayerState(UUID playerUuid) { private Optional<PLAYER_STATE> determinePlayerState(UUID playerUuid) {

View File

@ -35,7 +35,12 @@ public class PlayerTeleporterService implements RoundListener {
player.teleport(getLocationFromDestination(destination)); player.teleport(getLocationFromDestination(destination));
} }
private Location getLocationFromDestination(DESTINATION destination) { public void reset() {
placementCount.clear();
usedLocations.clear();
}
public Location getLocationFromDestination(DESTINATION destination) {
return switch (destination) { return switch (destination) {
case START_AREA -> { case START_AREA -> {
Location startCenter = Config.DESTINATION.START_CENTER; Location startCenter = Config.DESTINATION.START_CENTER;

View File

@ -2,6 +2,7 @@ package com.alttd.hunger_games.services;
import com.alttd.hunger_games.config.Config; import com.alttd.hunger_games.config.Config;
import com.alttd.hunger_games.data_objects.GameStage; import com.alttd.hunger_games.data_objects.GameStage;
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.game.GameStageHandler; import com.alttd.hunger_games.game.GameStageHandler;
import lombok.Setter; import lombok.Setter;
@ -99,6 +100,13 @@ public class Round {
final Instant endTime = Instant.now().plus(duration); final Instant endTime = Instant.now().plus(duration);
countdownFuture = scheduledExecutorService.scheduleAtFixedRate(() -> { countdownFuture = scheduledExecutorService.scheduleAtFixedRate(() -> {
if (roundState.equals(ROUND_STATE.FINALE)) {
if (countdownFuture != null) {
countdownFuture.cancel(false);
countdownFuture = null;
}
return;
}
Duration timeLeft = Duration.between(Instant.now(), endTime); Duration timeLeft = Duration.between(Instant.now(), endTime);
if (bossBarService != null) { if (bossBarService != null) {
bossBarService.updateCountdown(timeLeft, duration, roundState); bossBarService.updateCountdown(timeLeft, duration, roundState);

View File

@ -99,8 +99,8 @@ public class RoundService implements RoundListener {
player.setHealth(20); player.setHealth(20);
player.setFoodLevel(20); player.setFoodLevel(20);
player.getInventory().clear(); player.getInventory().clear();
player.sendExperienceChange(-player.getExp(), -player.getLevel()); player.setTotalExperience(0);
player.setExp(0); player.setExp(0.0f);
player.setLevel(0); player.setLevel(0);
round.endRound(); round.endRound();
} }
@ -129,12 +129,15 @@ public class RoundService implements RoundListener {
player.setFoodLevel(20); player.setFoodLevel(20);
player.setGameMode(GameMode.SURVIVAL); player.setGameMode(GameMode.SURVIVAL);
player.getInventory().clear(); player.getInventory().clear();
player.setTotalExperience(0);
player.setExp(0.0f);
player.setLevel(0);
}); });
startFreeze(); startFreeze();
WorldBorderUtil.resetBorder(); WorldBorderUtil.resetBorder();
} }
if (roundState.equals(ROUND_STATE.FINALE)) { if (roundState.equals(ROUND_STATE.FINALE)) {
WorldBorderUtil.setSize(100); WorldBorderUtil.setSize(75);
} }
if (!roundState.equals(ROUND_STATE.ENDED)) { if (!roundState.equals(ROUND_STATE.ENDED)) {
Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.GAME.NEXT_STATE, Placeholder.parsed("round", roundState.getHumanReadableName()))); Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.GAME.NEXT_STATE, Placeholder.parsed("round", roundState.getHumanReadableName())));