Fix sending messages on Velocity proxies

This commit is contained in:
Luck 2020-10-15 23:35:59 +01:00
parent 6e7c49488d
commit b6e5b0bd47
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
7 changed files with 118 additions and 26 deletions

View File

@ -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;

View File

@ -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<FileWatcher> getFileWatcher();
/**
* Gets the http client used by the plugin.
*
* @return the http client
*/
OkHttpClient getHttpClient();
/**
* Gets the bytebin instance in use by platform.
*

View File

@ -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'

View File

@ -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;
}

View File

@ -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<LPVelocityPlugin, Comma
locale = ((Player) source).getPlayerSettings().getLocale();
}
Component rendered = GlobalTranslator.render(message, locale);
source.sendMessage(rendered);
AdventureCompat.sendMessage(source, rendered);
}
@Override

View File

@ -26,7 +26,6 @@
package me.lucko.luckperms.velocity.listeners;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.LoginEvent;
@ -39,6 +38,7 @@ import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener;
import me.lucko.luckperms.velocity.LPVelocityPlugin;
import me.lucko.luckperms.velocity.service.PlayerPermissionProvider;
import me.lucko.luckperms.velocity.util.AdventureCompat;
import net.kyori.adventure.translation.GlobalTranslator;
@ -108,7 +108,7 @@ public class VelocityConnectionListener extends AbstractConnectionListener {
public void onPlayerLogin(LoginEvent e) {
final Player player = e.getPlayer();
if (this.deniedLogin.remove(player.getUniqueId())) {
e.setResult(ResultedEvent.ComponentResult.denied(GlobalTranslator.render(Message.LOADING_DATABASE_ERROR.build(), player.getPlayerSettings().getLocale())));
e.setResult(AdventureCompat.deniedResult(GlobalTranslator.render(Message.LOADING_DATABASE_ERROR.build(), player.getPlayerSettings().getLocale())));
}
}
@ -136,7 +136,7 @@ public class VelocityConnectionListener extends AbstractConnectionListener {
if (this.plugin.getConfiguration().get(ConfigKeys.CANCEL_FAILED_LOGINS)) {
// disconnect the user
e.setResult(ResultedEvent.ComponentResult.denied(GlobalTranslator.render(Message.LOADING_STATE_ERROR.build(), player.getPlayerSettings().getLocale())));
e.setResult(AdventureCompat.deniedResult(GlobalTranslator.render(Message.LOADING_STATE_ERROR.build(), player.getPlayerSettings().getLocale())));
} else {
// just send a message
this.plugin.getBootstrap().getScheduler().asyncLater(() -> {

View File

@ -0,0 +1,107 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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);
}
}
}