inital Commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.softwarerat.KitAPi;
|
||||
|
||||
import com.softwarerat.SQL.KitManager;
|
||||
import com.softwarerat.SQL.SQLHandler;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@AllArgsConstructor
|
||||
public class KitAPI {
|
||||
SQLHandler sqlHandler;
|
||||
KitManager kitManager;
|
||||
|
||||
|
||||
|
||||
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 void saveKit(Player player, String s){
|
||||
kitManager.saveKit(player,s);
|
||||
player.sendMessage("Kit wurde erfolgreich gespeichert");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.softwarerat;
|
||||
|
||||
import com.softwarerat.KitAPi.KitAPI;
|
||||
import com.softwarerat.SQL.KitManager;
|
||||
import com.softwarerat.SQL.SQLHandler;
|
||||
import lombok.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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.softwarerat.SQL;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.util.io.BukkitObjectInputStream;
|
||||
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
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;
|
||||
|
||||
public KitManager(SQLHandler asyncSQLHandler) {
|
||||
this.kits = new HashMap<>();
|
||||
this.playerKit = new ConcurrentHashMap<UUID, String>();
|
||||
this.playersWithKits = new ArrayList<>();
|
||||
this.asyncSQLHandler = asyncSQLHandler;
|
||||
|
||||
}
|
||||
//loads the Inventory by the UnHashing Algorithm
|
||||
public ItemStack[] loadKit(String base64) {
|
||||
System.out.println("Loading Kit with data: " + base64);
|
||||
try {
|
||||
return this.itemStackArrayFromBase64(base64);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
//gives The Player the Hashed Inventory
|
||||
public void giveKit(Player player, String base64) {
|
||||
player.getInventory().clear();
|
||||
// String playerKit = this.playerKit.get(player.getUniqueId());
|
||||
System.out.println(playerKit);
|
||||
ItemStack[] kit = loadKit(base64);
|
||||
for (int i = 0; i < kit.length; i++) {
|
||||
ItemStack itemStack = kit[i];
|
||||
if (itemStack != null) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
assert meta != null;
|
||||
meta.setUnbreakable(true);
|
||||
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
|
||||
itemStack.setItemMeta(meta);
|
||||
if (itemStack.getType().equals(Material.LEATHER_HELMET) ||
|
||||
itemStack.getType().equals(Material.CHAINMAIL_HELMET) ||
|
||||
itemStack.getType().equals(Material.IRON_HELMET) ||
|
||||
itemStack.getType().equals(Material.GOLDEN_HELMET) ||
|
||||
itemStack.getType().equals(Material.NETHERITE_HELMET) ||
|
||||
itemStack.getType().equals(Material.DIAMOND_HELMET)) {
|
||||
player.getInventory().setHelmet(itemStack);
|
||||
} else if (itemStack.getType().equals(Material.LEATHER_CHESTPLATE) ||
|
||||
itemStack.getType().equals(Material.CHAINMAIL_CHESTPLATE) ||
|
||||
itemStack.getType().equals(Material.IRON_CHESTPLATE) ||
|
||||
itemStack.getType().equals(Material.NETHERITE_CHESTPLATE) ||
|
||||
itemStack.getType().equals(Material.GOLDEN_CHESTPLATE) ||
|
||||
itemStack.getType().equals(Material.DIAMOND_CHESTPLATE)) {
|
||||
player.getInventory().setChestplate(itemStack);
|
||||
} else if (itemStack.getType().equals(Material.LEATHER_LEGGINGS) ||
|
||||
itemStack.getType().equals(Material.CHAINMAIL_LEGGINGS) ||
|
||||
itemStack.getType().equals(Material.IRON_LEGGINGS) ||
|
||||
itemStack.getType().equals(Material.GOLDEN_LEGGINGS) ||
|
||||
itemStack.getType().equals(Material.NETHERITE_LEGGINGS) ||
|
||||
itemStack.getType().equals(Material.DIAMOND_LEGGINGS)) {
|
||||
player.getInventory().setLeggings(itemStack);
|
||||
} else if (itemStack.getType().equals(Material.LEATHER_BOOTS) ||
|
||||
itemStack.getType().equals(Material.CHAINMAIL_BOOTS) ||
|
||||
itemStack.getType().equals(Material.IRON_BOOTS) ||
|
||||
itemStack.getType().equals(Material.GOLDEN_BOOTS) ||
|
||||
itemStack.getType().equals(Material.NETHERITE_BOOTS) ||
|
||||
itemStack.getType().equals(Material.DIAMOND_BOOTS)) {
|
||||
player.getInventory().setBoots(itemStack);
|
||||
} else {
|
||||
player.getInventory().setItem(i, itemStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.playersWithKits.add(player);
|
||||
}
|
||||
|
||||
public String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
|
||||
// Write the size of the inventory
|
||||
dataOutput.writeInt(items.length);
|
||||
// Save every element in the list
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
dataOutput.writeObject(items[i]);
|
||||
}
|
||||
// Serialize that array
|
||||
dataOutput.close();
|
||||
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Unable to save item stacks.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public ItemStack[] itemStackArrayFromBase64(String data) throws IOException {
|
||||
try {
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||
ItemStack[] items = new ItemStack[dataInput.readInt()];
|
||||
|
||||
// Read the serialized inventory
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
items[i] = (ItemStack) dataInput.readObject();
|
||||
}
|
||||
dataInput.close();
|
||||
return items;
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IOException("Unable to decode class type.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveKit(Player player, String name) {
|
||||
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();
|
||||
}
|
||||
this.kits.put(name, player.getInventory().getContents());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
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