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}") { implementation("net.kyori:adventure-text-minimessage:${VersionConstants.adventureMinimessageVersion}") {
exclude("net.kyori", "adventure-api") 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.velocitypowered:velocity-brigadier:1.0.0-SNAPSHOT")
compileOnly("com.electronwill.night-config:toml:3.6.3") 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 { tasks {

View file

@ -1,6 +1,8 @@
package net.frankheijden.serverutils.velocity.commands; package net.frankheijden.serverutils.velocity.commands;
import com.mojang.brigadier.CommandDispatcher; 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.command.CommandSource;
import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.plugin.PluginDescription;
@ -54,12 +56,36 @@ public class VelocityCommandServerUtils extends CommandServerUtils<VelocityPlugi
String commandName String commandName
) { ) {
ServerUtils plugin = ServerUtils.getInstance(); ServerUtils plugin = ServerUtils.getInstance();
CommandDispatcher<CommandSource> dispatcher = RVelocityCommandManager.getDispatcher( CommandManager proxyCommandManager = plugin.getProxy().getCommandManager();
plugin.getProxy().getCommandManager() CommandDispatcher<CommandSource> dispatcher = RVelocityCommandManager.getDispatcher(proxyCommandManager);
);
return builder builder.key("Name").value(dispatcher.getRoot().getChild(commandName).getName());
.key("Name").value(dispatcher.getRoot().getChild(commandName).getName())
.key("Plugin").value(plugin.getPluginCommandManager().findPluginId(commandName).orElse("<UNKNOWN>")); 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.command.CommandSource;
import com.velocitypowered.api.proxy.Player; 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.frankheijden.serverutils.common.entities.ServerUtilsAudience;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
public class VelocityAudience extends ServerUtilsAudience<CommandSource> { 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) { protected VelocityAudience(Audience audience, CommandSource source) {
super(audience, source); super(audience, source);
} }
@ -50,12 +24,6 @@ public class VelocityAudience extends ServerUtilsAudience<CommandSource> {
@Override @Override
public void sendMessage(Component component) { public void sendMessage(Component component) {
// Shading in adventure is fun when making a single distributable jar... source.sendMessage(component);
String serializedString = serializer.serialize(component);
try {
sendMessageMethodHandle.invoke(source, deserializeMethodHandle.invoke(deserializer, serializedString));
} catch (Throwable th) {
th.printStackTrace();
}
} }
} }

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) { 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. * Loads the plugin from their candidate PluginDescription.
*/ */
public static PluginDescription loadPlugin(Object javaPluginLoader, PluginDescription candidate) { public static PluginDescription loadPlugin(Object javaPluginLoader, PluginDescription candidate) {
return reflection.invoke( String fieldName = "createPluginFromCandidate";
javaPluginLoader, try {
"loadPlugin", reflection.getClazz().getDeclaredMethod(fieldName, PluginDescription.class);
ClassObject.of(PluginDescription.class, candidate) } catch (NoSuchMethodException ex) {
); fieldName = "loadPlugin";
}
return reflection.invoke(javaPluginLoader, fieldName, ClassObject.of(PluginDescription.class, candidate));
} }
public static Module createModule(Object javaPluginLoader, PluginContainer container) { public static Module createModule(Object javaPluginLoader, PluginContainer container) {

View file

@ -13,8 +13,18 @@ public class RVelocityPluginManager {
private RVelocityPluginManager() {} private RVelocityPluginManager() {}
/**
* Retrieves the plugin map. Key is the id of the plugin.
*/
public static Map<String, PluginContainer> getPlugins(PluginManager manager) { 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) { public static Map<Object, PluginContainer> getPluginInstances(PluginManager manager) {

View file

@ -3,7 +3,7 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins { plugins {
`java-library` `java-library`
`maven-publish` `maven-publish`
id("com.github.johnrengelman.shadow") version "7.0.0" id("com.github.johnrengelman.shadow") version "7.1.0"
} }
group = "net.frankheijden.serverutils" group = "net.frankheijden.serverutils"
@ -50,7 +50,7 @@ subprojects {
tasks { tasks {
build { build {
dependsOn("checkstyleMain", "checkstyleTest", "test") dependsOn("shadowJar", "checkstyleMain", "checkstyleTest", "test")
} }
compileJava { compileJava {
@ -86,8 +86,10 @@ subprojects {
relocate("cloud.commandframework", "${dependencyDir}.cloud") relocate("cloud.commandframework", "${dependencyDir}.cloud")
relocate("me.lucko.commodore", "${dependencyDir}.commodore") relocate("me.lucko.commodore", "${dependencyDir}.commodore")
relocate("io.leangen.geantyref", "${dependencyDir}.typetoken") relocate("io.leangen.geantyref", "${dependencyDir}.typetoken")
relocate("net.kyori.adventure", "${dependencyDir}.adventure") if (project.name != "Velocity") {
relocate("net.kyori.examination", "${dependencyDir}.examination") relocate("net.kyori.adventure", "${dependencyDir}.adventure")
relocate("net.kyori.examination", "${dependencyDir}.examination")
}
relocate("net.kyori.adventure.text.minimessage", "${dependencyDir}.adventure.text.minimessage") relocate("net.kyori.adventure.text.minimessage", "${dependencyDir}.adventure.text.minimessage")
relocate("dev.frankheijden.minecraftreflection", "${dependencyDir}.minecraftreflection") relocate("dev.frankheijden.minecraftreflection", "${dependencyDir}.minecraftreflection")
} }
@ -151,7 +153,6 @@ tasks.withType<ShadowJar> {
fun outputTasks(): List<Task> { fun outputTasks(): List<Task> {
return listOf( return listOf(
"shadowJar",
":Bukkit:shadowJar", ":Bukkit:shadowJar",
":Bungee:shadowJar", ":Bungee:shadowJar",
":Velocity:shadowJar", ":Velocity:shadowJar",