Add winner declaration message and endRound logic with proper message broadcasting and error handling
This commit is contained in:
parent
c8e85f186a
commit
cf011ae72f
|
|
@ -12,6 +12,7 @@ import com.alttd.hunger_games.services.PlayerService;
|
||||||
import com.alttd.hunger_games.services.PlayerTeleporterService;
|
import com.alttd.hunger_games.services.PlayerTeleporterService;
|
||||||
import com.alttd.hunger_games.services.Round;
|
import com.alttd.hunger_games.services.Round;
|
||||||
import com.alttd.hunger_games.services.RoundService;
|
import com.alttd.hunger_games.services.RoundService;
|
||||||
|
import com.alttd.hunger_games.services.StatService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
@ -39,9 +40,9 @@ public final class Main extends JavaPlugin {
|
||||||
private void registerServices() {
|
private void registerServices() {
|
||||||
round = Round.createSingletonInstance();
|
round = Round.createSingletonInstance();
|
||||||
lootService = new LootService();
|
lootService = new LootService();
|
||||||
roundService = RoundService.createSingletonInstance(round, this, lootService);
|
|
||||||
playerTeleporterService = PlayerTeleporterService.createSingletonInstance();
|
|
||||||
statService = new StatService(this, round);
|
statService = new StatService(this, round);
|
||||||
|
roundService = RoundService.createSingletonInstance(round, this, lootService, statService);
|
||||||
|
playerTeleporterService = PlayerTeleporterService.createSingletonInstance();
|
||||||
playerService = PlayerService.createSingletonInstance(round, roundService, playerTeleporterService);
|
playerService = PlayerService.createSingletonInstance(round, roundService, playerTeleporterService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 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 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 CHEST_EMPTY = "<red>This chest is empty!</red>";
|
||||||
|
public static String WINNER = "<gold><player> has won the Hunger Games!</gold>";
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static void load() {
|
private static void load() {
|
||||||
|
|
@ -88,6 +89,7 @@ public class Messages extends AbstractConfig {
|
||||||
STARTED = config.getString(prefix, "started", STARTED);
|
STARTED = config.getString(prefix, "started", STARTED);
|
||||||
BORDER_SHRINK = config.getString(prefix, "border-shrink", BORDER_SHRINK);
|
BORDER_SHRINK = config.getString(prefix, "border-shrink", BORDER_SHRINK);
|
||||||
CHEST_EMPTY = config.getString(prefix, "chest-empty", CHEST_EMPTY);
|
CHEST_EMPTY = config.getString(prefix, "chest-empty", CHEST_EMPTY);
|
||||||
|
WINNER = config.getString(prefix, "winner", WINNER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,11 @@ public class Round {
|
||||||
listeners.forEach(roundListener -> roundListener.stateChange(roundState));
|
listeners.forEach(roundListener -> roundListener.stateChange(roundState));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void endRound() {
|
||||||
|
roundState = ROUND_STATE.ENDED;
|
||||||
|
listeners.forEach(roundListener -> roundListener.stateChange(roundState));
|
||||||
|
}
|
||||||
|
|
||||||
private void nextStage() {
|
private void nextStage() {
|
||||||
Optional<ROUND_STATE> optionalNextRoundState = roundState.next();
|
Optional<ROUND_STATE> optionalNextRoundState = roundState.next();
|
||||||
if (optionalNextRoundState.isEmpty()) {
|
if (optionalNextRoundState.isEmpty()) {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,14 @@
|
||||||
package com.alttd.hunger_games.services;
|
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.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 com.alttd.hunger_games.services.StatService;
|
|
||||||
import lombok.Getter;
|
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.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
|
|
@ -13,6 +16,7 @@ import org.bukkit.event.HandlerList;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class RoundService implements RoundListener {
|
public class RoundService implements RoundListener {
|
||||||
|
|
||||||
private static RoundService instance = null;
|
private static RoundService instance = null;
|
||||||
|
|
@ -25,6 +29,8 @@ public class RoundService implements RoundListener {
|
||||||
private ROUND_STATE roundState;
|
private ROUND_STATE roundState;
|
||||||
private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>();
|
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) {
|
public static RoundService createSingletonInstance(Round round, Main main, LootService lootService, StatService statService) {
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
throw new IllegalStateException("RoundService is already initialized.");
|
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) {
|
private RoundService(Round round, Main main, LootService lootService, StatService statService) {
|
||||||
|
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;
|
||||||
|
|
@ -66,10 +73,17 @@ public class RoundService implements RoundListener {
|
||||||
|
|
||||||
private void declareWinner(UUID winnerUUID) {
|
private void declareWinner(UUID winnerUUID) {
|
||||||
Player player = Bukkit.getPlayer(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.setPlacement(winnerUUID, 1);
|
||||||
statService.setWon(winnerUUID, true);
|
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() {
|
protected void clear() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user