From 9d58522fc6a884ae9657c492b1f8a6539976bec7 Mon Sep 17 00:00:00 2001 From: Nathan Curran <30569566+Focusvity@users.noreply.github.com> Date: Mon, 5 Apr 2021 22:26:29 +1000 Subject: [PATCH 1/2] Improve plugin performance (FS-151) This is what Freedom-01 is running on and this will have to do until we start a rewrite. --- .../me/totalfreedom/tfguilds/TFGuilds.java | 13 +++ .../command/DeleteWarpSubcommand.java | 2 +- .../tfguilds/command/SetWarpSubcommand.java | 2 +- .../tfguilds/command/WarpSubcommand.java | 4 +- .../me/totalfreedom/tfguilds/guild/Guild.java | 18 ++- .../tfguilds/listener/ChatListener.java | 2 - .../tfguilds/sql/SQLDatabase.java | 109 +++++++++--------- .../tfguilds/sql/SQLGuildData.java | 45 ++++---- .../tfguilds/sql/SQLRankData.java | 18 ++- .../tfguilds/sql/SQLUserData.java | 21 ++-- .../tfguilds/sql/SQLWarpData.java | 44 ++++--- .../tfguilds/sql/SQLWorldData.java | 15 ++- 12 files changed, 164 insertions(+), 129 deletions(-) diff --git a/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java b/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java index 2fff217..71d9793 100644 --- a/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java +++ b/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java @@ -1,11 +1,15 @@ package me.totalfreedom.tfguilds; +import java.util.HashMap; +import java.util.Map; import me.totalfreedom.tfguilds.bridge.TFMBridge; import me.totalfreedom.tfguilds.command.GuildChatCommand; import me.totalfreedom.tfguilds.command.GuildChatSpyCommand; import me.totalfreedom.tfguilds.command.GuildCommand; import me.totalfreedom.tfguilds.command.TFGuildsCommand; import me.totalfreedom.tfguilds.config.Config; +import me.totalfreedom.tfguilds.guild.Guild; +import me.totalfreedom.tfguilds.guild.GuildWarp; import me.totalfreedom.tfguilds.listener.ChatListener; import me.totalfreedom.tfguilds.listener.JoinListener; import me.totalfreedom.tfguilds.sql.SQLDatabase; @@ -36,18 +40,27 @@ public final class TFGuilds extends JavaPlugin public SQLWarpData warpData; public SQLWorldData worldData; + + // TEMP FIX UNTIL REWRITE + public Map guilds; + public Map warps; + @Override public void onEnable() { plugin = this; config = new Config("config.yml"); bridge = new TFMBridge(); + guilds = new HashMap<>(); + warps = new HashMap<>(); sql = new SQLDatabase(); guildData = new SQLGuildData(); rankData = new SQLRankData(); userData = new SQLUserData(); warpData = new SQLWarpData(); worldData = new SQLWorldData(); + guildData.getAll(); + warpData.getAll(); loadCommands(); loadListeners(); GLog.info("Enabled " + this.getDescription().getFullName()); diff --git a/src/main/java/me/totalfreedom/tfguilds/command/DeleteWarpSubcommand.java b/src/main/java/me/totalfreedom/tfguilds/command/DeleteWarpSubcommand.java index 1e2fb53..9d284f1 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/DeleteWarpSubcommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/DeleteWarpSubcommand.java @@ -43,7 +43,7 @@ public class DeleteWarpSubcommand extends Common implements CommandExecutor } String warpName = StringUtils.join(args, " ", 1, args.length); - if (!Guild.warpExists(guild.getIdentifier(), warpName)) + if (!guild.warpExists(warpName)) { sender.sendMessage(ChatColor.RED + "Warp not found."); return true; diff --git a/src/main/java/me/totalfreedom/tfguilds/command/SetWarpSubcommand.java b/src/main/java/me/totalfreedom/tfguilds/command/SetWarpSubcommand.java index 5e309f9..e58860d 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/SetWarpSubcommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/SetWarpSubcommand.java @@ -42,7 +42,7 @@ public class SetWarpSubcommand extends Common implements CommandExecutor } String warpName = StringUtils.join(args, " ", 1, args.length); - if (Guild.warpExists(guild.getIdentifier(), warpName)) + if (guild.warpExists(warpName)) { sender.sendMessage(ChatColor.RED + "A warp with that name already exists."); return true; diff --git a/src/main/java/me/totalfreedom/tfguilds/command/WarpSubcommand.java b/src/main/java/me/totalfreedom/tfguilds/command/WarpSubcommand.java index b6c4e3b..245a11d 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/WarpSubcommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/WarpSubcommand.java @@ -38,13 +38,13 @@ public class WarpSubcommand extends Common implements CommandExecutor } String warpName = StringUtils.join(args, " ", 1, args.length); - if (!Guild.warpExists(guild.getIdentifier(), warpName)) + if (!guild.warpExists(warpName)) { sender.sendMessage(ChatColor.RED + "Warp not found."); return true; } - GuildWarp warp = plugin.warpData.get(guild.getIdentifier(), warpName); + GuildWarp warp = guild.getWarp(warpName); Location warpLoc = new Location(warp.getWorld(), warp.getX(), warp.getY(), warp.getZ()); player.teleport(warpLoc); sender.sendMessage(tl(PREFIX + "Warping to \"" + warpName + "\".")); diff --git a/src/main/java/me/totalfreedom/tfguilds/guild/Guild.java b/src/main/java/me/totalfreedom/tfguilds/guild/Guild.java index 9925154..52a060b 100644 --- a/src/main/java/me/totalfreedom/tfguilds/guild/Guild.java +++ b/src/main/java/me/totalfreedom/tfguilds/guild/Guild.java @@ -289,7 +289,7 @@ public class Guild public static List getGuildList() { List g = new ArrayList<>(); - for (Guild guild : plugin.guildData.getAll()) + for (Guild guild : plugin.guilds.values()) { g.add(guild.getName()); } @@ -299,13 +299,23 @@ public class Guild public static List getGuildWarps() { List warps = new ArrayList<>(); - for (GuildWarp warp : plugin.warpData.getAll()) + for (GuildWarp warp : plugin.warps.values()) { warps.add(warp.getWarpName()); } return warps; } + public GuildWarp getWarp(String warpName) + { + GuildWarp warp = plugin.warps.get(identifier); + if (warp != null && warp.getWarpName().equalsIgnoreCase(warpName)) + { + return warp; + } + return null; + } + public String getInformation() { return Common.tl(Common.PREFIX + "Guild Information\n" + @@ -393,7 +403,7 @@ public class Guild public static Guild getGuild(String identifier) { - return plugin.guildData.get(identifier); + return plugin.guilds.get(identifier); } public static Guild getGuild(Player player) @@ -416,7 +426,7 @@ public class Guild return GuildWarp.createGuildWarp(identifier, warpName, player); } - public static boolean warpExists(String identifier, String warpName) + public boolean warpExists(String warpName) { return plugin.warpData.exists(identifier, warpName); } diff --git a/src/main/java/me/totalfreedom/tfguilds/listener/ChatListener.java b/src/main/java/me/totalfreedom/tfguilds/listener/ChatListener.java index 2dfe501..bd3cc51 100644 --- a/src/main/java/me/totalfreedom/tfguilds/listener/ChatListener.java +++ b/src/main/java/me/totalfreedom/tfguilds/listener/ChatListener.java @@ -84,8 +84,6 @@ public class ChatListener implements Listener if (guild.hasTag()) { - // This seems to result in the entry being logged twice on the console, which is silly... Not sure if there was a good reason for it. - //System.out.println(GUtil.colorize(guild.getTag().replace("%rank%", display)) + ChatColor.RESET + " " + e.getFormat()); e.setFormat(GUtil.colorize(guild.getTag().replace("%rank%", display)) + ChatColor.RESET + " " + e.getFormat()); } } diff --git a/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java b/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java index ad9769b..6743a19 100644 --- a/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java +++ b/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java @@ -10,79 +10,82 @@ import me.totalfreedom.tfguilds.TFGuilds; public class SQLDatabase { private static final String DATABASE_FILENAME = "database.db"; - private static final TFGuilds plugin = TFGuilds.getPlugin(); - - private final File file; + private final File FILE; + private Connection connection; public SQLDatabase() { - File file = new File(plugin.getDataFolder(), DATABASE_FILENAME); + File file = new File(TFGuilds.getPlugin().getDataFolder(), DATABASE_FILENAME); if (!file.exists()) { try { file.createNewFile(); - plugin.saveResource(DATABASE_FILENAME, false); + TFGuilds.getPlugin().saveResource(DATABASE_FILENAME, false); } catch (IOException ex) { ex.printStackTrace(); } } - this.file = file; - } - - public Connection getConnection() - { + this.FILE = file; try { - Connection connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath()); - connection.prepareStatement("CREATE TABLE IF NOT EXISTS `users` (\n" + - "\t`id` INT,\n" + - "\t`uuid` TINYTEXT,\n" + - "\t`tag` BOOL\n" + - ");").execute(); - connection.prepareStatement("CREATE TABLE IF NOT EXISTS `worlds` (\n" + - "\t`id` SMALLINT,\n" + - "\t`name` TINYTEXT\n" + - ");").execute(); - connection.prepareStatement("CREATE TABLE IF NOT EXISTS `ranks` (\n" + - "\t`guild_identifier` TEXT,\n" + - "\t`identifier` TEXT,\n" + - "\t`name` TEXT,\n" + - "\t`members` TEXT\n" + - ");").execute(); - connection.prepareStatement("CREATE TABLE IF NOT EXISTS `guilds` (\n" + - "\t`identifier` TEXT,\n" + - "\t`name` TEXT,\n" + - "\t`owner` INT,\n" + - "\t`moderators` TEXT,\n" + - "\t`members` TEXT,\n" + - "\t`tag` TEXT,\n" + - "\t`state` TINYINT,\n" + - "\t`ranks` TEXT,\n" + - "\t`motd` TEXT,\n" + - "\t`x` DOUBLE,\n" + - "\t`y` DOUBLE,\n" + - "\t`z` DOUBLE,\n" + - "\t`world` SMALLINT,\n" + - "\t`default_rank` TEXT,\n" + - "\t`creation` BIGINT\n" + - ");").execute(); - connection.prepareStatement("CREATE TABLE IF NOT EXISTS `warps` (\n" + - "\t`identifier` TEXT,\n" + - "\t`warp_name` TEXT,\n" + - "\t`x` DOUBLE,\n" + - "\t`y` DOUBLE,\n" + - "\t`z` DOUBLE,\n" + - "\t`world` SMALLINT\n" + - ");").execute(); - return connection; + connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath()); + createTables(); } catch (SQLException ex) { ex.printStackTrace(); } - return null; + } + + public Connection getConnection() + { + return connection; + } + + private void createTables() throws SQLException + { + connection.prepareStatement("CREATE TABLE IF NOT EXISTS `users` (\n" + + "\t`id` INT,\n" + + "\t`uuid` TINYTEXT,\n" + + "\t`tag` BOOL\n" + + ");").execute(); + connection.prepareStatement("CREATE TABLE IF NOT EXISTS `worlds` (\n" + + "\t`id` SMALLINT,\n" + + "\t`name` TINYTEXT\n" + + ");").execute(); + connection.prepareStatement("CREATE TABLE IF NOT EXISTS `ranks` (\n" + + "\t`guild_identifier` TEXT,\n" + + "\t`identifier` TEXT,\n" + + "\t`name` TEXT,\n" + + "\t`members` TEXT\n" + + ");").execute(); + connection.prepareStatement("CREATE TABLE IF NOT EXISTS `guilds` (\n" + + "\t`identifier` TEXT,\n" + + "\t`name` TEXT,\n" + + "\t`owner` INT,\n" + + "\t`moderators` TEXT,\n" + + "\t`members` TEXT,\n" + + "\t`tag` TEXT,\n" + + "\t`state` TINYINT,\n" + + "\t`ranks` TEXT,\n" + + "\t`motd` TEXT,\n" + + "\t`x` DOUBLE,\n" + + "\t`y` DOUBLE,\n" + + "\t`z` DOUBLE,\n" + + "\t`world` SMALLINT,\n" + + "\t`default_rank` TEXT,\n" + + "\t`creation` BIGINT\n" + + ");").execute(); + connection.prepareStatement("CREATE TABLE IF NOT EXISTS `warps` (\n" + + "\t`identifier` TEXT,\n" + + "\t`warp_name` TEXT,\n" + + "\t`x` DOUBLE,\n" + + "\t`y` DOUBLE,\n" + + "\t`z` DOUBLE,\n" + + "\t`world` SMALLINT\n" + + ");").execute(); } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/tfguilds/sql/SQLGuildData.java b/src/main/java/me/totalfreedom/tfguilds/sql/SQLGuildData.java index 7cef2e4..6649083 100644 --- a/src/main/java/me/totalfreedom/tfguilds/sql/SQLGuildData.java +++ b/src/main/java/me/totalfreedom/tfguilds/sql/SQLGuildData.java @@ -20,6 +20,7 @@ import org.bukkit.entity.Player; public class SQLGuildData { + private static final TFGuilds plugin = TFGuilds.getPlugin(); private static final String TABLE_NAME = "guilds"; @@ -34,23 +35,13 @@ public class SQLGuildData public boolean exists(String identifier) { - try (Connection connection = plugin.sql.getConnection()) - { - PreparedStatement statement = connection.prepareStatement(SELECT); - statement.setString(1, identifier); - ResultSet set = statement.executeQuery(); - return set.next(); - } - catch (SQLException ex) - { - ex.printStackTrace(); - } - return false; + return plugin.guilds.containsKey(identifier); } public Guild get(String identifier) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT); statement.setString(1, identifier); @@ -103,7 +94,7 @@ public class SQLGuildData public Guild get(Player player) { - for (Guild guild : getAll()) + for (Guild guild : plugin.guilds.values()) { if (guild.getMembers().contains(player.getUniqueId())) { @@ -113,29 +104,29 @@ public class SQLGuildData return null; } - public List getAll() + public void getAll() { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT_ALL); ResultSet set = statement.executeQuery(); - List guilds = new ArrayList<>(); while (set.next()) { - guilds.add(get(set.getString("identifier"))); + String identifier = set.getString("identifier"); + plugin.guilds.put(identifier, get(identifier)); } - return guilds; } catch (SQLException ex) { ex.printStackTrace(); } - return null; } public Guild create(String identifier, String name, Player owner) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(INSERT); statement.setString(1, identifier); @@ -156,9 +147,11 @@ public class SQLGuildData long creation = System.currentTimeMillis(); statement.setLong(15, creation); statement.execute(); - return new Guild(identifier, name, owner.getUniqueId(), Collections.singletonList(owner.getUniqueId()), new ArrayList<>(), + Guild guild = new Guild(identifier, name, owner.getUniqueId(), Collections.singletonList(owner.getUniqueId()), new ArrayList<>(), ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + name + ChatColor.DARK_GRAY + "]", GuildState.INVITE_ONLY, new ArrayList<>(), null, new Location(Bukkit.getWorlds().get(0), 0.0, 100.0, 0.0), creation, null); + plugin.guilds.put(identifier, guild); + return guild; } catch (SQLException ex) { @@ -169,7 +162,8 @@ public class SQLGuildData public void save(Guild guild, String identifier) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(UPDATE); statement.setString(1, guild.getIdentifier()); @@ -204,6 +198,7 @@ public class SQLGuildData statement.setString(14, guild.getDefaultRank()); statement.setString(15, identifier); statement.execute(); + plugin.guilds.put(identifier, guild); } catch (SQLException ex) { @@ -218,11 +213,13 @@ public class SQLGuildData public void delete(Guild guild) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(DELETE); statement.setString(1, guild.getIdentifier()); statement.execute(); + plugin.guilds.remove(guild.getIdentifier()); } catch (SQLException ex) { diff --git a/src/main/java/me/totalfreedom/tfguilds/sql/SQLRankData.java b/src/main/java/me/totalfreedom/tfguilds/sql/SQLRankData.java index 2782b83..420dcc9 100644 --- a/src/main/java/me/totalfreedom/tfguilds/sql/SQLRankData.java +++ b/src/main/java/me/totalfreedom/tfguilds/sql/SQLRankData.java @@ -25,7 +25,8 @@ public class SQLRankData public boolean exists(String guildIdentifier, String identifier) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT); statement.setString(1, guildIdentifier); @@ -42,7 +43,8 @@ public class SQLRankData public GuildRank get(String guildIdentifier, String identifier) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT); statement.setString(1, guildIdentifier); @@ -72,7 +74,8 @@ public class SQLRankData public GuildRank create(String guildIdentifier, String identifier, String name) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(INSERT); statement.setString(1, guildIdentifier); @@ -91,7 +94,8 @@ public class SQLRankData public void save(GuildRank rank) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(UPDATE); statement.setString(1, rank.getName()); @@ -113,7 +117,8 @@ public class SQLRankData public void updateGuildIdentifier(GuildRank rank, String newIdentifier) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(UPDATE_GUILD); statement.setString(1, newIdentifier); @@ -129,7 +134,8 @@ public class SQLRankData public void delete(GuildRank rank) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(DELETE); statement.setString(1, rank.getIguild()); diff --git a/src/main/java/me/totalfreedom/tfguilds/sql/SQLUserData.java b/src/main/java/me/totalfreedom/tfguilds/sql/SQLUserData.java index 141a4a7..af25ec0 100644 --- a/src/main/java/me/totalfreedom/tfguilds/sql/SQLUserData.java +++ b/src/main/java/me/totalfreedom/tfguilds/sql/SQLUserData.java @@ -22,7 +22,8 @@ public class SQLUserData public boolean exists(UUID uuid) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT); statement.setString(1, uuid.toString()); @@ -38,7 +39,8 @@ public class SQLUserData public boolean existsID(int id) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT_ID); statement.setInt(1, id); @@ -58,7 +60,8 @@ public class SQLUserData { create(uuid); } - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT); statement.setString(1, uuid.toString()); @@ -77,7 +80,8 @@ public class SQLUserData public User get(int id) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT_ID); statement.setInt(1, id); @@ -96,7 +100,8 @@ public class SQLUserData public User create(UUID uuid) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(INSERT); int id = getUserCount() + 1; @@ -115,7 +120,8 @@ public class SQLUserData public void save(User user) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(UPDATE); statement.setBoolean(1, user.isTag()); @@ -130,7 +136,8 @@ public class SQLUserData public int getUserCount() { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(COUNT); ResultSet set = statement.executeQuery(); diff --git a/src/main/java/me/totalfreedom/tfguilds/sql/SQLWarpData.java b/src/main/java/me/totalfreedom/tfguilds/sql/SQLWarpData.java index 88ed374..0cec480 100644 --- a/src/main/java/me/totalfreedom/tfguilds/sql/SQLWarpData.java +++ b/src/main/java/me/totalfreedom/tfguilds/sql/SQLWarpData.java @@ -24,24 +24,14 @@ public class SQLWarpData public boolean exists(String identifier, String warpName) { - try (Connection connection = plugin.sql.getConnection()) - { - PreparedStatement statement = connection.prepareStatement(SELECT); - statement.setString(1, identifier); - statement.setString(2, warpName); - ResultSet set = statement.executeQuery(); - return set.next(); - } - catch (SQLException ex) - { - ex.printStackTrace(); - } - return false; + GuildWarp warp = plugin.warps.get(identifier); + return warp != null && warp.getWarpName().equalsIgnoreCase(warpName); } public GuildWarp get(String identifier, String warpName) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT); statement.setString(1, identifier); @@ -60,29 +50,29 @@ public class SQLWarpData return null; } - public List getAll() + public void getAll() { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT_ALL); ResultSet set = statement.executeQuery(); - List warps = new ArrayList<>(); while (set.next()) { - warps.add(get(set.getString("identifier"), set.getString("warp_name"))); + String identifier = set.getString("identifier"); + plugin.warps.put(identifier, get(identifier, set.getString("warp_name"))); } - return warps; } catch (SQLException ex) { ex.printStackTrace(); } - return null; } public GuildWarp create(String identifier, String warpName, Player player) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(INSERT); statement.setString(1, identifier); @@ -92,8 +82,10 @@ public class SQLWarpData statement.setDouble(5, player.getLocation().getZ()); statement.setInt(6, plugin.worldData.getWorldID(player.getWorld())); statement.execute(); - return new GuildWarp(identifier, warpName, player.getLocation().getX(), player.getLocation().getY(), + GuildWarp warp = new GuildWarp(identifier, warpName, player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), player.getWorld()); + plugin.warps.put(identifier, warp); + return warp; } catch (SQLException ex) { @@ -104,7 +96,8 @@ public class SQLWarpData public void save(GuildWarp warp) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(UPDATE); statement.setString(1, warp.getIguild()); @@ -114,6 +107,7 @@ public class SQLWarpData statement.setDouble(5, warp.getZ()); statement.setInt(6, plugin.worldData.getWorldID(warp.getWorld())); statement.execute(); + plugin.warps.put(warp.getIguild(), warp); } catch (SQLException ex) { @@ -123,12 +117,14 @@ public class SQLWarpData public void delete(GuildWarp warp) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(DELETE); statement.setString(1, warp.getIguild()); statement.setString(2, warp.getWarpName()); statement.execute(); + plugin.warps.remove(warp.getIguild()); } catch (SQLException ex) { diff --git a/src/main/java/me/totalfreedom/tfguilds/sql/SQLWorldData.java b/src/main/java/me/totalfreedom/tfguilds/sql/SQLWorldData.java index 91281e5..2a776bd 100644 --- a/src/main/java/me/totalfreedom/tfguilds/sql/SQLWorldData.java +++ b/src/main/java/me/totalfreedom/tfguilds/sql/SQLWorldData.java @@ -19,7 +19,8 @@ public class SQLWorldData public boolean exists(World world) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT); statement.setString(1, world.getName()); @@ -35,7 +36,8 @@ public class SQLWorldData public boolean existsID(int id) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(SELECT_ID); statement.setInt(1, id); @@ -51,7 +53,8 @@ public class SQLWorldData public int getWorldID(World world) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { if (!exists(world)) { @@ -77,7 +80,8 @@ public class SQLWorldData public World getWorld(int id) { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { if (!existsID(id)) { @@ -100,7 +104,8 @@ public class SQLWorldData public int getWorldCount() { - try (Connection connection = plugin.sql.getConnection()) + Connection connection = plugin.sql.getConnection(); + try { PreparedStatement statement = connection.prepareStatement(COUNT); ResultSet set = statement.executeQuery(); From c4d65c21d21e26d3629b249ce696cc42f7d2a7f5 Mon Sep 17 00:00:00 2001 From: Nathan Curran <30569566+Focusvity@users.noreply.github.com> Date: Sun, 11 Apr 2021 00:03:44 +1000 Subject: [PATCH 2/2] Fix Codacy issues --- src/main/java/me/totalfreedom/tfguilds/TFGuilds.java | 10 +++++----- .../java/me/totalfreedom/tfguilds/sql/SQLDatabase.java | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java b/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java index 71d9793..c68d26b 100644 --- a/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java +++ b/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java @@ -24,6 +24,11 @@ import org.bukkit.plugin.java.JavaPlugin; public final class TFGuilds extends JavaPlugin { + + // TEMP FIX UNTIL REWRITE + public Map guilds; + public Map warps; + private static TFGuilds plugin; public static TFGuilds getPlugin() @@ -40,11 +45,6 @@ public final class TFGuilds extends JavaPlugin public SQLWarpData warpData; public SQLWorldData worldData; - - // TEMP FIX UNTIL REWRITE - public Map guilds; - public Map warps; - @Override public void onEnable() { diff --git a/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java b/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java index 6743a19..0926641 100644 --- a/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java +++ b/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java @@ -10,7 +10,6 @@ import me.totalfreedom.tfguilds.TFGuilds; public class SQLDatabase { private static final String DATABASE_FILENAME = "database.db"; - private final File FILE; private Connection connection; public SQLDatabase() @@ -28,7 +27,6 @@ public class SQLDatabase ex.printStackTrace(); } } - this.FILE = file; try { connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());