Add support for latest Velocity 3.0.x

This commit is contained in:
Frank van der Heijden 2021-11-25 23:33:47 +01:00
parent 2a13f66500
commit 1ea8e24a1e
No known key found for this signature in database
GPG key ID: B808721C2DD5B5B8
6 changed files with 71 additions and 53 deletions

View file

@ -23,10 +23,10 @@ dependencies {
implementation("net.kyori:adventure-text-minimessage:${VersionConstants.adventureMinimessageVersion}") {
exclude("net.kyori", "adventure-api")
}
compileOnly("com.velocitypowered:velocity-api:3.0.0")
compileOnly("com.velocitypowered:velocity-api:3.1.0-SNAPSHOT")
compileOnly("com.velocitypowered:velocity-brigadier:1.0.0-SNAPSHOT")
compileOnly("com.electronwill.night-config:toml:3.6.3")
annotationProcessor("com.velocitypowered:velocity-api:3.0.0")
annotationProcessor("com.velocitypowered:velocity-api:3.1.0-SNAPSHOT")
}
tasks {

View file

@ -1,6 +1,8 @@
package net.frankheijden.serverutils.velocity.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription;
@ -54,12 +56,36 @@ public class VelocityCommandServerUtils extends CommandServerUtils<VelocityPlugi
String commandName
) {
ServerUtils plugin = ServerUtils.getInstance();
CommandDispatcher<CommandSource> dispatcher = RVelocityCommandManager.getDispatcher(
plugin.getProxy().getCommandManager()
);
CommandManager proxyCommandManager = plugin.getProxy().getCommandManager();
CommandDispatcher<CommandSource> dispatcher = RVelocityCommandManager.getDispatcher(proxyCommandManager);
return builder
.key("Name").value(dispatcher.getRoot().getChild(commandName).getName())
.key("Plugin").value(plugin.getPluginCommandManager().findPluginId(commandName).orElse("<UNKNOWN>"));
builder.key("Name").value(dispatcher.getRoot().getChild(commandName).getName());
CommandMeta meta = null;
try {
meta = proxyCommandManager.getCommandMeta(commandName);
} catch (Throwable ignored) {
//
}
String pluginName = null;
if (meta != null) {
if (meta.getPlugin() != null) {
pluginName = plugin.getProxy().getPluginManager().fromInstance(meta.getPlugin())
.map(c -> c.getDescription().getId())
.orElse(null);
}
CommandMeta finalMeta = meta;
builder.key("Aliases").value(listBuilderFunction.apply(b -> b.addAll(finalMeta.getAliases())));
}
if (pluginName == null) {
pluginName = plugin.getPluginCommandManager().findPluginId(commandName).orElse("<UNKNOWN>");
}
builder.key("Plugin").value(pluginName);
return builder;
}
}

View file

@ -2,38 +2,12 @@ package net.frankheijden.serverutils.velocity.entities;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import net.frankheijden.serverutils.common.entities.ServerUtilsAudience;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
public class VelocityAudience extends ServerUtilsAudience<CommandSource> {
private static final GsonComponentSerializer serializer = GsonComponentSerializer.gson();
private static Object deserializer;
private static MethodHandle deserializeMethodHandle;
private static MethodHandle sendMessageMethodHandle;
static {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
@SuppressWarnings("LineLength")
Class<?> deserializerClass = Class.forName("net.frankheijden.serverutils.dependencies.impl.adventure.text.serializer.gson.GsonComponentSerializer");
deserializer = deserializerClass.getDeclaredMethod("gson").invoke(null);
deserializeMethodHandle = lookup.unreflect(deserializerClass.getMethod("deserialize", Object.class));
Class<?> componentClass = Class.forName(
new String(new char[]{'n', 'e', 't'}) + ".kyori.adventure.text.Component" // relocate is smart
);
sendMessageMethodHandle = lookup.unreflect(CommandSource.class.getMethod("sendMessage", componentClass));
} catch (Throwable th) {
th.printStackTrace();
}
}
protected VelocityAudience(Audience audience, CommandSource source) {
super(audience, source);
}
@ -50,12 +24,6 @@ public class VelocityAudience extends ServerUtilsAudience<CommandSource> {
@Override
public void sendMessage(Component component) {
// Shading in adventure is fun when making a single distributable jar...
String serializedString = serializer.serialize(component);
try {
sendMessageMethodHandle.invoke(source, deserializeMethodHandle.invoke(deserializer, serializedString));
} catch (Throwable th) {
th.printStackTrace();
}
source.sendMessage(component);
}
}

View file

@ -26,19 +26,32 @@ public class RJavaPluginLoader {
);
}
/**
* Loads a candidate description from the given source.
*/
public static PluginDescription loadPluginDescription(Object javaPluginLoader, Path source) {
return reflection.invoke(javaPluginLoader, "loadPluginDescription", ClassObject.of(Path.class, source));
String fieldName = "loadCandidate";
try {
reflection.getClazz().getDeclaredMethod(fieldName, Path.class);
} catch (NoSuchMethodException ex) {
fieldName = "loadPluginDescription";
}
return reflection.invoke(javaPluginLoader, fieldName, ClassObject.of(Path.class, source));
}
/**
* Loads the plugin from their candidate PluginDescription.
*/
public static PluginDescription loadPlugin(Object javaPluginLoader, PluginDescription candidate) {
return reflection.invoke(
javaPluginLoader,
"loadPlugin",
ClassObject.of(PluginDescription.class, candidate)
);
String fieldName = "createPluginFromCandidate";
try {
reflection.getClazz().getDeclaredMethod(fieldName, PluginDescription.class);
} catch (NoSuchMethodException ex) {
fieldName = "loadPlugin";
}
return reflection.invoke(javaPluginLoader, fieldName, ClassObject.of(PluginDescription.class, candidate));
}
public static Module createModule(Object javaPluginLoader, PluginContainer container) {

View file

@ -13,8 +13,18 @@ public class RVelocityPluginManager {
private RVelocityPluginManager() {}
/**
* Retrieves the plugin map. Key is the id of the plugin.
*/
public static Map<String, PluginContainer> getPlugins(PluginManager manager) {
return reflection.get(manager, "plugins");
String fieldName = "plugins";
try {
reflection.getClazz().getField(fieldName);
} catch (NoSuchFieldException ex) {
fieldName = "pluginsById";
}
return reflection.get(manager, fieldName);
}
public static Map<Object, PluginContainer> getPluginInstances(PluginManager manager) {