From d52983c5afc95f091d238443fb5af51285a62b20 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 17 Mar 2018 15:03:40 +0000 Subject: [PATCH] Fix tristate check event on BungeeCord for console senders --- .../bungee/event/TristateCheckEvent.java | 34 +++++++++++++++---- .../BungeePermissionCheckListener.java | 17 ++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/event/TristateCheckEvent.java b/bungee/src/main/java/me/lucko/luckperms/bungee/event/TristateCheckEvent.java index 51bacd9d..24af1d3a 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/event/TristateCheckEvent.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/event/TristateCheckEvent.java @@ -29,26 +29,46 @@ import me.lucko.luckperms.api.Tristate; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Event; +import java.util.Objects; + /** * Copy of the internal BungeeCord PermissionCheckEvent, returning a tristate instead of a boolean. */ public class TristateCheckEvent extends Event { + public static Tristate call(CommandSender sender, String permission) { - return ProxyServer.getInstance().getPluginManager().callEvent(new TristateCheckEvent(sender, permission)).getResult(); + + // calculate a default value based on the internal behaviour of + // ConsoleCommandSender and UserConnection in order to replicate the + // behaviour of CommandSender#hasPermission + Tristate def = Tristate.UNDEFINED; + if (!(sender instanceof ProxiedPlayer)) { + // if not a ProxiedPlayer, assume it's console. + def = Tristate.TRUE; + } else if (sender.getPermissions().contains(permission)) { + def = Tristate.TRUE; + } + + return call(sender, permission, def); + } + + public static Tristate call(CommandSender sender, String permission, Tristate def) { + Objects.requireNonNull(sender, "sender"); + Objects.requireNonNull(permission, "permission"); + Objects.requireNonNull(def, "def"); + + TristateCheckEvent event = new TristateCheckEvent(sender, permission, def); + return ProxyServer.getInstance().getPluginManager().callEvent(event).getResult(); } private final CommandSender sender; private final String permission; - private Tristate result; - public TristateCheckEvent(CommandSender sender, String permission) { - this(sender, permission, sender.getPermissions().contains(permission) ? Tristate.TRUE : Tristate.UNDEFINED); - } - - public TristateCheckEvent(CommandSender sender, String permission, Tristate result) { + private TristateCheckEvent(CommandSender sender, String permission, Tristate result) { this.sender = sender; this.permission = permission; this.result = result; diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeePermissionCheckListener.java b/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeePermissionCheckListener.java index 0bfa304a..77a5366c 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeePermissionCheckListener.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeePermissionCheckListener.java @@ -117,4 +117,21 @@ public class BungeePermissionCheckListener implements Listener { this.plugin.getVerboseHandler().offerCheckData(CheckOrigin.PLATFORM_PERMISSION_CHECK, name, ContextSet.empty(), permission, result); this.plugin.getPermissionVault().offer(permission); } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onOtherTristateCheck(TristateCheckEvent e) { + if (e.getSender() instanceof ProxiedPlayer) { + return; + } + + Objects.requireNonNull(e.getPermission(), "permission"); + Objects.requireNonNull(e.getSender(), "sender"); + + String permission = e.getPermission(); + Tristate result = e.getResult(); + String name = "internal/" + e.getSender().getName(); + + this.plugin.getVerboseHandler().offerCheckData(CheckOrigin.PLATFORM_LOOKUP_CHECK, name, ContextSet.empty(), permission, result); + this.plugin.getPermissionVault().offer(permission); + } }