Fix the common module being relocated due to adventure
Making a single distributable jar with adventure is fun
This commit is contained in:
parent
f5636c082e
commit
15e3ba306c
5 changed files with 47 additions and 12 deletions
|
|
@ -27,8 +27,6 @@ dependencies {
|
|||
|
||||
shadowJar {
|
||||
relocate 'org.bstats', dependencyDir + '.bstats'
|
||||
relocate 'net.frankheijden.serverutils.common', dependencyDir + '.su.common'
|
||||
relocate 'net.kyori.adventure.text.minimessage', dependencyDir + '.adventure.text.minimessage'
|
||||
}
|
||||
|
||||
blossom {
|
||||
|
|
|
|||
|
|
@ -2,11 +2,38 @@ 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);
|
||||
}
|
||||
|
|
@ -20,4 +47,15 @@ public class VelocityAudience extends ServerUtilsAudience<CommandSource> {
|
|||
public boolean hasPermission(String permission) {
|
||||
return source.hasPermission(permission);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue