Refactor class creation and integrate FighterCreator
Abstracted default class creation logic into dedicated FighterCreator class for better modularity. Updated GameManager and ClassSelectionPhase to utilize this new structure, enabling dynamic class creation based on team color. Adjusted TODO.md to mark completed changes.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package com.alttd.ctf.game;
|
||||
|
||||
import com.alttd.ctf.game.phases.ClassSelectionPhase;
|
||||
import com.alttd.ctf.game_class.GameClass;
|
||||
import com.alttd.ctf.game_class.creation.FighterCreator;
|
||||
import com.alttd.ctf.game_class.implementations.Fighter;
|
||||
import com.alttd.ctf.team.Team;
|
||||
import com.alttd.ctf.team.TeamColor;
|
||||
import com.alttd.ctf.team.TeamPlayer;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -24,8 +27,7 @@ public class GameManager {
|
||||
|
||||
public GameManager() {
|
||||
phases = new HashMap<>();
|
||||
//TODO initialize this somewhere else (maybe load it from a json file?)
|
||||
phases.put(GamePhase.CLASS_SELECTION, new ClassSelectionPhase(this, new Fighter(List.of(Material.LEATHER_CHESTPLATE), List.of(), new ItemStack(Material.STONE), 15, 3, 5)));
|
||||
phases.put(GamePhase.CLASS_SELECTION, new ClassSelectionPhase(this, (FighterCreator::createFighter)));
|
||||
}
|
||||
|
||||
public Optional<GamePhase> getGamePhase() {
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.alttd.ctf.game.GameManager;
|
||||
import com.alttd.ctf.game.GamePhaseExecutor;
|
||||
import com.alttd.ctf.game_class.GameClass;
|
||||
import com.alttd.ctf.team.Team;
|
||||
import com.alttd.ctf.team.TeamColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
@@ -14,11 +15,16 @@ import java.util.Optional;
|
||||
public class ClassSelectionPhase implements GamePhaseExecutor {
|
||||
|
||||
private final GameManager gameManager;
|
||||
private final GameClass defaultClass;
|
||||
private final DefaultClassCreator defaultClassCreator;
|
||||
|
||||
public ClassSelectionPhase(@NotNull GameManager gameManager, @NotNull GameClass defaultClass) {
|
||||
@FunctionalInterface
|
||||
public interface DefaultClassCreator {
|
||||
@NotNull GameClass apply(TeamColor teamColor);
|
||||
}
|
||||
|
||||
public ClassSelectionPhase(@NotNull GameManager gameManager, DefaultClassCreator defaultClassCreator) {
|
||||
this.gameManager = gameManager;
|
||||
this.defaultClass = defaultClass;
|
||||
this.defaultClassCreator = defaultClassCreator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,11 +50,14 @@ public class ClassSelectionPhase implements GamePhaseExecutor {
|
||||
|
||||
@Override
|
||||
public void end() {
|
||||
gameManager.getTeams().forEach(team -> team.getPlayers().forEach(player -> {
|
||||
if (player.getGameClass() != null) {
|
||||
return;
|
||||
}
|
||||
defaultClass.apply(player);
|
||||
}));
|
||||
gameManager.getTeams().forEach(team -> {
|
||||
GameClass defaultClass = defaultClassCreator.apply(team.getColor());
|
||||
team.getPlayers().forEach(player -> {
|
||||
if (player.getGameClass() != null) {
|
||||
return;
|
||||
}
|
||||
defaultClass.apply(player);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.alttd.ctf.game_class.creation;
|
||||
|
||||
import com.alttd.ctf.game_class.GameClass;
|
||||
import com.alttd.ctf.game_class.implementations.Fighter;
|
||||
import com.alttd.ctf.team.TeamColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FighterCreator {
|
||||
|
||||
private static final MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
|
||||
public static GameClass createFighter(TeamColor teamColor) {
|
||||
return new Fighter(getArmor(), getTools(), getDisplayItem(teamColor), 15, 3, 5);
|
||||
}
|
||||
|
||||
private static List<Material> getArmor() {
|
||||
return (List.of(Material.LEATHER_CHESTPLATE));
|
||||
}
|
||||
|
||||
private static List<ItemStack> getTools() {
|
||||
return (List.of());
|
||||
}
|
||||
|
||||
private static ItemStack getDisplayItem(TeamColor teamColor) {
|
||||
ItemStack itemStack = new ItemStack(Material.IRON_SWORD);
|
||||
ItemMeta itemMeta = itemStack.getItemMeta();
|
||||
itemMeta.displayName(miniMessage.deserialize(String.format("<color:%s>Fighter</color>", teamColor.hex())));
|
||||
itemMeta.lore(List.of(
|
||||
miniMessage.deserialize("<gold>The Fighter throws fast and does good damage</gold>"),
|
||||
miniMessage.deserialize("<gold>This comes at the cost of health</gold>")
|
||||
));
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
return itemStack;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user