SGM/src/main/java/xyz/twovb/sgm/levels/LevelManager.java
2vb c34a2a4d55
Some checks are pending
Build plugin / build (push) Waiting to run
levels save and load properly
2024-07-02 19:17:45 -07:00

117 lines
4.3 KiB
Java

package xyz.twovb.sgm.levels;
/*
* Created by 2vb - 4/6/2024
*/
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.YamlConfiguration;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import xyz.twovb.sgm.SGM;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class LevelManager {
public static final String path = SGM.getInstance().getDataFolder().getPath() + "/levels/";
public static void loadLevels() {
// List all files (worlds) in the folder
File folder = new File(path);
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
// Check if sgm.yml exists in the world directory
File configFile = new File(file, "sgm.yml");
if (configFile.exists()) {
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
SGM.getInstance().getGameManager().enabledLevels.put(config.getString("game"), config.getString("name"));
// Load the world if sgm.yml exists
loadWorld(file.getName());
} else {
SGM.getInstance().getCLogger().log("Skipping directory " + file.getName() + ": sgm.yml not found.");
}
}
}
}
}
private static void loadWorld(String worldName) {
// Check if the world is already loaded
if (Bukkit.getWorld(worldName) != null) {
SGM.getInstance().getCLogger().log("World " + worldName + " is already loaded.");
return;
}
// Load the world
WorldCreator wc = new WorldCreator(path + worldName, new NamespacedKey(SGM.getInstance(), "level_" + worldName));
World world = wc.createWorld();
if (world != null) {
SGM.getInstance().getCLogger().log("Loaded world: " + worldName);
} else {
SGM.getInstance().getCLogger().error("Failed to load world: " + worldName);
}
}
public static CreationResult createLevel(String name, String game) {
File level = new File(path + name);
if (!SGM.getInstance().getGameManager().getRegisteredGames().contains(game.toLowerCase())) {
return CreationResult.INVALID_ARGS;
}
WorldCreator wc = new WorldCreator(path + name, new NamespacedKey(SGM.getInstance(), "level_" + name));
if (level.exists()) {
return CreationResult.WORLD_EXISTS;
}
wc.type(WorldType.FLAT);
wc.generatorSettings("{\"layers\": [{\"block\": \"air\", \"height\": 1}], \"biome\":\"plains\"}");
wc.generateStructures(false);
World world = wc.createWorld();
if (world == null) {
return CreationResult.UNKNOWN;
}
Location location = new Location(world, 0, 0, 0);
Block block = location.subtract(0, 1, 0).getBlock();
block.setType(Material.STONE);
world.setSpawnLocation(location);
world.setSpawnFlags(false, false);
genDataFile(level, name, game.toLowerCase());
return CreationResult.SUCCESS;
}
private static void genDataFile(File path, String name, String game) {
Map<String, Object> data = new HashMap<>();
data.put("name", name);
data.put("game", game);
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);
}
}
public static World getLevel(String name) {
return Bukkit.getWorld(new NamespacedKey(SGM.getInstance(), "level_" + name));
}
public enum CreationResult {
SUCCESS, WORLD_EXISTS, INVALID_ARGS, UNKNOWN
}
// public void deleteLevel(String name) {
// World world = Bukkit.getWorld(new NamespacedKey(SGM.getInstance(), "level_" + name));
// assert world != null;
// Bukkit.unloadWorld(world, false);
// }
}