Finish applyOptions function and cleaned it up as well.

This commit is contained in:
2vb 2024-07-04 17:50:49 -07:00
parent 3589584e54
commit a2ad900b5f
3 changed files with 67 additions and 66 deletions

View File

@ -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<Player> players;
private int bricks;
private final ArrayList<Location> redBrickSpawns = new ArrayList<Location>();
private final ArrayList<Location> blueBrickSpawns = new ArrayList<Location>();
private ArrayList<Location> redSpawnArea = new ArrayList<Location>();
private ArrayList<Location> blueSpawnArea = new ArrayList<Location>();
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<String> teamsToProcess = Arrays.asList("red", "blue");
for (String teamName : teamsToProcess) {
@ -76,50 +101,39 @@ public class CTB implements Minigame {
List<String> brickSpawns = team.getStringList("brickSpawns");
List<String> 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) {

View File

@ -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<Location> redBrickLocations = new ArrayList<Location>();
ArrayList<Location> blueBrickLocations = new ArrayList<Location>();
int territoryLevel = 1;
int boundaryLevel = 3;
}

View File

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