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:
Teriuihi 2025-01-24 21:03:19 +01:00
parent 5a87784d71
commit fcf5dc7b7c
4 changed files with 68 additions and 17 deletions

12
TODO.md
View File

@ -1,7 +1,7 @@
- [ ] Game manager
- [ ] Stores/creates/modifies teams
- [x] Stores/creates/modifies teams
- [ ] Allows for rollback of world through CoreProtect
- [ ] Creates teams (exclusively) (load from config?)
- [x] Creates teams (load from config?)
- [ ] Stores all players
- [ ] Allows starting, restarting, and ending game
- [ ] Limit playing area (x, y, and z) (could be done through WorldGuard)
@ -13,7 +13,7 @@
- [ ] Respawn player on team by calling respawn function
- [ ] Functions as starting point for team
- [ ] Check if player is in respawn point (for class changes
- [ ] Stores team colors (for armor)
- [x] Stores team colors (for armor)
- [ ] Allows creation of Team player object (exclusively)
- [ ] Manages respawns (config timer)
- [ ] Tracks flag
@ -23,14 +23,14 @@
- [ ] Flag location indicator (beacon beams, compass, particles)
- [ ] Snowball storage
- [ ] Team player
- [ ] Must be member of team
- [ ] Stores health
- [x] Must be member of team
- [x] Stores max health
- [ ] Stores individual score
- [ ] Class manager
- [ ] Stores list of all classes
- [ ] Allows Team member to select class
- [ ] Classes
- [ ] Fighter: lower health, higher dmg/throwing speed
- [x] Fighter: lower health, higher dmg/throwing speed
- [ ] Tank: Has shield, invincibility effect, slower (short + long cooldown)?
- [ ] Engineer: Better shovel, lower health, lower dmg, (can drop snowballs to team members?) store snow at base, can build?
- [ ] HARD Mage: Drops snowballs in area (casting cost)

View File

@ -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() {

View File

@ -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 -> {
gameManager.getTeams().forEach(team -> {
GameClass defaultClass = defaultClassCreator.apply(team.getColor());
team.getPlayers().forEach(player -> {
if (player.getGameClass() != null) {
return;
}
defaultClass.apply(player);
}));
});
});
}
}

View File

@ -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;
}
}