From 42d44bafe1fe0e4b7dd511af633bc9b716d9141b Mon Sep 17 00:00:00 2001 From: William278 Date: Thu, 17 Dec 2020 13:29:16 +0000 Subject: [PATCH] Optimised database retrieving data --- .../TabCompleters/homeTabCompleter.java | 7 +- .../william278/huskhomes2/Data/Database.java | 299 +++--------------- .../me/william278/huskhomes2/Data/SQL.java | 49 +-- .../huskhomes2/Events/onPlayerJoin.java | 9 +- .../me/william278/huskhomes2/HuskHomes.java | 5 + .../me/william278/huskhomes2/dataManager.java | 209 ++---------- .../william278/huskhomes2/settingHandler.java | 3 + 7 files changed, 130 insertions(+), 451 deletions(-) diff --git a/src/main/java/me/william278/huskhomes2/Commands/TabCompleters/homeTabCompleter.java b/src/main/java/me/william278/huskhomes2/Commands/TabCompleters/homeTabCompleter.java index 7bc27ab4..a892ce6c 100644 --- a/src/main/java/me/william278/huskhomes2/Commands/TabCompleters/homeTabCompleter.java +++ b/src/main/java/me/william278/huskhomes2/Commands/TabCompleters/homeTabCompleter.java @@ -36,7 +36,12 @@ public class homeTabCompleter implements TabCompleter { if (args.length == 1) { final List tabCompletions = new ArrayList<>(); - StringUtil.copyPartialMatches(args[0], homeTabCache.get(p.getUniqueId()), tabCompletions); + ArrayList homes = homeTabCache.get(p.getUniqueId()); + if (homes == null) { + updatePlayerHomeCache(p); + return new ArrayList<>(); + } + StringUtil.copyPartialMatches(args[0], homes, tabCompletions); Collections.sort(tabCompletions); diff --git a/src/main/java/me/william278/huskhomes2/Data/Database.java b/src/main/java/me/william278/huskhomes2/Data/Database.java index 399275b2..8f801c27 100644 --- a/src/main/java/me/william278/huskhomes2/Data/Database.java +++ b/src/main/java/me/william278/huskhomes2/Data/Database.java @@ -64,8 +64,8 @@ public abstract class Database { // Insert a home into the database public void addHome(Home home, int playerID) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -80,22 +80,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Insert a warp into the database public void addWarp(Warp warp) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -111,22 +102,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Delete a warp public void deleteWarp(String warpName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -138,22 +120,13 @@ public abstract class Database { } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Delete a warp public void deleteHome(String homeName, int playerID) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -166,22 +139,13 @@ public abstract class Database { } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Insert a player into the database public void addPlayer(Player p) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -196,22 +160,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Insert a player into the database public void addPlayer(UUID playerUUID, String playerName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -226,22 +181,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Insert a player into the database public void addPlayer(UUID playerUUID, String playerName, int homeSlots) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -256,21 +202,12 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } public void updatePlayerUsername(int playerID, String newName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -280,21 +217,12 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } public void setPlayerTeleporting(int playerID, boolean value) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -304,21 +232,12 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } public void setPlayerHomeSlots(int playerID, int newValue) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -328,21 +247,12 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } public void setRtpCooldown(int playerID, int newTime) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -352,22 +262,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Delete a teleportation point from SQL public void deleteTeleportationPoint(int locationID) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -378,22 +279,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update the location of a home public void setHomePrivacy(String homeName, int ownerID, boolean isPublic) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -406,22 +298,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update the location of a home public void setHomeTeleportPoint(String homeName, int ownerID, TeleportationPoint point) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -437,22 +320,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update the location of a home public void setWarpTeleportPoint(String warpName, TeleportationPoint point) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -467,21 +341,12 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update the description of a home public void setHomeDescription(String homeName, int ownerID, String newDescription) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -494,22 +359,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update the description of a warp public void setWarpDescription(String warpName, String newDescription) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -521,22 +377,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update the name of a home public void setHomeName(String oldHomeName, int ownerID, String newHomeName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -550,22 +397,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update the name of a warp public void setWarpName(String oldWarpName, String newWarpName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -578,22 +416,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update a player's last position teleport point public void setTeleportationLastPosition(int playerID, TeleportationPoint point) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -608,22 +437,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Update a player's destination teleport point public void setTeleportationDestination(int playerID, TeleportationPoint point) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -639,22 +459,13 @@ public abstract class Database { } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Clear a player's destination public void clearPlayerDestination(int playerID) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -663,22 +474,13 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } // Clear a player's last position public void clearPlayerLastPosition(int playerID) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; try { conn = getSQLConnection(); @@ -687,15 +489,6 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } diff --git a/src/main/java/me/william278/huskhomes2/Data/SQL.java b/src/main/java/me/william278/huskhomes2/Data/SQL.java index 41cbf181..06aa5b6e 100644 --- a/src/main/java/me/william278/huskhomes2/Data/SQL.java +++ b/src/main/java/me/william278/huskhomes2/Data/SQL.java @@ -2,6 +2,7 @@ package me.william278.huskhomes2.Data; import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.dataManager; +import org.bukkit.Bukkit; import java.io.File; import java.io.IOException; @@ -15,12 +16,18 @@ public class SQL extends Database { final String SQLiteDatabaseName = "HuskHomesData"; + private Connection connection; + + private void setConnection(Connection connection) { + this.connection = connection; + } + public SQL(HuskHomes instance) { super(instance); } // Initialise connection via mySQL - private Connection getMySQLConnection() { + private void getMySQLConnection() { String host = HuskHomes.settings.getMySQLhost(); int port = HuskHomes.settings.getMySQLport(); String database = HuskHomes.settings.getMySQLdatabase(); @@ -30,7 +37,7 @@ public class SQL extends Database { try { synchronized (HuskHomes.getInstance()) { Class.forName("com.mysql.jdbc.Driver"); - return DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&useSSL=false", username, password); + setConnection(DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&useSSL=false", username, password)); } } catch (SQLException e) { HuskHomes.disablePlugin("[!] Could not connect to the mySQL Database with the credentials provided!\n[!] Check that your host IP address and port are valid and that your username and password are valid and that the user has the correct access permissions on the database."); @@ -38,11 +45,10 @@ public class SQL extends Database { HuskHomes.disablePlugin("[!] A critical exception occurred when attempting to establish a connection to the mySQL database!"); e.printStackTrace(); } - return null; } // Initialise connection via SQLite - private Connection getSQLiteConnection() { + private void getSQLiteConnection() { File dataFolder = new File(plugin.getDataFolder(), SQLiteDatabaseName + ".db"); if (!dataFolder.exists()) { try { @@ -54,31 +60,36 @@ public class SQL extends Database { } } try { - if (connection != null && !connection.isClosed()) { - return connection; - } Class.forName("org.sqlite.JDBC"); - connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + SQLiteDatabaseName + ".db"); - return connection; + setConnection(DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + SQLiteDatabaseName + ".db")); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex); } catch (ClassNotFoundException ex) { plugin.getLogger().log(Level.SEVERE, "You need the SQLite JBDC library; please download and place this in the /lib folder."); } - return null; } public Connection getSQLConnection() { - String dataStorageType = HuskHomes.settings.getStorageType().toLowerCase(); - switch (dataStorageType) { - case "mysql": - return getMySQLConnection(); - case "sqlite": - return getSQLiteConnection(); - default: - plugin.getLogger().log(Level.WARNING, "An invalid data storage type was specified in config.yml; defaulting to SQLite"); - return getSQLiteConnection(); + try { + if (connection == null || connection.isClosed()) { + String dataStorageType = HuskHomes.settings.getStorageType().toLowerCase(); + switch (dataStorageType) { + case "mysql": + getMySQLConnection(); + break; + case "sqlite": + getSQLiteConnection(); + break; + default: + plugin.getLogger().log(Level.WARNING, "An invalid data storage type was specified in config.yml; defaulting to SQLite"); + getSQLiteConnection(); + break; + } + } + } catch (SQLException ex) { + plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection"); } + return connection; } public void load() { diff --git a/src/main/java/me/william278/huskhomes2/Events/onPlayerJoin.java b/src/main/java/me/william278/huskhomes2/Events/onPlayerJoin.java index c95f6be6..af48f246 100644 --- a/src/main/java/me/william278/huskhomes2/Events/onPlayerJoin.java +++ b/src/main/java/me/william278/huskhomes2/Events/onPlayerJoin.java @@ -1,5 +1,6 @@ package me.william278.huskhomes2.Events; +import me.william278.huskhomes2.Commands.TabCompleters.homeTabCompleter; import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.dataManager; import me.william278.huskhomes2.teleportManager; @@ -17,9 +18,15 @@ public class onPlayerJoin implements Listener { // Create player on SQL if they don't exist already if (!dataManager.playerExists(p)) { dataManager.createPlayer(p); - p.teleport(teleportManager.spawnLocation.getLocation()); + if (teleportManager.spawnLocation != null) { + p.teleport(teleportManager.spawnLocation.getLocation()); + } } else { + // Check if they've changed their name and update if so dataManager.checkPlayerNameChange(p); + + // Update their TAB cache for /home command + homeTabCompleter.updatePlayerHomeCache(p); } // If bungee mode, check if the player joined the server from a teleport and act accordingly diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index 873de083..2fda3a63 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -47,7 +47,9 @@ public final class HuskHomes extends JavaPlugin { // Register tab completers private static void registerTabCompleters(HuskHomes plugin) { plugin.getCommand("home").setTabCompleter(new homeTabCompleter()); + plugin.getCommand("delhome").setTabCompleter(new homeTabCompleter()); plugin.getCommand("warp").setTabCompleter(new warpTabCompleter()); + plugin.getCommand("delwarp").setTabCompleter(new warpTabCompleter()); plugin.getCommand("publichome").setTabCompleter(new publicHomeTabCompleter()); plugin.getCommand("edithome").setTabCompleter(new editHomeTabCompleter()); plugin.getCommand("editwarp").setTabCompleter(new editWarpTabCompleter()); @@ -68,6 +70,9 @@ public final class HuskHomes extends JavaPlugin { plugin.getCommand("setspawn").setTabCompleter(new emptyTabCompleter()); plugin.getCommand("back").setTabCompleter(new emptyTabCompleter()); + // Update caches + publicHomeTabCompleter.updatePublicHomeTabCache(); + warpTabCompleter.updateWarpsTabCache(); } // Register commands diff --git a/src/main/java/me/william278/huskhomes2/dataManager.java b/src/main/java/me/william278/huskhomes2/dataManager.java index dd3b9de1..42a19f5b 100644 --- a/src/main/java/me/william278/huskhomes2/dataManager.java +++ b/src/main/java/me/william278/huskhomes2/dataManager.java @@ -51,7 +51,6 @@ public class dataManager { private static Database database; private static Connection getConnection() { - initializeDatabase(); return database.getSQLConnection(); } @@ -62,8 +61,8 @@ public class dataManager { // Return a player's ID from their UUID private static Integer getPlayerId(UUID playerUUID) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -82,23 +81,14 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } // Return a player's ID from their username private static Integer getPlayerId(String playerUsername) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -117,23 +107,14 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } // Return an integer from the player table from a player ID public static Integer getPlayerInteger(Integer playerID, String column) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -153,15 +134,6 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } @@ -178,8 +150,8 @@ public class dataManager { // Return an integer from the player table from a player ID public static String getPlayerString(Integer playerID, String column) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -199,23 +171,14 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } // Return an integer from the player table from a player ID public static Boolean getPlayerTeleporting(Player p) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; Integer playerID = getPlayerId(p.getUniqueId()); ResultSet rs; @@ -236,15 +199,6 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } @@ -291,8 +245,8 @@ public class dataManager { // Return a player's homes. public static ArrayList getPlayerHomes(String playerName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; Integer playerID = getPlayerId(playerName); ResultSet rs; @@ -321,23 +275,14 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } // Return all the public homes public static ArrayList getPublicHomes() { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -361,23 +306,14 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } // Return an array of all the warps public static ArrayList getWarps() { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -401,23 +337,14 @@ public class dataManager { return warps; } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } // Return a warp with a given name (warp names are unique) public static Warp getWarp(String name) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -444,22 +371,13 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } public static void deleteHomeTeleportLocation(int ownerID, String homeName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -478,21 +396,12 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } public static void deleteWarpTeleportLocation(String warpName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -510,15 +419,6 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } } @@ -558,8 +458,8 @@ public class dataManager { // Return a home with a given owner username and home name public static Home getHome(String ownerUsername, String homeName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; Integer playerID = getPlayerId(ownerUsername); ResultSet rs; @@ -591,22 +491,13 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } public static Boolean warpExists(String warpName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -622,15 +513,6 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } @@ -640,8 +522,8 @@ public class dataManager { } public static Boolean homeExists(String ownerName, String homeName) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; Integer playerID = getPlayerId(ownerName); @@ -664,15 +546,6 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } @@ -682,8 +555,8 @@ public class dataManager { } public static Boolean playerExists(UUID playerUUID) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; String playerUUIDString = playerUUID.toString(); @@ -700,22 +573,13 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } public static TeleportationPoint getTeleportationPoint(Integer locationID) { - Connection conn = null; - PreparedStatement ps = null; + Connection conn; + PreparedStatement ps; ResultSet rs; try { @@ -736,15 +600,6 @@ public class dataManager { } } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } } return null; } diff --git a/src/main/java/me/william278/huskhomes2/settingHandler.java b/src/main/java/me/william278/huskhomes2/settingHandler.java index 35d48b67..b35ad441 100644 --- a/src/main/java/me/william278/huskhomes2/settingHandler.java +++ b/src/main/java/me/william278/huskhomes2/settingHandler.java @@ -139,6 +139,9 @@ public class settingHandler { try { FileConfiguration config = plugin.getConfig(); String worldName = (config.getString("spawn_command.position.world")); + if (worldName == null || worldName.equals("")) { + return null; + } double x = (config.getDouble("spawn_command.position.x")); double y = (config.getDouble("spawn_command.position.y")); double z = (config.getDouble("spawn_command.position.z"));