Files
ctf/src/main/java/com/alttd/ctf/team/TeamPlayer.java
T
auto 14158f73b8 Handle null player check in resetWorldBorder method.
Previously, the method assumed the player object was never null, which could cause potential issues. This update adds a null check to ensure stability and prevent unexpected errors when invoking the resetWorldBorder method.
2025-02-26 22:40:58 +01:00

103 lines
3.3 KiB
Java

package com.alttd.ctf.team;
import com.alttd.ctf.config.GameConfig;
import com.alttd.ctf.config.WorldBorderSettings;
import com.alttd.ctf.config.WorldBorderType;
import com.alttd.ctf.game.GamePhase;
import com.alttd.ctf.game_class.GameClass;
import com.alttd.ctf.game_class.GameClassRetrieval;
import com.alttd.ctf.gui.ClassSelectionGUI;
import com.alttd.ctf.stats.PlayerStat;
import com.alttd.ctf.stats.Stat;
import com.github.yannicklamprecht.worldborder.api.WorldBorderApi;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@Slf4j
@Getter
public class TeamPlayer {
private final PlayerStat playerStat;
private final UUID uuid;
private final Team team;
@Setter
private GameClass gameClass;
private boolean isDead = false;
protected TeamPlayer(Player player, Team team, PlayerStat playerStat) {
this.uuid = player.getUniqueId();
this.team = team;
this.playerStat = playerStat;
}
public void respawn(@NotNull Player player, @NotNull WorldBorderApi worldBorderApi, @NotNull GamePhase gamePhase) {
respawn(player, worldBorderApi, gamePhase, true);
}
public void respawn(Player player, WorldBorderApi worldBorderApi, GamePhase gamePhase, boolean teleport) {
Location spawnLocation = team.getSpawnLocation();
Location worldBorderCenter = team.getWorldBorderCenter();
List<GameClass> gameClasses = GameClassRetrieval.getGameClassesForTeam(team);
new ClassSelectionGUI(gameClasses, this, worldBorderApi, GamePhase.CLASS_SELECTION).open(player);
if (!teleport) {
resetWorldBorder(player, worldBorderApi, gamePhase, worldBorderCenter);
return;
}
player.teleportAsync(spawnLocation).thenAcceptAsync(unused ->
resetWorldBorder(player, worldBorderApi, gamePhase, worldBorderCenter));
isDead = false;
}
public void setDead() {
isDead = true;
}
public void resetWorldBorder(Player player, WorldBorderApi worldBorderApi, GamePhase gamePhase, Location worldBorderCenter) {
if (player == null || !player.isOnline()) {
return;
}
WorldBorderSettings worldBorderSettings = GameConfig.WORLD_BORDER.getGAME_PHASE_WORLD_BORDER().get(gamePhase);
if (worldBorderSettings == null) {
throw new IllegalStateException("All phases need to have world border settings");
}
if (worldBorderSettings.type().equals(WorldBorderType.FLAG)) {
worldBorderApi.resetWorldBorderToGlobal(player);
return;
}
worldBorderApi.setBorder(player, worldBorderSettings.size(), worldBorderCenter);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
TeamPlayer other = (TeamPlayer) obj;
return Objects.equals(uuid, other.uuid);
}
@Override
public int hashCode() {
return Objects.hash(uuid);
}
public void increaseStat(Stat stat) {
playerStat.increaseStat(stat);
}
public void increaseStat(Stat stat, double amount) {
playerStat.increaseStat(stat, amount);
}
}