130 lines
4.9 KiB
Java
130 lines
4.9 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.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class LevelManager {
|
|
|
|
public static final String path = SGM.getInstance().getDataFolder().getPath() + "/levels/";
|
|
|
|
public HashMap<String, List<String>> enabledMaps = new HashMap<>();
|
|
|
|
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());
|
|
SGM.getInstance().getGameManager().enabledLevels.put(game, name);
|
|
return CreationResult.SUCCESS;
|
|
}
|
|
|
|
private static void genDataFile(File path, String name, String game) {
|
|
// these are opposite
|
|
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 void loadLevels() {
|
|
for (String game : SGM.getInstance().getGameManager().getRegisteredGames()) {
|
|
enabledMaps.put(game, new ArrayList<>()); // Initialize an empty ArrayList for each game
|
|
}
|
|
// 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);
|
|
String game = config.getString("game");
|
|
String name = config.getString("name");
|
|
if (enabledMaps.containsKey(game)) {
|
|
enabledMaps.get(game).add(name);
|
|
// Load the world if sgm.yml exists
|
|
loadWorld(file.getName());
|
|
}
|
|
} else {
|
|
SGM.getInstance().getCLogger().log("Skipping directory " + file.getName() + ": sgm.yml not found.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
// }
|
|
|
|
} |