Migrate KitAPI from SQL to YAML-based storage
This commit is contained in:
@@ -6,38 +6,33 @@
|
||||
|
||||
<groupId>com.softwarerat</groupId>
|
||||
<artifactId>KitAPI</artifactId>
|
||||
<version>1.1-SNAPSHOT</version>
|
||||
<distributionManagement>
|
||||
<version>1.3-SNAPSHOT</version>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>forgejo</id>
|
||||
<name>Forgejo Maven</name>
|
||||
<url>http://heimserver.local:36107/api/packages/minecraft/maven</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>gitlab-maven</id>
|
||||
<url>https://gitlab.softwarerat.com/api/v4/projects/11/packages/maven</url>
|
||||
<id>papermc-repo</id>
|
||||
<url>https://repo.papermc.io/repository/maven-public/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>gitlab-maven</id>
|
||||
<url>https://gitlab.softwarerat.com/api/v4/projects/11/packages/maven</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
<repository>
|
||||
<id>gitea</id>
|
||||
<url>http://heimserver.local:36107/api/packages/minecraft/maven</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<properties>
|
||||
<maven.compiler.source>18</maven.compiler.source>
|
||||
<maven.compiler.target>18</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</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>
|
||||
<dependency>
|
||||
<groupId>de.mint</groupId>
|
||||
<artifactId>AsyncMySQLPoolHandler</artifactId>
|
||||
<version>1.0.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.papermc.paper</groupId>
|
||||
<artifactId>paper-api</artifactId>
|
||||
@@ -47,7 +42,7 @@
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.28</version>
|
||||
<version>1.18.42</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
package com.softwarerat.KitAPi;
|
||||
|
||||
import com.softwarerat.SQL.KitManager;
|
||||
import com.softwarerat.SQL.SQLHandler;
|
||||
import lombok.AllArgsConstructor;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class KitAPI {
|
||||
SQLHandler sqlHandler;
|
||||
KitManager kitManager;
|
||||
|
||||
YamlHandler yamlHandler;
|
||||
KitManager kitManager;
|
||||
|
||||
public void giveKit(Player player, String s) {
|
||||
String b64 = yamlHandler.loadKit(s);
|
||||
kitManager.giveKit(player, b64);
|
||||
player.sendMessage("erfolgreich kit ausgestellt");
|
||||
}
|
||||
|
||||
public void giveKit(Player player, String s){
|
||||
String b64 = sqlHandler.loadKit(s);
|
||||
kitManager.giveKit(player,b64);
|
||||
player.sendMessage("erfolgreich kit ausgestellt");
|
||||
}
|
||||
public ArrayList<String> getKits(){
|
||||
return sqlHandler.getKits();
|
||||
public ArrayList<String> getKits() {
|
||||
return yamlHandler.getKits();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void saveKit(Player player, String s){
|
||||
kitManager.saveKit(player,s);
|
||||
player.sendMessage("Kit wurde erfolgreich gespeichert");
|
||||
}
|
||||
public void saveKit(Player player, String s) {
|
||||
kitManager.saveKit(player, s);
|
||||
player.sendMessage("Kit wurde erfolgreich gespeichert");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-15
@@ -1,4 +1,4 @@
|
||||
package com.softwarerat.SQL;
|
||||
package com.softwarerat.KitAPi;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -16,22 +16,22 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class KitManager {
|
||||
private final HashMap<String, ItemStack[]> kits;
|
||||
private final ConcurrentHashMap<UUID, String> playerKit;
|
||||
private final ArrayList<Player> playersWithKits;
|
||||
private SQLHandler asyncSQLHandler;
|
||||
private YamlHandler yamlHandler;
|
||||
|
||||
public KitManager(SQLHandler asyncSQLHandler) {
|
||||
public KitManager(YamlHandler yamlHandler) {
|
||||
this.kits = new HashMap<>();
|
||||
this.playerKit = new ConcurrentHashMap<UUID, String>();
|
||||
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) {
|
||||
System.out.println("Loading Kit with data: " + base64);
|
||||
try {
|
||||
@@ -40,10 +40,11 @@ public class KitManager {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
//gives The Player the Hashed Inventory
|
||||
|
||||
// gives The Player the Hashed Inventory
|
||||
public void giveKit(Player player, String base64) {
|
||||
player.getInventory().clear();
|
||||
// String playerKit = this.playerKit.get(player.getUniqueId());
|
||||
// String playerKit = this.playerKit.get(player.getUniqueId());
|
||||
System.out.println(playerKit);
|
||||
ItemStack[] kit = loadKit(base64);
|
||||
for (int i = 0; i < kit.length; i++) {
|
||||
@@ -114,7 +115,7 @@ public class KitManager {
|
||||
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||
ItemStack[] items = new ItemStack[dataInput.readInt()];
|
||||
|
||||
// Read the serialized inventory
|
||||
// Read the serialized inventory
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
items[i] = (ItemStack) dataInput.readObject();
|
||||
}
|
||||
@@ -129,12 +130,9 @@ public class KitManager {
|
||||
String kitBase64 = this.itemStackArrayToBase64(player.getInventory().getContents());
|
||||
System.out.println(kitBase64);
|
||||
|
||||
try {
|
||||
System.out.println(kitBase64);
|
||||
asyncSQLHandler.saveKit(name, kitBase64);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(kitBase64);
|
||||
yamlHandler.saveKit(name, kitBase64);
|
||||
|
||||
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;
|
||||
|
||||
import com.softwarerat.KitAPi.KitAPI;
|
||||
import com.softwarerat.SQL.KitManager;
|
||||
import com.softwarerat.SQL.SQLHandler;
|
||||
import com.softwarerat.KitAPi.KitManager;
|
||||
import com.softwarerat.KitAPi.Yaml;
|
||||
import com.softwarerat.KitAPi.YamlHandler;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Main {
|
||||
@Getter
|
||||
KitAPI kitAPI;
|
||||
SQLHandler sqlHandler;
|
||||
KitManager kitManager;
|
||||
public void enableAPI(String host,String user,String password,String Database){
|
||||
kitManager = new KitManager(sqlHandler);
|
||||
sqlHandler= new SQLHandler(kitManager,host,user,password,Database);
|
||||
kitAPI = new KitAPI(sqlHandler,kitManager);
|
||||
}
|
||||
|
||||
Yaml yaml;
|
||||
YamlHandler yamlHandler;
|
||||
KitAPI kitAPI;
|
||||
KitManager kitManager;
|
||||
|
||||
public void enableAPI(String yamlPath, String yamlFileName) {
|
||||
|
||||
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