package xyz.twovb.sgm.games; /* * Created by 2vb - 5/7/2024 */ /* * Created by 2vb - 4/6/2024 */ import lombok.Getter; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import xyz.twovb.sgm.SGM; import xyz.twovb.toolbox.managers.PlaceholderManager; import xyz.twovb.toolbox.utils.ChatUtils; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.UUID; public class GameManager { @Getter private static final Map activeGames = new HashMap(); @Getter private final ArrayList registeredGames = new ArrayList(); private final Map> gameMappings = new HashMap<>(); public static Minigame findGame(UUID gameId) { return activeGames.get(gameId); } public static boolean isInGame(Player player) { for (Minigame game : activeGames.values()) { if (game.getPlayers().contains(player)) { return true; } } return false; } 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); 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))); } } // 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)) { game.addPlayer(player); } } public void removePlayerFromGame(Player player, UUID gameId) { Minigame game = findGame(gameId); 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) { 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); } } } }