Refactor game class handling and fix GUI inventory usage.
Simplified game class retrieval by removing redundant data structures and combining logic. Updated GUI inventory to ensure tasks are run on the main thread using Bukkit's scheduler. Improved code organization by centralizing class selection logic in `TeamPlayer`.
This commit is contained in:
parent
a52efb9dbb
commit
33578027d3
|
|
@ -11,6 +11,8 @@ import com.alttd.ctf.events.SnowballEvent;
|
|||
import com.alttd.ctf.flag.Flag;
|
||||
import com.alttd.ctf.flag.FlagTryCaptureEvent;
|
||||
import com.alttd.ctf.game.GameManager;
|
||||
import com.alttd.ctf.gui.ClassSelectionGUI;
|
||||
import com.alttd.ctf.gui.GUIInventory;
|
||||
import com.alttd.ctf.gui.GUIListener;
|
||||
import com.alttd.ctf.json_config.JacksonConfig;
|
||||
import com.alttd.ctf.json_config.JsonConfigManager;
|
||||
|
|
@ -37,6 +39,8 @@ public class Main extends JavaPlugin {
|
|||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
GUIInventory.setMain(this); // sorry
|
||||
ClassSelectionGUI.setMain(this); // sorry
|
||||
Package pkg = Main.class.getPackage();
|
||||
String version = pkg.getImplementationVersion();
|
||||
log.info("Plugin enabled, version {}", version);
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@ 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.alttd.ctf.game_class.GameClassRetrieval;
|
||||
import com.alttd.ctf.gui.ClassSelectionGUI;
|
||||
import com.alttd.ctf.team.Team;
|
||||
import com.alttd.ctf.team.TeamColor;
|
||||
import com.alttd.ctf.team.TeamPlayer;
|
||||
|
|
@ -19,8 +17,6 @@ import org.bukkit.Bukkit;
|
|||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
|
|
@ -48,6 +44,7 @@ public class ClassSelectionPhase implements GamePhaseExecutor {
|
|||
CircularIterator<Team> teamCircularIterator = new CircularIterator<>(gameManager.getTeams());
|
||||
if (teamCircularIterator.hasNext()) {
|
||||
Bukkit.getOnlinePlayers().stream()
|
||||
.filter(player -> !player.hasPermission("ctf.bypass"))
|
||||
.filter(player -> gameManager.getTeamPlayer(player.getUniqueId()).isEmpty())
|
||||
.forEach(player -> {
|
||||
Team team = teamCircularIterator.next();
|
||||
|
|
@ -58,17 +55,6 @@ public class ClassSelectionPhase implements GamePhaseExecutor {
|
|||
log.warn("No teams to add players to");
|
||||
}
|
||||
teleportPlayersToStartingZone();
|
||||
HashMap<Integer, List<GameClass>> gameClassesForAllTeams = GameClassRetrieval.getGameClassesForAllTeams(gameManager);
|
||||
Bukkit.getOnlinePlayers().forEach(player -> {
|
||||
Optional<TeamPlayer> optionalTeamPlayer = gameManager.getTeamPlayer(player.getUniqueId());
|
||||
if (optionalTeamPlayer.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
TeamPlayer teamPlayer = optionalTeamPlayer.get();
|
||||
List<GameClass> gameClasses = gameClassesForAllTeams.get(teamPlayer.getTeam().getId());
|
||||
new ClassSelectionGUI(gameClasses, teamPlayer, worldBorderApi, GamePhase.CLASS_SELECTION)
|
||||
.open(player);
|
||||
});
|
||||
}
|
||||
|
||||
private void teleportPlayersToStartingZone() {
|
||||
|
|
|
|||
|
|
@ -15,16 +15,16 @@ public class GameClassRetrieval {
|
|||
public static HashMap<Integer, List<GameClass>> getGameClassesForAllTeams(GameManager gameManager) {
|
||||
final HashMap<Integer, List<GameClass>> gameClasses = new HashMap<>();
|
||||
gameManager.getTeams().forEach(team -> {
|
||||
gameClasses.putAll(getGameClassesForTeam(team));
|
||||
gameClasses.put(team.getId(), getGameClassesForTeam(team));
|
||||
});
|
||||
return gameClasses;
|
||||
}
|
||||
|
||||
public static HashMap<Integer, List<GameClass>> getGameClassesForTeam(Team team) {
|
||||
final HashMap<Integer, List<GameClass>> gameClasses = new HashMap<>();
|
||||
gameClasses.computeIfAbsent(team.getId(), teamId -> new ArrayList<>()).add(FighterCreator.createFighter(team.getColor()));
|
||||
gameClasses.computeIfAbsent(team.getId(), teamId -> new ArrayList<>()).add(TankCreator.createTank(team.getColor()));
|
||||
gameClasses.computeIfAbsent(team.getId(), teamId -> new ArrayList<>()).add(EngineerCreator.createEngineer(team.getColor()));
|
||||
public static List<GameClass> getGameClassesForTeam(Team team) {
|
||||
final List<GameClass> gameClasses = new ArrayList<>();
|
||||
gameClasses.add(FighterCreator.createFighter(team.getColor()));
|
||||
gameClasses.add(TankCreator.createTank(team.getColor()));
|
||||
gameClasses.add(EngineerCreator.createEngineer(team.getColor()));
|
||||
return gameClasses;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
package com.alttd.ctf.gui;
|
||||
|
||||
import com.alttd.ctf.Main;
|
||||
import com.alttd.ctf.game.GamePhase;
|
||||
import com.alttd.ctf.game_class.GameClass;
|
||||
import com.alttd.ctf.team.TeamPlayer;
|
||||
import com.github.yannicklamprecht.worldborder.api.WorldBorderApi;
|
||||
import lombok.Setter;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
|
@ -13,6 +16,9 @@ import java.util.List;
|
|||
|
||||
public class ClassSelectionGUI extends GUIInventory {
|
||||
|
||||
@Setter
|
||||
private static Main main;
|
||||
|
||||
public ClassSelectionGUI(@NotNull List<GameClass> gameClasses, @NotNull TeamPlayer teamPlayer,
|
||||
@NotNull WorldBorderApi worldBorderApi, @NotNull GamePhase gamePhase) {
|
||||
super(InventoryType.CHEST, teamPlayer.getTeam().getName().append(MiniMessage.miniMessage().deserialize(" - class selection")));
|
||||
|
|
@ -24,7 +30,7 @@ public class ClassSelectionGUI extends GUIInventory {
|
|||
for (GameClass gameClass : gameClasses) {
|
||||
setItem(pos++, gameClass.getDisplayItem(), player -> {
|
||||
gameClass.apply(teamPlayer, worldBorderApi, gamePhase, true);
|
||||
player.closeInventory(InventoryCloseEvent.Reason.PLUGIN);
|
||||
Bukkit.getScheduler().runTask(main, () -> player.closeInventory(InventoryCloseEvent.Reason.PLUGIN));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,22 @@
|
|||
package com.alttd.ctf.gui;
|
||||
|
||||
import com.alttd.ctf.Main;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Merchant;
|
||||
import org.bukkit.inventory.MerchantInventory;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class GUIInventory implements GUI {
|
||||
|
||||
@Setter
|
||||
private static Main main;
|
||||
|
||||
@Getter
|
||||
protected final Inventory inventory;
|
||||
protected final HashMap<Integer, GUIAction> guiActions;
|
||||
|
|
@ -35,7 +38,7 @@ public abstract class GUIInventory implements GUI {
|
|||
}
|
||||
|
||||
public void open(Player player) {
|
||||
player.openInventory(inventory);
|
||||
Bukkit.getScheduler().runTask(main, () -> player.openInventory(inventory));
|
||||
GUIByUUID.put(player.getUniqueId(), this);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package com.alttd.ctf.team;
|
|||
|
||||
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.github.yannicklamprecht.worldborder.api.WorldBorderApi;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
|
@ -10,8 +12,7 @@ import org.bukkit.Location;
|
|||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Getter
|
||||
|
|
@ -34,6 +35,10 @@ public class TeamPlayer {
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
#Sat Feb 08 21:02:07 CET 2025
|
||||
buildNumber=19
|
||||
#Sat Feb 08 21:33:29 CET 2025
|
||||
buildNumber=29
|
||||
version=0.1
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user