rewrite to yaml
This commit is contained in:
@@ -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>
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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 PlayerHome getHomeByName(String name){
|
public void addHome(String name, String uuid, String world, int x, int y, int z) {
|
||||||
PreparedStatement ps = null;
|
String basePath = "homes." + name;
|
||||||
try {
|
yaml.set(basePath + ".UUID", uuid);
|
||||||
ps = msql.getConnection().prepareStatement("SELECT * FROM homes WHERE Name = '" + name + "';");
|
yaml.set(basePath + ".World", world);
|
||||||
} catch (SQLException ex) {
|
yaml.set(basePath + ".x", x);
|
||||||
System.getLogger(Homes.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex);
|
yaml.set(basePath + ".y", y);
|
||||||
}
|
yaml.set(basePath + ".z", z);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println("Loaded home");
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get a PlayerHome by name
|
||||||
|
public PlayerHome getHomeByName(String name) {
|
||||||
|
String basePath = "homes." + name;
|
||||||
|
if (!yaml.getConfig().contains(basePath)) return null;
|
||||||
|
|
||||||
//FIX: creation with new sql handler
|
String uuid = yaml.getString(basePath + ".UUID");
|
||||||
public void createNewHome(PlayerHome playerHome){
|
String world = yaml.getString(basePath + ".World");
|
||||||
PreparedStatement ps = null;
|
int x = yaml.getInt(basePath + ".x");
|
||||||
try {
|
int y = yaml.getInt(basePath + ".y");
|
||||||
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()+"');");
|
int z = yaml.getInt(basePath + ".z");
|
||||||
} catch (SQLException e) {
|
|
||||||
// TODO Auto-generated catch block
|
return new PlayerHome(name, uuid, world, x, y, z);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
ps.executeUpdate();
|
// Get all homes for a player by UUID
|
||||||
} catch (SQLException e) {
|
public List<PlayerHome> getPlayerHomes(String uuid) {
|
||||||
// TODO Auto-generated catch block
|
List<PlayerHome> homes = new ArrayList<>();
|
||||||
e.printStackTrace();
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return homes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete a home by name
|
||||||
|
public void deleteHome(String name) {
|
||||||
|
if (yaml.getConfig().contains("homes." + name)) {
|
||||||
|
yaml.set("homes." + name, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user