Migrate KitAPI from SQL to YAML-based storage
This commit is contained in:
@@ -6,38 +6,33 @@
|
|||||||
|
|
||||||
<groupId>com.softwarerat</groupId>
|
<groupId>com.softwarerat</groupId>
|
||||||
<artifactId>KitAPI</artifactId>
|
<artifactId>KitAPI</artifactId>
|
||||||
<version>1.1-SNAPSHOT</version>
|
<version>1.3-SNAPSHOT</version>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
<repository>
|
<repository>
|
||||||
<id>gitlab-maven</id>
|
<id>forgejo</id>
|
||||||
<url>https://gitlab.softwarerat.com/api/v4/projects/11/packages/maven</url>
|
<name>Forgejo Maven</name>
|
||||||
|
<url>http://heimserver.local:36107/api/packages/minecraft/maven</url>
|
||||||
</repository>
|
</repository>
|
||||||
<snapshotRepository>
|
|
||||||
<id>gitlab-maven</id>
|
|
||||||
<url>https://gitlab.softwarerat.com/api/v4/projects/11/packages/maven</url>
|
|
||||||
</snapshotRepository>
|
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>papermc-repo</id>
|
||||||
|
<url>https://repo.papermc.io/repository/maven-public/</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>gitea</id>
|
||||||
|
<url>http://heimserver.local:36107/api/packages/minecraft/maven</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
<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>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>gitlab-zyonic</id>
|
|
||||||
<url>https://gitlab.zyonicsoftware.com/api/v4/projects/119/packages/maven</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<id>papermc-repo</id>
|
|
||||||
<url>https://repo.papermc.io/repository/maven-public/</url>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>de.mint</groupId>
|
|
||||||
<artifactId>AsyncMySQLPoolHandler</artifactId>
|
|
||||||
<version>1.0.5</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.papermc.paper</groupId>
|
<groupId>io.papermc.paper</groupId>
|
||||||
<artifactId>paper-api</artifactId>
|
<artifactId>paper-api</artifactId>
|
||||||
@@ -47,7 +42,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.18.28</version>
|
<version>1.18.42</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
package com.softwarerat.KitAPi;
|
package com.softwarerat.KitAPi;
|
||||||
|
|
||||||
import com.softwarerat.SQL.KitManager;
|
import java.util.ArrayList;
|
||||||
import com.softwarerat.SQL.SQLHandler;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class KitAPI {
|
public class KitAPI {
|
||||||
SQLHandler sqlHandler;
|
|
||||||
|
YamlHandler yamlHandler;
|
||||||
KitManager kitManager;
|
KitManager kitManager;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void giveKit(Player player, String s) {
|
public void giveKit(Player player, String s) {
|
||||||
String b64 = sqlHandler.loadKit(s);
|
String b64 = yamlHandler.loadKit(s);
|
||||||
kitManager.giveKit(player, b64);
|
kitManager.giveKit(player, b64);
|
||||||
player.sendMessage("erfolgreich kit ausgestellt");
|
player.sendMessage("erfolgreich kit ausgestellt");
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> getKits() {
|
public ArrayList<String> getKits() {
|
||||||
return sqlHandler.getKits();
|
return yamlHandler.getKits();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-10
@@ -1,4 +1,4 @@
|
|||||||
package com.softwarerat.SQL;
|
package com.softwarerat.KitAPi;
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -16,21 +16,21 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
public class KitManager {
|
public class KitManager {
|
||||||
private final HashMap<String, ItemStack[]> kits;
|
private final HashMap<String, ItemStack[]> kits;
|
||||||
private final ConcurrentHashMap<UUID, String> playerKit;
|
private final ConcurrentHashMap<UUID, String> playerKit;
|
||||||
private final ArrayList<Player> playersWithKits;
|
private final ArrayList<Player> playersWithKits;
|
||||||
private SQLHandler asyncSQLHandler;
|
private YamlHandler yamlHandler;
|
||||||
|
|
||||||
public KitManager(SQLHandler asyncSQLHandler) {
|
public KitManager(YamlHandler yamlHandler) {
|
||||||
this.kits = new HashMap<>();
|
this.kits = new HashMap<>();
|
||||||
this.playerKit = new ConcurrentHashMap<UUID, String>();
|
this.playerKit = new ConcurrentHashMap<UUID, String>();
|
||||||
this.playersWithKits = new ArrayList<>();
|
this.playersWithKits = new ArrayList<>();
|
||||||
this.asyncSQLHandler = asyncSQLHandler;
|
this.yamlHandler = yamlHandler;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// loads the Inventory by the UnHashing Algorithm
|
// loads the Inventory by the UnHashing Algorithm
|
||||||
public ItemStack[] loadKit(String base64) {
|
public ItemStack[] loadKit(String base64) {
|
||||||
System.out.println("Loading Kit with data: " + base64);
|
System.out.println("Loading Kit with data: " + base64);
|
||||||
@@ -40,6 +40,7 @@ public class KitManager {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// gives The Player the Hashed Inventory
|
// gives The Player the Hashed Inventory
|
||||||
public void giveKit(Player player, String base64) {
|
public void giveKit(Player player, String base64) {
|
||||||
player.getInventory().clear();
|
player.getInventory().clear();
|
||||||
@@ -129,12 +130,9 @@ public class KitManager {
|
|||||||
String kitBase64 = this.itemStackArrayToBase64(player.getInventory().getContents());
|
String kitBase64 = this.itemStackArrayToBase64(player.getInventory().getContents());
|
||||||
System.out.println(kitBase64);
|
System.out.println(kitBase64);
|
||||||
|
|
||||||
try {
|
|
||||||
System.out.println(kitBase64);
|
System.out.println(kitBase64);
|
||||||
asyncSQLHandler.saveKit(name, kitBase64);
|
yamlHandler.saveKit(name, kitBase64);
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
this.kits.put(name, player.getInventory().getContents());
|
this.kits.put(name, player.getInventory().getContents());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package com.softwarerat.KitAPi;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<String> getKits() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
throw new UnsupportedOperationException("Unimplemented method 'getKits'");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.softwarerat.KitAPi;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
|
||||||
|
public class YamlHandler {
|
||||||
|
|
||||||
|
private final Yaml yaml;
|
||||||
|
|
||||||
|
public YamlHandler(String path) {
|
||||||
|
this.yaml = new Yaml(path, "kits.yml");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load a kit by name
|
||||||
|
public String loadKit(String name) {
|
||||||
|
return yaml.getString("kits." + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all kit names
|
||||||
|
public ArrayList<String> getKits() {
|
||||||
|
ArrayList<String> kits = new ArrayList<>();
|
||||||
|
|
||||||
|
if (yaml.getConfig().contains("kits")) {
|
||||||
|
ConfigurationSection section = yaml.getConfig().getConfigurationSection("kits");
|
||||||
|
if (section != null) {
|
||||||
|
kits.addAll(section.getKeys(false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return kits;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save a kit
|
||||||
|
public void saveKit(String name, String kitBase64) {
|
||||||
|
yaml.set("kits." + name, kitBase64);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +1,26 @@
|
|||||||
package com.softwarerat;
|
package com.softwarerat;
|
||||||
|
|
||||||
import com.softwarerat.KitAPi.KitAPI;
|
import com.softwarerat.KitAPi.KitAPI;
|
||||||
import com.softwarerat.SQL.KitManager;
|
import com.softwarerat.KitAPi.KitManager;
|
||||||
import com.softwarerat.SQL.SQLHandler;
|
import com.softwarerat.KitAPi.Yaml;
|
||||||
|
import com.softwarerat.KitAPi.YamlHandler;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
public class Main {
|
|
||||||
@Getter
|
@Getter
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
Yaml yaml;
|
||||||
|
YamlHandler yamlHandler;
|
||||||
KitAPI kitAPI;
|
KitAPI kitAPI;
|
||||||
SQLHandler sqlHandler;
|
|
||||||
KitManager kitManager;
|
KitManager kitManager;
|
||||||
public void enableAPI(String host,String user,String password,String Database){
|
|
||||||
kitManager = new KitManager(sqlHandler);
|
public void enableAPI(String yamlPath, String yamlFileName) {
|
||||||
sqlHandler= new SQLHandler(kitManager,host,user,password,Database);
|
|
||||||
kitAPI = new KitAPI(sqlHandler,kitManager);
|
yaml = new Yaml(yamlPath, yamlFileName);
|
||||||
|
yamlHandler = new YamlHandler(yamlPath);
|
||||||
|
kitManager = new KitManager(yamlHandler);
|
||||||
|
|
||||||
|
kitAPI = new KitAPI( yamlHandler, kitManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
package com.softwarerat.SQL;
|
|
||||||
|
|
||||||
import de.mint.asyncmysqlpoolhandler.configservice.ConfigBuilder;
|
|
||||||
import de.mint.asyncmysqlpoolhandler.configservice.ConfigPoolFramework;
|
|
||||||
import de.mint.asyncmysqlpoolhandler.enumservice.EnumPoolFramework;
|
|
||||||
import de.mint.asyncmysqlpoolhandler.mainservice.AsyncMySQLPoolHandler;
|
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
public class SQLHandler {
|
|
||||||
|
|
||||||
|
|
||||||
private final AsyncMySQLPoolHandler MySQLPoolHandler;
|
|
||||||
private final ConfigPoolFramework configPoolFramework = ConfigBuilder.getConfigBuilder().build();
|
|
||||||
|
|
||||||
public SQLHandler(KitManager kitManager, String hostname, String user, String pw, String DB) {
|
|
||||||
|
|
||||||
this.MySQLPoolHandler = new AsyncMySQLPoolHandler(hostname, user, pw, DB, EnumPoolFramework.HIKARICP, configPoolFramework);
|
|
||||||
this.openConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void openConnection() {
|
|
||||||
this.MySQLPoolHandler.openPool();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Basic SQL Things
|
|
||||||
private ResultSet executeQuery(String sql) throws ExecutionException, InterruptedException {
|
|
||||||
return this.MySQLPoolHandler.executeQueryAsync(sql).get();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void executeUpdate(String sql) throws ExecutionException, InterruptedException {
|
|
||||||
this.MySQLPoolHandler.executeUpdateAsync(sql).get();
|
|
||||||
}
|
|
||||||
public String loadKit(String KitName) {
|
|
||||||
System.out.println("Loading Kit");
|
|
||||||
ResultSet resultSet = null;
|
|
||||||
try {
|
|
||||||
resultSet = this.executeQuery("SELECT * FROM kit WHERE KitName = '" + KitName + "';");
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
if (resultSet != null) {
|
|
||||||
try {
|
|
||||||
resultSet.next();
|
|
||||||
return resultSet.getString("KitData");
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println("Loaded Kit");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
public ArrayList<String> getKits() {
|
|
||||||
ArrayList<String> kits = new ArrayList<String>();
|
|
||||||
ResultSet resultSet = null;
|
|
||||||
try {
|
|
||||||
resultSet = this.executeQuery("SELECT * FROM kit");
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
if (resultSet != null) {
|
|
||||||
try {
|
|
||||||
while (resultSet.next()) {
|
|
||||||
kits.add(resultSet.getString("KitName"));
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return kits;
|
|
||||||
}
|
|
||||||
public void saveKit(String name, String data) throws ExecutionException, InterruptedException {
|
|
||||||
System.out.println("Adding Kit");
|
|
||||||
System.out.println("Kit Meta:/n " + data);
|
|
||||||
System.out.println(data);
|
|
||||||
// this.executeUpdate("DELETE FROM kit WHERE KitName = '" + name + "'");
|
|
||||||
this.executeUpdate("INSERT INTO kit (KitName, KitData) VALUES ('" + name + "','" + data + "');");
|
|
||||||
System.out.println("erfolgreich.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user