mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2026-11-20 13:29:42 +00:00
Use native adventure methods on Paper (#4292)
This commit is contained in:
4 files changed
+147
-12
No files matched your search
@@ -25,6 +25,8 @@
|
||||
|
||||
package me.lucko.luckperms.bukkit;
|
||||
|
||||
import me.lucko.luckperms.bukkit.util.PaperAdventureBridge;
|
||||
import me.lucko.luckperms.common.locale.TranslationManager;
|
||||
import me.lucko.luckperms.common.sender.Sender;
|
||||
import me.lucko.luckperms.common.sender.SenderFactory;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
@@ -35,14 +37,21 @@ import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.command.RemoteConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BukkitSenderFactory extends SenderFactory<LPBukkitPlugin, CommandSender> {
|
||||
private final BukkitAudiences audiences;
|
||||
public abstract class BukkitSenderFactory extends SenderFactory<LPBukkitPlugin, CommandSender> {
|
||||
|
||||
public static BukkitSenderFactory create(LPBukkitPlugin plugin) {
|
||||
PaperAdventureBridge bridge = PaperAdventureBridge.INSTANCE;
|
||||
return bridge != null
|
||||
? new PaperBridgeBukkitSenderFactory(plugin, bridge)
|
||||
: new AdventurePlatformBukkitSenderFactory(plugin);
|
||||
|
||||
}
|
||||
|
||||
public BukkitSenderFactory(LPBukkitPlugin plugin) {
|
||||
super(plugin);
|
||||
this.audiences = BukkitAudiences.create(plugin.getLoader());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,12 +74,14 @@ public class BukkitSenderFactory extends SenderFactory<LPBukkitPlugin, CommandSe
|
||||
protected void sendMessage(CommandSender sender, Component message) {
|
||||
// we can safely send async for players and the console - otherwise, send it sync
|
||||
if (sender instanceof Player || sender instanceof ConsoleCommandSender || sender instanceof RemoteConsoleCommandSender) {
|
||||
this.audiences.sender(sender).sendMessage(message);
|
||||
sendMessage0(sender, message);
|
||||
} else {
|
||||
getPlugin().getBootstrap().getScheduler().executeSync(sender, () -> this.audiences.sender(sender).sendMessage(message));
|
||||
getPlugin().getBootstrap().getScheduler().executeSync(sender, () -> sendMessage0(sender, message));
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void sendMessage0(CommandSender sender, Component message);
|
||||
|
||||
@Override
|
||||
protected Tristate getPermissionValue(CommandSender sender, String node) {
|
||||
if (sender.hasPermission(node)) {
|
||||
@@ -97,9 +108,48 @@ public class BukkitSenderFactory extends SenderFactory<LPBukkitPlugin, CommandSe
|
||||
return sender instanceof ConsoleCommandSender || sender instanceof RemoteConsoleCommandSender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
this.audiences.close();
|
||||
/**
|
||||
* Sender factory that uses native Paper adventure support to send messages.
|
||||
*/
|
||||
private static class PaperBridgeBukkitSenderFactory extends BukkitSenderFactory {
|
||||
private final PaperAdventureBridge bridge;
|
||||
|
||||
public PaperBridgeBukkitSenderFactory(LPBukkitPlugin plugin, PaperAdventureBridge bridge) {
|
||||
super(plugin);
|
||||
this.bridge = bridge;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendMessage0(CommandSender sender, Component message) {
|
||||
Locale locale = null;
|
||||
if (sender instanceof Player) {
|
||||
locale = ((Player) sender).locale();
|
||||
}
|
||||
Component rendered = TranslationManager.render(message, locale);
|
||||
this.bridge.sendMessage(sender, rendered);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sender factory that uses the adventure-platform library to send messages.
|
||||
*/
|
||||
private static class AdventurePlatformBukkitSenderFactory extends BukkitSenderFactory {
|
||||
private final BukkitAudiences audiences;
|
||||
|
||||
public AdventurePlatformBukkitSenderFactory(LPBukkitPlugin plugin) {
|
||||
super(plugin);
|
||||
this.audiences = BukkitAudiences.create(plugin.getLoader());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendMessage0(CommandSender sender, Component message) {
|
||||
this.audiences.sender(sender).sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
this.audiences.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin {
|
||||
|
||||
@Override
|
||||
protected void setupSenderFactory() {
|
||||
this.senderFactory = new BukkitSenderFactory(this);
|
||||
this.senderFactory = BukkitSenderFactory.create(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <[email protected]>
|
||||
* 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.bukkit.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public final class PaperAdventureBridge {
|
||||
|
||||
public static final PaperAdventureBridge INSTANCE;
|
||||
static {
|
||||
PaperAdventureBridge bridge = null;
|
||||
try {
|
||||
bridge = new PaperAdventureBridge();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
INSTANCE = bridge;
|
||||
}
|
||||
|
||||
private final Method serializerDeserialize;
|
||||
private final Method sendMessage;
|
||||
private final Object serializerInstance;
|
||||
|
||||
private PaperAdventureBridge() throws Exception {
|
||||
String adventurePkg = "net.kyo".concat("ri.adventure.");
|
||||
Class<?> audienceClass = Class.forName(adventurePkg + "audience.Audience");
|
||||
Class<?> componentClass = Class.forName(adventurePkg + "text.Component");
|
||||
Class<?> serializerClass = Class.forName(adventurePkg + "text.serializer.gson.GsonComponentSerializer");
|
||||
|
||||
if (CommandSender.class.isAssignableFrom(audienceClass)) {
|
||||
throw new IllegalStateException("CommandSender does not implement Audience");
|
||||
}
|
||||
|
||||
this.serializerDeserialize = serializerClass.getMethod("deserializeFromTree", JsonElement.class);
|
||||
this.sendMessage = audienceClass.getMethod("sendMessage", componentClass);
|
||||
this.serializerInstance = serializerClass.getMethod("gson").invoke(null);
|
||||
}
|
||||
|
||||
public Object toPlatformComponent(Component component) {
|
||||
JsonElement json = GsonComponentSerializer.gson().serializeToTree(component);
|
||||
try {
|
||||
return this.serializerDeserialize.invoke(this.serializerInstance, json);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessage(CommandSender audience, Component message) {
|
||||
try {
|
||||
this.sendMessage.invoke(audience, toPlatformComponent(message));
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
package me.lucko.luckperms.velocity.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.event.ResultedEvent.ComponentResult;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -54,7 +55,7 @@ public final class AdventureCompat {
|
||||
Class<?> componentClass = Class.forName(adventurePkg + "text.Component");
|
||||
Class<?> serializerClass = Class.forName(adventurePkg + "text.serializer.gson.GsonComponentSerializer");
|
||||
|
||||
PLATFORM_SERIALIZER_DESERIALIZE = serializerClass.getMethod("deserialize", Object.class);
|
||||
PLATFORM_SERIALIZER_DESERIALIZE = serializerClass.getMethod("deserializeFromTree", JsonElement.class);
|
||||
PLATFORM_SEND_MESSAGE = audienceClass.getMethod("sendMessage", componentClass);
|
||||
PLATFORM_COMPONENT_RESULT_DENIED = ComponentResult.class.getMethod("denied", componentClass);
|
||||
PLATFORM_SERIALIZER_INSTANCE = serializerClass.getMethod("gson").invoke(null);
|
||||
@@ -64,7 +65,7 @@ public final class AdventureCompat {
|
||||
}
|
||||
|
||||
public static Object toPlatformComponent(Component component) {
|
||||
String json = GsonComponentSerializer.gson().serialize(component);
|
||||
JsonElement json = GsonComponentSerializer.gson().serializeToTree(component);
|
||||
try {
|
||||
return PLATFORM_SERIALIZER_DESERIALIZE.invoke(PLATFORM_SERIALIZER_INSTANCE, json);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
|
||||
Reference in new issue
Block a user