Merge pull request #10 from Taahh/master

Completely remove TFM as a dependency and use reflections instead
This commit is contained in:
Paldiu 2021-08-02 15:40:50 -05:00 committed by GitHub
commit 3a8691b18b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 167 additions and 140 deletions

View file

@ -11,7 +11,7 @@
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="TFGuilds" target="11" />
<module name="TFGuilds" target="16" />
</bytecodeTargetLevel>
</component>
</project>

View file

@ -1,106 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>SPIGOT</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.16-R0.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.27" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.AtlasMediaGroup:TotalFreedomMod:main-caaa067096-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.plugins:maven-checkstyle-plugin:3.1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-artifact:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-core:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-settings:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-settings-builder:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-repository-metadata:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-model-builder:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-aether-provider:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.aether:aether-impl:1.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.aether:aether-spi:1.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.aether:aether-api:1.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.aether:aether-util:1.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.sisu:sisu-inject-plexus:1.4.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.sisu:sisu-inject-bean:1.4.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.sisu:sisu-guice:noaop:2.1.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-classworlds:2.2.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.plexus:plexus-sec-dispatcher:1.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.sonatype.plexus:plexus-cipher:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-model:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-plugin-api:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.reporting:maven-reporting-api:3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.reporting:maven-reporting-impl:2.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.shared:maven-shared-utils:0.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jsr305:2.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-core:1.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: xerces:xercesImpl:2.9.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: xml-apis:xml-apis:1.3.04" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.httpcomponents:httpclient:4.0.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.httpcomponents:httpcore:4.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-codec:commons-codec:1.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-validator:commons-validator:1.3.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-digester:commons-digester:1.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-logging:commons-logging:1.0.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-sink-api:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-logging-api:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-decoration-model:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-site-renderer:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-module-xhtml:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-module-fml:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-i18n:1.0-beta-7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.velocity:velocity-tools:2.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-chain:commons-chain:1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: dom4j:dom4j:1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: oro:oro:2.0.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: sslext:sslext:1.2-0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.struts:struts-core:1.3.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.struts:struts-taglib:1.3.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.struts:struts-tiles:1.3.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-integration-tools:1.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-io:commons-io:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-container-default:1.0-alpha-9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: junit:junit:3.8.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: classworlds:classworlds:1.1-alpha-2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-component-annotations:2.0.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-resources:1.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-utils:3.3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-interpolation:1.26" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-velocity:1.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.puppycrawl.tools:checkstyle:8.29" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: info.picocli:picocli:4.1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: antlr:antlr:2.7.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.antlr:antlr4-runtime:4.8-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-beanutils:commons-beanutils:1.9.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.sf.saxon:Saxon-HE:9.9.1-6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.velocity:velocity:1.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-collections:commons-collections:3.2.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.xml.bind:jaxb-api:2.3.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.activation:javax.activation-api:1.2.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.junit.jupiter:junit-jupiter:5.4.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.junit.jupiter:junit-jupiter-api:5.4.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apiguardian:apiguardian-api:1.0.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.opentest4j:opentest4j:1.1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.junit.platform:junit-platform-commons:1.4.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.junit.jupiter:junit-jupiter-params:5.4.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.4.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.junit.platform:junit-platform-engine:1.4.2" level="project" />
</component>
</module>

View file

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.totalfreedom</groupId>
<groupId>com.github.AtlasMediaGroup</groupId>
<artifactId>TFGuilds</artifactId>
<version>2021.06-RC3</version>
<packaging>jar</packaging>
@ -72,11 +72,5 @@
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.AtlasMediaGroup</groupId>
<artifactId>TotalFreedomMod</artifactId>
<version>main-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

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

View file

@ -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.ReflectionsHelper;
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 = ReflectionsHelper.getField(getTfm(), "al");
Method isAdmin = ReflectionsHelper.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 = ReflectionsHelper.getField(getTfm(), "al");
Method isAdmin = ReflectionsHelper.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 = ReflectionsHelper.getField(getTfm(), "al");
Method isVanished = ReflectionsHelper.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 = ReflectionsHelper.getField(getTfm(), "pl");
Method getPlayer = ReflectionsHelper.getMethod(pl, "getPlayer", Player.class);
try
{
Object fPlayer = getPlayer.invoke(pl, player);
Method getTag = ReflectionsHelper.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 = ReflectionsHelper.getField(getTfm(), "pl");
Method getPlayer = ReflectionsHelper.getMethod(pl, "getPlayer", Player.class);
try
{
Object fPlayer = getPlayer.invoke(pl, player);
Method setTag = ReflectionsHelper.getMethod(fPlayer, "setTag", String.class);
setTag.invoke(fPlayer, (Object)null);
}
catch (IllegalAccessException | InvocationTargetException e)
{
e.printStackTrace();
}
}
}

View file

@ -19,7 +19,6 @@ public class Config extends YamlConfiguration
if (!file.exists())
{
options().copyDefaults(true);
plugin.saveResource(fileName, false);
}

View file

@ -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"),

View file

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

View file

@ -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,23 @@ public class SQLDatabase
}
try
{
switch (ConfigEntry.CONNECTION_TYPE.getString().toLowerCase())
{
case "sqlite":
connection = DriverManager.getConnection("jdbc:sqlite:" + createDBFile(plugin).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;
default:
connection = DriverManager.getConnection("jdbc:sqlite:" + createDBFile(plugin).getAbsolutePath().replace("%20", " "));
}
createTables();
plugin.getLogger().info("Connection to the MySQL server established!");
}
@ -60,7 +73,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," +
@ -82,4 +95,22 @@ public class SQLDatabase
"`rowid` INTEGER AUTO_INCREMENT PRIMARY KEY)")
.execute();
}
private File createDBFile(TFGuilds plugin)
{
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();
}
}
return file;
}
}

View file

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

View file

@ -0,0 +1,38 @@
package me.totalfreedom.tfguilds.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionsHelper
{
public static <T> 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 <T> 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;
}
}

View file

@ -2,6 +2,10 @@
# TFGuilds - Configuration
#
settings:
# Types: mysql, sqlite
connection: sqlite
server:
# Log guild chat to console?
guild_chat_logging: true