Add winner declaration message and endRound logic with proper message broadcasting and error handling

This commit is contained in:
akastijn 2026-06-15 23:17:47 +02:00
parent c8e85f186a
commit cf011ae72f
4 changed files with 27 additions and 5 deletions

View File

@ -12,6 +12,7 @@ import com.alttd.hunger_games.services.PlayerService;
import com.alttd.hunger_games.services.PlayerTeleporterService;
import com.alttd.hunger_games.services.Round;
import com.alttd.hunger_games.services.RoundService;
import com.alttd.hunger_games.services.StatService;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
@ -39,9 +40,9 @@ public final class Main extends JavaPlugin {
private void registerServices() {
round = Round.createSingletonInstance();
lootService = new LootService();
roundService = RoundService.createSingletonInstance(round, this, lootService);
playerTeleporterService = PlayerTeleporterService.createSingletonInstance();
statService = new StatService(this, round);
roundService = RoundService.createSingletonInstance(round, this, lootService, statService);
playerTeleporterService = PlayerTeleporterService.createSingletonInstance();
playerService = PlayerService.createSingletonInstance(round, roundService, playerTeleporterService);
}

View File

@ -81,6 +81,7 @@ public class Messages extends AbstractConfig {
public static String STARTED = "<green><bold>The Hunger Games has begun! Good luck!</bold></green>";
public static String BORDER_SHRINK = "<red>The border is shrinking to <size> blocks!</red>";
public static String CHEST_EMPTY = "<red>This chest is empty!</red>";
public static String WINNER = "<gold><player> has won the Hunger Games!</gold>";
@SuppressWarnings("unused")
private static void load() {
@ -88,6 +89,7 @@ public class Messages extends AbstractConfig {
STARTED = config.getString(prefix, "started", STARTED);
BORDER_SHRINK = config.getString(prefix, "border-shrink", BORDER_SHRINK);
CHEST_EMPTY = config.getString(prefix, "chest-empty", CHEST_EMPTY);
WINNER = config.getString(prefix, "winner", WINNER);
}
}

View File

@ -46,6 +46,11 @@ public class Round {
listeners.forEach(roundListener -> roundListener.stateChange(roundState));
}
public void endRound() {
roundState = ROUND_STATE.ENDED;
listeners.forEach(roundListener -> roundListener.stateChange(roundState));
}
private void nextStage() {
Optional<ROUND_STATE> optionalNextRoundState = roundState.next();
if (optionalNextRoundState.isEmpty()) {

View File

@ -1,11 +1,14 @@
package com.alttd.hunger_games.services;
import com.alttd.hunger_games.Main;
import com.alttd.hunger_games.config.Messages;
import com.alttd.hunger_games.data_objects.PLAYER_STATE;
import com.alttd.hunger_games.data_objects.ROUND_STATE;
import com.alttd.hunger_games.event_listeners.PlayerMovementListener;
import com.alttd.hunger_games.services.StatService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
@ -13,6 +16,7 @@ import org.bukkit.event.HandlerList;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
public class RoundService implements RoundListener {
private static RoundService instance = null;
@ -25,6 +29,8 @@ public class RoundService implements RoundListener {
private ROUND_STATE roundState;
private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>();
private final Round round;
public static RoundService createSingletonInstance(Round round, Main main, LootService lootService, StatService statService) {
if (instance != null) {
throw new IllegalStateException("RoundService is already initialized.");
@ -34,6 +40,7 @@ public class RoundService implements RoundListener {
}
private RoundService(Round round, Main main, LootService lootService, StatService statService) {
this.round = round;
this.roundState = round.register(this);
this.main = main;
this.lootService = lootService;
@ -66,10 +73,17 @@ public class RoundService implements RoundListener {
private void declareWinner(UUID winnerUUID) {
Player player = Bukkit.getPlayer(winnerUUID);
if (player == null) {
log.error("Winner {} not found", winnerUUID);
Bukkit.broadcast(MiniMessage.miniMessage().deserialize("<red>Winner not found</red>"));
return;
}
statService.setPlacement(winnerUUID, 1);
statService.setWon(winnerUUID, true);
//TODO message
//TODO reset round
Bukkit.broadcast(MiniMessage.miniMessage().deserialize(Messages.GAME.WINNER, Placeholder.component("player", player.name())));
round.endRound();
}
protected void clear() {