SteamWar/SpigotCore
Archiviert
13
0

Adding personal kits for WarGears o.a.

Dieser Commit ist enthalten in:
Lixfel 2020-01-19 17:52:48 +01:00
Ursprung 92c3a810a6
Commit cf913ae61d

Datei anzeigen

@ -0,0 +1,62 @@
package de.steamwar.sql;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import java.io.StringReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
public class PersonalKit {
private final int userID;
private final String gamemode;
private String inventory;
private String armor;
private PersonalKit(ResultSet rs) throws SQLException {
userID = rs.getInt("UserID");
gamemode = rs.getString("GameMode");
inventory = rs.getString("Inventory");
armor = rs.getString("Armor");
}
public static PersonalKit get(int userID, String gamemode){
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = '" + userID + "' AND GameMode = '" + SQL.disarmString(gamemode) + "'");
try {
if(!rs.next())
return null;
return new PersonalKit(rs);
} catch (SQLException e) {
throw new SecurityException("Failed loading personal kit", e);
}
}
public static PersonalKit save(int userID, String gamemode, 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" +
" ('" + userID + "', '" + gamemode + "', '" + SQL.disarmString(inventoryConfig.saveToString()) + "', '" + SQL.disarmString(armorConfig.saveToString()) + "')" +
" ON DUPLICATE KEY UPDATE" +
" Inventory = VALUES(Inventory), Armor = VALUES(Armor)");
return get(userID, gamemode);
}
public ItemStack[] getInventory(){
YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(inventory));
return Objects.requireNonNull(config.getList("Inventory")).toArray(new ItemStack[0]);
}
public ItemStack[] getArmor(){
YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(armor));
return Objects.requireNonNull(config.getList("Armor")).toArray(new ItemStack[0]);
}
}