Gracefully handle null or offline players in game phases.

Added checks to ensure players are online and not null before interacting with them during the game phases. This prevents potential null pointer exceptions and improves overall code robustness. Also added a null check for the executorService in the CombatPhase end method.
This commit is contained in:
Teriuihi 2025-02-15 23:44:15 +01:00
parent c09c55ed7a
commit e04892156c
3 changed files with 14 additions and 5 deletions

View File

@ -32,6 +32,8 @@ public class CombatPhase implements GamePhaseExecutor {
@Override
public void end(GamePhase ignored) {
executorService.shutdown();
if (executorService != null) {
executorService.shutdown();
}
}
}

View File

@ -4,11 +4,11 @@ import com.alttd.ctf.flag.Flag;
import com.alttd.ctf.game.GameManager;
import com.alttd.ctf.game.GamePhase;
import com.alttd.ctf.game.GamePhaseExecutor;
import com.alttd.ctf.game_class.GameClass;
import com.github.yannicklamprecht.worldborder.api.WorldBorderApi;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@Slf4j
public class GatheringPhase implements GamePhaseExecutor {
@ -37,8 +37,12 @@ public class GatheringPhase implements GamePhaseExecutor {
return;
}
gameManager.getTeams().forEach(team -> {
team.getPlayers().forEach(player -> {
player.resetWorldBorder(Bukkit.getPlayer(player.getUuid()), worldBorderApi, nextPhase, flag.getFlagLocation());
team.getPlayers().forEach(teamPlayer -> {
Player player = Bukkit.getPlayer(teamPlayer.getUuid());
if (player == null || !player.isOnline()) {
return;
}
teamPlayer.resetWorldBorder(player, worldBorderApi, nextPhase, flag.getFlagLocation());
});
});
}

View File

@ -50,7 +50,10 @@ public class TeamPlayer {
resetWorldBorder(player, worldBorderApi, gamePhase, worldBorderCenter));
}
public void resetWorldBorder(Player player, WorldBorderApi worldBorderApi, GamePhase gamePhase, Location worldBorderCenter) {
public void resetWorldBorder(@NotNull Player player, WorldBorderApi worldBorderApi, GamePhase gamePhase, Location worldBorderCenter) {
if (!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");