From 3889f8683d75677e35ada48b0477270a85d8411a Mon Sep 17 00:00:00 2001 From: Simon Chuu Date: Sat, 19 Jan 2019 00:15:40 -0500 Subject: [PATCH] #2578: Add easy way to get proxy command iff it is enabled for that sender --- .../net/md_5/bungee/api/event/ChatEvent.java | 23 +++++++++++++ .../md_5/bungee/api/plugin/PluginManager.java | 33 +++++++++++++++---- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/event/ChatEvent.java b/api/src/main/java/net/md_5/bungee/api/event/ChatEvent.java index 067a800c..9b36b91e 100644 --- a/api/src/main/java/net/md_5/bungee/api/event/ChatEvent.java +++ b/api/src/main/java/net/md_5/bungee/api/event/ChatEvent.java @@ -3,8 +3,11 @@ package net.md_5.bungee.api.event; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.Connection; import net.md_5.bungee.api.plugin.Cancellable; +import net.md_5.bungee.api.plugin.PluginManager; /** * Event called when a player sends a message to a server. @@ -39,4 +42,24 @@ public class ChatEvent extends TargetedEvent implements Cancellable { return message.length() > 0 && message.charAt( 0 ) == '/'; } + + /** + * Checks whether this message is run on this proxy server. + * + * @return if this command runs on the proxy + * @see PluginManager#isExecutableCommand(java.lang.String, net.md_5.bungee.api.CommandSender) + */ + public boolean isProxyCommand() + { + if ( !isCommand() ) + { + return false; + } + + int index = message.indexOf( " " ); + String commandName = ( index == -1 ) ? message.substring( 1 ) : message.substring( 1, index ); + CommandSender sender = ( getSender() instanceof CommandSender ) ? (CommandSender) getSender() : null; + + return ProxyServer.getInstance().getPluginManager().isExecutableCommand( commandName, sender ); + } } diff --git a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java index 3a871787..bf894e4a 100644 --- a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java +++ b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java @@ -111,6 +111,32 @@ public class PluginManager } } + private Command getCommandIfEnabled(String commandName, CommandSender sender) + { + String commandLower = commandName.toLowerCase( Locale.ROOT ); + + // Check if command is disabled when a player sent the command + if ( ( sender instanceof ProxiedPlayer ) && proxy.getDisabledCommands().contains( commandLower ) ) + { + return null; + } + + return commandMap.get( commandLower ); + } + + /** + * Checks if the command is registered and can possibly be executed by the + * sender (without taking permissions into account). + * + * @param commandName the name of the command + * @param sender the sender executing the command + * @return whether the command will be handled + */ + public boolean isExecutableCommand(String commandName, CommandSender sender) + { + return getCommandIfEnabled( commandName, sender ) != null; + } + public boolean dispatchCommand(CommandSender sender, String commandLine) { return dispatchCommand( sender, commandLine, null ); @@ -133,12 +159,7 @@ public class PluginManager return false; } - String commandName = split[0].toLowerCase( Locale.ROOT ); - if ( sender instanceof ProxiedPlayer && proxy.getDisabledCommands().contains( commandName ) ) - { - return false; - } - Command command = commandMap.get( commandName ); + Command command = getCommandIfEnabled( split[0], sender ); if ( command == null ) { return false;