From e32c80af3f977e8ff41434993c249f2b15ab1dc8 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 7 Jan 2021 15:25:52 +0100 Subject: [PATCH] Extend PersonalKit --- .../src/de/steamwar/sql/PersonalKit.java | 64 ++++++++++++++++--- 1 file changed, 56 insertions(+), 8 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java index f839f0d..7169a87 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -25,11 +25,14 @@ import org.bukkit.inventory.ItemStack; import java.io.StringReader; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; public class PersonalKit { private final int userID; + private String name; private final String gamemode; private String inventory; private String armor; @@ -39,30 +42,42 @@ public class PersonalKit { gamemode = rs.getString("GameMode"); inventory = rs.getString("Inventory"); armor = rs.getString("Armor"); + name = rs.getString("Name"); } - public static PersonalKit get(int userID, String gamemode){ + public static List get(int userID, String gamemode){ ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?", userID, gamemode); try { - if(!rs.next()) - return null; - - return new PersonalKit(rs); + List list = new ArrayList<>(); + while (rs.next()) + list.add(new PersonalKit(rs)); + return list; } catch (SQLException e) { throw new SecurityException("Failed loading personal kit", e); } } - public static PersonalKit save(int userID, String gamemode, ItemStack[] inventory, ItemStack[] armor){ + public static PersonalKit get(int userID, String gamemode, String name) { + ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name); + try { + if(!rs.next()) + return null; + return new PersonalKit(rs); + } catch (SQLException e) { + throw new SecurityException("Failed loading personal kit", e); + } + } + + public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){ YamlConfiguration inventoryConfig = new YamlConfiguration(); inventoryConfig.set("Inventory", inventory); YamlConfiguration armorConfig = new YamlConfiguration(); armorConfig.set("Armor", armor); - SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Inventory, Armor) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor)", + SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor)", userID, gamemode, inventoryConfig.saveToString(), armorConfig.saveToString()); - return get(userID, gamemode); + return get(userID, gamemode, name); } public ItemStack[] getInventory(){ @@ -74,4 +89,37 @@ public class PersonalKit { YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(armor)); return Objects.requireNonNull(config.getList("Armor")).toArray(new ItemStack[0]); } + + public int getUserID() { + return userID; + } + + public String getName() { + return name; + } + + public String getGamemode() { + return gamemode; + } + + public void setName(String name) { + this.name = name; + SQL.update("UPDATE PersonalKit SET Name = ?", this.name); + } + + public void setInventory(ItemStack[] inventory) { + YamlConfiguration inventoryConfig = new YamlConfiguration(); + inventoryConfig.set("Inventory", inventory); + + this.inventory = inventoryConfig.saveToString(); + SQL.update("UPDATE PersonalKit SET Inventory = ?", this.inventory); + } + + public void setArmor(ItemStack[] armor) { + YamlConfiguration armorConfig = new YamlConfiguration(); + armorConfig.set("Armor", armor); + + this.armor = armorConfig.saveToString(); + SQL.update("UPDATE PersonalKit SET Armor = ?", this.armor); + } }