From 66e6b46cb2c0b64c2042a913897c4e7d01d22d14 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 18 Jan 2026 18:35:52 +0100 Subject: [PATCH] rewrite to yaml --- pom.xml | 24 ++-- src/main/java/com/softwarerat/Main.java | 10 +- src/main/java/com/softwarerat/SQL/Homes.java | 123 +++++++------------ src/main/java/com/softwarerat/SQL/MySQL.java | 39 ------ src/main/java/com/softwarerat/SQL/Yaml.java | 64 ++++++++++ 5 files changed, 128 insertions(+), 132 deletions(-) delete mode 100644 src/main/java/com/softwarerat/SQL/MySQL.java create mode 100644 src/main/java/com/softwarerat/SQL/Yaml.java diff --git a/pom.xml b/pom.xml index 4060460..137d1e8 100644 --- a/pom.xml +++ b/pom.xml @@ -7,16 +7,7 @@ com.softwarerat HomesAPI 1.1-SNAPSHOT - - - gitlab-maven - https://gitlab.softwarerat.com/api/v4/projects/14/packages/maven - - - gitlab-maven - https://gitlab.softwarerat.com/api/v4/projects/14/packages/maven - - + 18 18 @@ -29,6 +20,17 @@ 1.18.42 provided + + io.papermc.paper + paper-api + 1.21.11-R0.1-SNAPSHOT + provided + - + + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + diff --git a/src/main/java/com/softwarerat/Main.java b/src/main/java/com/softwarerat/Main.java index a4da622..ef001d3 100644 --- a/src/main/java/com/softwarerat/Main.java +++ b/src/main/java/com/softwarerat/Main.java @@ -2,17 +2,17 @@ package com.softwarerat; import com.softwarerat.SQL.Homes; -import com.softwarerat.SQL.MySQL; +import com.softwarerat.SQL.Yaml; import lombok.Getter; @Getter public class Main { - MySQL msql; + Yaml yaml; Homes homes; - public void enableAPI(String user, String pw, String Database, String host, int port){ - msql = new MySQL(host, port, Database, user, host); - homes = new Homes(msql); + public void enableAPI(String path, String filename){ + yaml = new Yaml(path, filename); + homes = new Homes(yaml); } } \ No newline at end of file diff --git a/src/main/java/com/softwarerat/SQL/Homes.java b/src/main/java/com/softwarerat/SQL/Homes.java index 47f45a0..17e0e7e 100644 --- a/src/main/java/com/softwarerat/SQL/Homes.java +++ b/src/main/java/com/softwarerat/SQL/Homes.java @@ -1,95 +1,64 @@ package com.softwarerat.SQL; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; public class Homes { - MySQL msql; - public Homes(MySQL msql) { - msql = msql; + + private final Yaml yaml; + + public Homes(Yaml yaml) { + this.yaml = yaml; } + // Add or update a player home + public void addHome(String name, String uuid, String world, int x, int y, int z) { + String basePath = "homes." + name; + yaml.set(basePath + ".UUID", uuid); + yaml.set(basePath + ".World", world); + yaml.set(basePath + ".x", x); + yaml.set(basePath + ".y", y); + yaml.set(basePath + ".z", z); + } - public PlayerHome getHomeByName(String name){ - PreparedStatement ps = null; - try { - ps = msql.getConnection().prepareStatement("SELECT * FROM homes WHERE Name = '" + name + "';"); - } catch (SQLException ex) { - System.getLogger(Homes.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex); - } - ResultSet resultSet = null; - try { - resultSet = ps.executeQuery(); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - if (resultSet != null) { - try { - resultSet.next(); - return new PlayerHome(resultSet.getString("Name"),resultSet.getString("UUID"),resultSet.getString("World"),resultSet.getInt("x"),resultSet.getInt("y"), resultSet.getInt("z")); - } catch (SQLException e) { + // Get a PlayerHome by name + public PlayerHome getHomeByName(String name) { + String basePath = "homes." + name; + if (!yaml.getConfig().contains(basePath)) return null; - throw new RuntimeException(e); + String uuid = yaml.getString(basePath + ".UUID"); + String world = yaml.getString(basePath + ".World"); + int x = yaml.getInt(basePath + ".x"); + int y = yaml.getInt(basePath + ".y"); + int z = yaml.getInt(basePath + ".z"); + + return new PlayerHome(name, uuid, world, x, y, z); + } + + // Get all homes for a player by UUID + public List getPlayerHomes(String uuid) { + List homes = new ArrayList<>(); + if (!yaml.getConfig().contains("homes")) return homes; + + for (String homeName : yaml.getConfig().getConfigurationSection("homes").getKeys(false)) { + String homeUUID = yaml.getString("homes." + homeName + ".UUID"); + if (homeUUID != null && homeUUID.equals(uuid)) { + homes.add(getHomeByName(homeName)); } } - System.out.println("Loaded home"); - return null; + return homes; } - - //FIX: creation with new sql handler - public void createNewHome(PlayerHome playerHome){ - PreparedStatement ps = null; - try { - ps = msql.getConnection().prepareStatement("INSERT INTO homes (Name, UUID, World, x, y, z) VALUES ('"+playerHome.getName()+"','"+playerHome.getUUID()+"','"+playerHome.getWorld()+"','"+playerHome.getX()+"','"+playerHome.getY()+"','"+playerHome.getZ()+"');"); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - try { - ps.executeUpdate(); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + // Delete a home by name + public void deleteHome(String name) { + if (yaml.getConfig().contains("homes." + name)) { + yaml.set("homes." + name, null); } } - //ToDo: DeleteHome method - public ArrayList getPlayerHomes(String UUID) { - ArrayList homes = new ArrayList<>(); - ResultSet resultSet = null; - PreparedStatement ps = null; - try { - ps = msql.getConnection().prepareStatement("SELECT * FROM homes WHERE uuid = '" + UUID + "';"); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - try { - resultSet = ps.executeQuery(); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - if (resultSet != null) { - try { - while (true){ - resultSet.next(); - homes.add(new PlayerHome(resultSet.getString("Name"),resultSet.getString("UUID"),resultSet.getString("World"),resultSet.getInt("x"),resultSet.getInt("y"), resultSet.getInt("z"))); - - } - - } catch (SQLException e) { - - throw new RuntimeException(e); - } - } - System.out.println("Loaded Homes from player "); - return null; + // Check if a home exists + public boolean homeExists(String name) { + return yaml.getConfig().contains("homes." + name); } -} +} \ No newline at end of file diff --git a/src/main/java/com/softwarerat/SQL/MySQL.java b/src/main/java/com/softwarerat/SQL/MySQL.java deleted file mode 100644 index cc3e7dd..0000000 --- a/src/main/java/com/softwarerat/SQL/MySQL.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.softwarerat.SQL; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class MySQL { - - private static Connection con; - private static String Host; - private static int port; - private static String database; - private static String user; - private static String password; - - public MySQL(String Host, int port, String database, String user, String password) { - MySQL.Host = Host; - MySQL.port = port; - MySQL.database = database; - MySQL.user = user; - MySQL.password = password; - connect(); - } - - public void connect() { - try { - con = DriverManager.getConnection("jdbc:mysql://45.85.219.106:3306/Bansystem?autoRecconnect=true", "craftunity", "zFtf5zFTS*ncJrXT"); - } catch (SQLException var1) { - System.out.println("SQL Disconected"); - System.out.println("SQL Error"); - - } - - } - - public Connection getConnection() { - return con; - } -} diff --git a/src/main/java/com/softwarerat/SQL/Yaml.java b/src/main/java/com/softwarerat/SQL/Yaml.java new file mode 100644 index 0000000..2e0d24c --- /dev/null +++ b/src/main/java/com/softwarerat/SQL/Yaml.java @@ -0,0 +1,64 @@ +package com.softwarerat.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); + } +} \ No newline at end of file