From d8191836a82c5e83621bf63929647d05d464b30a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 10 Jan 2021 16:58:20 +0100 Subject: [PATCH] Remove Multiple Kits with same Name --- .../src/de/steamwar/sql/PersonalKit.java | 74 +++++++++++-------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java index 263850c..c572d14 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -31,7 +31,6 @@ import java.util.Objects; public class PersonalKit { - private final int kitID; private final int userID; private String name; private final String gamemode; @@ -40,7 +39,6 @@ public class PersonalKit { private boolean inUse; private PersonalKit(ResultSet rs) throws SQLException { - kitID = rs.getInt("KitID"); userID = rs.getInt("UserID"); gamemode = rs.getString("GameMode"); inventory = rs.getString("Inventory"); @@ -61,27 +59,21 @@ public class PersonalKit { } } - public static List get(int userID, String gamemode, String name) { + 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 { - List list = new ArrayList<>(); - while (rs.next()) - list.add(new PersonalKit(rs)); - return list; + if(!rs.next()) + return null; + return new PersonalKit(rs); } catch (SQLException e) { throw new SecurityException("Failed loading personal kit", e); } } - public static void 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, Name, Inventory, Armor) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor)", - userID, gamemode, name, inventoryConfig.saveToString(), armorConfig.saveToString()); + public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){ + SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor), Name = VALUES(name)", + userID, gamemode, name, getInventoryConfig(inventory), getArmorConfig(armor)); + return get(userID, gamemode, name); } public static PersonalKit getKitInUse(int userID, String gamemode) { @@ -103,6 +95,17 @@ public class PersonalKit { } } + public static boolean nameInUse(int userID, String gamemode, String name) { + ResultSet set = SQL.select("SELECT COUNT(*) AS Count FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name); + try { + if(!set.next()) + return true; + return set.getInt("Count") > 0; + } catch (SQLException e) { + throw new SecurityException("Failed loading personal kit", e); + } + } + public ItemStack[] getInventory(){ YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(inventory)); return Objects.requireNonNull(config.getList("Inventory")).toArray(new ItemStack[0]); @@ -131,31 +134,44 @@ public class PersonalKit { public void setInUse(boolean inUse) { this.inUse = inUse; - SQL.update("UPDATE PersonalKit SET InUse = ? WHERE KitID = ?", this.inUse, kitID); + updateDB(); } public void setName(String name) { this.name = name; - SQL.update("UPDATE PersonalKit SET Name = ? WHERE KitID = ?", this.name, kitID); + updateDB(); } public void setInventory(ItemStack[] inventory) { - YamlConfiguration inventoryConfig = new YamlConfiguration(); - inventoryConfig.set("Inventory", inventory); - - this.inventory = inventoryConfig.saveToString(); - SQL.update("UPDATE PersonalKit SET Inventory = ? WHERE KitID = ?", this.inventory, kitID); + this.inventory = getInventoryConfig(inventory); + updateDB(); } public void setArmor(ItemStack[] armor) { - YamlConfiguration armorConfig = new YamlConfiguration(); - armorConfig.set("Armor", armor); - - this.armor = armorConfig.saveToString(); - SQL.update("UPDATE PersonalKit SET Armor = ? WHERE KitID = ?", this.armor, kitID); + this.armor = getArmorConfig(armor); + updateDB(); } public void delete() { - SQL.update("DELETE FROM `PersonalKit` WHERE KitID = ?", kitID); + SQL.update("DELETE FROM `PersonalKit` WHERE KitID = ?"); + } + + private static String getInventoryConfig(ItemStack[] inventory) { + YamlConfiguration inventoryConfig = new YamlConfiguration(); + inventoryConfig.set("Inventory", inventory); + + return inventoryConfig.saveToString(); + } + + private static String getArmorConfig(ItemStack[] armor) { + YamlConfiguration armorConfig = new YamlConfiguration(); + armorConfig.set("Armor", armor); + + return armorConfig.saveToString(); + } + + private void updateDB() { + SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor, InUse) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor), Name = VALUES(Name), InUse = VALUES(InUse)", + userID, gamemode, name, inventory, armor, inUse); } }