Ty ChatGPT for loading options :)
This commit is contained in:
parent
86957224b2
commit
90b3a1922d
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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) {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ public class TestGame implements Minigame {
|
|||
private final UUID uuid;
|
||||
private final List<Player> 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) {
|
||||
|
|
|
@ -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<Player> players;
|
||||
private int bricks;
|
||||
private World gameWorld;
|
||||
private CommandSender owner;
|
||||
private GameState state;
|
||||
|
||||
public CTB() {
|
||||
|
@ -45,11 +44,70 @@ 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);
|
||||
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<String> teamsToProcess = Arrays.asList("red", "blue");
|
||||
|
||||
for (String teamName : teamsToProcess) {
|
||||
ConfigurationSection team = config.getConfigurationSection("teams." + teamName);
|
||||
if (team != null) {
|
||||
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
|
||||
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() {
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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<Location> redBrickSpawns = new ArrayList<>();
|
||||
ArrayList<Location> blueBrickSpawns = new ArrayList<>();
|
||||
ArrayList<Location> redBrickLocations = new ArrayList<Location>();
|
||||
ArrayList<Location> blueBrickLocations = new ArrayList<Location>();
|
||||
int territoryLevel = 1;
|
||||
int spawnLevel = 3;
|
||||
int boundaryLevel = 5;
|
||||
int boundaryLevel = 3;
|
||||
|
||||
}
|
||||
|
|
|
@ -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,21 +58,61 @@ public class LevelManager {
|
|||
}
|
||||
|
||||
private static void genDataFile(File path, String name, String game) {
|
||||
// probably rewriting tmr
|
||||
// these are opposite
|
||||
Map<String, Object> 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");
|
||||
|
||||
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<String, Object> 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(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) {
|
||||
return Bukkit.getWorld(new NamespacedKey(SGM.getInstance(), "level_" + name));
|
||||
|
|
1
src/main/resources/config.yml
Normal file
1
src/main/resources/config.yml
Normal file
|
@ -0,0 +1 @@
|
|||
retry-attempts: 3 # How many times SGM will attempt to load options.yml before failing.
|
|
@ -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
|
Loading…
Reference in New Issue
Block a user