Refactor world border handling: centralize logic in WorldBorderUtil and integrate into RoundService and GameStageHandler. Add logging for player teleportation and improve player reset process.
This commit is contained in:
parent
a3085ad4b4
commit
897c8ab14d
|
|
@ -1,27 +1,17 @@
|
|||
package com.alttd.hunger_games.game;
|
||||
|
||||
import com.alttd.hunger_games.config.Config;
|
||||
import com.alttd.hunger_games.config.Messages;
|
||||
import com.alttd.hunger_games.utils.WorldBorderUtil;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.WorldBorder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@UtilityClass
|
||||
public class GameStageHandler {
|
||||
|
||||
private static final int BORDER_TRANSITION_SECONDS = 10;
|
||||
|
||||
public static void handleStageChange(int worldBorderSize) {
|
||||
World world = getWorld().orElseThrow(() -> new IllegalStateException("No worlds available during state change,"));
|
||||
|
||||
WorldBorder border = world.getWorldBorder();
|
||||
border.setSize(worldBorderSize, BORDER_TRANSITION_SECONDS);
|
||||
WorldBorderUtil.setSize(worldBorderSize);
|
||||
|
||||
String message = Messages.GAME.BORDER_SHRINK
|
||||
.replace("<size>", String.valueOf(worldBorderSize));
|
||||
|
|
@ -33,26 +23,9 @@ public class GameStageHandler {
|
|||
}
|
||||
|
||||
public static void handleWarmup() {
|
||||
Location center = Config.DESTINATION.START_CENTER;
|
||||
if (center == null || center.getWorld() == null) {
|
||||
throw new IllegalStateException("Start center is not set during warmup.");
|
||||
}
|
||||
|
||||
WorldBorder border = center.getWorld().getWorldBorder();
|
||||
border.setCenter(center.getX(), center.getZ());
|
||||
border.setSize(Config.ROUND.INITIAL_BORDER_SIZE);
|
||||
|
||||
broadcast(Messages.GAME.WARMUP);
|
||||
}
|
||||
|
||||
private static Optional<World> getWorld() {
|
||||
Location center = Config.DESTINATION.START_CENTER;
|
||||
if (center != null && center.getWorld() != null) {
|
||||
return Optional.ofNullable(center.getWorld());
|
||||
}
|
||||
return Bukkit.getWorlds().isEmpty() ? Optional.empty() : Optional.of(Bukkit.getWorlds().getFirst());
|
||||
}
|
||||
|
||||
private static void broadcast(String message) {
|
||||
Component component = MiniMessage.miniMessage().deserialize(message);
|
||||
Bukkit.broadcast(component);
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import com.alttd.hunger_games.config.Config;
|
|||
import com.alttd.hunger_games.data_objects.DESTINATION;
|
||||
import com.alttd.hunger_games.data_objects.ROUND_STATE;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
|
@ -12,6 +13,7 @@ import java.util.HashSet;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
@NoArgsConstructor
|
||||
public class PlayerTeleporterService implements RoundListener {
|
||||
|
||||
|
|
@ -29,6 +31,7 @@ public class PlayerTeleporterService implements RoundListener {
|
|||
}
|
||||
|
||||
public void teleportPlayer(Player player, DESTINATION destination) {
|
||||
log.info("Teleporting {} to {}}", player.getName(), destination.name());
|
||||
player.teleport(getLocationFromDestination(destination));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ 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.event_listeners.PlayerMovementListener;
|
||||
import com.alttd.hunger_games.utils.WorldBorderUtil;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
|
@ -24,11 +25,11 @@ import java.util.stream.Collectors;
|
|||
public class RoundService implements RoundListener {
|
||||
|
||||
private static RoundService instance = null;
|
||||
private PlayerMovementListener playerMovementListener;
|
||||
private final PlayerTeleporterService playerTeleporterService;
|
||||
private final Main main;
|
||||
private final LootService lootService;
|
||||
private final StatService statService;
|
||||
private PlayerMovementListener playerMovementListener;
|
||||
|
||||
@Getter
|
||||
private ROUND_STATE roundState;
|
||||
|
|
@ -98,6 +99,9 @@ public class RoundService implements RoundListener {
|
|||
player.setHealth(20);
|
||||
player.setFoodLevel(20);
|
||||
player.getInventory().clear();
|
||||
player.sendExperienceChange(-player.getExp(), -player.getLevel());
|
||||
player.setExp(0);
|
||||
player.setLevel(0);
|
||||
round.endRound();
|
||||
}
|
||||
|
||||
|
|
@ -127,12 +131,14 @@ public class RoundService implements RoundListener {
|
|||
player.getInventory().clear();
|
||||
});
|
||||
startFreeze();
|
||||
WorldBorderUtil.resetBorder();
|
||||
}
|
||||
if (roundState.equals(ROUND_STATE.ENDED)) {
|
||||
//Don't announce the game ended since we already declare a winner
|
||||
return;
|
||||
if (roundState.equals(ROUND_STATE.FINALE)) {
|
||||
WorldBorderUtil.setSize(100);
|
||||
}
|
||||
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())));
|
||||
}
|
||||
|
||||
private void stopFreeze() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
package com.alttd.hunger_games.utils;
|
||||
|
||||
import com.alttd.hunger_games.config.Config;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.WorldBorder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@UtilityClass
|
||||
public class WorldBorderUtil {
|
||||
|
||||
private static final int BORDER_TRANSITION_SECONDS = 10;
|
||||
|
||||
public static void setSize(int size) {
|
||||
setSize(size, BORDER_TRANSITION_SECONDS);
|
||||
}
|
||||
|
||||
public static void setSize(int size, int seconds) {
|
||||
getWorld().ifPresent(world -> {
|
||||
WorldBorder border = world.getWorldBorder();
|
||||
border.setSize(size, seconds);
|
||||
});
|
||||
}
|
||||
|
||||
public static void resetBorder() {
|
||||
Location center = Config.DESTINATION.START_CENTER;
|
||||
if (center == null || center.getWorld() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
WorldBorder border = center.getWorld().getWorldBorder();
|
||||
border.setCenter(center.getX(), center.getZ());
|
||||
border.setSize(Config.ROUND.INITIAL_BORDER_SIZE);
|
||||
}
|
||||
|
||||
private static Optional<World> getWorld() {
|
||||
Location center = Config.DESTINATION.START_CENTER;
|
||||
if (center != null && center.getWorld() != null) {
|
||||
return Optional.ofNullable(center.getWorld());
|
||||
}
|
||||
return Bukkit.getWorlds().isEmpty() ? Optional.empty() : Optional.of(Bukkit.getWorlds().getFirst());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user