From c9a82832c04b7ed70349658eea8ee7b0b25c5943 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 1 Nov 2021 12:34:21 +0100 Subject: [PATCH 1/3] Remove deprecated APIs --- .../de/steamwar/core/VersionedCallable.java | 51 ------------------- .../de/steamwar/core/VersionedRunnable.java | 45 ---------------- .../src/de/steamwar/sql/BauweltMember.java | 44 ++-------------- .../src/de/steamwar/sql/CheckedSchematic.java | 28 +--------- .../src/de/steamwar/sql/Fight.java | 22 -------- .../src/de/steamwar/sql/PersonalKit.java | 12 ----- 6 files changed, 7 insertions(+), 195 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java delete mode 100644 SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java deleted file mode 100644 index 0da1b6f..0000000 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * - * This file is a part of the SteamWar software. - * - * Copyright (C) 2020 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * / - */ - -package de.steamwar.core; - -import java.util.concurrent.Callable; - -public class VersionedCallable { - - private Callable callable; - private int minVersion; - - public VersionedCallable(Callable callable, int minVersion) { - this.callable = callable; - this.minVersion = minVersion; - } - - @Deprecated - public static T call(VersionedCallable... versionedCallables) { - for (int i = versionedCallables.length - 1; i >= 0; i--) { - VersionedCallable versionedCallable = versionedCallables[i]; - if (Core.getVersion() >= versionedCallable.minVersion) { - try { - return versionedCallable.callable.call(); - } catch (Exception e) { - throw new RuntimeException("Could not run version dependent code", e); - } - } - } - throw new SecurityException(); - } - -} diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java deleted file mode 100644 index 604169a..0000000 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * - * This file is a part of the SteamWar software. - * - * Copyright (C) 2020 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * / - */ - -package de.steamwar.core; - -public class VersionedRunnable { - - private Runnable runnable; - private int minVersion; - - public VersionedRunnable(Runnable runnable, int minVersion) { - this.runnable = runnable; - this.minVersion = minVersion; - } - - @Deprecated - public static void call(VersionedRunnable... versionedRunnables) { - for (int i = versionedRunnables.length - 1; i >= 0; i--) { - VersionedRunnable versionedRunnable = versionedRunnables[i]; - if (Core.getVersion() >= versionedRunnable.minVersion) { - versionedRunnable.runnable.run(); - return; - } - } - } - -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java index 273f1ce..31a7ed0 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java @@ -30,8 +30,8 @@ public class BauweltMember{ private final int bauweltID; private final int memberID; - private boolean worldEdit; - private boolean world; + private final boolean worldEdit; + private final boolean world; private static final List members = new ArrayList<>(); @@ -39,36 +39,14 @@ public class BauweltMember{ members.clear(); } - private BauweltMember(int ownerID, int memberID, boolean worldEdit, boolean world, boolean updateDB){ + private BauweltMember(int ownerID, int memberID, boolean worldEdit, boolean world) { bauweltID = ownerID; this.memberID = memberID; this.worldEdit = worldEdit; this.world = world; - if(updateDB) - updateDB(); members.add(this); } - public BauweltMember(int ownerID, int memberID, boolean worldEdit, boolean world){ - this(ownerID, memberID, worldEdit, world, true); - } - - public BauweltMember(UUID ownerID, UUID memberID, boolean worldEdit, boolean world){ - 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); - } - public static BauweltMember getBauMember(UUID ownerID, UUID memberID){ return getBauMember(SteamwarUser.get(ownerID).getId(), SteamwarUser.get(memberID).getId()); } @@ -80,7 +58,7 @@ public class BauweltMember{ return getMember.select(rs -> { if(!rs.next()) return null; - return new BauweltMember(ownerID, memberID, rs.getBoolean("WorldEdit"), rs.getBoolean("World"), false); + return new BauweltMember(ownerID, memberID, rs.getBoolean("WorldEdit"), rs.getBoolean("World")); }, ownerID, memberID); } @@ -92,7 +70,7 @@ public class BauweltMember{ 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)); + members.add(new BauweltMember(bauweltID, rs.getInt("MemberID"), rs.getBoolean("WorldEdit"), rs.getBoolean("World"))); return members; }, bauweltID); } @@ -109,19 +87,7 @@ public class BauweltMember{ return worldEdit; } - @Deprecated - public void setWorldEdit(boolean worldEdit) { - this.worldEdit = worldEdit; - updateDB(); - } - public boolean isWorld() { 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 db8df93..e3f31cc 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -36,32 +36,13 @@ public class CheckedSchematic { private final Timestamp endTime; private final String declineReason; - private CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ + private CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ this.schemName = schemName; this.schemOwner = schemOwner; this.validator = validator; this.startTime = startTime; this.endTime = endTime; this.declineReason = declineReason; - if(insertDB) - 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 (?, ?, ?, ?, ?, ?)", - schemName, schemOwner, validator, startTime, endTime, declineReason); } public static List getLastDeclined(UUID schemOwner){ @@ -72,16 +53,11 @@ public class CheckedSchematic { 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)); + history.add(new CheckedSchematic(rs.getString("SchemName"), schemOwner, rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"))); return history; }, schemOwner); } - @Deprecated - public void remove() { - SQL.update("DELETE FROM CheckedSchematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); - } - public String getSchemName() { return schemName; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java index aa3b54b..584d17e 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -20,8 +20,6 @@ package de.steamwar.sql; import java.io.InputStream; -import java.sql.Blob; -import java.sql.SQLException; import java.sql.Timestamp; import java.util.function.Consumer; @@ -45,15 +43,6 @@ public class Fight { }); } - @Deprecated - public static InputStream getReplay(int fightID) { - return getReplay.select(rs -> { - rs.next(); - Blob replay = rs.getBlob("Replay"); - return replay.getBinaryStream(); - }, fightID); - } - public static void getReplay(int fightID, Consumer reader) { getReplay.select(rs -> { rs.next(); @@ -62,17 +51,6 @@ public class Fight { }, fightID); } - @Deprecated - public static void setReplay(int fightID, byte[] data) { - Blob blob = SQL.blob(); - try { - blob.setBytes(1, data); - } catch (SQLException e) { - throw new SecurityException(e); - } - setReplay.update(blob, fightID); - } - public static void setReplay(int fightID, InputStream data) { setReplay.update(data, fightID); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java index f66d07b..b4f0f41 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -90,18 +90,6 @@ public class PersonalKit { }, 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 { - if(!set.next()) - return true; - return set.getInt("Count") > 0; - } catch (SQLException e) { - throw new SecurityException("Failed loading personal kit", e); - } - } - public ItemStack[] getInventory(){ YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(inventory)); return Objects.requireNonNull(config.getList("Inventory")).toArray(new ItemStack[0]); From cbc26862354d9803d548bec4c9942e7857b8e6c7 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 21 Nov 2021 08:12:22 +0100 Subject: [PATCH 2/3] Fix merge + remove direct sql --- .../src/de/steamwar/core/Core.java | 4 +- .../src/de/steamwar/core/ErrorHandler.java | 4 +- .../src/de/steamwar/sql/BauweltMember.java | 4 +- .../src/de/steamwar/sql/CheckedSchematic.java | 8 +- SpigotCore_Main/src/de/steamwar/sql/Elo.java | 4 +- .../src/de/steamwar/sql/Event.java | 2 +- .../src/de/steamwar/sql/EventFight.java | 6 +- .../src/de/steamwar/sql/Fight.java | 8 +- .../src/de/steamwar/sql/FightPlayer.java | 2 +- .../src/de/steamwar/sql/NodeDownload.java | 2 +- .../src/de/steamwar/sql/NodeMember.java | 10 +- .../src/de/steamwar/sql/PersonalKit.java | 13 +- SpigotCore_Main/src/de/steamwar/sql/SQL.java | 201 ------------------ .../src/de/steamwar/sql/SWException.java | 2 +- .../src/de/steamwar/sql/Schematic.java | 2 +- .../src/de/steamwar/sql/SchematicNode.java | 67 +++--- .../src/de/steamwar/sql/Statement.java | 159 ++++++++++++++ .../src/de/steamwar/sql/SteamwarUser.java | 6 +- SpigotCore_Main/src/de/steamwar/sql/Team.java | 4 +- .../src/de/steamwar/sql/TeamTeilnahme.java | 6 +- .../src/de/steamwar/sql/UserConfig.java | 6 +- 21 files changed, 236 insertions(+), 284 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/SQL.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/Statement.java diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 4c28502..0dcb0c1 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -25,7 +25,7 @@ import de.steamwar.core.events.ChattingEvent; import de.steamwar.core.events.ChunkListener; import de.steamwar.core.events.PlayerJoinedEvent; import de.steamwar.core.events.WorldLoadEvent; -import de.steamwar.sql.SQL; +import de.steamwar.sql.Statement; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -85,7 +85,7 @@ public class Core extends JavaPlugin{ public void onDisable() { ChunkListener.protocol.close(); errorHandler.unregister(); - SQL.close(); + Statement.close(); } public static Core getInstance() { diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java index 051efac..7853144 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorHandler.java @@ -19,8 +19,8 @@ package de.steamwar.core; -import de.steamwar.sql.SQL; import de.steamwar.sql.SWException; +import de.steamwar.sql.Statement; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -83,7 +83,7 @@ public class ErrorHandler extends Handler { if(stacktrace.contains("POI data mismatch")) return; - if(!SQL.connectionStable()) + if(!Statement.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 31a7ed0..305aac5 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java @@ -25,8 +25,8 @@ 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 static final Statement getMember = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?"); + private static final Statement getMembers = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?"); private final int bauweltID; private final int memberID; diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index d80b518..e130c70 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -26,10 +26,8 @@ import java.util.UUID; public class CheckedSchematic { - private static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC"); - private static final SQL.Statement nodeHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); - private static final SQL.Statement insert = new SQL.Statement("INSERT INTO CheckedSchematic (NodeId, NodeName, NodeOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?, ?)"); - private static final SQL.Statement setNodeNull = new SQL.Statement("UPDATE CheckedSchematic SET NodeId = NULL WHERE NodeId = ?"); + private static final Statement checkHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC"); + private static final Statement nodeHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); private final Integer node; private final int validator; @@ -57,7 +55,7 @@ public class CheckedSchematic { Timestamp startTime = rs.getTimestamp("StartTime"); Timestamp endTime = rs.getTimestamp("EndTime"); String declineReason = rs.getString("DeclineReason"); - lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason, false)); + lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason)); } return lastDeclined; }, node); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java index f077199..6fa6504 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Elo.java @@ -22,8 +22,8 @@ package de.steamwar.sql; 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)"); + private static final Statement get = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?"); + private static final Statement set = new Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); public static int getElo(int userId, String gameMode){ return get.select(rs -> { diff --git a/SpigotCore_Main/src/de/steamwar/sql/Event.java b/SpigotCore_Main/src/de/steamwar/sql/Event.java index a447f3c..12ab5dd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Event.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Event.java @@ -25,7 +25,7 @@ import java.sql.Timestamp; public class Event { - private static final SQL.Statement get = new SQL.Statement("SELECT * FROM Event WHERE EventID = ?"); + private static final Statement get = new Statement("SELECT * FROM Event WHERE EventID = ?"); private final int eventID; private final String eventName; diff --git a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java index 0487855..684cd7a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java @@ -24,9 +24,9 @@ import java.sql.SQLException; public class EventFight { - 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 static final Statement get = new Statement("SELECT * FROM EventFight WHERE FightID = ?"); + private static final Statement setResult = new Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?"); + private static final Statement setFight = new Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?"); private final int eventID; private final int fightID; diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java index 584d17e..3f9e2cd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -26,10 +26,10 @@ import java.util.function.Consumer; 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 = ?"); + private static final Statement create = new Statement("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + private static final Statement lastId = new Statement("SELECT LAST_INSERT_ID() AS FightID"); + private static final Statement getReplay = new Statement("SELECT Replay FROM Fight WHERE FightID = ?"); + private static final Statement setReplay = new 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); diff --git a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java index 69e6399..4814560 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java +++ b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java @@ -22,7 +22,7 @@ package de.steamwar.sql; public class FightPlayer { private FightPlayer(){} - private static final SQL.Statement create = new SQL.Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)"); + private static final Statement create = new Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)"); 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/NodeDownload.java b/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java index acc7a11..b19348d 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java @@ -27,7 +27,7 @@ import java.time.Instant; public class NodeDownload { - private static final SQL.Statement createLink = new SQL.Statement("INSERT INTO NodeDownload (NodeId, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)"); + private static final Statement createLink = new Statement("INSERT INTO NodeDownload (NodeId, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)"); private static final String BASE = "https://steamwar.de/download.php?schem="; diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java index f2f1d90..cca8bd3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java @@ -26,11 +26,11 @@ import java.util.Set; public class NodeMember { - private static final SQL.Statement getNodeMember = new SQL.Statement("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?"); - private static final SQL.Statement getNodeMembers = new SQL.Statement("SELECT * FROM NodeMember WHERE NodeId = ?"); - private static final SQL.Statement getSchematics = new SQL.Statement("SELECT * FROM NodeMember WHERE UserId = ?"); - private static final SQL.Statement createNodeMember = new SQL.Statement("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)"); - private static final SQL.Statement deleteNodeMember = new SQL.Statement("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?"); + private static final Statement getNodeMember = new Statement("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?"); + private static final Statement getNodeMembers = new Statement("SELECT * FROM NodeMember WHERE NodeId = ?"); + private static final Statement getSchematics = new Statement("SELECT * FROM NodeMember WHERE UserId = ?"); + private static final Statement createNodeMember = new Statement("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)"); + private static final Statement deleteNodeMember = new Statement("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?"); public static NodeMember getNodeMember(int node, int member) { return getNodeMember.select(rs -> { diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java index b4f0f41..7e9663a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -31,11 +31,11 @@ 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 static final Statement getKits = new Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?"); + private static final Statement getKit = new Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?"); + private static final Statement getKitInUse = new Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = 1"); + private static final Statement delete = new Statement("DELETE FROM `PersonalKit` WHERE UserID = ? AND GameMode = ? AND Name = ?"); + private static final Statement update = new 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; @@ -74,8 +74,7 @@ public class PersonalKit { 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)); + update.update(userID, gamemode, name, saveInvConfig("Inventory", inventory), saveInvConfig("Armor", armor), true); return get(userID, gamemode, name); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQL.java b/SpigotCore_Main/src/de/steamwar/sql/SQL.java deleted file mode 100644 index fc8c7a2..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/SQL.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.sql; - -import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException; -import de.steamwar.core.Core; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.sql.*; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; - - -public class SQL { - private SQL(){} - - private static Connection con; - private static final String URL; - private static final String USER; - private static final String PASSWORD; - - static { - File file = new File(Core.getInstance().getDataFolder(), "MySQL.yml"); - YamlConfiguration config = YamlConfiguration.loadConfiguration(file); - - if(!file.exists()) - throw new SecurityException("SQL-ConfigFile not found!"); - - URL = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE"); - USER = config.getString("USER"); - PASSWORD = config.getString("PASSWORD"); - - connect(); - } - - private static void connect() { - try { - con = DriverManager.getConnection(URL + "?useServerPrepStmts=true", USER, PASSWORD); - } catch (SQLException e) { - throw new SecurityException("Could not start SQL connection", e); - } - } - - public static void close() { - for (Statement statement : Statement.statements) { - try { - statement.st.close(); - } catch (SQLException e) { - Core.getInstance().getLogger().log(Level.INFO, "Could not close statement", e); - } - } - - try { - con.close(); - } catch (SQLException e) { - Core.getInstance().getLogger().log(Level.INFO, "Could not close SQL connection", e); - } - } - - private static void reset() { - close(); - connect(); - - try { - for (Statement statement : Statement.statements) { - statement.init(); - } - } catch (SQLException ex) { - throw new SecurityException("Could not reprepare SQL Statements", ex); - } - } - - public static boolean connectionStable() { - try { - return !con.isClosed(); - } catch (SQLException e) { - return false; - } - } - - @Deprecated - static void update(String qry, Object... objects) { - try { - prepare(qry, objects).executeUpdate(); - } catch (SQLException e) { - reset(); - throw new SecurityException("Could not execute SQL statement", e); - } - } - - @Deprecated - static ResultSet select(String qry, Object... objects) { - try { - return prepare(qry, objects).executeQuery(); - } catch (SQLException e) { - reset(); - throw new SecurityException("Could not execute SQL statement", e); - } - } - - @Deprecated - static Blob blob() { - try { - return con.createBlob(); - } catch (SQLException e) { - reset(); - throw new SecurityException("Could not execute SQL statement", e); - } - } - - @Deprecated - private static PreparedStatement prepare(String qry, Object... objects) throws SQLException { - PreparedStatement st = con.prepareStatement(qry); - for(int i = 0; i < objects.length; i++){ - st.setObject(i+1, objects[i]); - } - return st; - } - - public static class Statement { - private static final List statements = new ArrayList<>(); - - private final String sql; - private PreparedStatement st; - - Statement(String sql) { - this.sql = sql; - statements.add(this); - try { - init(); - } catch (SQLException e) { - throw new SecurityException("Could not init statement", e); - } - } - - private synchronized void init() throws SQLException { - st = con.prepareStatement(sql); - } - - T select(ResultSetUser user, Object... objects) { - return prepare(() -> { - ResultSet rs = st.executeQuery(); - T result = user.use(rs); - rs.close(); - return result; - }, objects); - } - - void update(Object... objects) { - prepare(st::executeUpdate, objects); - } - - private synchronized T prepare(SQLRunnable runnable, Object... objects) { - try { - try { - setObjects(objects); - return runnable.run(); - } catch (CommunicationsException e) { - reset(); - setObjects(objects); - return runnable.run(); - } - } catch (SQLException e) { - throw new SecurityException("Could not execute SQL statement", e); - } - } - - private void setObjects(Object... objects) throws SQLException { - for (int i = 0; i < objects.length; i++) { - st.setObject(i + 1, objects[i]); - } - } - - interface ResultSetUser { - T use(ResultSet rs) throws SQLException; - } - - private interface SQLRunnable { - T run() throws SQLException; - } - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index e1b4cb3..99c34ac 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -25,7 +25,7 @@ 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 (?, ?, ?)"); + private static final Statement insert = new Statement("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)"); public static void log(String message, String stacktrace){ message += "\n"; diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index efe4b66..4b82884 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -33,7 +33,7 @@ import java.util.UUID; @Deprecated public class Schematic { - private static final SQL.Statement getSchemsOfType = new SQL.Statement("SELECT DISTINCT s.SchemID, s.SchemName, s.SchemOwner, s.Item, s.SchemType, s.Rank, s.SchemFormat FROM Schematic s LEFT JOIN SchemMember sm ON sm.SchemName = s.SchemName AND sm.SchemOwner = s.SchemOwner WHERE s.SchemType = ? AND (s.SchemOwner = ? OR sm.Member = ?) ORDER BY s.SchemName"); + private static final Statement getSchemsOfType = new Statement("SELECT DISTINCT s.SchemID, s.SchemName, s.SchemOwner, s.Item, s.SchemType, s.Rank, s.SchemFormat FROM Schematic s LEFT JOIN SchemMember sm ON sm.SchemName = s.SchemName AND sm.SchemOwner = s.SchemOwner WHERE s.SchemType = ? AND (s.SchemOwner = ? OR sm.Member = ?) ORDER BY s.SchemName"); private final SchematicNode node; diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 70a4b8a..fa33be0 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -21,8 +21,6 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.core.Core; -import de.steamwar.core.VersionedCallable; -import de.steamwar.core.VersionedRunnable; import de.steamwar.core.WorldEditWrapper; import org.bukkit.entity.Player; @@ -35,38 +33,37 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.time.Instant; import java.util.*; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; import java.util.zip.GZIPInputStream; public class SchematicNode { - private static final SQL.Statement createNode = new SQL.Statement("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)"); - private static final SQL.Statement getSchematicNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL"); - private static final SQL.Statement getSchematicNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?"); - private static final SQL.Statement getSchematicsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); - private static final SQL.Statement getSchematicsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?"); - private static final SQL.Statement getDirsInNode_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL AND NodeType is NULL"); - private static final SQL.Statement getDirsInNode = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? AND NodeType is NULL"); - private static final SQL.Statement getSchematicDirectory_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); - private static final SQL.Statement getSchematicDirectory = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); - private static final SQL.Statement getSchematicNodeO_Null = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); - private static final SQL.Statement getSchematicNodeO = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); - private static final SQL.Statement getSchematicNodeId = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?"); - private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); - private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); - private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND ((s.NodeOwner = ? AND s.ParentNode IS NULL) OR NOT s.NodeOwner = ?) GROUP BY s.NodeId ORDER BY s.NodeName"); - private static final SQL.Statement getAccessibleByUserByTypeInNode = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?"); - private static final SQL.Statement getAccessibleByUserByTypeInNode_Null = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null"); - private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); - private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN"); - private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); - private static final SQL.Statement getAllParentsOfNode = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN"); - private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); - private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); - private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); - private static final SQL.Statement selSchemData = new SQL.Statement("SELECT NodeData FROM SchematicNode WHERE NodeId = ?"); - private static final SQL.Statement deleteNode = new SQL.Statement("DELETE FROM SchematicNode WHERE NodeId = ?"); + private static final Statement createNode = new Statement("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)"); + private static final Statement getSchematicNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicsInNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL"); + private static final Statement getSchematicsInNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ?"); + private static final Statement getDirsInNode_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode is NULL AND NodeType is NULL"); + private static final Statement getDirsInNode = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE ParentNode = ? AND NodeType is NULL"); + private static final Statement getSchematicDirectory_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicDirectory = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicNodeO_Null = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL"); + private static final Statement getSchematicNodeO = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?"); + private static final Statement getSchematicNodeId = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ?"); + private static final Statement getAllSchemsOfTypeOwner = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); + private static final Statement getAllSchemsOfType = new Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); + private static final Statement getAccessibleByUser = new Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND ((s.NodeOwner = ? AND s.ParentNode IS NULL) OR NOT s.NodeOwner = ?) GROUP BY s.NodeId ORDER BY s.NodeName"); + private static final Statement getAccessibleByUserByTypeInNode = new Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?"); + private static final Statement getAccessibleByUserByTypeInNode_Null = new Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null"); + private static final Statement getAccessibleByUserByType = new Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?"); + private static final Statement getAllSchematicsAccessibleByUser = new Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN"); + private static final Statement isSchematicAccessibleForUser = new Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN LEFT Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1"); + private static final Statement getAllParentsOfNode = new Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? UNION SELECT SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT * FROM RSN"); + private static final Statement countNodes = new Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); + private static final Statement updateDB = new Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); + private static final Statement updateDatabase = new Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); + private static final Statement selSchemData = new Statement("SELECT NodeData FROM SchematicNode WHERE NodeId = ?"); + private static final Statement deleteNode = new Statement("DELETE FROM SchematicNode WHERE NodeId = ?"); public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); @@ -118,7 +115,7 @@ public class SchematicNode { if (parent != null && parent == 0) { parent = null; } - SQL.Statement.ResultSetUser user = rs -> { + Statement.ResultSetUser user = rs -> { while (rs.next()) { SchematicNode node = new SchematicNode(rs); return node; @@ -135,7 +132,7 @@ public class SchematicNode { public static List getSchematicNodeInNode(Integer parent) { if(parent != null && parent == 0) parent = null; - SQL.Statement.ResultSetUser> user = rs -> { + Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); while (rs.next()) nodes.add(new SchematicNode(rs)); @@ -151,7 +148,7 @@ public class SchematicNode { public static List getSchematicDirectoryInNode(Integer parent) { if(parent != null && parent == 0) parent = null; - SQL.Statement.ResultSetUser> user = rs -> { + Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); while (rs.next()) nodes.add(new SchematicNode(rs)); @@ -167,7 +164,7 @@ public class SchematicNode { public static SchematicNode getSchematicDirectory(String name, Integer parent) { if(parent != null && parent == 0) parent = null; - SQL.Statement.ResultSetUser user = rs -> { + Statement.ResultSetUser user = rs -> { while (rs.next()) { SchematicNode node = new SchematicNode(rs); if(node.isDir()) @@ -186,7 +183,7 @@ public class SchematicNode { public static SchematicNode getSchematicNode(String name, Integer parent) { if(parent != null && parent == 0) parent = null; - SQL.Statement.ResultSetUser user = rs -> { + Statement.ResultSetUser user = rs -> { while (rs.next()) { return new SchematicNode(rs); } @@ -208,7 +205,7 @@ public class SchematicNode { } public static List getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) { - SQL.Statement.ResultSetUser> user = rs -> { + Statement.ResultSetUser> user = rs -> { List nodes = new ArrayList<>(); while (rs.next()) { nodes.add(new SchematicNode(rs)); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Statement.java b/SpigotCore_Main/src/de/steamwar/sql/Statement.java new file mode 100644 index 0000000..48719e6 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Statement.java @@ -0,0 +1,159 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package de.steamwar.sql; + +import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException; +import de.steamwar.core.Core; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +public class Statement { + + private static final String URL; + private static final String USER; + private static final String PASSWORD; + + private static Connection con; + + private static final List statements = new ArrayList<>(); + + static { + File file = new File(Core.getInstance().getDataFolder(), "MySQL.yml"); + YamlConfiguration config = YamlConfiguration.loadConfiguration(file); + + if(!file.exists()) + throw new SecurityException("SQL-ConfigFile not found!"); + + URL = "jdbc:mysql://" + config.getString("HOST") + ":" + config.getString("PORT") + "/" + config.getString("DATABASE"); + USER = config.getString("USER"); + PASSWORD = config.getString("PASSWORD"); + + Statement.connect(); + } + + private static void connect() { + try { + con = DriverManager.getConnection(URL + "?useServerPrepStmts=true", USER, PASSWORD); + } catch (SQLException e) { + throw new SecurityException("Could not start SQL connection", e); + } + } + + public static void close() { + for (Statement statement : statements) { + try { + statement.st.close(); + } catch (SQLException e) { + Core.getInstance().getLogger().log(Level.INFO, "Could not close statement", e); + } + } + + try { + con.close(); + } catch (SQLException e) { + Core.getInstance().getLogger().log(Level.INFO, "Could not close SQL connection", e); + } + } + + private static void reset() { + close(); + connect(); + + try { + for (Statement statement : statements) { + statement.init(); + } + } catch (SQLException ex) { + throw new SecurityException("Could not reprepare SQL Statements", ex); + } + } + + public static boolean connectionStable() { + try { + return !con.isClosed(); + } catch (SQLException e) { + return false; + } + } + + private final String sql; + private PreparedStatement st; + + Statement(String sql) { + this.sql = sql; + try { + init(); + } catch (SQLException e) { + throw new SecurityException("Could not init statement", e); + } + statements.add(this); + } + + private synchronized void init() throws SQLException { + st = con.prepareStatement(sql); + } + + T select(ResultSetUser user, Object... objects) { + return prepare(() -> { + ResultSet rs = st.executeQuery(); + T result = user.use(rs); + rs.close(); + return result; + }, objects); + } + + void update(Object... objects) { + prepare(st::executeUpdate, objects); + } + + private synchronized T prepare(SQLRunnable runnable, Object... objects) { + try { + try { + setObjects(objects); + return runnable.run(); + } catch (CommunicationsException e) { + reset(); + setObjects(objects); + return runnable.run(); + } + } catch (SQLException e) { + throw new SecurityException("Could not execute SQL statement", e); + } + } + + private void setObjects(Object... objects) throws SQLException { + for (int i = 0; i < objects.length; i++) { + st.setObject(i + 1, objects[i]); + } + } + + interface ResultSetUser { + T use(ResultSet rs) throws SQLException; + } + + private interface SQLRunnable { + T run() throws SQLException; + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java index f8f2bcb..a66a875 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java @@ -28,9 +28,9 @@ 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 Statement getId = new Statement("SELECT * FROM UserData WHERE id = ?"); + private static final Statement getUUID = new Statement("SELECT * FROM UserData WHERE UUID = ?"); + private static final Statement getName = new Statement("SELECT * FROM UserData WHERE lower(UserName) = ?"); private static final Map byUUID = new HashMap<>(); private static final Map byName = new HashMap<>(); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Team.java b/SpigotCore_Main/src/de/steamwar/sql/Team.java index 5f45e7c..c12e994 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Team.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Team.java @@ -26,8 +26,8 @@ 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 static final Statement get = new Statement("SELECT * FROM Team WHERE TeamID = ?"); + private static final Statement getMembers = new Statement("SELECT id FROM UserData WHERE Team = ?"); private final int teamId; private final String teamKuerzel; diff --git a/SpigotCore_Main/src/de/steamwar/sql/TeamTeilnahme.java b/SpigotCore_Main/src/de/steamwar/sql/TeamTeilnahme.java index cd350d0..4a3dd5e 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/TeamTeilnahme.java +++ b/SpigotCore_Main/src/de/steamwar/sql/TeamTeilnahme.java @@ -26,9 +26,9 @@ import java.util.Set; public class TeamTeilnahme { private TeamTeilnahme(){} - private static final SQL.Statement byEventTeam = new SQL.Statement("SELECT * FROM TeamTeilnahme WHERE TeamID = ? AND EventID = ?"); - private static final SQL.Statement byEvent = new SQL.Statement("SELECT * FROM TeamTeilnahme WHERE EventID = ?"); - private static final SQL.Statement byTeam = new SQL.Statement("SELECT * FROM TeamTeilnahme WHERE TeamID = ?"); + private static final Statement byEventTeam = new Statement("SELECT * FROM TeamTeilnahme WHERE TeamID = ? AND EventID = ?"); + private static final Statement byEvent = new Statement("SELECT * FROM TeamTeilnahme WHERE EventID = ?"); + private static final Statement byTeam = new Statement("SELECT * FROM TeamTeilnahme WHERE TeamID = ?"); public static boolean nimmtTeil(int teamID, int eventID){ return byEventTeam.select(ResultSet::next, teamID, eventID); diff --git a/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java b/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java index 62a6d08..a47932a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java +++ b/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java @@ -24,9 +24,9 @@ import java.util.UUID; public class 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 = ?"); + private static final Statement get = new Statement("SELECT Value FROM UserConfig WHERE User = ? AND Config = ?"); + private static final Statement set = new Statement("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)"); + private static final Statement delete = new Statement("DELETE FROM UserConfig WHERE User = ? AND Config = ?"); public static String getConfig(UUID player, String config) { return getConfig(SteamwarUser.get(player).getId(), config); From af1b06b9ed2e035f56fb3f61f5dd0ed9addba442 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 8 Jan 2022 08:58:42 +0100 Subject: [PATCH 3/3] Remove further deprecated apis --- .../comms/packets/PrepareSchemPacket.java | 8 - .../src/de/steamwar/inventory/SWListInv.java | 7 - .../de/steamwar/sql/DownloadSchematic.java | 29 --- .../src/de/steamwar/sql/Punishment.java | 4 +- .../src/de/steamwar/sql/Schematic.java | 204 ------------------ .../src/de/steamwar/sql/SchematicMember.java | 96 --------- 6 files changed, 2 insertions(+), 346 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/Schematic.java delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java index 8c0968d..aba2380 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java @@ -21,7 +21,6 @@ package de.steamwar.comms.packets; import com.google.common.io.ByteArrayDataOutput; import de.steamwar.comms.PacketIdManager; -import de.steamwar.sql.Schematic; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import de.steamwar.sql.SteamwarUser; @@ -38,13 +37,6 @@ public class PrepareSchemPacket extends SpigotPacket{ this.schematicType = schematicType; } - @Deprecated - public PrepareSchemPacket(SteamwarUser user, Schematic schematic, SchematicType schematicType) { - this.user = user; - this.schematic = schematic.getNode(); - this.schematicType = schematicType; - } - @Override public int getName() { return PacketIdManager.PREPARE_SCHEM; diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java index d0d9c22..a9671ab 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java @@ -19,7 +19,6 @@ package de.steamwar.inventory; -import de.steamwar.sql.Schematic; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SchematicType; import org.bukkit.Bukkit; @@ -28,7 +27,6 @@ import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import java.util.*; -import java.util.stream.Collectors; public class SWListInv extends SWInventory { @@ -138,11 +136,6 @@ public class SWListInv extends SWInventory { } return schemList; } - - @Deprecated - public static List> getSchemList(SchematicType type, int steamwarUserId){ - return getSchemnodeList(type, steamwarUserId).stream().map(schematicNodeSWListEntry -> new SWListEntry(schematicNodeSWListEntry.getItem(), Schematic.wrap(schematicNodeSWListEntry.getObject()))).collect(Collectors.toList()); - } private boolean sizeBiggerMax(){ return dynamicSize ? elements.size() > 54 : elements.size() > 45; diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java deleted file mode 100644 index 64812f9..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.sql; - -public class DownloadSchematic { - private DownloadSchematic(){} - - @Deprecated - public static String getLink(Schematic schem){ - return NodeDownload.getLink(schem.getNode()); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/Punishment.java b/SpigotCore_Main/src/de/steamwar/sql/Punishment.java index 85ec258..1124272 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Punishment.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Punishment.java @@ -31,8 +31,8 @@ import java.util.function.Consumer; public class Punishment { - private static final SQL.Statement getPunishment = new SQL.Statement("SELECT * FROM Punishments WHERE UserId = ? AND Type = ? ORDER BY PunishmentId DESC LIMIT 1"); - private static final SQL.Statement getPunishments = new SQL.Statement("SELECT * FROM Punishments WHERE PunishmentId IN (SELECT MAX(PunishmentId) FROM Punishments WHERE UserId = ? GROUP BY Type)"); + private static final Statement getPunishment = new Statement("SELECT * FROM Punishments WHERE UserId = ? AND Type = ? ORDER BY PunishmentId DESC LIMIT 1"); + private static final Statement getPunishments = new Statement("SELECT * FROM Punishments WHERE PunishmentId IN (SELECT MAX(PunishmentId) FROM Punishments WHERE UserId = ? GROUP BY Type)"); public static Punishment getPunishmentOfPlayer(int user, PunishmentType type) { return getPunishment.select(rs -> { diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java deleted file mode 100644 index eb16f8c..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ /dev/null @@ -1,204 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -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; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import java.util.stream.Collectors; - -@Deprecated -public class Schematic { - - private static final Statement getSchemsOfType = new Statement("SELECT DISTINCT s.SchemID, s.SchemName, s.SchemOwner, s.Item, s.SchemType, s.Rank, s.SchemFormat FROM Schematic s LEFT JOIN SchemMember sm ON sm.SchemName = s.SchemName AND sm.SchemOwner = s.SchemOwner WHERE s.SchemType = ? AND (s.SchemOwner = ? OR sm.Member = ?) ORDER BY s.SchemName"); - - private final SchematicNode node; - - private Schematic(SchematicNode node) { - this.node = node; - } - - public static Schematic wrap(SchematicNode node) { - return new Schematic(node); - } - - public static void createSchem(String schemName, UUID schemOwner, String item, SchematicType schemType){ - createSchem(schemName, SteamwarUser.get(schemOwner).getId(), item, schemType); - } - - public static void createSchem(String schemName, int schemOwner, String item, SchematicType schemType){ - SchematicNode.createSchematicNode(schemOwner, schemName, null, schemType.toDB(), item); - } - - public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ - return getSchemFromDB(schemName, SteamwarUser.get(schemOwner).getId()); - } - - public static Schematic getSchemFromDB(String schemName, int schemOwner){ - SchematicNode node = SchematicNode.getSchematicNode(schemOwner, schemName, 0); - if(node != null) { - return new Schematic(node); - } else { - Optional n = SchematicNode.getSchematicsAccessibleByUser(schemOwner, 0).stream().filter(node1 -> node1.getName().equals(schemName)).findAny(); - if(n.isPresent()) { - return new Schematic(n.get()); - } - } - return null; - } - - public static Schematic getSchemFromDB(int schemID){ - SchematicNode node = SchematicNode.getSchematicNode(schemID); - if(node != null) { - return new Schematic(node); - } else { - throw new SecurityException("Failed to load Schematics"); - } - } - - public static List getSchemsAccessibleByUser(UUID schemOwner){ - return getSchemsAccessibleByUser(SteamwarUser.get(schemOwner).getId()); - } - - public static List getSchemsAccessibleByUser(int schemOwner){ - List schematics = new ArrayList<>(); - SchematicNode.getSchematicsAccessibleByUser(schemOwner, null) - .forEach(node1 -> { - if (!node1.isDir()) schematics.add(new Schematic(node1)); - }); - return schematics; - } - - public static List getSchemsOfType(UUID schemOwner, SchematicType schemType){ - return getSchemsOfType(SteamwarUser.get(schemOwner).getId(), schemType); - } - - public static List getSchemsOfType(int schemOwner, SchematicType schemType){ - List schematics = new ArrayList<>(); - SchematicNode.getAllAccessibleSchematicsOfType(schemOwner, schemType.toDB()) - .forEach(node1 -> { - if (!node1.isDir()) schematics.add(new Schematic(node1)); - }); - return schematics; - } - - public static List getAllSchemsOfType(SchematicType schemType){ - List schematics = new ArrayList<>(); - SchematicNode.getAllSchematicsOfType(schemType.toDB()) - .forEach(node1 -> schematics.add(new Schematic(node1))); - return schematics; - } - - public int getSchemID() { - return node.getId(); - } - - public String getSchemName() { - return node.getName(); - } - - public int getSchemOwner() { - return node.getOwner(); - } - - public int getRank(){ - return node.getRank(); - } - - public String getItem() { - return node.getItem(); - } - - public void setItem(String item) { - node.setItem(item); - } - - public void setRank(int rank){ - node.setRank(rank); - } - - public SchematicType getSchemType() { - return node.getSchemtype(); - } - - public void setSchemType(SchematicType schemType) { - node.setType(schemType.toDB()); - } - - public boolean availible(){ - return true; - } - - public InputStream schemData() throws IOException { - return node.schemData(); - } - - public static Clipboard clipboardFromStream(InputStream is, boolean schemFormat) { - try { - return WorldEditWrapper.impl.getClipboard(is, schemFormat); - } catch (IOException e) { - throw new SecurityException("Could not read schem", e); - } - } - - public Clipboard load() throws IOException, NoClipboardException { - return clipboardFromStream(schemData(), node.getSchemFormat()); - } - - public void loadToPlayer(Player player) throws IOException, NoClipboardException { - InputStream is = schemData(); - WorldEditWrapper.impl.setPlayerClipboard(player, is, node.getSchemFormat()); - } - - public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, false); - } - - public void saveFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, Core.getVersion() > 12); - } - - public void saveFromBytes(byte[] bytes, boolean newFormat) { - node.saveFromBytes(bytes, newFormat); - } - - private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { - node.saveFromPlayer(player, newFormat); - } - - public void remove(){ - node.delete(); - } - - public SchematicNode getNode() { - return node; - } - - @Deprecated - public static class WrongVersionException extends Exception{} -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java deleted file mode 100644 index e84fa54..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.sql; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.UUID; - -@Deprecated -public class SchematicMember { - - private final NodeMember member; - - private SchematicMember(NodeMember member){ - this.member = member; - } - - public SchematicMember(String schemName, int schemOwner, int schemMember){ - this(NodeMember.createNodeMember(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), schemMember)); - } - - public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ - this(NodeMember.createNodeMember(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), SteamwarUser.get(schemMember).getId())); - } - - public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){ - return getSchemMemberFromDB(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId()); - } - - public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ - NodeMember member = NodeMember.getNodeMember(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID(), schemMember); - if(member == null) { - return null; - } - return new SchematicMember(member); - } - - public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ - Optional nodeMember = NodeMember.getSchematics(schemMember) - .stream().filter(member1 -> SchematicNode.getSchematicNode(member1.getNode()).getName().equalsIgnoreCase(schemName)).findFirst(); - return nodeMember.map(SchematicMember::new).orElse(null); - } - - public static List getSchemMembers(String schemName, UUID schemOwner){ - return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId()); - } - - public static List getSchemMembers(String schemName, int schemOwner){ - List members = new ArrayList<>(); - NodeMember.getNodeMembers(Schematic.getSchemFromDB(schemName, schemOwner).getSchemID()) - .forEach(member1 -> members.add(new SchematicMember(member1))); - return members; - } - - public static List getAccessibleSchems(UUID schemMember){ - return getAccessibleSchems(SteamwarUser.get(schemMember).getId()); - } - - public static List getAccessibleSchems(int schemMember){ - List members = new ArrayList<>(); - NodeMember.getSchematics(schemMember) - .forEach(member1 -> members.add(new SchematicMember(member1))); - return members; - } - - public int getSchemOwner() { - return SchematicNode.getSchematicNode(member.getNode()).getOwner(); - } - - public String getSchemName() { - return SchematicNode.getSchematicNode(member.getNode()).getName(); - } - - public int getMember() { - return member.getMember(); - } - - public void remove(){ - member.delete(); - } -}