Refactor reflections and use MinecraftReflection

This commit is contained in:
Frank van der Heijden 2020-12-21 00:11:31 +01:00
parent 295eb77452
commit 8a855d6935
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
34 changed files with 305 additions and 1148 deletions

View file

@ -6,7 +6,6 @@ import co.aikar.commands.CommandCompletions;
import net.frankheijden.serverutils.bungee.commands.CommandPlugins;
import net.frankheijden.serverutils.bungee.commands.CommandServerUtils;
import net.frankheijden.serverutils.bungee.entities.BungeePlugin;
import net.frankheijden.serverutils.bungee.entities.BungeeReflection;
import net.frankheijden.serverutils.bungee.listeners.BungeeListener;
import net.frankheijden.serverutils.bungee.managers.BungeePluginManager;
import net.frankheijden.serverutils.bungee.reflection.RPluginClassLoader;
@ -38,7 +37,6 @@ public class ServerUtils extends Plugin {
ServerUtilsApp.init(this, plugin);
new Metrics(this, ServerUtilsApp.BSTATS_METRICS_ID);
new BungeeReflection();
this.commandManager = new BungeeCommandManager(this);
commandManager.registerCommand(new CommandPlugins());

View file

@ -255,13 +255,7 @@ public class CommandServerUtils extends BaseCommand {
return;
}
Plugin plugin;
try {
plugin = RPluginManager.getPlugin(proxy.getPluginManager(), cmd);
} catch (IllegalAccessException ex) {
ex.printStackTrace();
return;
}
Plugin plugin = RPluginManager.getPlugin(proxy.getPluginManager(), cmd);
if (plugin == null) {
return;
}

View file

@ -1,12 +0,0 @@
package net.frankheijden.serverutils.bungee.entities;
import net.frankheijden.serverutils.common.reflection.ReflectionUtils;
import net.frankheijden.serverutils.common.reflection.VersionParam;
public class BungeeReflection extends ReflectionUtils {
@Override
public boolean isCompatible(VersionParam param) {
return true;
}
}

View file

@ -1,55 +1,33 @@
package net.frankheijden.serverutils.bungee.reflection;
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Map;
import java.util.Set;
import dev.frankheijden.minecraftreflection.MinecraftReflection;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.plugin.PluginDescription;
public class RPluginClassLoader {
private static Class<?> loaderClass;
private static Map<String, Field> fields;
private static Constructor<?> constructor;
private static final MinecraftReflection reflection = MinecraftReflection
.of("net.md_5.bungee.api.plugin.PluginClassloader");
static {
try {
loaderClass = Class.forName("net.md_5.bungee.api.plugin.PluginClassloader");
constructor = loaderClass.getDeclaredConstructor(ProxyServer.class, PluginDescription.class, URL[].class);
constructor.setAccessible(true);
fields = getAllFields(loaderClass,
fieldOf("allLoaders"),
fieldOf("plugin"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Object newInstance(ProxyServer proxy, PluginDescription desc, URL... urls) throws Exception {
return constructor.newInstance(proxy, desc, urls);
public static Object newInstance(ProxyServer proxy, PluginDescription desc, URL... urls) {
return reflection.newInstance(proxy, desc, urls);
}
/**
* Retrieves the PluginClassLoader of a specific plugin.
* @param plugin The plugin to lookup the PluginClassLoader for.
* @return The PluginClassLoader.
* @throws ReflectiveOperationException Iff a reflection error occurred.
*/
@SuppressWarnings("unchecked")
public static Object getPluginClassLoader(Plugin plugin) throws ReflectiveOperationException {
Set<Object> allLoaders = (Set<Object>) get(fields, null, "allLoaders");
public static Object getPluginClassLoader(Plugin plugin) {
Set<Object> allLoaders = reflection.get(null, "allLoaders");
if (allLoaders == null) return null;
Object matchingLoader = null;
for (Object loader : allLoaders) {
if (plugin.equals(get(fields, loader, "plugin"))) {
if (plugin.equals(reflection.get(loader, "plugin"))) {
matchingLoader = loader;
break;
}

View file

@ -1,12 +1,8 @@
package net.frankheijden.serverutils.bungee.reflection;
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
import com.google.common.collect.Multimap;
import java.lang.reflect.Field;
import java.util.Map;
import dev.frankheijden.minecraftreflection.MinecraftReflection;
import net.frankheijden.serverutils.common.utils.MapUtils;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Plugin;
@ -14,61 +10,40 @@ import org.yaml.snakeyaml.Yaml;
public class RPluginManager {
private static Class<?> pluginManagerClass;
private static Map<String, Field> fields;
static {
try {
pluginManagerClass = Class.forName("net.md_5.bungee.api.plugin.PluginManager");
fields = getAllFields(pluginManagerClass,
fieldOf("yaml"),
fieldOf("plugins"),
fieldOf("commandMap"),
fieldOf("toLoad"),
fieldOf("commandsByPlugin"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static final MinecraftReflection reflection = MinecraftReflection
.of("net.md_5.bungee.api.plugin.PluginManager");
/**
* Clears the plugin from the PluginManager.
* @param instance The instance of the PluginManager.
* @param plugin The plugin to clear.
* @throws ReflectiveOperationException Iff a reflection error happened.
*/
@SuppressWarnings("rawtypes")
public static void clearPlugin(Object instance, Plugin plugin) throws ReflectiveOperationException {
public static void clearPlugin(Object instance, Plugin plugin) {
String pluginName = plugin.getDescription().getName();
MapUtils.remove((Map) get(fields, instance, "plugins"), pluginName);
MapUtils.remove((Map) get(fields, instance, "toLoad"), pluginName);
MapUtils.remove(reflection.get(instance, "plugins"), pluginName);
MapUtils.remove(reflection.get(instance, "toLoad"), pluginName);
}
@SuppressWarnings("unchecked")
public static Map<String, Plugin> getPlugins(Object instance) throws ReflectiveOperationException {
return (Map<String, Plugin>) get(fields, instance, "plugins");
public static Map<String, Plugin> getPlugins(Object instance) {
return reflection.get(instance, "plugins");
}
public static Yaml getYaml(Object instance) throws IllegalAccessException {
return (Yaml) get(fields, instance, "yaml");
public static Yaml getYaml(Object instance) {
return reflection.get(instance, "yaml");
}
@SuppressWarnings("unchecked")
public static Map<String, Command> getCommands(Object instance) throws IllegalAccessException {
return (Map<String, Command>) get(fields, instance, "commandMap");
return reflection.get(instance, "commandMap");
}
/**
* Retrieves the registered plugin of the command.
* @param instance The PluginManager instance.
* @param cmd The command to check the plugin of.
* @return The plugin of the command
* @throws IllegalAccessException Iff some reflection error occurred.
* @return The plugin of the command.
*/
@SuppressWarnings("unchecked")
public static Plugin getPlugin(Object instance, Command cmd) throws IllegalAccessException {
Object obj = get(fields, instance, "commandsByPlugin");
Multimap<Plugin, Command> plugins = (Multimap<Plugin, Command>) obj;
public static Plugin getPlugin(Object instance, Command cmd) {
Multimap<Plugin, Command> plugins = reflection.get(instance, "commandsByPlugin");
if (plugins == null) return null;
for (Map.Entry<Plugin, Command> entry : plugins.entries()) {