From 11b3e194ec59b834175eca7826a4813bc7c64ed4 Mon Sep 17 00:00:00 2001 From: spacerocket62 Date: Wed, 28 Jul 2021 15:12:32 -0700 Subject: [PATCH] Completely remove TFM as a dependency and use reflections instead Simplify removing color codes by using ChatColor#stripColor Have an option to use sqlite or mysql, by default it will be sqlite --- TFGuilds.iml | 106 ------------------ pom.xml | 8 +- .../me/totalfreedom/tfguilds/TFGuilds.java | 9 +- .../me/totalfreedom/tfguilds/TFMBridge.java | 81 +++++++++++-- .../totalfreedom/tfguilds/config/Config.java | 1 - .../tfguilds/config/ConfigEntry.java | 1 + .../me/totalfreedom/tfguilds/guild/Guild.java | 6 +- .../tfguilds/sql/SQLDatabase.java | 38 +++++-- .../me/totalfreedom/tfguilds/util/GUtil.java | 6 +- .../tfguilds/util/Reflections.java | 38 +++++++ src/main/resources/config.yml | 4 + 11 files changed, 162 insertions(+), 136 deletions(-) delete mode 100644 TFGuilds.iml create mode 100644 src/main/java/me/totalfreedom/tfguilds/util/Reflections.java diff --git a/TFGuilds.iml b/TFGuilds.iml deleted file mode 100644 index f2e91f0..0000000 --- a/TFGuilds.iml +++ /dev/null @@ -1,106 +0,0 @@ - - - - - - - SPIGOT - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 95761d7..74016ba 100644 --- a/pom.xml +++ b/pom.xml @@ -4,9 +4,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - me.totalfreedom + com.github.AtlasMediaGroup TFGuilds - 2021.06-RC3 + master-4ec00d977c-1 jar TFGuilds @@ -72,11 +72,11 @@ 1.16.5-R0.1-SNAPSHOT provided - + \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java b/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java index 0954330..3d2cd17 100644 --- a/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java +++ b/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java @@ -1,5 +1,6 @@ package me.totalfreedom.tfguilds; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -49,8 +50,14 @@ public class TFGuilds extends JavaPlugin @Override public void onDisable() { + try + { + getSQL().getConnection().close(); + } + catch (SQLException throwables) + { + } config.save(); - this.plugin = null; } public Config getConfig() diff --git a/src/main/java/me/totalfreedom/tfguilds/TFMBridge.java b/src/main/java/me/totalfreedom/tfguilds/TFMBridge.java index 4ec8c28..a1211e6 100644 --- a/src/main/java/me/totalfreedom/tfguilds/TFMBridge.java +++ b/src/main/java/me/totalfreedom/tfguilds/TFMBridge.java @@ -1,8 +1,9 @@ package me.totalfreedom.tfguilds; -import me.totalfreedom.totalfreedommod.TotalFreedomMod; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import me.totalfreedom.tfguilds.util.Reflections; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -11,18 +12,18 @@ public class TFMBridge { private final TFGuilds plugin = TFGuilds.getPlugin(); - private TotalFreedomMod tfm = null; + private /*TotalFreedomMod*/ Plugin tfm = null; - public TotalFreedomMod getTfm() + public /*TotalFreedomMod*/ Plugin getTfm() { if (tfm == null) { try { final Plugin tfmPlugin = plugin.getServer().getPluginManager().getPlugin("TotalFreedomMod"); - if (tfmPlugin != null && tfmPlugin.isEnabled() && tfmPlugin instanceof TotalFreedomMod) + if (tfmPlugin != null && tfmPlugin.isEnabled()) { - tfm = (TotalFreedomMod)tfmPlugin; + tfm = /*(TotalFreedomMod)*/tfmPlugin; } } catch (Exception ex) @@ -40,7 +41,17 @@ public class TFMBridge Bukkit.getLogger().warning("TotalFreedomMod not detected, checking operator status instead."); return player.isOp(); } - return getTfm().al.isAdmin(player); + Object al = Reflections.getField(getTfm(), "al"); + Method isAdmin = Reflections.getMethod(al, "isAdmin", Player.class); + try + { + return (boolean)isAdmin.invoke(al, player) /*getTfm().al.isAdmin(player)*/; + } + catch (IllegalAccessException | InvocationTargetException e) + { + e.printStackTrace(); + } + return false; } public boolean isAdmin(CommandSender sender) @@ -50,7 +61,17 @@ public class TFMBridge Bukkit.getLogger().warning("TotalFreedomMod not detected, checking operator status instead."); return sender.isOp(); } - return getTfm().al.isAdmin(sender); + Object al = Reflections.getField(getTfm(), "al"); + Method isAdmin = Reflections.getMethod(al, "isAdmin", CommandSender.class); + try + { + return (boolean)isAdmin.invoke(al, sender) /*getTfm().al.isAdmin(player)*/; + } + catch (IllegalAccessException | InvocationTargetException e) + { + e.printStackTrace(); + } + return false; } public boolean isVanished(Player player) @@ -60,7 +81,17 @@ public class TFMBridge Bukkit.getLogger().warning("TotalFreedomMod not detected, vanish will return false."); return false; } - return getTfm().al.isVanished(player.getName()); + Object al = Reflections.getField(getTfm(), "al"); + Method isVanished = Reflections.getMethod(al, "isVanished", String.class); + try + { + return (boolean)isVanished.invoke(al, player.getName()) /*getTfm().al.isVanished(player.getName)*/; + } + catch (IllegalAccessException | InvocationTargetException e) + { + e.printStackTrace(); + } + return false; } public String getTag(Player player) @@ -69,7 +100,22 @@ public class TFMBridge { return null; } - return ChatColor.stripColor(getTfm().pl.getPlayer(player).getTag()); + + Object pl = Reflections.getField(getTfm(), "pl"); + Method getPlayer = Reflections.getMethod(pl, "getPlayer", Player.class); + try + { + Object fPlayer = getPlayer.invoke(pl, player); + Method getTag = Reflections.getMethod(fPlayer, "getTag"); + + return (String)getTag.invoke(fPlayer); + } + catch (IllegalAccessException | InvocationTargetException e) + { + e.printStackTrace(); + } + + return "" /*ChatColor.stripColor(getTfm().pl.getPlayer(player).getTag())*/; } public void clearTag(Player player) @@ -78,6 +124,19 @@ public class TFMBridge { return; } - getTfm().pl.getPlayer(player).setTag(null); +// getTfm().pl.getPlayer(player).setTag(null); + Object pl = Reflections.getField(getTfm(), "pl"); + Method getPlayer = Reflections.getMethod(pl, "getPlayer", Player.class); + try + { + Object fPlayer = getPlayer.invoke(pl, player); + Method setTag = Reflections.getMethod(fPlayer, "setTag", String.class); + + setTag.invoke(fPlayer, (Object)null); + } + catch (IllegalAccessException | InvocationTargetException e) + { + e.printStackTrace(); + } } } diff --git a/src/main/java/me/totalfreedom/tfguilds/config/Config.java b/src/main/java/me/totalfreedom/tfguilds/config/Config.java index ac5daae..636179e 100644 --- a/src/main/java/me/totalfreedom/tfguilds/config/Config.java +++ b/src/main/java/me/totalfreedom/tfguilds/config/Config.java @@ -19,7 +19,6 @@ public class Config extends YamlConfiguration if (!file.exists()) { - options().copyDefaults(true); plugin.saveResource(fileName, false); } diff --git a/src/main/java/me/totalfreedom/tfguilds/config/ConfigEntry.java b/src/main/java/me/totalfreedom/tfguilds/config/ConfigEntry.java index 950bff7..2889c02 100644 --- a/src/main/java/me/totalfreedom/tfguilds/config/ConfigEntry.java +++ b/src/main/java/me/totalfreedom/tfguilds/config/ConfigEntry.java @@ -9,6 +9,7 @@ public enum ConfigEntry GUILD_TAGS("server.guild_tags"), GUILD_TAG_MAX_LENGTH("server.guild_tag_max_length"), GLOBAL_TAG_MAX_LENGTH("server.global_tag_max_length"), + CONNECTION_TYPE("settings.connection"), MYSQL_HOST("mysql.host"), MYSQL_PORT("mysql.port"), MYSQL_USERNAME("mysql.username"), diff --git a/src/main/java/me/totalfreedom/tfguilds/guild/Guild.java b/src/main/java/me/totalfreedom/tfguilds/guild/Guild.java index f4c5d7c..a21fd23 100644 --- a/src/main/java/me/totalfreedom/tfguilds/guild/Guild.java +++ b/src/main/java/me/totalfreedom/tfguilds/guild/Guild.java @@ -135,7 +135,7 @@ public class Guild while (set.next()) { String id = set.getString("id"); - UUID owner = User.getUserFromId(set.getInt("owner")).getUuid(); + UUID owner = /*User.getUserFromId(set.getInt("owner")).getUuid()*/ UUID.fromString(set.getString("owner")); List moderators = new ArrayList<>(); if (set.getString("moderators") != null) { @@ -512,7 +512,7 @@ public class Guild { statement.setString(1, id); statement.setString(2, name); - statement.setInt(3, User.getUserFromUuid(owner).getId()); + statement.setString(3, owner.toString() /*User.getUserFromUuid(owner).getId()*/); statement.setString(4, null); statement.setString(5, null); statement.setString(6, tag); @@ -528,7 +528,7 @@ public class Guild } else { - statement.setInt(1, User.getUserFromUuid(owner).getId()); + statement.setString(1, owner.toString()); statement.setString(2, moderators.isEmpty() ? null : getModeratorIds()); statement.setString(3, members.isEmpty() ? null : getMemberIds()); statement.setString(4, tag); diff --git a/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java b/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java index 5844953..4e0594e 100644 --- a/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java +++ b/src/main/java/me/totalfreedom/tfguilds/sql/SQLDatabase.java @@ -1,5 +1,7 @@ package me.totalfreedom.tfguilds.sql; +import java.io.File; +import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -20,12 +22,34 @@ public class SQLDatabase } try { - connection = DriverManager.getConnection(String.format("jdbc:mysql://%s:%d/%s", - ConfigEntry.MYSQL_HOST.getString(), - ConfigEntry.MYSQL_PORT.getInteger(), - ConfigEntry.MYSQL_DATABASE.getString()), - ConfigEntry.MYSQL_USERNAME.getString(), - password); + switch (ConfigEntry.CONNECTION_TYPE.getString().toLowerCase()) + { + case "sqlite": + File file = new File(plugin.getDataFolder(), "database.db"); + if (!file.exists()) + { + try + { + file.createNewFile(); + plugin.getLogger().info("Creating database.db file"); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath().replace("%20", " ")); + break; + case "mysql": + connection = DriverManager.getConnection(String.format("jdbc:mysql://%s:%d/%s", + ConfigEntry.MYSQL_HOST.getString(), + ConfigEntry.MYSQL_PORT.getInteger(), + ConfigEntry.MYSQL_DATABASE.getString()), + ConfigEntry.MYSQL_USERNAME.getString(), + password); + break; + } + createTables(); plugin.getLogger().info("Connection to the MySQL server established!"); } @@ -60,7 +84,7 @@ public class SQLDatabase connection.prepareStatement("CREATE TABLE IF NOT EXISTS `guilds` (" + "`id` TEXT," + "`name` TEXT," + - "`owner` INT," + + "`owner` TEXT," + "`moderators` TEXT," + "`members` TEXT," + "`tag` TEXT," + diff --git a/src/main/java/me/totalfreedom/tfguilds/util/GUtil.java b/src/main/java/me/totalfreedom/tfguilds/util/GUtil.java index 8d97620..fe7158a 100644 --- a/src/main/java/me/totalfreedom/tfguilds/util/GUtil.java +++ b/src/main/java/me/totalfreedom/tfguilds/util/GUtil.java @@ -40,7 +40,7 @@ public class GUtil public static String removeColorCodes(String string) { - String s = null; + /*String s = null; if (string != null) { Matcher matcher = CHAT_COLOR_FORMAT.matcher(string); @@ -48,8 +48,8 @@ public class GUtil { s = string.replaceAll(matcher.group(), ""); } - } - return s; + }*/ + return ChatColor.stripColor(string); } public static boolean containsBlacklistedWord(String string) diff --git a/src/main/java/me/totalfreedom/tfguilds/util/Reflections.java b/src/main/java/me/totalfreedom/tfguilds/util/Reflections.java new file mode 100644 index 0000000..1341407 --- /dev/null +++ b/src/main/java/me/totalfreedom/tfguilds/util/Reflections.java @@ -0,0 +1,38 @@ +package me.totalfreedom.tfguilds.util; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +public class Reflections +{ + + public static Object getField(T instance, String name) { + try + { + Field f = instance.getClass().getDeclaredField(name); + f.setAccessible(true); + return f.get(instance); + } + catch (NoSuchFieldException | IllegalAccessException e) + { + e.printStackTrace(); + } + return null; + } + + public static Method getMethod(T instance, String name, Class... parameterTypes) { + try + { + Method f = instance.getClass().getDeclaredMethod(name, parameterTypes); + f.setAccessible(true); + return f; + } + catch (NoSuchMethodException e) + { + e.printStackTrace(); + } + return null; + } + + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5586de3..cae444f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -2,6 +2,10 @@ # TFGuilds - Configuration # +settings: + # Types: mysql, sqlite + connection: sqlite + server: # Log guild chat to console? guild_chat_logging: true