Migrate from SQL classes to homeAPI package structure

This commit is contained in:
Laura
2026-08-09 15:03:52 +02:00
parent 66e6b46cb2
commit 0d1b4239bd
6 changed files with 102 additions and 71 deletions
@@ -0,0 +1,79 @@
package com.softwarerat.homeAPI.SQL;
import java.util.ArrayList;
import java.util.List;
public class Homes {
private final Yaml yaml;
public Homes(Yaml yaml) {
this.yaml = yaml;
}
/**
* Retrieves a specific home by name for a specific player.
*/
/**
* Retrieves a home by name only.
* In this version, the Name is the unique identifier.
*/
public PlayerHome getHomeByName(String name) {
if (!yaml.getConfig().contains(name)) {
return null;
}
// The path is now just "Name.key"
String uuid = yaml.getConfig().getString(name + ".uuid");
String world = yaml.getConfig().getString(name + ".world");
int x = yaml.getConfig().getInt(name + ".x");
int y = yaml.getConfig().getInt(name + ".y");
int z = yaml.getConfig().getInt(name + ".z");
return new PlayerHome(name, uuid, world, x, y, z);
}
/**
* Saves a new home or updates an existing one.
*/
public void createNewHome(PlayerHome playerHome) {
String path = playerHome.getUUID() + "." + playerHome.getName();
yaml.getConfig().set(path + ".world", playerHome.getWorld());
yaml.getConfig().set(path + ".x", playerHome.getX());
yaml.getConfig().set(path + ".y", playerHome.getY());
yaml.getConfig().set(path + ".z", playerHome.getZ());
yaml.save(); // Write changes to disk
}
/**
* Removes a home from the YAML file.
*/
public void deleteHome(String uuid, String name) {
String path = uuid + "." + name;
if (yaml.getConfig().contains(path)) {
yaml.getConfig().set(path, null); // Setting to null removes the key
yaml.save();
}
}
/**
* Gets all homes associated with a specific Player UUID.
*/
public List<PlayerHome> getPlayerHomes(String uuid) {
List<PlayerHome> homes = new ArrayList<>();
// Loop through every top-level key (every home name in the file)
for (String homeName : yaml.getConfig().getKeys(false)) {
String ownerUUID = yaml.getConfig().getString(homeName + ".uuid");
if (uuid.equals(ownerUUID)) {
homes.add(getHomeByName(homeName));
}
}
return homes;
}
}
@@ -0,0 +1,17 @@
package com.softwarerat.homeAPI.SQL;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class PlayerHome {
String Name;
String UUID;
String World;
private double x;
private double y;
private double z;
}
@@ -0,0 +1,64 @@
package com.softwarerat.homeAPI.SQL;
import java.io.File;
import java.io.IOException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
public class Yaml {
private final File file;
private FileConfiguration config;
public Yaml(String path, String fileName) {
this.file = new File(path, fileName); // FIX HERE
load();
}
public void load() {
this.config = YamlConfiguration.loadConfiguration(file);
}
public void save() {
try {
config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void reload() {
load();
}
public FileConfiguration getConfig() {
return config;
}
public void set(String path, Object value) {
config.set(path, value);
save();
}
public Object get(String path) {
return config.get(path);
}
public String getString(String path) {
return config.getString(path);
}
public int getInt(String path) {
return config.getInt(path);
}
public boolean getBoolean(String path) {
return config.getBoolean(path);
}
}