Files
HomesAPI/src/main/java/com/softwarerat/homeAPI/SQL/Homes.java
T

79 lines
2.3 KiB
Java

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;
}
}