rewrite to yaml

This commit is contained in:
Laura
2026-01-18 18:35:52 +01:00
parent fe47f5d407
commit 66e6b46cb2
5 changed files with 128 additions and 132 deletions
+13 -11
View File
@@ -7,16 +7,7 @@
<groupId>com.softwarerat</groupId> <groupId>com.softwarerat</groupId>
<artifactId>HomesAPI</artifactId> <artifactId>HomesAPI</artifactId>
<version>1.1-SNAPSHOT</version> <version>1.1-SNAPSHOT</version>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.softwarerat.com/api/v4/projects/14/packages/maven</url>
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
<url>https://gitlab.softwarerat.com/api/v4/projects/14/packages/maven</url>
</snapshotRepository>
</distributionManagement>
<properties> <properties>
<maven.compiler.source>18</maven.compiler.source> <maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target> <maven.compiler.target>18</maven.compiler.target>
@@ -29,6 +20,17 @@
<version>1.18.42</version> <version>1.18.42</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21.11-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
</repositories>
</project> </project>
+5 -5
View File
@@ -2,17 +2,17 @@ package com.softwarerat;
import com.softwarerat.SQL.Homes; import com.softwarerat.SQL.Homes;
import com.softwarerat.SQL.MySQL; import com.softwarerat.SQL.Yaml;
import lombok.Getter; import lombok.Getter;
@Getter @Getter
public class Main { public class Main {
MySQL msql; Yaml yaml;
Homes homes; Homes homes;
public void enableAPI(String user, String pw, String Database, String host, int port){ public void enableAPI(String path, String filename){
msql = new MySQL(host, port, Database, user, host); yaml = new Yaml(path, filename);
homes = new Homes(msql); homes = new Homes(yaml);
} }
} }
+46 -77
View File
@@ -1,95 +1,64 @@
package com.softwarerat.SQL; package com.softwarerat.SQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class Homes { public class Homes {
MySQL msql;
public Homes(MySQL msql) { private final Yaml yaml;
msql = msql;
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){ // Get a PlayerHome by name
PreparedStatement ps = null; public PlayerHome getHomeByName(String name) {
try { String basePath = "homes." + name;
ps = msql.getConnection().prepareStatement("SELECT * FROM homes WHERE Name = '" + name + "';"); if (!yaml.getConfig().contains(basePath)) return null;
} 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) {
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<PlayerHome> getPlayerHomes(String uuid) {
List<PlayerHome> 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 homes;
return null;
} }
// Delete a home by name
//FIX: creation with new sql handler public void deleteHome(String name) {
public void createNewHome(PlayerHome playerHome){ if (yaml.getConfig().contains("homes." + name)) {
PreparedStatement ps = null; yaml.set("homes." + name, 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();
} }
} }
//ToDo: DeleteHome method // Check if a home exists
public ArrayList<PlayerHome> getPlayerHomes(String UUID) { public boolean homeExists(String name) {
ArrayList<PlayerHome> homes = new ArrayList<>(); return yaml.getConfig().contains("homes." + name);
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;
} }
} }
@@ -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;
}
}
@@ -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);
}
}