From a2ad900b5fe1b2a4e78e5783d7d33df5226fc1c1 Mon Sep 17 00:00:00 2001 From: 2vb <2vb@protonmail.com> Date: Thu, 4 Jul 2024 17:50:49 -0700 Subject: [PATCH] Finish applyOptions function and cleaned it up as well. --- .../sgm/games/impl/capturethebrick/CTB.java | 96 +++++++++++-------- .../impl/capturethebrick/CTBOptions.java | 17 ---- .../games/capturethebrick/options.yml | 20 ++-- 3 files changed, 67 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTBOptions.java 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 06266b0..b2f0cdb 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 @@ -14,6 +14,7 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.HandlerList; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -23,18 +24,26 @@ import xyz.twovb.sgm.games.Minigame; import xyz.twovb.sgm.levels.LevelManager; import xyz.twovb.toolbox.api.CustomPlayer; import xyz.twovb.toolbox.utils.ChatUtils; +import xyz.twovb.toolbox.utils.CustomLogger; import java.io.File; import java.io.IOException; import java.util.*; public class CTB implements Minigame { + final static String name = "CaptureTheBrick"; private final UUID uuid; private final List players; - private int bricks; + private final ArrayList redBrickSpawns = new ArrayList(); + private final ArrayList blueBrickSpawns = new ArrayList(); + private ArrayList redSpawnArea = new ArrayList(); + private ArrayList blueSpawnArea = new ArrayList(); private World gameWorld; private GameState state; + private int bricks; + private int territoryLevel; + private int boundsLevel; public CTB() { this.uuid = UUID.randomUUID(); @@ -58,16 +67,32 @@ public class CTB implements Minigame { } } + private Location parseString(String string) { + String[] coords = string.trim().split(","); + if (coords.length == 3) { + return new Location(gameWorld, Integer.parseInt(coords[0]), Integer.parseInt(coords[1]), Integer.parseInt(coords[2])); + } else { + return null; + } + } + + // Attempts to load the game's options as a YamlConfiguration + // before processing each section in the same order as the file. 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()); + // Set map information + ConfigurationSection mapInfoSection = config.getConfigurationSection("mapInfo"); + if (mapInfoSection != null) { + this.bricks = mapInfoSection.getInt("brickCount"); + this.territoryLevel = mapInfoSection.getInt("territory"); + this.boundsLevel = mapInfoSection.getInt("boundary"); + } else { return false; } - // Process teams section + // Process team information List teamsToProcess = Arrays.asList("red", "blue"); for (String teamName : teamsToProcess) { @@ -76,50 +101,39 @@ public class CTB implements Minigame { 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 + // Get brick locations if (brickSpawns.size() >= bricks) { - for (String brickSpawn : brickSpawns) { - String[] coords = brickSpawn.split(","); - if (coords.length == 3) { - int x = Integer.parseInt(coords[0]); - int y = Integer.parseInt(coords[1]); - int z = Integer.parseInt(coords[2]); - // Now you have x, y, z coordinates to work with - placeBrick(new Location(gameWorld, x, y, z), teamName); - } else { - SGM.getInstance().getCLogger().error("Invalid brick spawn: " + coords[0]); - return false; + for (String rawBrickLoc : brickSpawns) { + Location brickLoc = parseString(rawBrickLoc); + if (teamName.equals("red")) { + this.redBrickSpawns.add(brickLoc); + } else if (teamName.equals("blue")) { + this.blueBrickSpawns.add(brickLoc); } } } else { - SGM.getInstance().getCLogger().error("Not enough brick spawns!"); + SGM.getInstance().getCLogger().error("Invalid brick spawns. Please fix before trying again."); return false; } + + // Hopefully that works! + if (playerSpawnArea.size() == 2) { + for (String rawSpawnLoc : playerSpawnArea) { + Location spawnLoc = parseString(rawSpawnLoc); + if (teamName.equals("red")) { + this.redSpawnArea.add(spawnLoc); + } else if (teamName.equals("blue")) { + this.blueSpawnArea.add(spawnLoc); + } + } + } else { + SGM.getInstance().getCLogger().error("Invalid spawn area. Please fix before trying again."); + return false; + } + } else { + return false; } } - - // 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; } @@ -157,6 +171,7 @@ public class CTB implements Minigame { } players.clear(); // Clear player list GameManager.getActiveGames().remove(uuid); + HandlerList.unregisterAll(this); } @Override @@ -220,7 +235,6 @@ public class CTB implements Minigame { } } -// if(e.getFrom().getX() != e.getTo().getX() || e.getFrom().getZ() != e.getTo().getZ()) { // full block } @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 deleted file mode 100644 index a23451e..0000000 --- a/src/main/java/xyz/twovb/sgm/games/impl/capturethebrick/CTBOptions.java +++ /dev/null @@ -1,17 +0,0 @@ -package xyz.twovb.sgm.games.impl.capturethebrick; -/* - * Created by 2vb - 3/7/2024 - */ - -import org.bukkit.Location; - -import java.util.ArrayList; - -public interface CTBOptions { - int brickCount = 2; - ArrayList redBrickLocations = new ArrayList(); - ArrayList blueBrickLocations = new ArrayList(); - int territoryLevel = 1; - int boundaryLevel = 3; - -} diff --git a/src/main/resources/games/capturethebrick/options.yml b/src/main/resources/games/capturethebrick/options.yml index 6c2045f..5b2514e 100644 --- a/src/main/resources/games/capturethebrick/options.yml +++ b/src/main/resources/games/capturethebrick/options.yml @@ -1,19 +1,23 @@ -brickCount: 2 +mapInfo: + brickCount: 2 + territory: 1 + boundary: 3 teams: red: brickSpawns: - 0,0,0 - 0,0,0 + # The "y" coordinate does not matter for this value, + # it will always teleport the player to the highest block. playerSpawnArea: - - 0,0 - - 0,0 + - 0,0,0 + - 0,0,0 blue: brickSpawns: - 0,0,0 - 0,0,0 + # The "y" coordinate does not matter for this value, + # it will always teleport the player to the highest block. playerSpawnArea: - - 0,0 - - 0,0 -terrainInfo: - territory: 1 - boundary: 3 + - 0,0,0 + - 0,0,0 \ No newline at end of file