diff --git a/src/main/java/xyz/twovb/sgm/SGM.java b/src/main/java/xyz/twovb/sgm/SGM.java index 15bacd2..c07d7d6 100644 --- a/src/main/java/xyz/twovb/sgm/SGM.java +++ b/src/main/java/xyz/twovb/sgm/SGM.java @@ -48,6 +48,7 @@ public final class SGM extends JavaPlugin { configManager = new ConfigManager(this); gameManager = new GameManager(); levelManager = new LevelManager(); + this.saveDefaultConfig(); Messages = configManager.loadConfig("messages.yml"); BuildInfo = new YamlConfiguration(); try { 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 fb4560f..d9a9b60 100644 --- a/src/main/java/xyz/twovb/sgm/commands/impl/GameCommand.java +++ b/src/main/java/xyz/twovb/sgm/commands/impl/GameCommand.java @@ -14,6 +14,7 @@ import org.bukkit.entity.Player; 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.toolbox.api.CustomPlayer; import xyz.twovb.toolbox.utils.ChatUtils; @@ -41,7 +42,7 @@ public class GameCommand { @Permission("sgm.games.init") void init(@Context Player player, @Arg("name") String name) { CustomPlayer cPlayer = new CustomPlayer(player); - SGM.getInstance().getGameManager().initGame(new TestGame(), player, name); + SGM.getInstance().getGameManager().initGame(new CTB(), player, name); } @Execute(name = "start") diff --git a/src/main/java/xyz/twovb/sgm/commands/impl/SGMCommand.java b/src/main/java/xyz/twovb/sgm/commands/impl/SGMCommand.java index a2e2079..9ed6c49 100644 --- a/src/main/java/xyz/twovb/sgm/commands/impl/SGMCommand.java +++ b/src/main/java/xyz/twovb/sgm/commands/impl/SGMCommand.java @@ -6,6 +6,8 @@ import dev.rollczi.litecommands.annotations.execute.Execute; import org.bukkit.command.CommandSender; import xyz.twovb.sgm.SGM; +import xyz.twovb.sgm.games.Minigame; +import xyz.twovb.sgm.games.impl.capturethebrick.CTB; import xyz.twovb.toolbox.managers.PlaceholderManager; import xyz.twovb.toolbox.utils.ChatUtils; @@ -26,5 +28,6 @@ public class SGMCommand { } @Execute(name = "a") - void a(@Context CommandSender sender) {} + void a(@Context CommandSender sender) { + } } diff --git a/src/main/java/xyz/twovb/sgm/games/GameManager.java b/src/main/java/xyz/twovb/sgm/games/GameManager.java index ed2f551..e5643b8 100644 --- a/src/main/java/xyz/twovb/sgm/games/GameManager.java +++ b/src/main/java/xyz/twovb/sgm/games/GameManager.java @@ -78,7 +78,8 @@ public class GameManager { } 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. Skipping."); + SGM.getInstance().getCLogger().log(game.getName() + "'s game options are nowhere to be found. Please ensure this is intended behaviour."); + registeredGames.add(intName); } else { SGM.getInstance().getCLogger().error("Failed to register game " + game.getName() + ". Reason: " + errMsg); } diff --git a/src/main/java/xyz/twovb/sgm/games/Minigame.java b/src/main/java/xyz/twovb/sgm/games/Minigame.java index e8e3c61..3359636 100644 --- a/src/main/java/xyz/twovb/sgm/games/Minigame.java +++ b/src/main/java/xyz/twovb/sgm/games/Minigame.java @@ -43,8 +43,6 @@ public interface Minigame extends Listener { GameState getState(); - CommandSender getOwner(); - default void sendMessageToAllPlayers(String message) { for (Player player : getPlayers()) { CustomPlayer cPlayer = new CustomPlayer(player); @@ -59,17 +57,12 @@ public interface Minigame extends Listener { if (tempDir.mkdirs()) { FileUtils.copyDirectory(mapDir, tempDir); WorldCreator wc = new WorldCreator(LevelManager.gamePath + tempName, new NamespacedKey(SGM.getInstance(), tempName)); - World world = wc.createWorld(); - if (world == null) return null; - world.setAutoSave(false); - return world; + return wc.createWorld(); } else { return null; } } - - enum GameState { PRESTART, READY, STARTED, ENDING } diff --git a/src/main/java/xyz/twovb/sgm/games/impl/TestGame.java b/src/main/java/xyz/twovb/sgm/games/impl/TestGame.java index c1d96e4..b1f47bf 100644 --- a/src/main/java/xyz/twovb/sgm/games/impl/TestGame.java +++ b/src/main/java/xyz/twovb/sgm/games/impl/TestGame.java @@ -26,7 +26,6 @@ public class TestGame implements Minigame { private final UUID uuid; private final List players; private World gameWorld; - private CommandSender owner; private GameState state; public TestGame() { @@ -38,7 +37,6 @@ public class TestGame implements Minigame { public void init(CommandSender owner, String world) throws IOException { // Perform initialization logic here Bukkit.getPluginManager().registerEvents(this, SGM.getInstance()); - this.owner = owner; gameWorld = createGameWorld(world, uuid); state = GameState.READY; owner.sendMessage("A new " + name + " instance with ID " + uuid + " has been created and is now ready!"); @@ -119,11 +117,6 @@ public class TestGame implements Minigame { return state; } - @Override - public CommandSender getOwner() { - return owner; - } - @Override public void onTick() { if (state == GameState.STARTED && players.size() <= 1) { 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 ae90fbb..a92161b 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,10 +1,5 @@ package xyz.twovb.sgm.games.impl.capturethebrick; - -/* - * Created by 2vb - 3/7/2024 - */ - /* * Created by 2vb - 3/7/2024 */ @@ -13,6 +8,7 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -21,19 +17,22 @@ import org.bukkit.event.player.PlayerQuitEvent; import xyz.twovb.sgm.SGM; import xyz.twovb.sgm.games.GameManager; import xyz.twovb.sgm.games.Minigame; +import xyz.twovb.sgm.levels.LevelManager; +import xyz.twovb.toolbox.utils.ChatUtils; +import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.UUID; public class CTB implements Minigame { - final static String name = "CaptureTheBrick"; private final UUID uuid; private final List players; + private int bricks; private World gameWorld; - private CommandSender owner; private GameState state; public CTB() { @@ -45,12 +44,71 @@ public class CTB implements Minigame { public void init(CommandSender owner, String world) throws IOException { // Perform initialization logic here Bukkit.getPluginManager().registerEvents(this, SGM.getInstance()); - this.owner = owner; gameWorld = createGameWorld(world, uuid); - state = GameState.READY; - owner.sendMessage("A new " + name + " instance with ID " + uuid + " has been created and is now ready!"); + gameWorld.setAutoSave(false); + if (applyOptions()) { + state = GameState.READY; + owner.sendMessage("A new " + name + " instance with ID " + uuid + " has been created and is now ready!"); + } else { + owner.sendMessage(ChatUtils.translate(SGM.getInstance().getMessages().getString("sgm.game.cant-start"))); + } } + private boolean applyOptions() { + File configFile = new File(LevelManager.gamePath + name.toLowerCase() + "/options.yml"); + YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); + + if (!configFile.exists()) { + SGM.getInstance().getCLogger().log("options.yml does not exist at " + configFile.getAbsolutePath()); + return false; + } + + // Process teams section + List teamsToProcess = Arrays.asList("red", "blue"); + + for (String teamName : teamsToProcess) { + ConfigurationSection team = config.getConfigurationSection("teams." + teamName); + if (team != null) { + List brickSpawns = team.getStringList("brickSpawns"); + List playerSpawnArea = team.getStringList("playerSpawnArea"); + + // Log or process team-specific values + SGM.getInstance().getCLogger().log(teamName + " Player Spawn Area: " + playerSpawnArea); + + // Log each team's brick spawns + for (int i = 0; i < brickSpawns.size(); i++) { + SGM.getInstance().getCLogger().log(teamName + " Brick Spawn " + (i + 1) + ": " + brickSpawns.get(i)); + // Process each brick spawn here if needed + } + } + } + + // Process terrainInfo section + ConfigurationSection terrainInfoSection = config.getConfigurationSection("terrainInfo"); + if (terrainInfoSection != null) { + int territory = terrainInfoSection.getInt("territory"); + int boundary = terrainInfoSection.getInt("boundary"); + + // Log or process terrainInfo values + SGM.getInstance().getCLogger().log("Territory: " + territory); + SGM.getInstance().getCLogger().log("Boundary: " + boundary); + } + + // Additional configuration values outside of teams and terrainInfo + String game = config.getString("game"); + int brickCount = config.getInt("brickCount"); + + // Log or process additional configuration values + SGM.getInstance().getCLogger().log("Game: " + game); + SGM.getInstance().getCLogger().log("Brick Count: " + brickCount); + + return true; + } + +// private void placeBrick(Integer integer) { +// +// } + @Override public void start() { if (state == GameState.READY) { @@ -127,11 +185,6 @@ public class CTB implements Minigame { return state; } - @Override - public CommandSender getOwner() { - return owner; - } - @Override public void onTick() { if (state == GameState.STARTED && players.size() <= 1) { diff --git a/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTBOptions.java b/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTBOptions.java index 5ffa2b3..a23451e 100644 --- a/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTBOptions.java +++ b/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTBOptions.java @@ -1,22 +1,17 @@ package xyz.twovb.sgm.games.impl.capturethebrick; - /* * Created by 2vb - 3/7/2024 */ -import org.bukkit.ChatColor; import org.bukkit.Location; import java.util.ArrayList; public interface CTBOptions { - - int maxTeams = 2; int brickCount = 2; - ArrayList redBrickSpawns = new ArrayList<>(); - ArrayList blueBrickSpawns = new ArrayList<>(); + ArrayList redBrickLocations = new ArrayList(); + ArrayList blueBrickLocations = new ArrayList(); int territoryLevel = 1; - int spawnLevel = 3; - int boundaryLevel = 5; + int boundaryLevel = 3; } diff --git a/src/main/java/xyz/twovb/sgm/levels/LevelManager.java b/src/main/java/xyz/twovb/sgm/levels/LevelManager.java index 0f5c0fe..6f5de01 100644 --- a/src/main/java/xyz/twovb/sgm/levels/LevelManager.java +++ b/src/main/java/xyz/twovb/sgm/levels/LevelManager.java @@ -9,12 +9,14 @@ import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import xyz.twovb.sgm.SGM; import xyz.twovb.toolbox.api.CustomPlayer; import java.io.File; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; @@ -56,20 +58,60 @@ public class LevelManager { } private static void genDataFile(File path, String name, String game) { - // probably rewriting tmr - // these are opposite Map data = new HashMap<>(); data.put("game", game); data.put("name", name); + DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); - File yamlFile = new File(path + "/sgm.yml"); - try (FileWriter writer = new FileWriter(yamlFile)) { - yaml.dump(data, writer); - } catch (IOException e) { - SGM.getInstance().getCLogger().error(e); - } + + File optFile = new File(gamePath + game + "/options.yml"); + + // Attempt to load options.yml + new BukkitRunnable() { + private final int maxAttempts = SGM.getInstance().getConfig().getInt("retry-attempts"); + private int attempts = 0; + + @Override + public void run() { + if (attempts >= maxAttempts) { + cancel(); + SGM.getInstance().getCLogger().error("Unable to load game options after " + attempts + " attempts."); + return; + } + + try (FileReader reader = new FileReader(optFile)) { + Map optData = yaml.load(reader); + if (optData != null) { + SGM.getInstance().getCLogger().log("Loaded YAML data: " + optData); + data.putAll(optData); // Append loaded data to new data + cancel(); // Cancel the task once data is successfully loaded + } + } catch (IOException e) { + attempts++; + } + } + }.runTaskTimerAsynchronously(SGM.getInstance(), 0L, 20L); // Run the task asynchronously with 20 ticks delay + + // Schedule a task to write the combined data to the target YAML file once loaded + new BukkitRunnable() { + @Override + public void run() { + // Check if data has been loaded + if (data.isEmpty()) { + return; // Exit if data is empty (not loaded yet) + } + + // Write the combined data to the target YAML file + File yamlFile = new File(path, "sgm.yml"); + try (FileWriter writer = new FileWriter(yamlFile)) { + yaml.dump(data, writer); + } catch (IOException e) { + SGM.getInstance().getCLogger().error("Error writing data file: " + e.getMessage()); + } + } + }.runTaskTimer(SGM.getInstance(), 20L, 20L); // Run the task synchronously with 20 ticks delay after initial load attempt } public static World getLevel(String name) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..02d755c --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1 @@ +retry-attempts: 3 # How many times SGM will attempt to load options.yml before failing. \ No newline at end of file diff --git a/src/main/resources/games/capturethebrick/options.yml b/src/main/resources/games/capturethebrick/options.yml index e69de29..7647e30 100644 --- a/src/main/resources/games/capturethebrick/options.yml +++ b/src/main/resources/games/capturethebrick/options.yml @@ -0,0 +1,19 @@ +brickCount: 2 +teams: + red: + brickSpawns: + - 0,0,0 + - 0,1,0 + playerSpawnArea: + - 0,0 + - 0,0 + blue: + brickSpawns: + - 0,0,0 + - 0,0,0 + playerSpawnArea: + - 0,0 + - 0,0 +terrainInfo: + territory: 1 + boundary: 3