Converted HashMaps to pure H2

Interested in monitoring performance
Fixes #3
main
Etzelia 2018-10-20 13:22:32 -05:00
parent 14c844182c
commit 1848569b0a
6 changed files with 69 additions and 95 deletions

View File

@ -23,7 +23,7 @@ public class Database extends DataSource {
private void create() {
try {
Connection connection = getConnection();
final String CREATE = "CREATE TABLE IF NOT EXISTS `aliases` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `uuid` VARCHAR(36) NOT NULL, `from` VARCHAR(200) NOT NULL, `to` VARCHAR(200) NOT NULL, `type` VARCHAR(6) NOT NULL );";
final String CREATE = "CREATE TABLE IF NOT EXISTS `aliases` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `uuid` VARCHAR(36) NOT NULL, `from` VARCHAR(200) NOT NULL, `to` VARCHAR(200) NOT NULL, `type` VARCHAR(7) NOT NULL );";
PreparedStatement pst = connection.prepareStatement(CREATE);
pst.execute();
connection.close();
@ -88,6 +88,54 @@ public class Database extends DataSource {
return results;
}
public List<Alias> getAliases(String uuid, AliasType type) {
List<Alias> results = new ArrayList<>();
try {
Connection connection = getConnection();
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ? AND `type` = ?;";
PreparedStatement pst = connection.prepareStatement(SELECT);
pst.setString(1, uuid);
pst.setString(2, type.name());
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Alias alias = new Alias();
alias.setUuid(uuid);
alias.setFrom(rs.getString("from"));
alias.setTo(rs.getString("to"));
alias.setType(AliasType.valueOf(rs.getString("type")));
results.add(alias);
}
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return results;
}
public Alias getAlias(String uuid, String from) {
try {
Connection connection = getConnection();
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ? AND `from` = ?;";
PreparedStatement pst = connection.prepareStatement(SELECT);
pst.setString(1, uuid);
pst.setString(2, from);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
Alias alias = new Alias();
alias.setUuid(uuid);
alias.setFrom(rs.getString("from"));
alias.setTo(rs.getString("to"));
alias.setType(AliasType.valueOf(rs.getString("type")));
return alias;
}
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public int exists(String uuid, String from) {
try {
Connection connection = getConnection();

View File

@ -7,9 +7,10 @@ import xyz.etztech.mixtape.commands.CommandAlias;
import xyz.etztech.mixtape.commands.CommandMixtape;
import xyz.etztech.mixtape.commands.CommandSlashAlias;
import xyz.etztech.mixtape.listeners.CommandPreprocessListener;
import xyz.etztech.mixtape.listeners.PlayerJoinListener;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.logging.Logger;
public final class Mixtape extends JavaPlugin {
@ -22,8 +23,6 @@ public final class Mixtape extends JavaPlugin {
private static int loops;
private static Map<String, Map<String, String>> chatAliases = new HashMap<>();
private static Map<String, Map<String, String>> commandAliases = new HashMap<>();
private static boolean global;
@Override
@ -39,7 +38,6 @@ public final class Mixtape extends JavaPlugin {
this.getCommand("/alias").setExecutor(new CommandSlashAlias(this));
getServer().getPluginManager().registerEvents(new CommandPreprocessListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
}
@Override
@ -68,51 +66,6 @@ public final class Mixtape extends JavaPlugin {
return database;
}
public static void loadAliases(String uuid) {
Map<String, String> chats = new HashMap<>();
Map<String, String> commands = new HashMap<>();
List<Alias> aliases = database.getAliases(uuid);
for (Alias alias : aliases) {
if (alias.getType().equals(Database.AliasType.CHAT)) {
chats.put(alias.getFrom(), alias.getTo());
} else if (alias.getType().equals(Database.AliasType.COMMAND)) {
commands.put(alias.getFrom(), alias.getTo());
}
}
chatAliases.put(uuid, chats);
commandAliases.put(uuid, commands);
Bukkit.getConsoleSender().sendMessage(String.format("Loaded %s aliases for %s", aliases.size(), uuid));
}
public static void setCommandAlias(String uuid, String alias, String command) {
Map<String, String> map = new HashMap<>();
if (commandAliases.containsKey(uuid)) {
map = commandAliases.get(uuid);
}
map.put(alias, command);
commandAliases.put(uuid, map);
database.insert(uuid, alias, command, Database.AliasType.COMMAND);
}
public static Map<String, String> getCommandAliases(String uuid) {
return commandAliases.containsKey(uuid) ? commandAliases.get(uuid) : new HashMap<>();
}
public static void setChatAlias(String uuid, String alias, String command) {
Map<String, String> map = new HashMap<>();
if (chatAliases.containsKey(uuid)) {
map = chatAliases.get(uuid);
}
map.put(alias, command);
chatAliases.put(uuid, map);
database.insert(uuid, alias, command, Database.AliasType.CHAT);
}
public static Map<String, String> getChatAliases(String uuid) {
return chatAliases.containsKey(uuid) ? chatAliases.get(uuid) : new HashMap<>();
}
public static boolean getGlobal() {
return global;

View File

@ -6,6 +6,7 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.etztech.mixtape.Database;
import xyz.etztech.mixtape.Mixtape;
import xyz.etztech.mixtape.MixtapeUtil;
@ -40,7 +41,7 @@ public class CommandAlias implements CommandExecutor {
String command = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
Mixtape.setChatAlias(player.getUniqueId().toString(), alias, command);
Mixtape.getDatabase().insert(player.getUniqueId().toString(), alias, command, Database.AliasType.CHAT);
player.sendMessage(ChatColor.GREEN + "Chat alias created.");
return true;

View File

@ -6,6 +6,7 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.etztech.mixtape.Database;
import xyz.etztech.mixtape.Mixtape;
import xyz.etztech.mixtape.MixtapeUtil;
@ -40,7 +41,7 @@ public class CommandSlashAlias implements CommandExecutor {
String command = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
Mixtape.setCommandAlias(player.getUniqueId().toString(), alias, command);
Mixtape.getDatabase().insert(player.getUniqueId().toString(), alias, command, Database.AliasType.COMMAND);
player.sendMessage(ChatColor.GREEN + "Command alias created.");
return true;

View File

@ -10,6 +10,8 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import xyz.etztech.mixtape.Alias;
import xyz.etztech.mixtape.Database;
import xyz.etztech.mixtape.Mixtape;
import xyz.etztech.mixtape.MixtapeUtil;
@ -84,30 +86,30 @@ public class CommandPreprocessListener implements Listener {
}
// Command aliases
Map<String, String> commands = Mixtape.getCommandAliases(player.getUniqueId().toString());
for (String alias : commands.keySet()) {
if (alias.equalsIgnoreCase(base)) {
String resolved = resolve(commands.get(alias), args);
resolved = resolved.startsWith("/") ? resolved : "/" + resolved;
List<Alias> commands = Mixtape.getDatabase().getAliases(player.getUniqueId().toString(), Database.AliasType.COMMAND);
for (Alias alias : commands) {
if (alias.getFrom().equalsIgnoreCase(base)) {
String resolved = resolve(alias.getTo(), args);
if (!ERROR.equalsIgnoreCase(resolved)) {
resolved = resolved.startsWith("/") ? resolved : "/" + resolved;
player.chat(resolved);
} else {
player.sendMessage(ChatColor.RED + usage(base, commands.get(alias)));
player.sendMessage(ChatColor.RED + usage(base, alias.getTo()));
}
return true;
}
}
// Chat aliases
Map<String, String> chats = Mixtape.getChatAliases(player.getUniqueId().toString());
for (String alias : chats.keySet()) {
if (alias.equalsIgnoreCase(base)) {
String resolved = resolve(chats.get(alias), args);
List<Alias> chats = Mixtape.getDatabase().getAliases(player.getUniqueId().toString(), Database.AliasType.CHAT);
for (Alias alias : chats) {
if (alias.getFrom().equalsIgnoreCase(base)) {
String resolved = resolve(alias.getTo(), args);
if (!ERROR.equalsIgnoreCase(resolved)) {
resolved = resolved.startsWith("/") ? "." + resolved : resolved;
player.chat(resolved);
} else {
player.sendMessage(ChatColor.RED + usage(base, chats.get(alias)));
player.sendMessage(ChatColor.RED + usage(base, alias.getTo()));
}
return true;
}

View File

@ -1,31 +0,0 @@
package xyz.etztech.mixtape.listeners;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import xyz.etztech.mixtape.Mixtape;
public class PlayerJoinListener implements Listener {
Mixtape plugin;
public PlayerJoinListener(Mixtape plugin) {
this.plugin = plugin;
}
@EventHandler(priority= EventPriority.HIGH, ignoreCancelled=true)
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
Mixtape.loadAliases(player.getUniqueId().toString());
}
}