mdbd/src/main/java/dev/loki/map/TemplateManager.java
2026-06-10 23:00:12 +02:00

111 lines
3.6 KiB
Java

package dev.loki.map;
import dev.loki.mdbd;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import java.io.File;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
public class TemplateManager {
private final mdbd plugin;
private final File templatesFolder;
private final Map<String, MapTemplate> templates;
private final Map<String, PresetMap> presets;
public TemplateManager(mdbd plugin) {
this.plugin = plugin;
this.templatesFolder = new File(plugin.getDataFolder(), "templates");
this.templates = new HashMap<>();
this.presets = new HashMap<>();
if (!templatesFolder.exists()) {
templatesFolder.mkdirs();
}
}
public void loadAllTemplates() {
templates.clear();
File[] files = templatesFolder.listFiles((dir, name) -> name.endsWith(".nbt"));
if (files == null) return;
for (File file : files) {
String name = file.getName().replace(".nbt", "");
MapTemplate template = new MapTemplate(name);
if (template.loadFromFile(file)) {
templates.put(name, template);
plugin.getLogger().info("Loaded template: " + name);
}
}
}
public MapTemplate getTemplate(String name) {
return templates.get(name);
}
public boolean hasTemplate(String name) {
return templates.containsKey(name);
}
public void saveTemplate(String name, Location corner1, Location corner2) {
MapTemplate template = new MapTemplate(name);
template.saveArea(corner1, corner2);
File file = new File(templatesFolder, name + ".nbt");
template.saveToFile(file);
templates.put(name, template);
plugin.getLogger().info("Saved template: " + name);
}
public Map<String, MapTemplate> getAllTemplates() {
return new HashMap<>(templates);
}
public void registerPreset(String name, PresetMap preset) {
presets.put(name, preset);
}
public PresetMap getPreset(String name) {
return presets.get(name);
}
public void loadDefaultPresets() {
// Generator presets (3x3x3 structure)
presets.put("generator_default", new PresetMap("generator_default",
"generator", new Location(null, 1, 0, 1)));
// Hook preset (2x3x2)
presets.put("hook_default", new PresetMap("hook_default",
"hook", new Location(null, 1, 0, 1)));
// Chest preset (1x1x1)
presets.put("chest_default", new PresetMap("chest_default",
"chest", new Location(null, 0, 0, 0)));
// Hatch preset (2x1x2)
presets.put("hatch_default", new PresetMap("hatch_default",
"hatch", new Location(null, 1, 0, 1)));
}
public boolean placeObject(String templateName, Location location) {
MapTemplate template = templates.get(templateName);
if (template == null || !template.isLoaded()) return false;
template.place(location, false);
return true;
}
public boolean placeObject(String templateName, Location location,
org.bukkit.block.structure.StructureRotation rotation) {
MapTemplate template = templates.get(templateName);
if (template == null || !template.isLoaded()) return false;
template.placeWithRotation(location, rotation,
org.bukkit.block.structure.Mirror.NONE);
return true;
}
public record PresetMap(String name, String templateName, Location offset) {}
}