From b9c00c50073aaadb34a4518bfc8142434e278c18 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 10 Oct 2021 08:30:30 +0200 Subject: [PATCH 1/5] Add ignore line --- SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java index 0bc0815..9750562 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java @@ -129,6 +129,7 @@ public class ErrorHandler extends Handler { startsWith.add("Failed to save player data for "); startsWith.add("Failed to check session lock for world located at"); startsWith.add("Saving oversized chunk "); + startsWith.add("Ignoring plugin channel in incoming"); ignoreStartsWith = Collections.unmodifiableList(startsWith); List contains = new ArrayList<>(); From 768a5388135ac9912b1a05153834fcc915ded812 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 12 Oct 2021 18:04:29 +0200 Subject: [PATCH 2/5] Fix saving on old servers --- SpigotCore_Main/src/de/steamwar/sql/Schematic.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index dbbd123..b364072 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -20,6 +20,7 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; +import de.steamwar.core.Core; import de.steamwar.core.WorldEditWrapper; import org.bukkit.entity.Player; @@ -219,7 +220,7 @@ public class Schematic { } public void saveFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, true); + saveFromPlayer(player, Core.getVersion() > 12); } public void saveFromBytes(byte[] bytes, boolean newFormat) { From 88f957d1df02abf552dfef07a78e6fdeb5cb4ee3 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 20 Oct 2021 17:45:13 +0200 Subject: [PATCH 3/5] Reduce error spam --- SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java index 9750562..e5d5bdf 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java @@ -129,7 +129,8 @@ public class ErrorHandler extends Handler { startsWith.add("Failed to save player data for "); startsWith.add("Failed to check session lock for world located at"); startsWith.add("Saving oversized chunk "); - startsWith.add("Ignoring plugin channel in incoming"); + startsWith.add("Ignoring plugin channel"); + startsWith.add("Ignoring incoming plugin"); ignoreStartsWith = Collections.unmodifiableList(startsWith); List contains = new ArrayList<>(); From 4682f6506322be7ccd30e8095971b9ea86f1036c Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 21 Oct 2021 11:50:56 +0200 Subject: [PATCH 4/5] Prevent shutdown error deadlocks --- SpigotCore_Main/src/de/steamwar/core/Core.java | 7 +++++-- SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index cb51d12..8655496 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -56,6 +56,8 @@ public class Core extends JavaPlugin{ version = 12; } + private ErrorHandler errorHandler; + @Override public void onLoad() { setInstance(this); @@ -63,7 +65,7 @@ public class Core extends JavaPlugin{ @Override public void onEnable() { - new ErrorHandler(); + errorHandler = new ErrorHandler(); Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this); Bukkit.getPluginManager().registerEvents(new ChattingEvent(), this); @@ -80,7 +82,8 @@ public class Core extends JavaPlugin{ } @Override - public void onDisable(){ + public void onDisable() { + errorHandler.unregister(); SQL.close(); } diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java index e5d5bdf..bbc8d50 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java @@ -39,6 +39,10 @@ public class ErrorHandler extends Handler { Logger.getLogger("").addHandler(this); } + void unregister() { + Logger.getLogger("").removeHandler(this); + } + @Override public void publish(LogRecord logRecord) { if(logRecord.getLevel().intValue() < Level.WARNING.intValue()) From 1bc25aac99454a3fb54e6ab3f24b77cb35f54e77 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 22 Oct 2021 15:58:21 +0200 Subject: [PATCH 5/5] New Statement system + ErrorHandler --- .../src/de/steamwar/core/ErrorHandler.java | 4 ++ .../src/de/steamwar/sql/BauweltMember.java | 42 ++++++--------- .../src/de/steamwar/sql/CheckedSchematic.java | 34 +++++------- .../de/steamwar/sql/DownloadSchematic.java | 4 +- SpigotCore_Main/src/de/steamwar/sql/Elo.java | 21 ++++---- .../src/de/steamwar/sql/Event.java | 17 ++---- .../src/de/steamwar/sql/EventFight.java | 32 ++++++----- .../src/de/steamwar/sql/Fight.java | 31 +++++------ .../src/de/steamwar/sql/FightPlayer.java | 11 ++-- .../src/de/steamwar/sql/PersonalKit.java | 54 ++++++++----------- SpigotCore_Main/src/de/steamwar/sql/SQL.java | 51 ++++++++---------- .../src/de/steamwar/sql/SWException.java | 4 +- .../src/de/steamwar/sql/SteamwarUser.java | 31 ++++++----- SpigotCore_Main/src/de/steamwar/sql/Team.java | 29 +++++----- .../src/de/steamwar/sql/UserConfig.java | 27 ++++------ 15 files changed, 169 insertions(+), 223 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java index bbc8d50..b71cb1f 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java @@ -19,6 +19,7 @@ package de.steamwar.core; +import de.steamwar.sql.SQL; import de.steamwar.sql.SWException; import java.io.ByteArrayOutputStream; @@ -80,6 +81,9 @@ public class ErrorHandler extends Handler { if(stacktrace.contains("POI data mismatch")) return; + if(!SQL.connectionStable()) + return; + SWException.log(message, stacktrace); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java index 622b545..273f1ce 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java @@ -19,14 +19,15 @@ package de.steamwar.sql; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; import java.util.UUID; public class BauweltMember{ + + private static final SQL.Statement getMember = new SQL.Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?"); + private static final SQL.Statement getMembers = new SQL.Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?"); + private final int bauweltID; private final int memberID; private boolean worldEdit; @@ -56,11 +57,13 @@ public class BauweltMember{ this(SteamwarUser.get(ownerID).getId(), SteamwarUser.get(memberID).getId(), worldEdit, world, true); } + @Deprecated public void remove(){ SQL.update("DELETE FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", bauweltID, memberID); members.remove(this); } + @Deprecated private void updateDB(){ SQL.update("INSERT INTO BauweltMember (BauweltID, MemberID, WorldEdit, World) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE WorldEdit = VALUES(WorldEdit), World = VALUES(World)", bauweltID, memberID, worldEdit, world); @@ -74,17 +77,11 @@ public class BauweltMember{ for(BauweltMember member : members) if(member.memberID == memberID) return member; - ResultSet member = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", ownerID, memberID); - try { - if(member == null || !member.next()){ + return getMember.select(rs -> { + if(!rs.next()) return null; - } - boolean worldEdit = member.getBoolean("WorldEdit"); - boolean testblock = member.getBoolean("World"); - return new BauweltMember(ownerID, memberID, worldEdit, testblock, false); - } catch (SQLException e) { - throw new SecurityException("Did not get member", e); - } + return new BauweltMember(ownerID, memberID, rs.getBoolean("WorldEdit"), rs.getBoolean("World"), false); + }, ownerID, memberID); } public static List getMembers(UUID bauweltID){ @@ -92,19 +89,12 @@ public class BauweltMember{ } public static List getMembers(int bauweltID){ - try{ - ResultSet memberlist = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = ?", bauweltID); - List members = new LinkedList<>(); - while(memberlist.next()){ - int memberID = memberlist.getInt("MemberID"); - boolean worldEdit = memberlist.getBoolean("WorldEdit"); - boolean testblock = memberlist.getBoolean("World"); - members.add(new BauweltMember(bauweltID, memberID, worldEdit, testblock, false)); - } + return getMembers.select(rs -> { + List members = new ArrayList<>(); + while(rs.next()) + members.add(new BauweltMember(bauweltID, rs.getInt("MemberID"), rs.getBoolean("WorldEdit"), rs.getBoolean("World"), false)); return members; - }catch(SQLException e){ - throw new SecurityException("Did not get members", e); - } + }, bauweltID); } public int getBauweltID() { @@ -119,6 +109,7 @@ public class BauweltMember{ return worldEdit; } + @Deprecated public void setWorldEdit(boolean worldEdit) { this.worldEdit = worldEdit; updateDB(); @@ -128,6 +119,7 @@ public class BauweltMember{ return world; } + @Deprecated public void setWorld(boolean world) { this.world = world; updateDB(); diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index 4f13f10..db8df93 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -19,19 +19,16 @@ package de.steamwar.sql; -import org.bukkit.Bukkit; - -import java.sql.ResultSet; -import java.sql.SQLException; import java.sql.Timestamp; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import java.util.UUID; -import java.util.logging.Level; public class CheckedSchematic { + private static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE SchemOwner = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); + private final String schemName; private final int schemOwner; private final int validator; @@ -50,14 +47,17 @@ public class CheckedSchematic { insertDB(); } + @Deprecated public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ this(schemName, schemOwner, validator, startTime, endTime, declineReason, true); } + @Deprecated public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); } + @Deprecated private void insertDB(){ SQL.update("INSERT INTO CheckedSchematic" + " (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)", @@ -69,23 +69,15 @@ public class CheckedSchematic { } public static List getLastDelined(int schemOwner){ - List lastDeclined = new LinkedList<>(); - try{ - ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE SchemOwner = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC", schemOwner); - while(lastRS.next()){ - String schemName = lastRS.getString("SchemName"); - int validator = lastRS.getInt("Validator"); - Timestamp startTime = lastRS.getTimestamp("StartTime"); - Timestamp endTime = lastRS.getTimestamp("EndTime"); - String declineReason = lastRS.getString("DeclineReason"); - lastDeclined.add(new CheckedSchematic(schemName, schemOwner, validator, startTime, endTime, declineReason, false)); - } - }catch(SQLException e){ - Bukkit.getLogger().log(Level.SEVERE, "getLastDeclined failed", e); - } - return lastDeclined; + return checkHistory.select(rs -> { + List history = new ArrayList<>(); + while(rs.next()) + history.add(new CheckedSchematic(rs.getString("SchemName"), schemOwner, rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false)); + return history; + }, schemOwner); } + @Deprecated public void remove() { SQL.update("DELETE FROM CheckedSchematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java index 4f55754..104f952 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java @@ -28,6 +28,8 @@ import java.time.Instant; public class DownloadSchematic { private DownloadSchematic(){} + private static final SQL.Statement createLink = new SQL.Statement("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)"); + private static final String BASE = "https://steamwar.de/download.php?schem="; public static String getLink(Schematic schem){ @@ -40,7 +42,7 @@ public class DownloadSchematic { digest.reset(); digest.update((Instant.now().toString() + schem.getSchemOwner() + schem.getSchemID()).getBytes()); String hash = BaseEncoding.base16().encode(digest.digest()); - SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getSchemID(), hash); + createLink.update(schem.getSchemID(), hash); return BASE + hash; } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java index 0bcdd7e..b52f215 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Elo.java @@ -19,24 +19,21 @@ package de.steamwar.sql; -import java.sql.ResultSet; -import java.sql.SQLException; - public class Elo { private Elo(){} + private static final SQL.Statement get = new SQL.Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?"); + private static final SQL.Statement set = new SQL.Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); + public static int getElo(int userId, String gameMode){ - ResultSet rs = SQL.select("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?", userId, gameMode); - try{ - if(!rs.next()) - return 1000; - return rs.getInt("Elo"); - }catch(SQLException e){ - throw new SecurityException("Could not get elo", e); - } + return get.select(rs -> { + if(rs.next()) + return rs.getInt("id"); + return 1000; + }, userId, gameMode); } public static void setElo(int userId, String gameMode, int elo){ - SQL.update("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)", userId, gameMode, elo); + set.update(userId, gameMode, elo); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Event.java b/SpigotCore_Main/src/de/steamwar/sql/Event.java index 3b1e2a4..a447f3c 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Event.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Event.java @@ -19,15 +19,14 @@ package de.steamwar.sql; -import org.bukkit.Bukkit; - import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; -import java.util.logging.Level; public class Event { + private static final SQL.Statement get = new SQL.Statement("SELECT * FROM Event WHERE EventID = ?"); + private final int eventID; private final String eventName; private final Timestamp start; @@ -47,16 +46,10 @@ public class Event { } public static Event get(int eventID){ - ResultSet rs = SQL.select("SELECT * FROM Event WHERE EventID = ?", eventID); - try{ - if(!rs.next()) - throw new IllegalArgumentException(); - + return get.select(rs -> { + rs.next(); return new Event(rs); - }catch (SQLException e){ - Bukkit.getLogger().log(Level.SEVERE, "Failed to load Event", e); - throw new SecurityException(); - } + }, eventID); } public int getEventID() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java index 5b5d4f5..0487855 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java @@ -19,19 +19,21 @@ package de.steamwar.sql; -import org.bukkit.Bukkit; - import java.sql.ResultSet; import java.sql.SQLException; -import java.util.logging.Level; public class EventFight { - private int eventID; - private int fightID; - private int teamBlue; - private int teamRed; - private int kampfleiter; - private int ergebnis; + + private static final SQL.Statement get = new SQL.Statement("SELECT * FROM EventFight WHERE FightID = ?"); + private static final SQL.Statement setResult = new SQL.Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?"); + private static final SQL.Statement setFight = new SQL.Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?"); + + private final int eventID; + private final int fightID; + private final int teamBlue; + private final int teamRed; + private final int kampfleiter; + private final int ergebnis; private EventFight(ResultSet rs) throws SQLException{ this.eventID = rs.getInt("EventID"); @@ -43,23 +45,19 @@ public class EventFight { } public static EventFight get(int fightID){ - ResultSet rs = SQL.select("SELECT * FROM EventFight WHERE FightID = " + fightID); - try{ + return get.select(rs -> { rs.next(); return new EventFight(rs); - }catch (SQLException e){ - Bukkit.getLogger().log(Level.SEVERE, "Failed to load EventFight", e); - } - return null; + }, fightID); } public void setErgebnis(int winner){ - SQL.update("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?", winner, fightID); + setResult.update(winner, fightID); } public void setFight(int fight){ //Fight.FightID, not EventFight.FightID - SQL.update("UPDATE EventFight SET Fight = ? WHERE FightID = ?", fight, fightID); + setFight.update(fight, fightID); } public int getTeamBlue() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java index 7604b12..dfc7a97 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -21,42 +21,35 @@ package de.steamwar.sql; import java.io.InputStream; import java.sql.Blob; -import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; public class Fight { private Fight(){} + private static final SQL.Statement create = new SQL.Statement("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + private static final SQL.Statement lastId = new SQL.Statement("SELECT LAST_INSERT_ID() AS FightID"); + private static final SQL.Statement getReplay = new SQL.Statement("SELECT Replay FROM Fight WHERE FightID = ?"); + private static final SQL.Statement setReplay = new SQL.Statement("UPDATE Fight SET Replay = ? WHERE FightID = ?"); + public static int create(String gamemode, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ return create(gamemode, arena, null, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); } public static int create(String gamemode, String server, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ - SQL.update("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", - gamemode, server, arena, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); - ResultSet rs = SQL.select("SELECT LAST_INSERT_ID() AS FightID"); - try{ - if(!rs.next()) - throw new SecurityException("No last insert id"); - + create.update(gamemode, server, arena, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); + return lastId.select(rs -> { + rs.next(); return rs.getInt("FightID"); - } catch (SQLException e) { - throw new SecurityException(e); - } + }); } public static InputStream getReplay(int fightID) { - ResultSet rs = SQL.select("SELECT Replay FROM Fight WHERE FightID = ?", fightID); - try { + return getReplay.select(rs -> { rs.next(); Blob replay = rs.getBlob("Replay"); - if(replay == null) - throw new SecurityException("Replay null"); return replay.getBinaryStream(); - } catch (SQLException e) { - throw new SecurityException(e); - } + }, fightID); } public static void setReplay(int fightID, byte[] data) { @@ -66,6 +59,6 @@ public class Fight { } catch (SQLException e) { throw new SecurityException(e); } - SQL.update("UPDATE Fight SET Replay = ? WHERE FightID = ?", blob, fightID); + setReplay.update(blob, fightID); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java index dae5a73..69e6399 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java +++ b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java @@ -22,14 +22,9 @@ package de.steamwar.sql; public class FightPlayer { private FightPlayer(){} - public static void create(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut){ - SQL.update("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)", - fightID, userID, blue ? 1 : 2, kit, kills, isOut); - } + private static final SQL.Statement create = new SQL.Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)"); - @Deprecated - public static void create(int fightID, int userID, String kit, int kills, boolean isOut){ - SQL.update("INSERT INTO FightPlayer (FightID, UserID, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?)", - fightID, userID, kit, kills, isOut); + public static void create(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut){ + create.update(fightID, userID, blue ? 1 : 2, kit, kills, isOut); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java index 912266a..f66d07b 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -31,6 +31,12 @@ import java.util.Objects; public class PersonalKit { + private static final SQL.Statement getKits = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?"); + private static final SQL.Statement getKit = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?"); + private static final SQL.Statement getKitInUse = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = 1"); + private static final SQL.Statement delete = new SQL.Statement("DELETE FROM `PersonalKit` WHERE UserID = ? AND GameMode = ? AND Name = ?"); + private static final SQL.Statement update = new SQL.Statement("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)"); + private final int userID; private String name; private final String gamemode; @@ -48,26 +54,20 @@ public class PersonalKit { } public static List get(int userID, String gamemode){ - ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?", userID, gamemode); - try { + return getKits.select(rs -> { 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); - } + }, userID, gamemode); } 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); - } + return getKit.select(rs -> { + if(rs.next()) + return new PersonalKit(rs); + return null; + }, userID, gamemode, name); } public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){ @@ -80,24 +80,17 @@ public class PersonalKit { } 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<>(); + return getKitInUse.select(rs -> { + if(!rs.next()) + return null; + PersonalKit kit = new PersonalKit(rs); 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); - } + new PersonalKit(rs).setUse(false); //TODO: Violation of integrity, should not be necessary? + return kit; + }, userID, gamemode); } + @Deprecated 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 { @@ -165,7 +158,7 @@ public class PersonalKit { } public void delete() { - SQL.update("DELETE FROM `PersonalKit` WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name); + delete.update(userID, gamemode, name); } private static String saveInvConfig(String name, ItemStack[] inv) { @@ -176,7 +169,6 @@ public class PersonalKit { } 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); + update.update(userID, gamemode, name, inventory, armor, inUse); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQL.java b/SpigotCore_Main/src/de/steamwar/sql/SQL.java index 65546cd..74e68b9 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQL.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQL.java @@ -53,7 +53,7 @@ public class SQL { private static void connect() { try { - con = DriverManager.getConnection(url + "?autoreconnect=true", user, password); + con = DriverManager.getConnection(url + "?autoReconnect=true&useServerPrepStmts=true", user, password); } catch (SQLException e) { throw new SecurityException("Could not start SQL connection", e); } @@ -71,14 +71,14 @@ public class SQL { try { con.close(); } catch (SQLException e) { - Core.getInstance().getLogger().log(Level.WARNING, "Could not close SQL-Connection", e); + Core.getInstance().getLogger().log(Level.INFO, "Could not close SQL connection", e); } } - private static void reset(SQLException e) { - Core.getInstance().getLogger().log(Level.WARNING, "SQL Exception thrown", e); + private static void reset() { close(); connect(); + try { for (Statement statement : Statement.statements) { statement.init(); @@ -87,17 +87,21 @@ public class SQL { throw new SecurityException("Could not reprepare SQL Statements", ex); } } + + public static boolean connectionStable() { + try { + return !con.isClosed(); + } catch (SQLException e) { + return false; + } + } static void update(String qry, Object... objects) { try { prepare(qry, objects).executeUpdate(); } catch (SQLException e) { - reset(e); - try { - prepare(qry, objects).executeUpdate(); - } catch (SQLException ex) { - throw new SecurityException("Could not perform update", ex); - } + reset(); + throw new SecurityException("Could not execute SQL statement", e); } } @@ -105,12 +109,8 @@ public class SQL { try { return prepare(qry, objects).executeQuery(); } catch (SQLException e) { - reset(e); - try { - return prepare(qry, objects).executeQuery(); - } catch (SQLException ex) { - throw new SecurityException("Could not perform select", ex); - } + reset(); + throw new SecurityException("Could not execute SQL statement", e); } } @@ -118,12 +118,8 @@ public class SQL { try { return con.createBlob(); } catch (SQLException e) { - reset(e); - try { - return con.createBlob(); - } catch (SQLException ex) { - throw new SecurityException("Could not create blob", ex); - } + reset(); + throw new SecurityException("Could not execute SQL statement", e); } } @@ -147,7 +143,7 @@ public class SQL { try { init(); } catch (SQLException e) { - reset(e); + throw new SecurityException("Could not init statement", e); } } @@ -173,13 +169,8 @@ public class SQL { setObjects(objects); return runnable.run(); } catch (SQLException e) { - reset(e); - try { - setObjects(objects); - return runnable.run(); - } catch (SQLException ex) { - throw new SecurityException("Could not execute SQL statement", ex); - } + reset(); + throw new SecurityException("Could not execute SQL statement", e); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index 850c124..e1b4cb3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -25,6 +25,8 @@ import org.bukkit.entity.Player; public class SWException { private SWException(){} + private static final SQL.Statement insert = new SQL.Statement("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)"); + public static void log(String message, String stacktrace){ message += "\n"; for(Player player : Bukkit.getOnlinePlayers()) @@ -36,6 +38,6 @@ public class SWException { else server = Bukkit.getWorlds().get(0).getName(); - SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)", server, message, stacktrace); + insert.update(server, message, stacktrace); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java index 76e8c2a..f8f2bcb 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java @@ -28,6 +28,10 @@ import java.util.*; public class SteamwarUser { + private static final SQL.Statement getId = new SQL.Statement("SELECT * FROM UserData WHERE id = ?"); + private static final SQL.Statement getUUID = new SQL.Statement("SELECT * FROM UserData WHERE UUID = ?"); + private static final SQL.Statement getName = new SQL.Statement("SELECT * FROM UserData WHERE lower(UserName) = ?"); + private static final Map byUUID = new HashMap<>(); private static final Map byName = new HashMap<>(); private static final Map byId = new HashMap<>(); @@ -88,35 +92,34 @@ public class SteamwarUser { return bedrock; } - private static SteamwarUser fromDB(String statement, Object identifier){ - ResultSet rs = SQL.select(statement, identifier); - try { - if(rs.next()) - return new SteamwarUser(rs); - } catch (SQLException e) { - throw new SecurityException("SQL Statement failed to get User", e); - } - return null; - } - public static SteamwarUser get(String userName){ SteamwarUser user = byName.get(userName.toLowerCase()); if(user == null) - user = fromDB("SELECT * FROM UserData WHERE lower(UserName) = ?", userName.toLowerCase()); + user = getName.select(rs -> { + if(rs.next()) + return new SteamwarUser(rs); + return null; + }, userName.toLowerCase()); return user; } public static SteamwarUser get(UUID uuid){ SteamwarUser user = byUUID.get(uuid); if(user == null) - user = fromDB("SELECT * FROM UserData WHERE UUID = ?", uuid.toString()); + user = getUUID.select(rs -> { + rs.next(); + return new SteamwarUser(rs); + }, uuid.toString()); return user; } public static SteamwarUser get(int id) { SteamwarUser user = byId.get(id); if(user == null) - user = fromDB("SELECT * FROM UserData WHERE id = ?", id); + user = getId.select(rs -> { + rs.next(); + return new SteamwarUser(rs); + }, id); return user; } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Team.java b/SpigotCore_Main/src/de/steamwar/sql/Team.java index 7841b23..5f45e7c 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Team.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Team.java @@ -21,10 +21,14 @@ package de.steamwar.sql; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; public class Team { + + private static final SQL.Statement get = new SQL.Statement("SELECT * FROM Team WHERE TeamID = ?"); + private static final SQL.Statement getMembers = new SQL.Statement("SELECT id FROM UserData WHERE Team = ?"); + private final int teamId; private final String teamKuerzel; private final String teamName; @@ -46,14 +50,10 @@ public class Team { public static Team get(int id){ if(id == 0) return pub; - ResultSet rs = SQL.select("SELECT * FROM Team WHERE TeamID = ?", id); - try { - if(!rs.next()) - return null; + return get.select(rs -> { + rs.next(); return new Team(rs); - } catch (SQLException e) { - throw new SecurityException("Could not load team", e); - } + }, id); } public int getTeamId() { @@ -73,14 +73,11 @@ public class Team { } public List getMembers(){ - try{ - ResultSet memberlist = SQL.select("SELECT id FROM UserData WHERE Team = ?", teamId); - List members = new LinkedList<>(); - while(memberlist.next()) - members.add(memberlist.getInt("id")); + return getMembers.select(rs -> { + List members = new ArrayList<>(); + while(rs.next()) + members.add(rs.getInt("id")); return members; - }catch(SQLException e){ - throw new SecurityException("Could not load Teammembers", e); - } + }, teamId); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java b/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java index 7c87302..62a6d08 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java +++ b/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java @@ -19,30 +19,25 @@ package de.steamwar.sql; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.UUID; public class UserConfig { + private UserConfig() {} - private UserConfig() { - - } + private static final SQL.Statement get = new SQL.Statement("SELECT Value FROM UserConfig WHERE User = ? AND Config = ?"); + private static final SQL.Statement set = new SQL.Statement("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)"); + private static final SQL.Statement delete = new SQL.Statement("DELETE FROM UserConfig WHERE User = ? AND Config = ?"); public static String getConfig(UUID player, String config) { return getConfig(SteamwarUser.get(player).getId(), config); } public static String getConfig(int player, String config) { - ResultSet configResult = SQL.select("SELECT * FROM UserConfig WHERE User = ? AND Config = ?", player, config); - try { - if (!configResult.next()) { - return null; - } - return configResult.getString("Value"); - } catch (SQLException e) { - throw new SecurityException(e.getMessage(), e); - } + return get.select(rs -> { + if(rs.next()) + return rs.getString("Value"); + return null; + }, player, config); } public static void updatePlayerConfig(UUID uuid, String config, String value) { @@ -54,7 +49,7 @@ public class UserConfig { removePlayerConfig(id, config); return; } - SQL.update("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)", id, config, value); + set.update(id, config, value); } public static void removePlayerConfig(UUID uuid, String config) { @@ -62,6 +57,6 @@ public class UserConfig { } public static void removePlayerConfig(int id, String config) { - SQL.update("DELETE FROM UserConfig WHERE User = ? AND Config = ?", id, config); + delete.update(id, config); } }