SteamWar/SpigotCore
Archiviert
13
0

Extend PersonalKit

Dieser Commit ist enthalten in:
Chaoscaot 2021-01-07 15:25:52 +01:00
Ursprung 753611dd8d
Commit e32c80af3f

Datei anzeigen

@ -25,11 +25,14 @@ import org.bukkit.inventory.ItemStack;
import java.io.StringReader; import java.io.StringReader;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class PersonalKit { public class PersonalKit {
private final int userID; private final int userID;
private String name;
private final String gamemode; private final String gamemode;
private String inventory; private String inventory;
private String armor; private String armor;
@ -39,30 +42,42 @@ public class PersonalKit {
gamemode = rs.getString("GameMode"); gamemode = rs.getString("GameMode");
inventory = rs.getString("Inventory"); inventory = rs.getString("Inventory");
armor = rs.getString("Armor"); armor = rs.getString("Armor");
name = rs.getString("Name");
} }
public static PersonalKit get(int userID, String gamemode){ public static List<PersonalKit> get(int userID, String gamemode){
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?", userID, gamemode); ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?", userID, gamemode);
try { try {
if(!rs.next()) List<PersonalKit> list = new ArrayList<>();
return null; while (rs.next())
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 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(); YamlConfiguration inventoryConfig = new YamlConfiguration();
inventoryConfig.set("Inventory", inventory); inventoryConfig.set("Inventory", inventory);
YamlConfiguration armorConfig = new YamlConfiguration(); YamlConfiguration armorConfig = new YamlConfiguration();
armorConfig.set("Armor", armor); 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()); userID, gamemode, inventoryConfig.saveToString(), armorConfig.saveToString());
return get(userID, gamemode); return get(userID, gamemode, name);
} }
public ItemStack[] getInventory(){ public ItemStack[] getInventory(){
@ -74,4 +89,37 @@ public class PersonalKit {
YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(armor)); YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(armor));
return Objects.requireNonNull(config.getList("Armor")).toArray(new ItemStack[0]); 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);
}
} }