diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java index cf4ab4fac..ebeb15e72 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java @@ -78,7 +78,6 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { private PermissionRegistry permissionRegistry; private LogDispatcher logDispatcher; private LuckPermsConfiguration configuration; - private OkHttpClient httpClient; private BytebinClient bytebin; private FileWatcher fileWatcher = null; private Storage storage; @@ -119,11 +118,11 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { this.configuration = new LuckPermsConfiguration(this, provideConfigurationAdapter()); // setup a bytebin instance - this.httpClient = new OkHttpClient.Builder() + OkHttpClient httpClient = new OkHttpClient.Builder() .callTimeout(15, TimeUnit.SECONDS) .build(); - this.bytebin = new BytebinClient(this.httpClient, getConfiguration().get(ConfigKeys.BYTEBIN_URL), "luckperms"); + this.bytebin = new BytebinClient(httpClient, getConfiguration().get(ConfigKeys.BYTEBIN_URL), "luckperms"); // now the configuration is loaded, we can create a storage factory and load initial dependencies StorageFactory storageFactory = new StorageFactory(this); @@ -317,11 +316,6 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { return this.configuration; } - @Override - public OkHttpClient getHttpClient() { - return this.httpClient; - } - @Override public BytebinClient getBytebin() { return this.bytebin; diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java index accb59459..ffbd27351 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java @@ -57,8 +57,6 @@ import me.lucko.luckperms.common.verbose.VerboseHandler; import net.luckperms.api.query.QueryOptions; -import okhttp3.OkHttpClient; - import java.util.Collections; import java.util.List; import java.util.Optional; @@ -234,13 +232,6 @@ public interface LuckPermsPlugin { */ Optional getFileWatcher(); - /** - * Gets the http client used by the plugin. - * - * @return the http client - */ - OkHttpClient getHttpClient(); - /** * Gets the bytebin instance in use by platform. * diff --git a/velocity/build.gradle b/velocity/build.gradle index a81373103..ac6d2b1b2 100644 --- a/velocity/build.gradle +++ b/velocity/build.gradle @@ -8,10 +8,12 @@ repositories { } dependencies { - compile project(':common') + compileOnly 'com.velocitypowered:velocity-api:1.1.0-SNAPSHOT' annotationProcessor 'com.velocitypowered:velocity-api:1.1.0-SNAPSHOT' + + compile project(':common') } blossom { @@ -27,7 +29,7 @@ shadowJar { include(dependency('me.lucko.luckperms:.*')) } - //relocate 'net.kyori.adventure', 'me.lucko.luckperms.lib.adventure' + relocate 'net.kyori.adventure', 'me.lucko.luckperms.lib.adventure' relocate 'net.kyori.event', 'me.lucko.luckperms.lib.eventbus' relocate 'com.github.benmanes.caffeine', 'me.lucko.luckperms.lib.caffeine' relocate 'okio', 'me.lucko.luckperms.lib.okio' diff --git a/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java b/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java index 2b072d02b..0a21de08d 100644 --- a/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java +++ b/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java @@ -96,9 +96,6 @@ public class LPVelocityPlugin extends AbstractLuckPermsPlugin { dependencies.add(Dependency.CONFIGURATE_CORE); dependencies.add(Dependency.CONFIGURATE_YAML); dependencies.add(Dependency.SNAKEYAML); - - // already included in the proxy - dependencies.remove(Dependency.ADVENTURE); return dependencies; } diff --git a/velocity/src/main/java/me/lucko/luckperms/velocity/VelocitySenderFactory.java b/velocity/src/main/java/me/lucko/luckperms/velocity/VelocitySenderFactory.java index e96439bd0..c5bf1e220 100644 --- a/velocity/src/main/java/me/lucko/luckperms/velocity/VelocitySenderFactory.java +++ b/velocity/src/main/java/me/lucko/luckperms/velocity/VelocitySenderFactory.java @@ -32,6 +32,7 @@ import me.lucko.luckperms.common.locale.TranslationManager; import me.lucko.luckperms.common.sender.Sender; import me.lucko.luckperms.common.sender.SenderFactory; import me.lucko.luckperms.velocity.service.CompatibilityUtil; +import me.lucko.luckperms.velocity.util.AdventureCompat; import net.kyori.adventure.text.Component; import net.kyori.adventure.translation.GlobalTranslator; @@ -68,7 +69,7 @@ public class VelocitySenderFactory extends SenderFactory { diff --git a/velocity/src/main/java/me/lucko/luckperms/velocity/util/AdventureCompat.java b/velocity/src/main/java/me/lucko/luckperms/velocity/util/AdventureCompat.java new file mode 100644 index 000000000..9468ebf14 --- /dev/null +++ b/velocity/src/main/java/me/lucko/luckperms/velocity/util/AdventureCompat.java @@ -0,0 +1,107 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.velocity.util; + +import com.velocitypowered.api.event.ResultedEvent.ComponentResult; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; + +import java.lang.reflect.Method; + +/** + * Converts between platform and native adventure objects. + * + * We shade + relocate adventure in LuckPerms. This is because we use a slightly modified version. + * Unfortunately (for us), Velocity also uses the same library. This class converts between "our" + * adventure components and Velocity's. + */ +public final class AdventureCompat { + private AdventureCompat() {} + + private static final String PLATFORM_ADVENTURE_PACKAGE; + + private static final Class PLATFORM_AUDIENCE; + private static final Class PLATFORM_IDENTITY; + private static final Class PLATFORM_COMPONENT; + private static final Class PLATFORM_SERIALIZER; + + private static final Method PLATFORM_SERIALIZER_GETTER; + private static final Method PLATFORM_SERIALIZER_DESERIALIZE; + private static final Method PLATFORM_IDENTITY_NIL_GETTER; + private static final Method PLATFORM_SEND_MESSAGE; + private static final Method PLATFORM_COMPONENT_RESULT_DENIED; + + private static final Object PLATFORM_SERIALIZER_INSTANCE; + private static final Object PLATFORM_IDENTITY_INSTANCE; + + static { + PLATFORM_ADVENTURE_PACKAGE = "net.kyo".concat("ri.adventure."); + try { + PLATFORM_AUDIENCE = Class.forName(PLATFORM_ADVENTURE_PACKAGE + "audience.Audience"); + PLATFORM_IDENTITY = Class.forName(PLATFORM_ADVENTURE_PACKAGE + "identity.Identity"); + PLATFORM_COMPONENT = Class.forName(PLATFORM_ADVENTURE_PACKAGE + "text.Component"); + PLATFORM_SERIALIZER = Class.forName(PLATFORM_ADVENTURE_PACKAGE + "text.serializer.gson.GsonComponentSerializer"); + + PLATFORM_SERIALIZER_GETTER = PLATFORM_SERIALIZER.getMethod("gson"); + PLATFORM_SERIALIZER_DESERIALIZE = PLATFORM_SERIALIZER.getMethod("deserialize", Object.class); + PLATFORM_IDENTITY_NIL_GETTER = PLATFORM_IDENTITY.getMethod("nil"); + PLATFORM_SEND_MESSAGE = PLATFORM_AUDIENCE.getMethod("sendMessage", PLATFORM_IDENTITY, PLATFORM_COMPONENT); + PLATFORM_COMPONENT_RESULT_DENIED = ComponentResult.class.getMethod("denied", PLATFORM_COMPONENT); + + PLATFORM_SERIALIZER_INSTANCE = PLATFORM_SERIALIZER_GETTER.invoke(null); + PLATFORM_IDENTITY_INSTANCE = PLATFORM_IDENTITY_NIL_GETTER.invoke(null); + } catch (ReflectiveOperationException e) { + throw new ExceptionInInitializerError(e); + } + } + + public static Object toPlatformComponent(Component component) { + String json = GsonComponentSerializer.gson().serialize(component); + try { + return PLATFORM_SERIALIZER_DESERIALIZE.invoke(PLATFORM_SERIALIZER_INSTANCE, json); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + + public static void sendMessage(Object audience, Component message) { + try { + PLATFORM_SEND_MESSAGE.invoke(audience, PLATFORM_IDENTITY_INSTANCE, toPlatformComponent(message)); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + + public static ComponentResult deniedResult(Component message) { + try { + return (ComponentResult) PLATFORM_COMPONENT_RESULT_DENIED.invoke(null, toPlatformComponent(message)); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + +}