SteamWar/SpigotCore
Archiviert
13
0

Remove Multiple Kits with same Name

Dieser Commit ist enthalten in:
Chaoscaot 2021-01-10 16:58:20 +01:00
Ursprung 5266bb6492
Commit d8191836a8

Datei anzeigen

@ -31,7 +31,6 @@ import java.util.Objects;
public class PersonalKit { public class PersonalKit {
private final int kitID;
private final int userID; private final int userID;
private String name; private String name;
private final String gamemode; private final String gamemode;
@ -40,7 +39,6 @@ public class PersonalKit {
private boolean inUse; private boolean inUse;
private PersonalKit(ResultSet rs) throws SQLException { private PersonalKit(ResultSet rs) throws SQLException {
kitID = rs.getInt("KitID");
userID = rs.getInt("UserID"); userID = rs.getInt("UserID");
gamemode = rs.getString("GameMode"); gamemode = rs.getString("GameMode");
inventory = rs.getString("Inventory"); inventory = rs.getString("Inventory");
@ -61,27 +59,21 @@ public class PersonalKit {
} }
} }
public static List<PersonalKit> 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); ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name);
try { try {
List<PersonalKit> list = new ArrayList<>(); if(!rs.next())
while (rs.next()) return null;
list.add(new PersonalKit(rs)); return new PersonalKit(rs);
return list;
} catch (SQLException e) { } catch (SQLException e) {
throw new SecurityException("Failed loading personal kit", e); throw new SecurityException("Failed loading personal kit", e);
} }
} }
public static void create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){ public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){
YamlConfiguration inventoryConfig = new YamlConfiguration(); SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor), Name = VALUES(name)",
inventoryConfig.set("Inventory", inventory); userID, gamemode, name, getInventoryConfig(inventory), getArmorConfig(armor));
return get(userID, gamemode, name);
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 getKitInUse(int userID, String gamemode) { 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(){ public ItemStack[] getInventory(){
YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(inventory)); YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(inventory));
return Objects.requireNonNull(config.getList("Inventory")).toArray(new ItemStack[0]); return Objects.requireNonNull(config.getList("Inventory")).toArray(new ItemStack[0]);
@ -131,31 +134,44 @@ public class PersonalKit {
public void setInUse(boolean inUse) { public void setInUse(boolean inUse) {
this.inUse = inUse; this.inUse = inUse;
SQL.update("UPDATE PersonalKit SET InUse = ? WHERE KitID = ?", this.inUse, kitID); updateDB();
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
SQL.update("UPDATE PersonalKit SET Name = ? WHERE KitID = ?", this.name, kitID); updateDB();
} }
public void setInventory(ItemStack[] inventory) { public void setInventory(ItemStack[] inventory) {
YamlConfiguration inventoryConfig = new YamlConfiguration(); this.inventory = getInventoryConfig(inventory);
inventoryConfig.set("Inventory", inventory); updateDB();
this.inventory = inventoryConfig.saveToString();
SQL.update("UPDATE PersonalKit SET Inventory = ? WHERE KitID = ?", this.inventory, kitID);
} }
public void setArmor(ItemStack[] armor) { public void setArmor(ItemStack[] armor) {
YamlConfiguration armorConfig = new YamlConfiguration(); this.armor = getArmorConfig(armor);
armorConfig.set("Armor", armor); updateDB();
this.armor = armorConfig.saveToString();
SQL.update("UPDATE PersonalKit SET Armor = ? WHERE KitID = ?", this.armor, kitID);
} }
public void delete() { 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);
} }
} }