Merge pull request 'MultiKits' (#88) from multi-kit into master
Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Commit
f43f24c27a
@ -25,44 +25,85 @@ 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;
|
||||||
|
private boolean inUse;
|
||||||
|
|
||||||
private PersonalKit(ResultSet rs) throws SQLException {
|
private PersonalKit(ResultSet rs) throws SQLException {
|
||||||
userID = rs.getInt("UserID");
|
userID = rs.getInt("UserID");
|
||||||
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");
|
||||||
|
inUse = rs.getBoolean("InUse");
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
List<PersonalKit> list = new ArrayList<>();
|
||||||
|
while (rs.next())
|
||||||
|
list.add(new PersonalKit(rs));
|
||||||
|
return list;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new SecurityException("Failed loading personal kit", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
try {
|
||||||
if(!rs.next())
|
if(!rs.next())
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return new PersonalKit(rs);
|
return new PersonalKit(rs);
|
||||||
} 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 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, saveInvConfig("Inventory", inventory), saveInvConfig("Armor", armor));
|
||||||
|
return get(userID, gamemode, name);
|
||||||
|
}
|
||||||
|
|
||||||
YamlConfiguration armorConfig = new YamlConfiguration();
|
public static PersonalKit getKitInUse(int userID, String gamemode) {
|
||||||
armorConfig.set("Armor", armor);
|
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = ?", userID, gamemode, true);
|
||||||
|
try {
|
||||||
|
List<PersonalKit> list = new ArrayList<>();
|
||||||
|
while (rs.next())
|
||||||
|
list.add(new PersonalKit(rs));
|
||||||
|
if(list.size() > 1) {
|
||||||
|
list.forEach(kit -> {
|
||||||
|
if(list.indexOf(kit) >= 1) kit.setUse(false);
|
||||||
|
});
|
||||||
|
list.removeIf(kit -> !kit.isInUse());
|
||||||
|
}
|
||||||
|
if(list.isEmpty()) return null;
|
||||||
|
return list.get(0);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new SecurityException("Failed loading personal kit", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Inventory, Armor) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor)",
|
public static boolean nameInUse(int userID, String gamemode, String name) {
|
||||||
userID, gamemode, inventoryConfig.saveToString(), armorConfig.saveToString());
|
ResultSet set = SQL.select("SELECT COUNT(*) AS Count FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name);
|
||||||
return get(userID, gamemode);
|
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(){
|
||||||
@ -74,4 +115,65 @@ 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 boolean isInUse() {
|
||||||
|
return inUse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInUse() {
|
||||||
|
PersonalKit kit = getKitInUse(userID, gamemode);
|
||||||
|
if(kit != null)
|
||||||
|
kit.setUse(false);
|
||||||
|
setUse(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setUse(boolean inUse) {
|
||||||
|
this.inUse = inUse;
|
||||||
|
updateDB();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
updateDB();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInventory(ItemStack[] inventory) {
|
||||||
|
this.inventory = saveInvConfig("Inventory", inventory);
|
||||||
|
updateDB();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArmor(ItemStack[] armor) {
|
||||||
|
this.armor = saveInvConfig("Armor", armor);
|
||||||
|
updateDB();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContainer(ItemStack[] inventory, ItemStack[] armor) {
|
||||||
|
this.armor = saveInvConfig("Armor", armor);
|
||||||
|
this.inventory = saveInvConfig("Inventory", inventory);
|
||||||
|
updateDB();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete() {
|
||||||
|
SQL.update("DELETE FROM `PersonalKit` WHERE KitID = ?");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String saveInvConfig(String name, ItemStack[] inv) {
|
||||||
|
YamlConfiguration armorConfig = new YamlConfiguration();
|
||||||
|
armorConfig.set(name, inv);
|
||||||
|
|
||||||
|
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