diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java index 314f246..cb92fcf 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java @@ -29,7 +29,7 @@ public class TPSWatcher { private static final TPSWatcher tps_TenSecond = new TPSWatcher(10000); private long lastTime = System.currentTimeMillis(); - private double tps = 20.0; + private double tps = TICK_DEFAULT; private TPSWatcher(long timeInterval) { Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> { @@ -46,12 +46,20 @@ public class TPSWatcher { return getTPS(TPSType.ONE_SECOND); } + public static double getTPS(double limit) { + return getTPS(TPSType.ONE_SECOND, limit); + } + public static double getTPSUnlimited() { return getTPSUnlimited(TPSType.ONE_SECOND); } public static double getTPS(TPSType tpsType) { - return Math.min(getTPSUnlimited(tpsType), 20.0); + return getTPS(tpsType, TICK_DEFAULT); + } + + public static double getTPS(TPSType tpsType, double limit) { + return Math.min(getTPSUnlimited(tpsType), limit); } public static double getTPSUnlimited(TPSType tpsType) { diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java index c96c3c4..7de1c87 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java @@ -28,11 +28,9 @@ import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class SWInventory implements Listener { @@ -55,9 +53,13 @@ public class SWInventory implements Listener { callbacks.put(-1, c); } + public void setItem(int pos, ItemStack itemStack, InvCallback c){ + inventory.setItem(pos, itemStack); + callbacks.put(pos, c); + } + public void setItem(int pos, SWItem item){ - inventory.setItem(pos, item.getItemStack()); - callbacks.put(pos, item.getCallback()); + setItem(pos, item.getItemStack(), item.getCallback()); } public void setItem(int pos, Material m, String name, InvCallback c){ @@ -91,9 +93,10 @@ public class SWInventory implements Listener { if(!player.equals(e.getWhoClicked())) return; - e.setCancelled(true); - if(callbacks.containsKey(e.getRawSlot()) && callbacks.get(e.getRawSlot()) != null) + if(callbacks.containsKey(e.getRawSlot()) && callbacks.get(e.getRawSlot()) != null) { + e.setCancelled(true); callbacks.get(e.getRawSlot()).clicked(e.getClick()); + } } @EventHandler diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java index f839f0d..912266a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -25,44 +25,88 @@ import org.bukkit.inventory.ItemStack; import java.io.StringReader; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; public class PersonalKit { private final int userID; + private String name; private final String gamemode; private String inventory; private String armor; + private boolean inUse; private PersonalKit(ResultSet rs) throws SQLException { userID = rs.getInt("UserID"); gamemode = rs.getString("GameMode"); inventory = rs.getString("Inventory"); armor = rs.getString("Armor"); + name = rs.getString("Name"); + inUse = rs.getBoolean("InUse"); } - public static PersonalKit get(int userID, String gamemode){ + public static List get(int userID, String gamemode){ ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?", userID, gamemode); + try { + List 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 { 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); + public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){ + if(armor == null) { + armor = new ItemStack[]{null, null, null, null}; + } + 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, saveInvConfig("Inventory", inventory), saveInvConfig("Armor", armor)); + return get(userID, gamemode, name); + } - YamlConfiguration armorConfig = new YamlConfiguration(); - armorConfig.set("Armor", armor); + public static PersonalKit getKitInUse(int userID, String gamemode) { + ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = ?", userID, gamemode, true); + try { + List 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)", - userID, gamemode, inventoryConfig.saveToString(), armorConfig.saveToString()); - return get(userID, gamemode); + 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(){ @@ -74,4 +118,65 @@ public class PersonalKit { YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(armor)); 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 UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name); + } + + 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); + } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index a9e72f0..0bbfb8b 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -67,6 +67,7 @@ public class SWException { reasons.add("Chunk file at ["); reasons.add("Ignoring unknown attribute"); reasons.add("Skipping player strafe phase because no player was found"); + reasons.add("Couldn't save chunk; already in use by another instance of Minecraft?"); ignorereasons = Collections.unmodifiableList(reasons); }