From 020aac870c6aa9e9420ed9cc7358e5fe13b4df85 Mon Sep 17 00:00:00 2001 From: Yaruma3341 Date: Sun, 12 Jan 2020 18:22:46 +0100 Subject: [PATCH 01/30] hotfix --- .../src/de/steamwar/scoreboard/SWScoreboard.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java index 223f0b2..9ccf6db 100644 --- a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java +++ b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java @@ -41,13 +41,9 @@ public class SWScoreboard { return true; } - public static boolean removeScoreboard(Player player) { + public static void removeScoreboard(Player player) { if(!playerBoards.containsKey(player)) - return false; - - playerBoards.remove(player); - if(player == null) - return true; + return; switch (Core.getVersion()) { case 8: @@ -68,10 +64,8 @@ public class SWScoreboard { case 15: ((SWScoreboard_15) playerBoards.get(player)).deleteScoreboard(player); break; - default: - return false; } - return true; + playerBoards.remove(player); } //Scoreboard Updater From c8b49ae7efc8d031e79dc0c991dfa0badb606e21 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 12 Jan 2020 18:52:51 +0100 Subject: [PATCH 02/30] Trying fixing failing schem saving --- SpigotCore_14/pom.xml | 6 ------ SpigotCore_14/src/de/steamwar/sql/Schematic_14.java | 10 +++------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/SpigotCore_14/pom.xml b/SpigotCore_14/pom.xml index 513b8b6..f9886be 100644 --- a/SpigotCore_14/pom.xml +++ b/SpigotCore_14/pom.xml @@ -45,11 +45,5 @@ 2.0 compile - - steamwar - SpigotCore_12 - 2.0 - compile - \ No newline at end of file diff --git a/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java b/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java index e6f739c..958bf8a 100644 --- a/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java +++ b/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java @@ -4,8 +4,8 @@ import com.sk89q.worldedit.EmptyClipboardException; import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; -import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; import com.sk89q.worldedit.session.ClipboardHolder; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -17,8 +17,8 @@ import java.io.InputStream; class Schematic_14 { private Schematic_14(){} - private static final ClipboardFormat SCHEMATIC = ClipboardFormats.findByAlias("schematic"); - private static final ClipboardFormat SCHEM = ClipboardFormats.findByAlias("schem"); + private static final ClipboardFormat SCHEMATIC = BuiltInClipboardFormat.MCEDIT_SCHEMATIC; + private static final ClipboardFormat SCHEM = BuiltInClipboardFormat.SPONGE_SCHEMATIC; static byte[] getPlayerClipboard(Player player, boolean schemFormat) throws IOException, NoClipboardException { ClipboardHolder clipboardHolder; @@ -35,10 +35,8 @@ class Schematic_14 { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try{ if(schemFormat){ - assert SCHEM != null; SCHEM.getWriter(outputStream).write(clipboard); }else{ - assert SCHEMATIC != null; SCHEMATIC.getWriter(outputStream).write(clipboard); } }catch(NullPointerException e){ @@ -60,10 +58,8 @@ class Schematic_14 { static Clipboard getClipboard(InputStream is, boolean schemFormat) throws IOException, NoClipboardException { try { if(schemFormat){ - assert SCHEM != null; return SCHEM.getReader(is).read(); }else{ - assert SCHEMATIC != null; return SCHEMATIC.getReader(is).read(); } } catch (NullPointerException e) { From a841f5dfe4bf7cc8e495d03a396c2795c3102eb1 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 13 Jan 2020 06:42:24 +0100 Subject: [PATCH 03/30] Fixes failing schematic saving --- SpigotCore_14/src/de/steamwar/sql/Schematic_14.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java b/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java index 958bf8a..8bca947 100644 --- a/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java +++ b/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java @@ -6,6 +6,7 @@ import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; +import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter; import com.sk89q.worldedit.session.ClipboardHolder; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -35,7 +36,9 @@ class Schematic_14 { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try{ if(schemFormat){ - SCHEM.getWriter(outputStream).write(clipboard); + ClipboardWriter writer = SCHEM.getWriter(outputStream); + writer.write(clipboard); + writer.close(); }else{ SCHEMATIC.getWriter(outputStream).write(clipboard); } From 3bfa83d55684af79cf3f5a9e61f856c43a1cd4dd Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 15 Jan 2020 18:49:41 +0100 Subject: [PATCH 04/30] Useless commit --- SpigotCore_Main/src/de/steamwar/core/Core.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index cccc24e..f84b991 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -42,7 +42,7 @@ public class Core extends JavaPlugin{ public void onDisable(){ SQL.closeConnection(); } - + public static Core getInstance() { return instance; } @@ -50,6 +50,7 @@ public class Core extends JavaPlugin{ return version; } + private static void setInstance(Core instance) { Core.instance = instance; } From 30bba43e4523abf495c058651abc13ab8bde53b1 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 18 Jan 2020 12:13:25 +0100 Subject: [PATCH 05/30] Saving exceptions to database --- .../src/de/steamwar/core/Core.java | 1 + .../src/de/steamwar/core/ErrorLogger.java | 42 +++++++++++++++++++ .../src/de/steamwar/sql/SWException.java | 25 +++++++++++ 3 files changed, 68 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/SWException.java diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index f84b991..0b0026d 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -36,6 +36,7 @@ public class Core extends JavaPlugin{ public void onEnable() { Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this); Bukkit.getPluginManager().registerEvents(new ChattingEvent(), this); + ErrorLogger.init(); } @Override diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java b/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java new file mode 100644 index 0000000..1b8bed6 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java @@ -0,0 +1,42 @@ +package de.steamwar.core; + +import de.steamwar.sql.SWException; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Core; +import org.apache.logging.log4j.core.*; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginFactory; + +@Plugin(name = "ErrorLogger", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE) +public class ErrorLogger extends AbstractAppender { + + public static void init(){ + final LoggerContext lc = (LoggerContext) LogManager.getContext(false); + ErrorLogger el = ErrorLogger.createAppender("SWErrorLogger"); + el.start(); + lc.getConfiguration().addAppender(el); + lc.getRootLogger().addAppender(lc.getConfiguration().getAppender(el.getName())); + lc.updateLoggers(); + } + + private ErrorLogger(String name) { + super(name, null, null); + } + + @PluginFactory + public static ErrorLogger createAppender( + @PluginAttribute("name") String name) { + return new ErrorLogger(name); + } + + @Override + public void append(LogEvent logEvent) { + if(logEvent.getLevel().isLessSpecificThan(Level.WARN)) + return; + + SWException.log(logEvent); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java new file mode 100644 index 0000000..718f458 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -0,0 +1,25 @@ +package de.steamwar.sql; + +import org.apache.logging.log4j.core.LogEvent; +import org.bukkit.Bukkit; + +public class SWException { + private SWException(){} + + public static void log(LogEvent logEvent){ + String server = SQL.disarmString(Bukkit.getWorlds().get(0).getName()); + StringBuilder stacktrace = new StringBuilder(logEvent.getSource().toString()); + + Throwable throwable = logEvent.getThrown(); + while(throwable != null){ + stacktrace.append("\nCaused by ").append(throwable.getMessage()); + + for(StackTraceElement ste : throwable.getStackTrace()) + stacktrace.append("\n").append(ste.toString()); + + throwable = throwable.getCause(); + } + + SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES ('" + server + "', '" + SQL.disarmString(logEvent.getMessage().getFormattedMessage()) + "', '" + SQL.disarmString(stacktrace.toString()) + "')"); + } +} From 052aa44d128456ac8c15b889fdcbe67611b68c72 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jan 2020 07:41:43 +0100 Subject: [PATCH 06/30] Removing pair completly --- .../src/de/steamwar/inventory/SWListInv.java | 46 ------------------- 1 file changed, 46 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java index 42b708b..f13de17 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWListInv.java @@ -2,7 +2,6 @@ package de.steamwar.inventory; import de.steamwar.sql.Schematic; import de.steamwar.sql.SchematicType; -import javafx.util.Pair; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -16,17 +15,6 @@ public class SWListInv extends SWInventory { private List> elements; private int page; - @Deprecated - public SWListInv(Player p, String t, ListCallback c, List> l) { - super(p, (l.size()>45) ? 54 : (l.size() + 9-l.size()%9), t); - callback = c; - //Only here for backwards compatibility - elements = new LinkedList<>(); - for(Pair pair : l) - elements.add(new SWListEntry<>(pair.getKey(), pair.getValue())); - page = 0; - } - public SWListInv(Player p, String t, List> l, ListCallback c){ super(p, (l.size()>45) ? 54 : (l.size() + 9-l.size()%9), t); callback = c; @@ -79,18 +67,6 @@ public class SWListInv extends SWInventory { void clicked(ClickType click, T element); } - @Deprecated - public static List> createPlayerList(Player without){ - List> onlinePlayers = new ArrayList<>(); - for(Player player : Bukkit.getOnlinePlayers()){ - if(without != null && player.getUniqueId().equals(without.getUniqueId())) - continue; - - onlinePlayers.add(new Pair<>(SWItem.getPlayerSkull(player), player.getUniqueId())); - } - return onlinePlayers; - } - public static List> createPlayerList(UUID without){ List> onlinePlayers = new ArrayList<>(); for(Player player : Bukkit.getOnlinePlayers()){ @@ -102,28 +78,6 @@ public class SWListInv extends SWInventory { return onlinePlayers; } - @Deprecated - public static List> getSchemList(int warkingUserId, SchematicType type){ - List> schemList = new ArrayList<>(); - - List schems; - if(type == null) - schems = Schematic.getSchemsAccessibleByUser(warkingUserId); - else - schems = Schematic.getSchemsOfType(warkingUserId, type); - - for(Schematic s : schems){ - Material m; - if(s.getItem().isEmpty()) - m = SWItem.getMaterial("CAULDRON_ITEM"); - else - m = SWItem.getMaterial(s.getItem()); - SWItem item = new SWItem(m,"§e" + s.getSchemName()); - schemList.add(new Pair<>(item, s)); - } - return schemList; - } - public static List> getSchemList(SchematicType type, int steamwarUserId){ List> schemList = new ArrayList<>(); From 21a2be5a431b94aeadb640d9ceced0620728699e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jan 2020 08:45:01 +0100 Subject: [PATCH 07/30] Adding statistics backend --- .../src/de/steamwar/sql/Fight.java | 24 +++++++++++++++++++ .../src/de/steamwar/sql/FightPlayer.java | 11 +++++++++ 2 files changed, 35 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/sql/Fight.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java new file mode 100644 index 0000000..54c2213 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -0,0 +1,24 @@ +package de.steamwar.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; + +public class Fight { + private Fight(){} + + public static int create(String gamemode, String arena, Timestamp starttime, int duration, int blueleader, int redleader, int blueschem, int redschem, int win, String wincondition){ + SQL.update("INSERT INTO Fight (GameMode, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (" + + "'" + SQL.disarmString(gamemode) + "', '" + SQL.disarmString(arena) + "', '" + starttime.toString() + "', " + duration + ", " + blueleader + ", " + redleader + ", " + blueschem + ", " + redschem + ", " + win + ", '" + SQL.disarmString(wincondition) + "'" + + ")"); + ResultSet rs = SQL.select("SELECT LAST_INSERT_ID() AS FightID"); + try{ + if(!rs.next()) + throw new SecurityException("No last insert id"); + + return rs.getInt("FightID"); + } catch (SQLException e) { + throw new SecurityException(e); + } + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java new file mode 100644 index 0000000..e5a98b2 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java @@ -0,0 +1,11 @@ +package de.steamwar.sql; + +public class FightPlayer { + private FightPlayer(){} + + 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 + ", '" + SQL.disarmString(kit) + "', " + kills + ", " + SQL.booleanToInt(isOut) + + ")"); + } +} From 364d3f8f8304d6f749b82717eba1dd331ff83073 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jan 2020 14:37:59 +0100 Subject: [PATCH 08/30] WIP: Dnamic SchemType rework --- .../src/de/steamwar/sql/SchematicType.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index 4f745cf..ff12ca0 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -3,8 +3,8 @@ package de.steamwar.sql; import java.util.HashMap; import java.util.Map; -public enum SchematicType { - Normal("", Type.NORMAL), +public class SchematicType { + public static final SchematicType Normal = new SchematicType("", Type.NORMAL); //Has to stay publicly availible CAirShip("CAS", Type.CHECK_TYPE), CWarGear("CWG", Type.CHECK_TYPE), @@ -46,11 +46,11 @@ public enum SchematicType { private final Type type; private SchematicType checkType; - SchematicType(String kuerzel, Type type){ + private SchematicType(String kuerzel, Type type){ this(kuerzel, type, null); } - SchematicType(String kuerzel, Type type, SchematicType checkType){ + private SchematicType(String kuerzel, Type type, SchematicType checkType){ this.kuerzel = kuerzel; this.type = type; this.checkType = checkType; @@ -88,6 +88,10 @@ public enum SchematicType { return fromDB.get(input.toLowerCase()); } + //public static List values(){ + //TODO + //} + enum Type{ NORMAL, CHECK_TYPE, From 92c3a810a671a88edfd5350be64a7e06b9b05c03 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jan 2020 14:40:58 +0100 Subject: [PATCH 09/30] Hotfix failing setup due to missing dependencies --- SpigotCore_Main/src/de/steamwar/core/Core.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 0b0026d..9be1d74 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -36,7 +36,8 @@ public class Core extends JavaPlugin{ public void onEnable() { Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this); Bukkit.getPluginManager().registerEvents(new ChattingEvent(), this); - ErrorLogger.init(); + if(version >= 12) + ErrorLogger.init(); } @Override From 5c176b0afbb67dcbff43fcb3da44911a96fb5991 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jan 2020 16:19:53 +0100 Subject: [PATCH 10/30] Dynamic instanciating of SchematicTypes --- .../src/de/steamwar/sql/SchematicType.java | 79 +++++++++---------- 1 file changed, 36 insertions(+), 43 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index ff12ca0..37d4c73 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -1,56 +1,45 @@ package de.steamwar.sql; -import java.util.HashMap; -import java.util.Map; +import de.steamwar.core.Core; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.util.*; public class SchematicType { - public static final SchematicType Normal = new SchematicType("", Type.NORMAL); //Has to stay publicly availible + public static final SchematicType Normal = new SchematicType("Normal", "", Type.NORMAL, null); //Has to stay publicly availible - CAirShip("CAS", Type.CHECK_TYPE), - CWarGear("CWG", Type.CHECK_TYPE), - CWarShip("CWS", Type.CHECK_TYPE), - CMiniWarGear("CMWG", Type.CHECK_TYPE), - AirShip("AS", Type.FIGHT_TYPE, CAirShip), - WarGear("WG", Type.FIGHT_TYPE, CWarGear), - WarShip("WS", Type.FIGHT_TYPE, CWarShip), - MiniWarGear("MWG", Type.FIGHT_TYPE, CMiniWarGear), - - CMPPWarGear1_12("CMPPWG", Type.CHECK_TYPE), - CSGWarShip("CSGWS", Type.CHECK_TYPE), - CWarGear1_10("CWG1.10", Type.CHECK_TYPE), - CWarShip1_9("CWS1.9", Type.CHECK_TYPE), - CWarShip1_8("CWS1.8", Type.CHECK_TYPE), - CWarGear1_7("CWG1.7", Type.CHECK_TYPE), - WarGear1_14("WG1.14", Type.FIGHT_TYPE, null), - MPPWarGear1_12("MPPWG", Type.FIGHT_TYPE, CMPPWarGear1_12), - SGWarShip("SGWS", Type.FIGHT_TYPE, CSGWarShip), - WarGear1_12("WG1.12", Type.FIGHT_TYPE, null), - WarShip1_12("WS1.12", Type.FIGHT_TYPE, null), - MiniWarGear1_12("MWG1.12", Type.FIGHT_TYPE, null), - WarGear1_10("WG1.10", Type.FIGHT_TYPE, CWarGear1_10), - WarShip1_9("WS1.9", Type.FIGHT_TYPE, CWarShip1_9), - WarShip1_8("WS1.8", Type.FIGHT_TYPE, CWarShip1_8), - WarGear1_7("WG1.7", Type.FIGHT_TYPE, CWarGear1_7) - - ; - - private static Map fromDB = new HashMap<>(); + private static final Map fromDB; + private static final List types; static{ - for(SchematicType type : values()){ - fromDB.put(type.toDB(), type); + File file = new File(Core.getInstance().getDataFolder(), "SchematicTypes.yml"); + + if(!file.exists()) + throw new SecurityException("SchematicType-ConfigFile not found!"); + + YamlConfiguration config = YamlConfiguration.loadConfiguration(file); + + List tmpTypes = new LinkedList<>(); + Map tmpFromDB = new HashMap<>(); + for(String type : config.getKeys(false)){ + String checktype = config.getString("checktype"); + SchematicType current = new SchematicType(type, config.getString("kuerzel"), Type.valueOf(config.getString("type")), checktype != null ? fromDB(checktype) : null); + tmpTypes.add(current); + tmpFromDB.put(type, current); } + + fromDB = Collections.unmodifiableMap(tmpFromDB); + types = Collections.unmodifiableList(tmpTypes); } + private final String name; private final String kuerzel; private final Type type; private SchematicType checkType; - private SchematicType(String kuerzel, Type type){ - this(kuerzel, type, null); - } - - private SchematicType(String kuerzel, Type type, SchematicType checkType){ + private SchematicType(String name, String kuerzel, Type type, SchematicType checkType){ + this.name = name; this.kuerzel = kuerzel; this.type = type; this.checkType = checkType; @@ -76,21 +65,25 @@ public class SchematicType { return type == Type.NORMAL; } + public String name(){ + return name; + } + public String getKuerzel() { return kuerzel; } public String toDB(){ - return name().toLowerCase(); + return name.toLowerCase(); } public static SchematicType fromDB(String input){ return fromDB.get(input.toLowerCase()); } - //public static List values(){ - //TODO - //} + public static List values(){ + return types; + } enum Type{ NORMAL, From f6f2c7ce9aab69a7ed88b14bc4ea38eaa8651358 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jan 2020 16:25:15 +0100 Subject: [PATCH 11/30] Remove logging of Block at XXX should be XXX messages --- SpigotCore_Main/src/de/steamwar/sql/SWException.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index 718f458..9bbc971 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -20,6 +20,10 @@ public class SWException { throwable = throwable.getCause(); } - SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES ('" + server + "', '" + SQL.disarmString(logEvent.getMessage().getFormattedMessage()) + "', '" + SQL.disarmString(stacktrace.toString()) + "')"); + String message = logEvent.getMessage().getFormattedMessage(); + if(message.startsWith("Block at")) + return; + + SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES ('" + server + "', '" + SQL.disarmString(message) + "', '" + SQL.disarmString(stacktrace.toString()) + "')"); } } From cf913ae61d445de13f0ca9185039762a508bc0fa Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jan 2020 17:52:48 +0100 Subject: [PATCH 12/30] Adding personal kits for WarGears o.a. --- .../src/de/steamwar/sql/PersonalKit.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java new file mode 100644 index 0000000..7de0222 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -0,0 +1,62 @@ +package de.steamwar.sql; + +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; + +import java.io.StringReader; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Objects; + +public class PersonalKit { + + private final int userID; + private final String gamemode; + private String inventory; + private String armor; + + private PersonalKit(ResultSet rs) throws SQLException { + userID = rs.getInt("UserID"); + gamemode = rs.getString("GameMode"); + inventory = rs.getString("Inventory"); + armor = rs.getString("Armor"); + } + + public static PersonalKit get(int userID, String gamemode){ + ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = '" + userID + "' AND GameMode = '" + SQL.disarmString(gamemode) + "'"); + try { + if(!rs.next()) + return null; + + return new PersonalKit(rs); + } catch (SQLException e) { + throw new SecurityException("Failed loading personal kit", e); + } + } + + public static PersonalKit save(int userID, String gamemode, ItemStack[] inventory, ItemStack[] armor){ + YamlConfiguration inventoryConfig = new YamlConfiguration(); + inventoryConfig.set("Inventory", inventory); + + YamlConfiguration armorConfig = new YamlConfiguration(); + armorConfig.set("Armor", armor); + + SQL.update("INSERT INTO PersonalKit" + + " (UserID, GameMode, Inventory, Armor)" + + " VALUES" + + " ('" + userID + "', '" + gamemode + "', '" + SQL.disarmString(inventoryConfig.saveToString()) + "', '" + SQL.disarmString(armorConfig.saveToString()) + "')" + + " ON DUPLICATE KEY UPDATE" + + " Inventory = VALUES(Inventory), Armor = VALUES(Armor)"); + return get(userID, gamemode); + } + + public ItemStack[] getInventory(){ + YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(inventory)); + return Objects.requireNonNull(config.getList("Inventory")).toArray(new ItemStack[0]); + } + + public ItemStack[] getArmor(){ + YamlConfiguration config = YamlConfiguration.loadConfiguration(new StringReader(armor)); + return Objects.requireNonNull(config.getList("Armor")).toArray(new ItemStack[0]); + } +} From 7f167e73ea19b2707edd946be5344901df4875af Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 20 Jan 2020 06:59:15 +0100 Subject: [PATCH 13/30] Hotfix SchematicType --- SpigotCore_Main/src/de/steamwar/sql/SchematicType.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index 37d4c73..6049606 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -1,6 +1,7 @@ package de.steamwar.sql; import de.steamwar.core.Core; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; @@ -23,8 +24,10 @@ public class SchematicType { List tmpTypes = new LinkedList<>(); Map tmpFromDB = new HashMap<>(); for(String type : config.getKeys(false)){ - String checktype = config.getString("checktype"); - SchematicType current = new SchematicType(type, config.getString("kuerzel"), Type.valueOf(config.getString("type")), checktype != null ? fromDB(checktype) : null); + ConfigurationSection section = config.getConfigurationSection(type); + assert section != null; + String checktype = section.getString("checktype"); + SchematicType current = new SchematicType(type, section.getString("kuerzel"), Type.valueOf(section.getString("type")), checktype != null ? fromDB(checktype) : null); tmpTypes.add(current); tmpFromDB.put(type, current); } From c1981cf66a12d4b9a981db3b8f14b23f331a730b Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 20 Jan 2020 07:00:48 +0100 Subject: [PATCH 14/30] Hotfix Schemtype fromDB name --- SpigotCore_Main/src/de/steamwar/sql/SchematicType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index 6049606..29fab1a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -29,7 +29,7 @@ public class SchematicType { String checktype = section.getString("checktype"); SchematicType current = new SchematicType(type, section.getString("kuerzel"), Type.valueOf(section.getString("type")), checktype != null ? fromDB(checktype) : null); tmpTypes.add(current); - tmpFromDB.put(type, current); + tmpFromDB.put(type.toLowerCase(), current); } fromDB = Collections.unmodifiableMap(tmpFromDB); From ed3e6a3e4676fbcabee4098070788158d51a8657 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 20 Jan 2020 07:05:52 +0100 Subject: [PATCH 15/30] Hotfix Schemtype fromDB NPE --- SpigotCore_Main/src/de/steamwar/sql/SchematicType.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index 29fab1a..068ceeb 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -81,6 +81,8 @@ public class SchematicType { } public static SchematicType fromDB(String input){ + if(input == null) + return null; return fromDB.get(input.toLowerCase()); } From 57ada4b9e0adeabcf5719412444e98362a1b8564 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 20 Jan 2020 07:07:19 +0100 Subject: [PATCH 16/30] Hotfix Schemtype fromDB NPE --- SpigotCore_Main/src/de/steamwar/sql/SchematicType.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index 068ceeb..32c81dd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -81,9 +81,7 @@ public class SchematicType { } public static SchematicType fromDB(String input){ - if(input == null) - return null; - return fromDB.get(input.toLowerCase()); + return fromDB.getOrDefault(input.toLowerCase(), null); } public static List values(){ From 9eb46e7a0fd6f1ca60b3145dd5533d26d95ef7c8 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 20 Jan 2020 07:15:53 +0100 Subject: [PATCH 17/30] Hotrevert to static instanciation --- .../src/de/steamwar/sql/SchematicType.java | 73 ++++++++++++++----- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index 32c81dd..c1e6f9e 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -1,16 +1,49 @@ package de.steamwar.sql; -import de.steamwar.core.Core; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; +import java.util.HashMap; +import java.util.Map; -import java.io.File; -import java.util.*; +public enum SchematicType { + Normal("", Type.NORMAL), + CAirShip("CAS", Type.CHECK_TYPE), + CWarGear("CWG", Type.CHECK_TYPE), + CWarShip("CWS", Type.CHECK_TYPE), + CMiniWarGear("CMWG", Type.CHECK_TYPE), + AirShip("AS", Type.FIGHT_TYPE, CAirShip), + WarGear("WG", Type.FIGHT_TYPE, CWarGear), + WarShip("WS", Type.FIGHT_TYPE, CWarShip), + MiniWarGear("MWG", Type.FIGHT_TYPE, CMiniWarGear), -public class SchematicType { - public static final SchematicType Normal = new SchematicType("Normal", "", Type.NORMAL, null); //Has to stay publicly availible + CMPPWarGear1_12("CMPPWG", Type.CHECK_TYPE), + CSGWarShip("CSGWS", Type.CHECK_TYPE), + CWarGear1_10("CWG1.10", Type.CHECK_TYPE), + CWarShip1_9("CWS1.9", Type.CHECK_TYPE), + CWarShip1_8("CWS1.8", Type.CHECK_TYPE), + CWarGear1_7("CWG1.7", Type.CHECK_TYPE), + WarGear1_14("WG1.14", Type.FIGHT_TYPE, null), + MPPWarGear1_12("MPPWG", Type.FIGHT_TYPE, CMPPWarGear1_12), + SGWarShip("SGWS", Type.FIGHT_TYPE, CSGWarShip), + WarGear1_12("WG1.12", Type.FIGHT_TYPE, null), + WarShip1_12("WS1.12", Type.FIGHT_TYPE, null), + MiniWarGear1_12("MWG1.12", Type.FIGHT_TYPE, null), + WarGear1_10("WG1.10", Type.FIGHT_TYPE, CWarGear1_10), + WarShip1_9("WS1.9", Type.FIGHT_TYPE, CWarShip1_9), + WarShip1_8("WS1.8", Type.FIGHT_TYPE, CWarShip1_8), + WarGear1_7("WG1.7", Type.FIGHT_TYPE, CWarGear1_7) - private static final Map fromDB; + ; + + + + //public static final SchematicType Normal = new SchematicType("Normal", "", Type.NORMAL, null); //Has to stay publicly availible + + private static final Map fromDB = new HashMap<>(); + static{ + for(SchematicType type : values()){ + fromDB.put(type.name().toLowerCase(), type); + } + } + /*private static final Map fromDB; private static final List types; static{ @@ -34,15 +67,19 @@ public class SchematicType { fromDB = Collections.unmodifiableMap(tmpFromDB); types = Collections.unmodifiableList(tmpTypes); - } + }*/ - private final String name; + //private final String name; private final String kuerzel; private final Type type; private SchematicType checkType; - private SchematicType(String name, String kuerzel, Type type, SchematicType checkType){ - this.name = name; + private SchematicType(String kuerzel, Type type){ + this(kuerzel, type, null); + } + + private SchematicType(String kuerzel, Type type, SchematicType checkType){ + //this.name = name; this.kuerzel = kuerzel; this.type = type; this.checkType = checkType; @@ -68,25 +105,25 @@ public class SchematicType { return type == Type.NORMAL; } - public String name(){ + /*public String name(){ return name; - } + }*/ public String getKuerzel() { return kuerzel; } - public String toDB(){ + /*public String toDB(){ return name.toLowerCase(); - } + }*/ public static SchematicType fromDB(String input){ return fromDB.getOrDefault(input.toLowerCase(), null); } - public static List values(){ + /*public static List values(){ return types; - } + }*/ enum Type{ NORMAL, From 8625447969be42253628d4bc0bf6562a712ff4b9 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 20 Jan 2020 07:17:40 +0100 Subject: [PATCH 18/30] Hotfix static instanciation --- SpigotCore_Main/src/de/steamwar/sql/SchematicType.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index c1e6f9e..f421352 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -113,9 +113,9 @@ public enum SchematicType { return kuerzel; } - /*public String toDB(){ - return name.toLowerCase(); - }*/ + public String toDB(){ + return name().toLowerCase(); + } public static SchematicType fromDB(String input){ return fromDB.getOrDefault(input.toLowerCase(), null); From c8bf5e7c76f693dcfa387fe50fc5efb3e4fa0484 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 21 Jan 2020 17:47:52 +0100 Subject: [PATCH 19/30] Adding exception for POI data mismatch --- SpigotCore_Main/src/de/steamwar/sql/SWException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index 9bbc971..67599f9 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -21,7 +21,7 @@ public class SWException { } String message = logEvent.getMessage().getFormattedMessage(); - if(message.startsWith("Block at")) + if(message.startsWith("Block at") || message.startsWith("POI data mismatch")) return; SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES ('" + server + "', '" + SQL.disarmString(message) + "', '" + SQL.disarmString(stacktrace.toString()) + "')"); From f3b5838a16c3ea8aa77b4d27ce376d490f40431f Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 21 Jan 2020 21:42:05 +0100 Subject: [PATCH 20/30] Hotfix NPE --- SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java index 0ec89b9..40e544d 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java @@ -92,7 +92,7 @@ public class SWInventory implements Listener { return; e.setCancelled(true); - if(callbacks.containsKey(e.getRawSlot())) + if(callbacks.containsKey(e.getRawSlot()) && callbacks.get(e.getRawSlot()) != null) callbacks.get(e.getRawSlot()).clicked(e.getClick()); } From f77604e6ec13363a3293f7ae08a99f9f818411d8 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 25 Jan 2020 09:20:30 +0100 Subject: [PATCH 21/30] Do not log complete server crashes --- SpigotCore_Main/src/de/steamwar/sql/SWException.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index 67599f9..ed0c81d 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -6,7 +6,12 @@ import org.bukkit.Bukkit; public class SWException { private SWException(){} + private static boolean logDisabled = true; + public static void log(LogEvent logEvent){ + if(logDisabled) + return; + String server = SQL.disarmString(Bukkit.getWorlds().get(0).getName()); StringBuilder stacktrace = new StringBuilder(logEvent.getSource().toString()); @@ -24,6 +29,11 @@ public class SWException { if(message.startsWith("Block at") || message.startsWith("POI data mismatch")) return; + if(message.equals("------------------------------")){ + message = "Server stopped responding"; + logDisabled = true; + } + SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES ('" + server + "', '" + SQL.disarmString(message) + "', '" + SQL.disarmString(stacktrace.toString()) + "')"); } } From f44fe64a436b425c651f1ed576aac84dab00d381 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 25 Jan 2020 09:35:02 +0100 Subject: [PATCH 22/30] Log players --- SpigotCore_Main/src/de/steamwar/sql/SWException.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index ed0c81d..4787e04 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -2,6 +2,7 @@ package de.steamwar.sql; import org.apache.logging.log4j.core.LogEvent; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; public class SWException { private SWException(){} @@ -34,6 +35,11 @@ public class SWException { logDisabled = true; } + message += "\n\n"; + + for(Player player : Bukkit.getOnlinePlayers()) + message += player.getName() + " "; + SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES ('" + server + "', '" + SQL.disarmString(message) + "', '" + SQL.disarmString(stacktrace.toString()) + "')"); } } From 59cac59fe924074378aad0c17b78ed46d07edcb7 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 25 Jan 2020 12:51:29 +0100 Subject: [PATCH 23/30] Revert "Hotfix static instanciation" This reverts commit 86254479 --- SpigotCore_Main/src/de/steamwar/sql/SchematicType.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index f421352..c1e6f9e 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -113,9 +113,9 @@ public enum SchematicType { return kuerzel; } - public String toDB(){ - return name().toLowerCase(); - } + /*public String toDB(){ + return name.toLowerCase(); + }*/ public static SchematicType fromDB(String input){ return fromDB.getOrDefault(input.toLowerCase(), null); From f34cfdd254e6f2912793b9ee514e0e6ec198d0d3 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 25 Jan 2020 12:51:45 +0100 Subject: [PATCH 24/30] Revert "Hotrevert to static instanciation" This reverts commit 9eb46e7a --- .../src/de/steamwar/sql/SchematicType.java | 73 +++++-------------- 1 file changed, 18 insertions(+), 55 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index c1e6f9e..32c81dd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -1,49 +1,16 @@ package de.steamwar.sql; -import java.util.HashMap; -import java.util.Map; +import de.steamwar.core.Core; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; -public enum SchematicType { - Normal("", Type.NORMAL), - CAirShip("CAS", Type.CHECK_TYPE), - CWarGear("CWG", Type.CHECK_TYPE), - CWarShip("CWS", Type.CHECK_TYPE), - CMiniWarGear("CMWG", Type.CHECK_TYPE), - AirShip("AS", Type.FIGHT_TYPE, CAirShip), - WarGear("WG", Type.FIGHT_TYPE, CWarGear), - WarShip("WS", Type.FIGHT_TYPE, CWarShip), - MiniWarGear("MWG", Type.FIGHT_TYPE, CMiniWarGear), +import java.io.File; +import java.util.*; - CMPPWarGear1_12("CMPPWG", Type.CHECK_TYPE), - CSGWarShip("CSGWS", Type.CHECK_TYPE), - CWarGear1_10("CWG1.10", Type.CHECK_TYPE), - CWarShip1_9("CWS1.9", Type.CHECK_TYPE), - CWarShip1_8("CWS1.8", Type.CHECK_TYPE), - CWarGear1_7("CWG1.7", Type.CHECK_TYPE), - WarGear1_14("WG1.14", Type.FIGHT_TYPE, null), - MPPWarGear1_12("MPPWG", Type.FIGHT_TYPE, CMPPWarGear1_12), - SGWarShip("SGWS", Type.FIGHT_TYPE, CSGWarShip), - WarGear1_12("WG1.12", Type.FIGHT_TYPE, null), - WarShip1_12("WS1.12", Type.FIGHT_TYPE, null), - MiniWarGear1_12("MWG1.12", Type.FIGHT_TYPE, null), - WarGear1_10("WG1.10", Type.FIGHT_TYPE, CWarGear1_10), - WarShip1_9("WS1.9", Type.FIGHT_TYPE, CWarShip1_9), - WarShip1_8("WS1.8", Type.FIGHT_TYPE, CWarShip1_8), - WarGear1_7("WG1.7", Type.FIGHT_TYPE, CWarGear1_7) +public class SchematicType { + public static final SchematicType Normal = new SchematicType("Normal", "", Type.NORMAL, null); //Has to stay publicly availible - ; - - - - //public static final SchematicType Normal = new SchematicType("Normal", "", Type.NORMAL, null); //Has to stay publicly availible - - private static final Map fromDB = new HashMap<>(); - static{ - for(SchematicType type : values()){ - fromDB.put(type.name().toLowerCase(), type); - } - } - /*private static final Map fromDB; + private static final Map fromDB; private static final List types; static{ @@ -67,19 +34,15 @@ public enum SchematicType { fromDB = Collections.unmodifiableMap(tmpFromDB); types = Collections.unmodifiableList(tmpTypes); - }*/ + } - //private final String name; + private final String name; private final String kuerzel; private final Type type; private SchematicType checkType; - private SchematicType(String kuerzel, Type type){ - this(kuerzel, type, null); - } - - private SchematicType(String kuerzel, Type type, SchematicType checkType){ - //this.name = name; + private SchematicType(String name, String kuerzel, Type type, SchematicType checkType){ + this.name = name; this.kuerzel = kuerzel; this.type = type; this.checkType = checkType; @@ -105,25 +68,25 @@ public enum SchematicType { return type == Type.NORMAL; } - /*public String name(){ + public String name(){ return name; - }*/ + } public String getKuerzel() { return kuerzel; } - /*public String toDB(){ + public String toDB(){ return name.toLowerCase(); - }*/ + } public static SchematicType fromDB(String input){ return fromDB.getOrDefault(input.toLowerCase(), null); } - /*public static List values(){ + public static List values(){ return types; - }*/ + } enum Type{ NORMAL, From f883220618b3876dcd743a92ac7ecf2fca86537d Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 25 Jan 2020 13:47:16 +0100 Subject: [PATCH 25/30] Fixing dynamic implementation --- SpigotCore_Main/src/de/steamwar/sql/SchematicType.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index 32c81dd..9d3cadd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -23,11 +23,15 @@ public class SchematicType { List tmpTypes = new LinkedList<>(); Map tmpFromDB = new HashMap<>(); + + tmpTypes.add(Normal); + tmpFromDB.put(Normal.name().toLowerCase(), Normal); + for(String type : config.getKeys(false)){ ConfigurationSection section = config.getConfigurationSection(type); assert section != null; String checktype = section.getString("checktype"); - SchematicType current = new SchematicType(type, section.getString("kuerzel"), Type.valueOf(section.getString("type")), checktype != null ? fromDB(checktype) : null); + SchematicType current = new SchematicType(type, section.getString("kuerzel"), Type.valueOf(section.getString("type")), checktype != null ? tmpFromDB.get(checktype) : null); tmpTypes.add(current); tmpFromDB.put(type.toLowerCase(), current); } From ef01e8f68550d23d3cbc401d9c7b56ac5746a2d6 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 25 Jan 2020 14:04:07 +0100 Subject: [PATCH 26/30] Fixing checktype classification --- SpigotCore_Main/src/de/steamwar/sql/SchematicType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java index 9d3cadd..b51dc38 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicType.java @@ -31,7 +31,7 @@ public class SchematicType { ConfigurationSection section = config.getConfigurationSection(type); assert section != null; String checktype = section.getString("checktype"); - SchematicType current = new SchematicType(type, section.getString("kuerzel"), Type.valueOf(section.getString("type")), checktype != null ? tmpFromDB.get(checktype) : null); + SchematicType current = new SchematicType(type, section.getString("kuerzel"), Type.valueOf(section.getString("type")), checktype != null ? tmpFromDB.get(checktype.toLowerCase()) : null); tmpTypes.add(current); tmpFromDB.put(type.toLowerCase(), current); } From 9df9439120ba164c9711bb409654aac480589ee4 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 26 Jan 2020 13:43:20 +0100 Subject: [PATCH 27/30] Hotfixing disabled --- SpigotCore_Main/src/de/steamwar/sql/SWException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index 4787e04..d65aa94 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -7,7 +7,7 @@ import org.bukkit.entity.Player; public class SWException { private SWException(){} - private static boolean logDisabled = true; + private static boolean logDisabled = false; public static void log(LogEvent logEvent){ if(logDisabled) From 0ecf0b1a4235a64ca52bb549e54484192467439b Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 1 Feb 2020 14:47:23 +0100 Subject: [PATCH 28/30] Hotfix of missing SchemData NPE --- SpigotCore_Main/src/de/steamwar/sql/Schematic.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index 9954681..d8ddf43 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -189,7 +189,10 @@ public class Schematic { ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = " + schemID); try { rs.next(); - InputStream is = rs.getBlob("SchemData").getBinaryStream(); + Blob blob = rs.getBlob("SchemData"); + if(blob == null) + throw new NoClipboardException(); + InputStream is = blob.getBinaryStream(); switch(Core.getVersion()){ case 8: Schematic_8.setPlayerClipboard(player, is); From 151ec960bb56a55e8537164ef240da4b103e6115 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 7 Feb 2020 16:45:02 +0100 Subject: [PATCH 29/30] Rework SQL statements to actually use PreparedStatements for security reasons --- .../src/de/steamwar/sql/BauweltMember.java | 14 ++---- .../src/de/steamwar/sql/CheckedSchematic.java | 13 +++-- .../de/steamwar/sql/DownloadSchematic.java | 4 +- .../src/de/steamwar/sql/Event.java | 18 +++---- .../src/de/steamwar/sql/EventFight.java | 24 ++++----- .../src/de/steamwar/sql/Fight.java | 5 +- .../src/de/steamwar/sql/FightPlayer.java | 5 +- .../src/de/steamwar/sql/PersonalKit.java | 10 ++-- SpigotCore_Main/src/de/steamwar/sql/SQL.java | 49 ++++++++++--------- .../src/de/steamwar/sql/SWException.java | 5 +- .../src/de/steamwar/sql/Schematic.java | 44 +++++++---------- .../src/de/steamwar/sql/SchematicMember.java | 15 +++--- .../src/de/steamwar/sql/SteamwarUser.java | 11 ++--- SpigotCore_Main/src/de/steamwar/sql/Team.java | 4 +- 14 files changed, 96 insertions(+), 125 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java index eebaec8..2f8afcb 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java @@ -36,17 +36,13 @@ public class BauweltMember{ } public void remove(){ - SQL.update("DELETE FROM BauweltMember WHERE BauweltID = " + bauweltID + " AND MemberID = " + memberID); + SQL.update("DELETE FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", bauweltID, memberID); members.remove(this); } private void updateDB(){ - SQL.update("INSERT INTO BauweltMember" + - " (BauweltID, MemberID, Build, WorldEdit, World)" + - " VALUES" + - " ('" + bauweltID + "', '" + memberID + "', '" + SQL.booleanToInt(build) + "', '" + SQL.booleanToInt(worldEdit) + "', '" + SQL.booleanToInt(world) + "')" + - " ON DUPLICATE KEY UPDATE" + - " Build = VALUES(Build), WorldEdit = VALUES(WorldEdit), World = VALUES(World)"); + SQL.update("INSERT INTO BauweltMember (BauweltID, MemberID, Build, WorldEdit, World) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Build = VALUES(Build), WorldEdit = VALUES(WorldEdit), World = VALUES(World)", + bauweltID, memberID, build, worldEdit, world); } public static BauweltMember getBauMember(UUID ownerID, UUID memberID){ @@ -57,7 +53,7 @@ public class BauweltMember{ for(BauweltMember member : members) if(member.memberID == memberID) return member; - ResultSet member = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = '" + ownerID + "' AND MemberID = '" + memberID + "'"); + ResultSet member = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", ownerID, memberID); try { if(member == null || !member.next()){ return null; @@ -77,7 +73,7 @@ public class BauweltMember{ public static List getMembers(int bauweltID){ try{ - ResultSet memberlist = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = '" + bauweltID + "'"); + ResultSet memberlist = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = ?", bauweltID); List members = new LinkedList<>(); while(memberlist.next()){ int memberID = memberlist.getInt("MemberID"); diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index 73929bc..974537f 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -21,12 +21,12 @@ public class CheckedSchematic { private final String declineReason; private CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason, boolean insertDB){ - this.schemName = SQL.disarmString(schemName); + this.schemName = schemName; this.schemOwner = schemOwner; this.validator = validator; this.startTime = startTime; this.endTime = endTime; - this.declineReason = SQL.disarmString(declineReason); + this.declineReason = declineReason; if(insertDB) insertDB(); } @@ -41,9 +41,8 @@ public class CheckedSchematic { private void insertDB(){ SQL.update("INSERT INTO CheckedSchematic" + - " (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason)" + - " VALUES" + - " ('"+ schemName + "', '" + schemOwner + "', '" + validator + "', '" + startTime.toString() + "', '" + endTime.toString() + "', '" + declineReason + "')"); + " (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)", + schemName, schemOwner, validator, startTime, endTime, declineReason); } public static List getLastDeclined(UUID schemOwner){ @@ -53,7 +52,7 @@ public class CheckedSchematic { public static List getLastDelined(int schemOwner){ List lastDeclined = new LinkedList<>(); try{ - ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE SchemOwner = '" + schemOwner + "' AND DeclineReason != '' ORDER BY EndTime DESC"); + ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE SchemOwner = ? AND DeclineReason != '' ORDER BY EndTime DESC", schemOwner); while(lastRS.next()){ String schemName = lastRS.getString("SchemName"); int validator = lastRS.getInt("Validator"); @@ -69,7 +68,7 @@ public class CheckedSchematic { } public void remove() { - SQL.update("DELETE FROM CheckedSchematic WHERE SchemOwner = " + this.schemOwner + " AND SchemName = '" + this.schemName + "'"); + SQL.update("DELETE FROM CheckedSchematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); } public String getSchemName() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java index ed43cec..7c78950 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java @@ -13,7 +13,7 @@ public class DownloadSchematic { private static final String BASE = "https://steamwar.de/download.php?schem="; public static String getLink(Schematic schem){ - ResultSet rs = SQL.select("SELECT * FROM SchemDownload WHERE SchemID = " + schem.getSchemID()); + ResultSet rs = SQL.select("SELECT * FROM SchemDownload WHERE SchemID = ?", schem.getSchemID()); try { if(rs.next()) return BASE + rs.getString("Link"); @@ -30,7 +30,7 @@ public class DownloadSchematic { cript.reset(); cript.update((Instant.now().toString() + schem.getSchemOwner() + schem.getSchemID()).getBytes()); String hash = DatatypeConverter.printHexBinary(cript.digest()); - SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (" + schem.getSchemID() + ", '" + hash + "')"); + SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?)", schem.getSchemID(), hash); return BASE + hash; } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Event.java b/SpigotCore_Main/src/de/steamwar/sql/Event.java index 2434212..7318320 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Event.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Event.java @@ -16,22 +16,22 @@ public class Event { private final int maximumTeamMembers; private final boolean publicSchemsOnly; - private Event(int eventID, String eventName, Timestamp start, Timestamp end, int maximumTeamMembers, boolean publicSchemsOnly){ - this.eventID = eventID; - this.eventName = eventName; - this.start = start; - this.end = end; - this.maximumTeamMembers = maximumTeamMembers; - this.publicSchemsOnly = publicSchemsOnly; + private Event(ResultSet rs) throws SQLException{ + this.eventID = rs.getInt("EventID"); + this.eventName = rs.getString("EventName"); + this.start = rs.getTimestamp("Start"); + this.end = rs.getTimestamp("End"); + this.maximumTeamMembers = rs.getInt("MaximumTeamMembers"); + this.publicSchemsOnly = rs.getBoolean("PublicSchemsOnly"); } public static Event get(int eventID){ - ResultSet rs = SQL.select("SELECT * FROM Event WHERE EventID = " + eventID); + ResultSet rs = SQL.select("SELECT * FROM Event WHERE EventID = ?", eventID); try{ if(!rs.next()) throw new IllegalArgumentException(); - return new Event(eventID, rs.getString("EventName"), rs.getTimestamp("Start"), rs.getTimestamp("End"), rs.getInt("MaximumTeamMembers"), rs.getBoolean("PublicSchemsOnly")); + return new Event(rs); }catch (SQLException e){ Bukkit.getLogger().log(Level.SEVERE, "Failed to load Event", e); throw new SecurityException(); diff --git a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java index 65a31cb..79eafdc 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java @@ -14,26 +14,20 @@ public class EventFight { private int kampfleiter; private int ergebnis; - private EventFight(int eventID, int fightID, int teamBlue, int teamRed, int kampfleiter, int ergebnis){ - this.eventID = eventID; - this.fightID = fightID; - this.teamBlue = teamBlue; - this.teamRed = teamRed; - this.kampfleiter = kampfleiter; - this.ergebnis = ergebnis; + private EventFight(ResultSet rs) throws SQLException{ + this.eventID = rs.getInt("EventID"); + this.fightID = rs.getInt("FightID"); + this.teamBlue = rs.getInt("TeamBlue"); + this.teamRed = rs.getInt("TeamRed"); + this.kampfleiter = rs.getInt("Kampfleiter"); + this.ergebnis = rs.getInt("Ergebnis"); } public static EventFight get(int fightID){ ResultSet rs = SQL.select("SELECT * FROM EventFight WHERE FightID = " + fightID); try{ rs.next(); - return new EventFight( - rs.getInt("EventID"), - fightID, - rs.getInt("TeamBlue"), - rs.getInt("TeamRed"), - rs.getInt("Kampfleiter"), - rs.getInt("Ergebnis")); + return new EventFight(rs); }catch (SQLException e){ Bukkit.getLogger().log(Level.SEVERE, "Failed to load EventFight", e); } @@ -41,7 +35,7 @@ public class EventFight { } public void setErgebnis(int winner){ - SQL.update("UPDATE EventFight SET Ergebnis = " + winner + " WHERE FightID = " + fightID); + SQL.update("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?", winner, 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 54c2213..e21a894 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -8,9 +8,8 @@ public class Fight { private Fight(){} public static int create(String gamemode, String arena, Timestamp starttime, int duration, int blueleader, int redleader, int blueschem, int redschem, int win, String wincondition){ - SQL.update("INSERT INTO Fight (GameMode, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (" + - "'" + SQL.disarmString(gamemode) + "', '" + SQL.disarmString(arena) + "', '" + starttime.toString() + "', " + duration + ", " + blueleader + ", " + redleader + ", " + blueschem + ", " + redschem + ", " + win + ", '" + SQL.disarmString(wincondition) + "'" + - ")"); + SQL.update("INSERT INTO Fight (GameMode, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + gamemode, arena, starttime, duration, blueleader, redleader,blueschem, redschem, win, wincondition); ResultSet rs = SQL.select("SELECT LAST_INSERT_ID() AS FightID"); try{ if(!rs.next()) diff --git a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java index e5a98b2..72923e6 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java +++ b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java @@ -4,8 +4,7 @@ public class FightPlayer { private FightPlayer(){} 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 + ", '" + SQL.disarmString(kit) + "', " + kills + ", " + SQL.booleanToInt(isOut) + - ")"); + SQL.update("INSERT INTO FightPlayer (FightID, UserID, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?)", + fightID, userID, kit, kills, isOut); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java index 7de0222..80d7f3f 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java +++ b/SpigotCore_Main/src/de/steamwar/sql/PersonalKit.java @@ -23,7 +23,7 @@ public class PersonalKit { } public static PersonalKit get(int userID, String gamemode){ - ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = '" + userID + "' AND GameMode = '" + SQL.disarmString(gamemode) + "'"); + ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?", userID, gamemode); try { if(!rs.next()) return null; @@ -41,12 +41,8 @@ public class PersonalKit { YamlConfiguration armorConfig = new YamlConfiguration(); armorConfig.set("Armor", armor); - SQL.update("INSERT INTO PersonalKit" + - " (UserID, GameMode, Inventory, Armor)" + - " VALUES" + - " ('" + userID + "', '" + gamemode + "', '" + SQL.disarmString(inventoryConfig.saveToString()) + "', '" + SQL.disarmString(armorConfig.saveToString()) + "')" + - " ON DUPLICATE KEY UPDATE" + - " Inventory = VALUES(Inventory), Armor = VALUES(Armor)"); + SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Inventory, Armor) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor)", + userID, gamemode, inventoryConfig.saveToString(), armorConfig.saveToString()); return get(userID, gamemode); } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQL.java b/SpigotCore_Main/src/de/steamwar/sql/SQL.java index 8375a99..034eea8 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQL.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQL.java @@ -33,13 +33,6 @@ public class SQL { connect(); } - - static Integer booleanToInt(boolean b){ - if(b) - return 1; - else - return 0; - } public static void closeConnection() { try { @@ -48,45 +41,53 @@ public class SQL { throw new SecurityException("Could not close connection", e); } } - - static Connection getCon(){ - return con; - } - static void update(String qry) { + static void update(String qry, Object... objects) { try { - PreparedStatement st = con.prepareStatement(qry); - st.executeUpdate(); + prepare(qry, objects).executeUpdate(); } catch (SQLException e) { reconnect(); try { - PreparedStatement st = con.prepareStatement(qry); - st.executeUpdate(); + prepare(qry, objects).executeUpdate(); } catch (SQLException ex) { throw new SecurityException("Could not perform update", ex); } } } - - static ResultSet select(String qry) { + + static ResultSet select(String qry, Object... objects){ try { - PreparedStatement st = con.prepareStatement(qry); - return st.executeQuery(); + return prepare(qry, objects).executeQuery(); } catch (SQLException e) { reconnect(); try { - PreparedStatement st = con.prepareStatement(qry); - return st.executeQuery(); + return prepare(qry, objects).executeQuery(); } catch (SQLException ex) { throw new SecurityException("Could not perform select", ex); } } } - static String disarmString(String s){ - return s.replace("'", ""); + static Blob blob(){ + try { + return con.createBlob(); + } catch (SQLException e) { + reconnect(); + try { + return con.createBlob(); + } catch (SQLException ex) { + throw new SecurityException("Could not create blob", ex); + } + } } + 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; + } private static void connect() { try { diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java index d65aa94..2a285bc 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SWException.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -13,7 +13,7 @@ public class SWException { if(logDisabled) return; - String server = SQL.disarmString(Bukkit.getWorlds().get(0).getName()); + String server = Bukkit.getWorlds().get(0).getName(); StringBuilder stacktrace = new StringBuilder(logEvent.getSource().toString()); Throwable throwable = logEvent.getThrown(); @@ -40,6 +40,7 @@ public class SWException { for(Player player : Bukkit.getOnlinePlayers()) message += player.getName() + " "; - SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES ('" + server + "', '" + SQL.disarmString(message) + "', '" + SQL.disarmString(stacktrace.toString()) + "')"); + SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)", + server, message, stacktrace.toString()); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index d8ddf43..1562406 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -4,11 +4,9 @@ import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.core.Core; import org.bukkit.entity.Player; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -42,12 +40,8 @@ public class Schematic { } public static void createSchem(String schemName, int schemOwner, String item, SchematicType schemType){ - SQL.update("INSERT INTO Schematic" + - " (SchemName, SchemOwner, Item, SchemType)" + - " VALUES" + - " ('" + schemName + "', '" + schemOwner + "', '" + item + "', '" + schemType.toDB() + "')" + - " ON DUPLICATE KEY UPDATE" + - " Item = VALUES(Item), SchemType = VALUES(SchemType)"); + SQL.update("INSERT INTO Schematic (SchemName, SchemOwner, Item, SchemType) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE Item = VALUES(Item), SchemType = VALUES(SchemType)", + schemName, schemOwner, item, schemType.toDB()); } public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ @@ -55,8 +49,7 @@ public class Schematic { } public static Schematic getSchemFromDB(String schemName, int schemOwner){ - schemName = SQL.disarmString(schemName); - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, SchemFormat FROM Schematic WHERE SchemName = '" + schemName + "' AND SchemOwner = '" + schemOwner + "'"); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, SchemFormat FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); try { if(schematic == null || !schematic.next()){ SchematicMember member = SchematicMember.getMemberBySchematic(schemName, schemOwner); @@ -77,7 +70,7 @@ public class Schematic { public static List getSchemsAccessibleByUser(int schemOwner){ try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, SchemFormat FROM Schematic WHERE SchemOwner = '" + schemOwner + "' ORDER BY SchemName"); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, SchemFormat FROM Schematic WHERE SchemOwner = ? ORDER BY SchemName", schemOwner); List schematics = new ArrayList<>(); while(schematic.next()){ schematics.add(new Schematic(schematic)); @@ -107,7 +100,7 @@ public class Schematic { public static List getAllSchemsOfType(SchematicType schemType){ try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, SchemFormat FROM Schematic WHERE SchemType = '" + schemType.toDB() + "'"); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, SchemFormat FROM Schematic WHERE SchemType = ?", schemType.toDB()); List schematics = new ArrayList<>(); while(schematic.next()){ schematics.add(new Schematic(schematic)); @@ -156,7 +149,7 @@ public class Schematic { if(Core.getVersion() <= 12 && schemFormat) throw new WrongVersionException(); - ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = " + schemID); + ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); try { rs.next(); Blob schemData = rs.getBlob("SchemData"); @@ -186,7 +179,7 @@ public class Schematic { if(Core.getVersion() <= 12 && schemFormat) throw new WrongVersionException(); - ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = " + schemID); + ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); try { rs.next(); Blob blob = rs.getBlob("SchemData"); @@ -227,34 +220,31 @@ public class Schematic { private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { try{ - PreparedStatement st = SQL.getCon().prepareStatement("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = " + schemID); - byte[] data; + Blob blob = SQL.blob(); switch(Core.getVersion()){ case 8: newFormat = false; - data = Schematic_8.getPlayerClipboard(player); + blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); break; case 9: newFormat = false; - data = Schematic_9.getPlayerClipboard(player); + blob.setBytes(1, Schematic_9.getPlayerClipboard(player)); break; case 10: newFormat = false; - data = Schematic_10.getPlayerClipboard(player); + blob.setBytes(1, Schematic_10.getPlayerClipboard(player)); break; case 14: - data = Schematic_14.getPlayerClipboard(player, newFormat); + blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); break; case 15: - data = Schematic_15.getPlayerClipboard(player, newFormat); + blob.setBytes(1, Schematic_15.getPlayerClipboard(player, newFormat)); break; default: newFormat = false; - data = Schematic_12.getPlayerClipboard(player); + blob.setBytes(1, Schematic_12.getPlayerClipboard(player)); } - st.setBlob(1, new ByteArrayInputStream(data)); - st.setBoolean(2, newFormat); - st.executeUpdate(); + SQL.update("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = ?", blob, newFormat, schemID); schemFormat = newFormat; }catch(SQLException e){ throw new IOException(e); @@ -262,8 +252,8 @@ public class Schematic { } public void remove(){ - SQL.update("DELETE FROM SchemMember WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "'"); - SQL.update("DELETE FROM Schematic WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "'"); + SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); + SQL.update("DELETE FROM Schematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); } 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 index 4fb7c5e..3a22741 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java @@ -28,10 +28,7 @@ public class SchematicMember { } private void updateDB(){ - SQL.update("INSERT INTO SchemMember" + - " (SchemName, SchemOwner, Member)" + - " VALUES" + - " ('" + schemName + "', '" + schemOwner + "', '" + member + "')"); + SQL.update("INSERT INTO SchemMember (SchemName, SchemOwner, Member) VALUES (?, ?, ?)", schemName, schemOwner, member); } public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){ @@ -39,7 +36,7 @@ public class SchematicMember { } public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = '" + schemName + "' AND SchemOwner = '" + schemOwner + "' AND Member = '" + schemMember + "'"); + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ? AND Member = ?", schemName, schemOwner, schemMember); try { if(schematicMember == null || !schematicMember.next()){ return null; @@ -51,7 +48,7 @@ public class SchematicMember { } public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = '" + schemName + "' AND Member = '" + schemMember + "'"); + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND Member = ?", schemName, schemMember); try { if(schematicMember == null || !schematicMember.next()){ return null; @@ -68,7 +65,7 @@ public class SchematicMember { } public static List getSchemMembers(String schemName, int schemOwner){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = '" + schemName + "' AND SchemOwner = '" + schemOwner + "'"); + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); try { List schematicMembers = new ArrayList<>(); while(schematicMember.next()){ @@ -86,7 +83,7 @@ public class SchematicMember { } public static List getAccessibleSchems(int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE Member = '" + schemMember + "'"); + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE Member = ?", schemMember); try { List schematicMembers = new ArrayList<>(); while(schematicMember.next()){ @@ -113,6 +110,6 @@ public class SchematicMember { } public void remove(){ - SQL.update("DELETE FROM SchemMember WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "' AND Member = '" + member + "'"); + SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?", schemOwner, schemName, member); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java index 55a1df2..ea2eac9 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java @@ -63,8 +63,8 @@ public class SteamwarUser { return team; } - private static SteamwarUser fromDB(String statement){ - ResultSet rs = SQL.select(statement); + private static SteamwarUser fromDB(String statement, Object identifier){ + ResultSet rs = SQL.select(statement, identifier); try { if(rs.next()) return new SteamwarUser(rs); @@ -75,24 +75,23 @@ public class SteamwarUser { } public static SteamwarUser get(String userName){ - userName = SQL.disarmString(userName); SteamwarUser user = byName.get(userName.toLowerCase()); if(user == null) - user = fromDB("SELECT * FROM UserData WHERE lower(UserName) = '" + userName.toLowerCase() + "'"); + user = fromDB("SELECT * FROM UserData WHERE lower(UserName) = ?", 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 = fromDB("SELECT * FROM UserData WHERE UUID = ?", 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 = fromDB("SELECT * FROM UserData WHERE id = ?", id); return user; } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Team.java b/SpigotCore_Main/src/de/steamwar/sql/Team.java index f82680e..2edd036 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Team.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Team.java @@ -27,7 +27,7 @@ public class Team { public static Team get(int id){ if(id == 0) return pub; - ResultSet rs = SQL.select("SELECT * FROM Team WHERE TeamID = " + id); + ResultSet rs = SQL.select("SELECT * FROM Team WHERE TeamID = ?", id); try { if(!rs.next()) return null; @@ -55,7 +55,7 @@ public class Team { public List getMembers(){ try{ - ResultSet memberlist = SQL.select("SELECT id FROM UserData WHERE Team = '" + teamId + "'"); + ResultSet memberlist = SQL.select("SELECT id FROM UserData WHERE Team = ?", teamId); List members = new LinkedList<>(); while(memberlist.next()) members.add(memberlist.getInt("id")); From b5c1eb082363b36ed5c453f722d016573f2b063b Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 10 Feb 2020 14:58:13 +0100 Subject: [PATCH 30/30] Fixing NPE on disconnect --- .../de/steamwar/scoreboard/SWScoreboard.java | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java index 9ccf6db..958b08d 100644 --- a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java +++ b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java @@ -45,25 +45,29 @@ public class SWScoreboard { if(!playerBoards.containsKey(player)) return; - switch (Core.getVersion()) { - case 8: - ((SWScoreboard_8) playerBoards.get(player)).deleteScoreboard(player); - break; - case 9: - ((SWScoreboard_9) playerBoards.get(player)).deleteScoreboard(player); - break; - case 10: - ((SWScoreboard_10) playerBoards.get(player)).deleteScoreboard(player); - break; - case 12: - ((SWScoreboard_12) playerBoards.get(player)).deleteScoreboard(player); - break; - case 14: - ((SWScoreboard_14) playerBoards.get(player)).deleteScoreboard(player); - break; - case 15: - ((SWScoreboard_15) playerBoards.get(player)).deleteScoreboard(player); - break; + try{ + switch (Core.getVersion()) { + case 8: + ((SWScoreboard_8) playerBoards.get(player)).deleteScoreboard(player); + break; + case 9: + ((SWScoreboard_9) playerBoards.get(player)).deleteScoreboard(player); + break; + case 10: + ((SWScoreboard_10) playerBoards.get(player)).deleteScoreboard(player); + break; + case 12: + ((SWScoreboard_12) playerBoards.get(player)).deleteScoreboard(player); + break; + case 14: + ((SWScoreboard_14) playerBoards.get(player)).deleteScoreboard(player); + break; + case 15: + ((SWScoreboard_15) playerBoards.get(player)).deleteScoreboard(player); + break; + } + }catch(NullPointerException e){ + //ignored } playerBoards.remove(player); }