Merge pull request #31 from FrankHeijden/fix/common-module-shade

Fix the common module being relocated due to adventure
This commit is contained in:
Frank van der Heijden 2021-10-05 12:51:31 +02:00 committed by GitHub
commit 22d6af916d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 12 deletions

View file

@ -25,8 +25,4 @@ processResources {
shadowJar { shadowJar {
relocate 'org.bstats', dependencyDir + '.bstats' relocate 'org.bstats', dependencyDir + '.bstats'
relocate 'net.kyori.adventure', rootDependencyDir + '.adventure'
relocate 'net.kyori.examination', rootDependencyDir + '.examination'
relocate 'net.frankheijden.serverutils.common', dependencyDir + '.su.common'
relocate 'net.kyori.adventure.text.minimessage', dependencyDir + '.adventure.text.minimessage'
} }

View file

@ -29,8 +29,4 @@ processResources {
shadowJar { shadowJar {
relocate 'org.bstats', dependencyDir + '.bstats' relocate 'org.bstats', dependencyDir + '.bstats'
relocate 'net.kyori.adventure', rootDependencyDir + '.adventure'
relocate 'net.kyori.examination', rootDependencyDir + '.examination'
relocate 'net.kyori.adventure.text.minimessage', dependencyDir + '.adventure.text.minimessage'
relocate 'net.frankheijden.serverutils.common', dependencyDir + '.su.common'
} }

View file

@ -27,8 +27,6 @@ dependencies {
shadowJar { shadowJar {
relocate 'org.bstats', dependencyDir + '.bstats' relocate 'org.bstats', dependencyDir + '.bstats'
relocate 'net.frankheijden.serverutils.common', dependencyDir + '.su.common'
relocate 'net.kyori.adventure.text.minimessage', dependencyDir + '.adventure.text.minimessage'
} }
blossom { blossom {

View file

@ -2,11 +2,38 @@ 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.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);
} }
@ -20,4 +47,15 @@ public class VelocityAudience extends ServerUtilsAudience<CommandSource> {
public boolean hasPermission(String permission) { public boolean hasPermission(String permission) {
return source.hasPermission(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();
}
}
} }

View file

@ -76,6 +76,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'
relocate 'net.kyori.examination', dependencyDir + '.examination'
relocate 'net.kyori.adventure.text.minimessage', dependencyDir + '.adventure.text.minimessage'
relocate 'dev.frankheijden.minecraftreflection', dependencyDir + '.minecraftreflection'
} }
shadowJar.dependsOn checkstyleMain, checkstyleTest, test shadowJar.dependsOn checkstyleMain, checkstyleTest, test
@ -90,11 +94,14 @@ dependencies {
implementation project(path: ':Bukkit', configuration: 'shadow') implementation project(path: ':Bukkit', configuration: 'shadow')
implementation project(path: ':Bungee', configuration: 'shadow') implementation project(path: ':Bungee', configuration: 'shadow')
implementation project(path: ':Velocity', configuration: 'shadow') implementation project(path: ':Velocity', configuration: 'shadow')
implementation("net.kyori:adventure-text-serializer-gson:${rootProject.adventureVersion}") {
exclude group: 'net.kyori', module: 'adventure-api'
exclude group: 'com.google.code.gson', module: 'gson'
}
} }
shadowJar { shadowJar {
relocate 'org.apache.commons.codec', dependencyDir + '.codec' relocate 'net.kyori.adventure.text.serializer.gson', dependencyDir + '.impl.adventure.text.serializer.gson'
relocate 'dev.frankheijden.minecraftreflection', dependencyDir + '.minecraftreflection'
archiveFileName = "${archiveBaseName.orNull}-${archiveVersion.orNull}.${archiveExtension.orNull}" archiveFileName = "${archiveBaseName.orNull}-${archiveVersion.orNull}.${archiveExtension.orNull}"
} }