Remove Multiple Kits with same Name
Dieser Commit ist enthalten in:
Ursprung
5266bb6492
Commit
d8191836a8
@ -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<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);
|
||||
try {
|
||||
List<PersonalKit> 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);
|
||||
}
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren