rewrite to yaml
This commit is contained in:
@@ -7,16 +7,7 @@
|
||||
<groupId>com.softwarerat</groupId>
|
||||
<artifactId>HomesAPI</artifactId>
|
||||
<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>
|
||||
<maven.compiler.source>18</maven.compiler.source>
|
||||
<maven.compiler.target>18</maven.compiler.target>
|
||||
@@ -29,6 +20,17 @@
|
||||
<version>1.18.42</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.papermc.paper</groupId>
|
||||
<artifactId>paper-api</artifactId>
|
||||
<version>1.21.11-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>papermc-repo</id>
|
||||
<url>https://repo.papermc.io/repository/maven-public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<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 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<PlayerHome> getPlayerHomes(String UUID) {
|
||||
ArrayList<PlayerHome> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user