diff --git a/src/main/java/xyz/twovb/sgm/commands/impl/GameCommand.java b/src/main/java/xyz/twovb/sgm/commands/impl/GameCommand.java index 15af353..d4658f7 100644 --- a/src/main/java/xyz/twovb/sgm/commands/impl/GameCommand.java +++ b/src/main/java/xyz/twovb/sgm/commands/impl/GameCommand.java @@ -1,10 +1,14 @@ package xyz.twovb.sgm.commands.impl; + +/* + * Created by 2vb - 5/7/2024 + */ + /* * Created by 2vb - 2/7/2024 */ -import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; import dev.rollczi.litecommands.annotations.argument.Arg; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; @@ -12,11 +16,8 @@ import dev.rollczi.litecommands.annotations.execute.Execute; import dev.rollczi.litecommands.annotations.permission.Permission; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.event.inventory.InventoryClickEvent; import xyz.twovb.sgm.SGM; import xyz.twovb.sgm.games.Minigame; -import xyz.twovb.sgm.games.impl.TestGame; -import xyz.twovb.sgm.games.impl.capturethebrick.CTB; import xyz.twovb.sgm.guis.InitGameGui; import xyz.twovb.toolbox.api.CustomPlayer; import xyz.twovb.toolbox.utils.ChatUtils; diff --git a/src/main/java/xyz/twovb/sgm/games/GameManager.java b/src/main/java/xyz/twovb/sgm/games/GameManager.java index e5643b8..f68771c 100644 --- a/src/main/java/xyz/twovb/sgm/games/GameManager.java +++ b/src/main/java/xyz/twovb/sgm/games/GameManager.java @@ -1,5 +1,10 @@ package xyz.twovb.sgm.games; + +/* + * Created by 2vb - 5/7/2024 + */ + /* * Created by 2vb - 4/6/2024 */ @@ -11,8 +16,8 @@ import xyz.twovb.sgm.SGM; import xyz.twovb.toolbox.managers.PlaceholderManager; import xyz.twovb.toolbox.utils.ChatUtils; -import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -25,19 +30,12 @@ public class GameManager { @Getter private final ArrayList registeredGames = new ArrayList(); + private final Map> gameMappings = new HashMap<>(); + public static Minigame findGame(UUID gameId) { return activeGames.get(gameId); } - public Minigame findGame(Player player) { - for (Minigame game : activeGames.values()) { - if (game.getPlayers().contains(player)) { - return game; - } - } - return null; - } - public static boolean isInGame(Player player) { for (Minigame game : activeGames.values()) { if (game.getPlayers().contains(player)) { @@ -47,16 +45,37 @@ public class GameManager { return false; } - public void initGame(Minigame game, CommandSender owner, String level) { + public Minigame findGame(Player player) { + for (Minigame game : activeGames.values()) { + if (game.getPlayers().contains(player)) { + return game; + } + } + return null; + } + + public void initGame(String internalName, CommandSender owner, String level) { try { + Minigame game = createGameInstance(internalName); game.init(owner, level); - } catch (IOException e) { + activeGames.put(game.getGameId(), game); + } catch (InstantiationException | IllegalAccessException | IOException | NoSuchMethodException | + InvocationTargetException e) { SGM.getInstance().getCLogger().error(e.getMessage()); owner.sendMessage(ChatUtils.translate(PlaceholderManager.setPlaceholders(SGM.getInstance().getMessages().getString("sgm.game.cant-start"), owner))); } - activeGames.put(game.getGameId(), game); } +// public void initGame(Minigame game, CommandSender owner, String level) { +// try { +// game.init(owner, level); +// } catch (IOException e) { +// SGM.getInstance().getCLogger().error(e.getMessage()); +// owner.sendMessage(ChatUtils.translate(PlaceholderManager.setPlaceholders(SGM.getInstance().getMessages().getString("sgm.game.cant-start"), owner))); +// } +// activeGames.put(game.getGameId(), game); +// } + public void addPlayerToGame(Player player, UUID gameId) { Minigame game = findGame(gameId); if (game.getState() == Minigame.GameState.READY && !game.getPlayers().contains(player) && !isInGame(player)) { @@ -69,17 +88,28 @@ public class GameManager { game.removePlayer(player); } + public Minigame createGameInstance(String internalName) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { + Class gameClass = gameMappings.get(internalName); + if (gameClass != null) { + return gameClass.getDeclaredConstructor().newInstance(); + } else { + throw new IllegalArgumentException("No game registered with internal name: " + internalName); + } + } + public void registerGame(Minigame game) { String intName = game.getName().toLowerCase(); try { SGM.getInstance().getConfigManager().loadConfig("games/" + intName + "/options.yml"); registeredGames.add(intName); + gameMappings.put(intName, game.getClass()); SGM.getInstance().getCLogger().log("Registered game " + game.getName()); - } catch(IllegalArgumentException error) { + } catch (IllegalArgumentException error) { String errMsg = error.getMessage(); if (errMsg.contains("The embedded resource") && errMsg.contains("cannot be found")) { SGM.getInstance().getCLogger().log(game.getName() + "'s game options are nowhere to be found. Please ensure this is intended behaviour."); registeredGames.add(intName); + gameMappings.put(intName, game.getClass()); } else { SGM.getInstance().getCLogger().error("Failed to register game " + game.getName() + ". Reason: " + errMsg); } diff --git a/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTB.java b/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTB.java index 3a0a2a5..0f7b701 100644 --- a/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTB.java +++ b/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTB.java @@ -1,5 +1,10 @@ package xyz.twovb.sgm.games.impl.capturethebrick; + +/* + * Created by 2vb - 5/7/2024 + */ + /* * Created by 2vb - 3/7/2024 */ @@ -152,7 +157,7 @@ public class CTB implements Minigame { for (Player player : players) { player.teleport(this.spawnLoc); } - for (int i = 0; i < bricks; i++) { + for (int i = 0; i <= bricks; i++) { for (Location brickLoc : redBrickSpawns) { placeBrick(brickLoc, "red"); } diff --git a/src/main/java/xyz/twovb/sgm/guis/MapGui.java b/src/main/java/xyz/twovb/sgm/guis/MapGui.java index 19705ad..ca58d86 100644 --- a/src/main/java/xyz/twovb/sgm/guis/MapGui.java +++ b/src/main/java/xyz/twovb/sgm/guis/MapGui.java @@ -1,5 +1,10 @@ package xyz.twovb.sgm.guis; + +/* + * Created by 2vb - 5/7/2024 + */ + import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; import com.github.stefvanschie.inventoryframework.pane.OutlinePane; @@ -49,8 +54,6 @@ public class MapGui { public void globalClick(InventoryClickEvent event) { event.setCancelled(true); -// event.getWhoClicked().sendMessage(this.game); -// gui.getInventory().clear(); } public void addMaps() { @@ -67,7 +70,9 @@ public class MapGui { // Handle click event if needed // Example: navigate to the map or show map details Player player = (Player) event.getWhoClicked(); - player.sendMessage("Clicked on map: " + mapName); + SGM.getInstance().getGameManager().initGame(this.game, player, mapName); + player.closeInventory(); +// player.sendMessage("Clicked on map: " + mapName); })); } } else { diff --git a/src/main/java/xyz/twovb/sgm/levels/LevelManager.java b/src/main/java/xyz/twovb/sgm/levels/LevelManager.java index 8ccaee1..f1340a1 100644 --- a/src/main/java/xyz/twovb/sgm/levels/LevelManager.java +++ b/src/main/java/xyz/twovb/sgm/levels/LevelManager.java @@ -1,5 +1,10 @@ package xyz.twovb.sgm.levels; + +/* + * Created by 2vb - 5/7/2024 + */ + /* * Created by 2vb - 4/6/2024 */ @@ -177,8 +182,6 @@ public class LevelManager { block.setType(Material.STONE); } } -// Block block = location.subtract(0, 2, 0).getBlock(); -// block.setType(Material.STONE); world.setSpawnLocation(location); world.setSpawnFlags(false, false); world.setGameRule(GameRule.KEEP_INVENTORY, true);