From ee1abaf546e0a21a86769d58aa46cce6a00a7391 Mon Sep 17 00:00:00 2001 From: Daniel V Date: Tue, 8 Jan 2019 22:38:26 -0500 Subject: [PATCH] Make /ignore,/leave persistent. --- pom.xml | 58 +++++++++ src/net/tnemc/tnc/core/ChatManager.java | 27 +--- src/net/tnemc/tnc/core/TheNewChat.java | 30 +++++ .../tnemc/tnc/core/command/IgnoreCommand.java | 44 ++++--- .../tnemc/tnc/core/command/TNECommand.java | 2 +- .../tnc/core/common/chat/db/DataProvider.java | 22 ++++ .../core/common/chat/db/IgnoredChannel.java | 43 ++++++ .../tnc/core/common/chat/db/SaveManager.java | 123 ++++++++++++++++++ .../tnc/core/common/chat/db/Version.java | 43 ++++++ .../tnc/core/common/chat/db/impl/H2.java | 51 ++++++++ .../tnc/core/common/chat/db/impl/SQLite.java | 50 +++++++ 11 files changed, 450 insertions(+), 43 deletions(-) create mode 100644 src/net/tnemc/tnc/core/common/chat/db/DataProvider.java create mode 100644 src/net/tnemc/tnc/core/common/chat/db/IgnoredChannel.java create mode 100644 src/net/tnemc/tnc/core/common/chat/db/SaveManager.java create mode 100644 src/net/tnemc/tnc/core/common/chat/db/Version.java create mode 100644 src/net/tnemc/tnc/core/common/chat/db/impl/H2.java create mode 100644 src/net/tnemc/tnc/core/common/chat/db/impl/SQLite.java diff --git a/pom.xml b/pom.xml index 9e68753..ab69d45 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,19 @@ 1.8 + + org.javalite + activejdbc-instrumentation + 2.0 + + + process-classes + + instrument + + + + org.apache.maven.plugins maven-shade-plugin @@ -91,8 +104,34 @@ placeholderapi http://repo.extendedclip.com/content/repositories/placeholderapi/ + + + javalite-snapshots + JavaLite Snapshots + http://repo.javalite.io/ + + false + + + true + + + + + javalite-plugin-snapshots + JavaLite Plugin Snapshots + http://repo.javalite.io/ + + false + + + true + + + + org.bukkit @@ -118,6 +157,25 @@ 2.9.2 provided + + org.javalite + activejdbc + 2.0 + jar + compile + + + com.zaxxer + HikariCP + 3.1.0 + compile + + + com.h2database + h2 + 1.4.197 + compile + org.slf4j diff --git a/src/net/tnemc/tnc/core/ChatManager.java b/src/net/tnemc/tnc/core/ChatManager.java index cd4390b..fc4a384 100644 --- a/src/net/tnemc/tnc/core/ChatManager.java +++ b/src/net/tnemc/tnc/core/ChatManager.java @@ -4,6 +4,7 @@ import net.tnemc.tnc.core.common.chat.ChatEntry; import net.tnemc.tnc.core.common.chat.ChatHandler; import net.tnemc.tnc.core.common.chat.ChatVariable; +import net.tnemc.tnc.core.common.chat.db.IgnoredChannel; import net.tnemc.tnc.core.common.chat.handlers.CoreHandler; import net.tnemc.tnc.core.common.chat.variables.core.DisplayVariable; import net.tnemc.tnc.core.common.chat.variables.core.LevelVariable; @@ -27,7 +28,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -58,7 +58,6 @@ public class ChatManager implements Listener { private Map replacements = new HashMap<>(); private Map channels = new HashMap<>(); - private Map> ignored = new HashMap<>(); /** * The core variables used @@ -255,9 +254,10 @@ private Collection getRecipients(final Collection recipients, fi final String permission, final String channel) { Collection newRecipients = new HashSet<>(); + TheNewChat.saveManager().open(); for(Player p : recipients) { if(!p.hasPermission(permission)) continue; - if(ignored.containsKey(channel) && ignored.get(channel).contains(p.getUniqueId().toString())) continue; + if(IgnoredChannel.exists(p.getUniqueId(), channel)) continue; if(radial) { if(p.getLocation().distance(player.getLocation()) <= radius) continue; } @@ -267,6 +267,7 @@ private Collection getRecipients(final Collection recipients, fi } newRecipients.add(p); } + TheNewChat.saveManager().close(); return newRecipients; } @@ -340,26 +341,6 @@ public void addHandler(ChatHandler handler) { handlersMap.put(handler.getTypes().keySet(), handler.getName()); } - public void ignore(final UUID id, String channel) { - if(ignored.containsKey(channel)) { - ignored.get(channel).add(id.toString()); - } - HashSet ignoring = new HashSet<>(); - ignoring.add(id.toString()); - ignored.put(channel, ignoring); - } - - public Map> getIgnored() { - return ignored; - } - - public boolean ignoring(final UUID id, String channel) { - if(ignored.containsKey(channel)) { - return ignored.get(channel).contains(id.toString()); - } - return false; - } - public LinkedHashMap getHandlers() { return handlers; } diff --git a/src/net/tnemc/tnc/core/TheNewChat.java b/src/net/tnemc/tnc/core/TheNewChat.java index 92405ff..3028edc 100644 --- a/src/net/tnemc/tnc/core/TheNewChat.java +++ b/src/net/tnemc/tnc/core/TheNewChat.java @@ -5,6 +5,8 @@ import net.tnemc.tnc.core.command.IgnoreCommand; import net.tnemc.tnc.core.command.ReloadCommand; import net.tnemc.tnc.core.command.TNECommand; +import net.tnemc.tnc.core.common.chat.db.SaveManager; +import net.tnemc.tnc.core.common.chat.db.Version; import net.tnemc.tnc.core.common.configuration.ConfigurationEntry; import net.tnemc.tnc.core.common.configuration.CoreConfigNodes; import net.tnemc.tnc.core.utils.FileMgmt; @@ -45,6 +47,9 @@ public class TheNewChat extends JavaPlugin { private File chatsFile; private FileConfiguration chatsConfiguration; + private String version = "0.0.1.0"; + + private SaveManager saveManager; public void onLoad() { instance = this; @@ -62,6 +67,23 @@ public void onEnable() { initializeConfigurations(); loadConfigurations(); + try { + new File(getDataFolder(), "TheNewChat.db").createNewFile(); + } catch(IOException ignore) { + } + + saveManager = new SaveManager(); + + saveManager.createTables(); + saveManager.open(); + if(Version.informationExists()) { + if(Version.outdated()) { + saveManager.updateTables(version); + Version.add(version); + } + } + saveManager.close(); + this.manager = new ChatManager(CoreConfigNodes.CORE_GENERAL_CHAT_HANDLER.getString()); commandManager = new CommandManager(); @@ -162,4 +184,12 @@ public static TheNewChat instance() { FileConfiguration getChatsConfiguration() { return chatsConfiguration; } + + public String getVersion() { + return version; + } + + public static SaveManager saveManager() { + return instance().saveManager; + } } \ No newline at end of file diff --git a/src/net/tnemc/tnc/core/command/IgnoreCommand.java b/src/net/tnemc/tnc/core/command/IgnoreCommand.java index ab63d60..8056918 100644 --- a/src/net/tnemc/tnc/core/command/IgnoreCommand.java +++ b/src/net/tnemc/tnc/core/command/IgnoreCommand.java @@ -1,7 +1,9 @@ package net.tnemc.tnc.core.command; import net.tnemc.tnc.core.TheNewChat; +import net.tnemc.tnc.core.common.chat.db.IgnoredChannel; import net.tnemc.tnc.core.common.configuration.CoreConfigNodes; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -29,7 +31,7 @@ public String getName() { @Override public String[] getAliases() { return new String[] { - "ignorec", "igc" + "ignorec", "igc", "leave" }; } @@ -53,27 +55,31 @@ public String[] getHelpLines() { @Override public boolean execute(CommandSender sender, String command, String[] arguments) { if(arguments.length > 0) { - final UUID id = getPlayer(sender).getUniqueId(); - if(plugin.getManager().getCommands().values().contains(arguments[0]) || arguments[0].equalsIgnoreCase("general")) { - final String handler = plugin.getManager().getHandler(arguments[0]); - boolean ignorable = (arguments[0].equalsIgnoreCase("general"))? CoreConfigNodes.CORE_GENERAL_CHAT_IGNORABLE.getBoolean() : - plugin.getManager().getChats().get(handler).get(arguments[0]).isIgnorable(); - if(ignorable) { - if(!plugin.getManager().ignoring(id, arguments[0])) { - plugin.getManager().ignore(id, arguments[0]); - sender.sendMessage(ChatColor.GOLD + "Now ignoring channel: " + arguments[0] + "."); - return true; - } else { - plugin.getManager().getIgnored().get(arguments[0]).remove(id.toString()); + Bukkit.getScheduler().runTaskAsynchronously(TheNewChat.instance(), ()->{ + final UUID id = getPlayer(sender).getUniqueId(); + if(plugin.getManager().getCommands().values().contains(arguments[0]) || arguments[0].equalsIgnoreCase("general")) { + final String handler = plugin.getManager().getHandler(arguments[0]); + boolean ignorable = (arguments[0].equalsIgnoreCase("general"))? CoreConfigNodes.CORE_GENERAL_CHAT_IGNORABLE.getBoolean() : + plugin.getManager().getChats().get(handler).get(arguments[0]).isIgnorable(); + if(ignorable) { + TheNewChat.saveManager().open(); + if(!IgnoredChannel.exists(id, arguments[0])) { + IgnoredChannel.add(id, arguments[0]); + sender.sendMessage(ChatColor.GOLD + "Now ignoring channel: " + arguments[0] + "."); + TheNewChat.saveManager().close(); + return; + } + IgnoredChannel.delete(id, arguments[0]); sender.sendMessage(ChatColor.GOLD + "No longer ignoring channel: " + arguments[0] + "."); - return true; + TheNewChat.saveManager().close(); + return; } + sender.sendMessage(ChatColor.RED + "Cannot ignore that channel."); + return; } - sender.sendMessage(ChatColor.RED + "Cannot ignore that channel."); - return false; - } - sender.sendMessage(ChatColor.RED + "Invalid channel."); - return false; + sender.sendMessage(ChatColor.RED + "Invalid channel."); + return; + }); } help(sender); return false; diff --git a/src/net/tnemc/tnc/core/command/TNECommand.java b/src/net/tnemc/tnc/core/command/TNECommand.java index 805a633..4b8c94e 100644 --- a/src/net/tnemc/tnc/core/command/TNECommand.java +++ b/src/net/tnemc/tnc/core/command/TNECommand.java @@ -1,7 +1,7 @@ package net.tnemc.tnc.core.command; import net.tnemc.tnc.core.TheNewChat; -import net.tnemc.tnk.core.utils.Message; +import net.tnemc.tnc.core.utils.Message; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; diff --git a/src/net/tnemc/tnc/core/common/chat/db/DataProvider.java b/src/net/tnemc/tnc/core/common/chat/db/DataProvider.java new file mode 100644 index 0000000..c1eaf90 --- /dev/null +++ b/src/net/tnemc/tnc/core/common/chat/db/DataProvider.java @@ -0,0 +1,22 @@ +package net.tnemc.tnc.core.common.chat.db; + +/** + * Created by creatorfromhell on 3/18/2018. + *

+ * The New Chat Minecraft Server Plugin + *

+ * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to + * Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. + */ +public interface DataProvider { + + String getDriver(); + Boolean dataSource(); + String dataSourceURL(); + String getURL(String file, String host, int port, String database); + + default void preConnect(String file, String host, int port, String database) { + + } +} \ No newline at end of file diff --git a/src/net/tnemc/tnc/core/common/chat/db/IgnoredChannel.java b/src/net/tnemc/tnc/core/common/chat/db/IgnoredChannel.java new file mode 100644 index 0000000..881dccd --- /dev/null +++ b/src/net/tnemc/tnc/core/common/chat/db/IgnoredChannel.java @@ -0,0 +1,43 @@ +package net.tnemc.tnc.core.common.chat.db; + +import org.javalite.activejdbc.Model; +import org.javalite.activejdbc.annotations.CompositePK; +import org.javalite.activejdbc.annotations.DbName; +import org.javalite.activejdbc.annotations.Table; + +import java.util.UUID; + +/** + * Created by creatorfromhell. + * + * The New VPN Blocker Minecraft Server Plugin + * + * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 + * International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ + * or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. + */ +@DbName("TNC") +@Table("tnc_ignored") +@CompositePK({ "uuid", "channel" }) +public class IgnoredChannel extends Model { + + public static boolean add(UUID id, String channel) { + IgnoredChannel chan = (exists(id, channel))? getChannel(id, channel) : new IgnoredChannel(); + chan.set("uuid", id.toString()); + chan.set("channel", channel); + + return chan.saveIt(); + } + + public static boolean exists(UUID id, String channel) { + return getChannel(id, channel) != null; + } + + public static IgnoredChannel getChannel(UUID id, String channel) { + return IgnoredChannel.findFirst("uuid = ? AND channel = ?", id.toString(), channel); + } + + public static void delete(UUID id, String channel) { + IgnoredChannel.delete("uuid = ? AND channel = ?", id.toString(), channel); + } +} \ No newline at end of file diff --git a/src/net/tnemc/tnc/core/common/chat/db/SaveManager.java b/src/net/tnemc/tnc/core/common/chat/db/SaveManager.java new file mode 100644 index 0000000..5b90313 --- /dev/null +++ b/src/net/tnemc/tnc/core/common/chat/db/SaveManager.java @@ -0,0 +1,123 @@ +package net.tnemc.tnc.core.common.chat.db; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import net.tnemc.tnc.core.TheNewChat; +import net.tnemc.tnc.core.common.chat.db.impl.H2; +import org.javalite.activejdbc.DB; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by creatorfromhell on 3/18/2018. + *

+ * The New Chat Minecraft Server Plugin + *

+ * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to + * Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. + */ +public class SaveManager { + + private Map> dataTables = new HashMap<>(); + private Map providers = new HashMap<>(); + + //Instances + private DB db = new DB("TNC"); + private final HikariConfig config; + private final HikariDataSource dataSource; + + private String type; + + private String file; + private String host; + private int port; + private String dbName; + private String user; + private String pass; + + public SaveManager() { + type = "h2"; + file = new File(TheNewChat.instance().getDataFolder(), "TheNewChat").getAbsolutePath(); + host = "localhost"; + port = 3306; + dbName = "TNC"; + user = "thenewchat"; + pass = "password"; + + //Initialize DataProviders. + providers.put("h2", new H2()); + + config = new HikariConfig(); + + if(providers.get(type).dataSource()) { + config.setDataSourceClassName(providers.get(type).dataSourceURL()); + } else { + config.setJdbcUrl(providers.get(type).getURL(file, host, port, dbName)); + config.setDriverClassName(providers.get(type).getDriver()); + } + + config.setUsername(user); + config.setPassword(pass); + + dataSource = new HikariDataSource(config); + + //initialize tables. + + List h2 = new ArrayList<>(); + + h2.add("CREATE TABLE IF NOT EXISTS `tnc_version` (" + + "`id` INTEGER NOT NULL UNIQUE," + + "`version_value` VARCHAR(15)" + + ") ENGINE = INNODB;"); + + h2.add("CREATE TABLE IF NOT EXISTS tnc_ignored (" + + "uuid VARCHAR(36) NOT NULL," + + "channel VARCHAR(100) NOT NULL," + + "PRIMARY KEY(uuid, channel)" + + ") ENGINE = INNODB;"); + dataTables.put("h2", h2); + } + + public void createTables() { + List tables = dataTables.get(type); + try { + DataProvider provider = providers.get(type); + provider.preConnect(file, host, port, dbName); + db.open(provider.getDriver(), provider.getURL(file, host, port, dbName), user, pass); + tables.forEach((table)->{ + db.exec(table); + }); + Version.add(TheNewChat.instance().getVersion()); + } finally { + db.close(); + } + } + + public void updateTables(String version) { + + } + + public void open() { + try { + db.open(dataSource); + } catch(Exception ignore) { + } + } + + public void close() { + db.close(); + } + + public void addProvider(String type, DataProvider provider) { + providers.put(type, provider); + } + + public void registerTables(String type, List tables) { + dataTables.put(type, tables); + } +} \ No newline at end of file diff --git a/src/net/tnemc/tnc/core/common/chat/db/Version.java b/src/net/tnemc/tnc/core/common/chat/db/Version.java new file mode 100644 index 0000000..eaf83fe --- /dev/null +++ b/src/net/tnemc/tnc/core/common/chat/db/Version.java @@ -0,0 +1,43 @@ +package net.tnemc.tnc.core.common.chat.db; + +import net.tnemc.tnc.core.TheNewChat; +import org.javalite.activejdbc.Model; +import org.javalite.activejdbc.annotations.DbName; +import org.javalite.activejdbc.annotations.IdName; +import org.javalite.activejdbc.annotations.Table; + +/** + * Created by creatorfromhell on 3/18/2018. + *

+ * The New Chat Minecraft Server Plugin + *

+ * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to + * Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. + */ +@DbName("TNC") +@Table("tnc_version") +@IdName("id") +public class Version extends Model { + + public static void add(String version) { + net.tnemc.tnc.core.common.chat.db.Version versionInstance = (informationExists())? get() : new net.tnemc.tnc.core.common.chat.db.Version(); + versionInstance.set("id", 1); + versionInstance.set("version_value", version); + versionInstance.saveIt(); + } + + public static boolean informationExists() { + return net.tnemc.tnc.core.common.chat.db.Version.findById(1) != null; + } + + public static net.tnemc.tnc.core.common.chat.db.Version get() { + return net.tnemc.tnc.core.common.chat.db.Version.findById(1); + } + + public static boolean outdated() { + if(informationExists()) + return !get().getString("version_value").equalsIgnoreCase(TheNewChat.instance().getVersion()); + return true; + } +} diff --git a/src/net/tnemc/tnc/core/common/chat/db/impl/H2.java b/src/net/tnemc/tnc/core/common/chat/db/impl/H2.java new file mode 100644 index 0000000..6d30e42 --- /dev/null +++ b/src/net/tnemc/tnc/core/common/chat/db/impl/H2.java @@ -0,0 +1,51 @@ +package net.tnemc.tnc.core.common.chat.db.impl; + + +import net.tnemc.tnc.core.common.chat.db.DataProvider; + +import java.io.File; +import java.io.IOException; + +/** + * Created by creatorfromhell on 3/18/2018. + *

+ * The New Chat Minecraft Server Plugin + *

+ * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to + * Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. + */ +public class H2 implements DataProvider { + @Override + public String getDriver() { + return "org.h2.Driver"; + } + + @Override + public Boolean dataSource() { + return false; + } + + @Override + public String dataSourceURL() { + return "org.h2.jdbcx.JdbcDataSource"; + } + + @Override + public String getURL(String file, String host, int port, String database) { + return "jdbc:h2:" + file; + } + + @Override + public void preConnect(String file, String host, int port, String database) { + if(!file.endsWith(".db")) file = file + ".db"; + File db = new File(file); + if(!db.exists()) { + try { + db.createNewFile(); + } catch(IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/net/tnemc/tnc/core/common/chat/db/impl/SQLite.java b/src/net/tnemc/tnc/core/common/chat/db/impl/SQLite.java new file mode 100644 index 0000000..2dd9a83 --- /dev/null +++ b/src/net/tnemc/tnc/core/common/chat/db/impl/SQLite.java @@ -0,0 +1,50 @@ +package net.tnemc.tnc.core.common.chat.db.impl; + +import net.tnemc.tnc.core.common.chat.db.DataProvider; + +import java.io.File; +import java.io.IOException; + +/** + * Created by creatorfromhell. + * + * The New VPN Blocker Minecraft Server Plugin + * + * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 + * International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ + * or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. + */ +public class SQLite implements DataProvider { + @Override + public String getDriver() { + return "org.sqlite.JDBC"; + } + + @Override + public Boolean dataSource() { + return false; + } + + @Override + public String dataSourceURL() { + return ""; + } + + @Override + public String getURL(String file, String host, int port, String database) { + return "jdbc:sqlite:" + file; + } + + @Override + public void preConnect(String file, String host, int port, String database) { + if(!file.endsWith(".db")) file = file + ".db"; + File db = new File(file); + if(!db.exists()) { + try { + db.createNewFile(); + } catch(IOException e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file