Merge pull request #6 from AtlasMediaGroup/FS-151

Improve plugin performance (FS-151)
This commit is contained in:
Ryan 2021-04-10 20:00:59 +01:00 committed by GitHub
commit a23247d70f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 162 additions and 129 deletions

View file

@ -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;
@ -20,6 +24,11 @@ import org.bukkit.plugin.java.JavaPlugin;
public final class TFGuilds extends JavaPlugin
{
// TEMP FIX UNTIL REWRITE
public Map<String, Guild> guilds;
public Map<String, GuildWarp> warps;
private static TFGuilds plugin;
public static TFGuilds getPlugin()
@ -42,12 +51,16 @@ public final class TFGuilds extends JavaPlugin
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());

View file

@ -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;

View file

@ -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;

View file

@ -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 + "\"."));

View file

@ -289,7 +289,7 @@ public class Guild
public static List<String> getGuildList()
{
List<String> 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<String> getGuildWarps()
{
List<String> 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);
}

View file

@ -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());
}
}

View file

@ -10,79 +10,80 @@ 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 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()
{
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();
}
}

View file

@ -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<Guild> 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<Guild> 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)
{

View file

@ -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());

View file

@ -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();

View file

@ -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<GuildWarp> 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<GuildWarp> 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)
{

View file

@ -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();