Describes a chat command. Defines properties, permission requirements, and usage information. Specifies a handler method.
Inheritance: IClassy
示例#1
0
        static void RaiseCommandRegisteredEvent(CommandDescriptor descriptor)
        {
            var h = CommandRegistered;

            if (h != null)
            {
                h(null, new CommandRegisteredEventArgs(descriptor));
            }
        }
示例#2
0
        internal static void RaiseCommandCalledEvent(CommandReader cmd, CommandDescriptor descriptor, Player player)
        {
            var h = CommandCalled;

            if (h != null)
            {
                CommandCalled(null, new CommandCalledEventArgs(cmd, descriptor, player));
            }
        }
示例#3
0
 internal CommandRegistrationException([NotNull] CommandDescriptor descriptor, [NotNull] string message)
     : base(message)
 {
     if (descriptor == null)
     {
         throw new ArgumentNullException("descriptor");
     }
     Descriptor = descriptor;
 }
示例#4
0
 /// <summary> Registers a custom command with fCraft.
 /// Raises CommandManager.CommandRegistering/CommandRegistered events. </summary>
 /// <param name="descriptor"> Command descriptor to register. May not be null. </param>
 /// <exception cref="ArgumentNullException"> If descriptor is null. </exception>
 /// <exception cref="CommandRegistrationException"> If command could not be registered. </exception>
 public static void RegisterCustomCommand([NotNull] CommandDescriptor descriptor)
 {
     if (descriptor == null)
     {
         throw new ArgumentNullException("descriptor");
     }
     descriptor.IsCustom = true;
     RegisterCommand(descriptor);
 }
        /// <summary> Parses and calls a specified command. </summary>
        /// <param name="player"> Player who issued the command. </param>
        /// <param name="cmd"> Command to be parsed and executed. </param>
        /// <param name="fromConsole"> Whether this command is being called from a non-player (e.g. Console). </param>
        /// <returns> True if the command was called, false if something prevented it from being called. </returns>
        public static bool ParseCommand([NotNull] Player player, [NotNull] Command cmd, bool fromConsole)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player");
            }
            if (cmd == null)
            {
                throw new ArgumentNullException("cmd");
            }
            CommandDescriptor descriptor = cmd.Descriptor;

            if (descriptor == null)
            {
                player.Message("Unknown command \"{0}\". See &H/Commands", cmd.Name);
                Logger.Log(LogType.UserCommand, "{0}: /{1}", player.Name, cmd.Name);
                return(false);
            }

            if (!descriptor.IsConsoleSafe && fromConsole)
            {
                player.Message("You cannot use this command from console.");
            }
            else
            {
                if (descriptor.Permissions != null)
                {
                    if (!descriptor.CanBeCalledBy(player.Info.Rank))
                    {
                        player.MessageNoAccess(descriptor);
                    }
                    else if (!descriptor.Call(player, cmd, true))
                    {
                        player.Message("Command was cancelled.");
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    if (descriptor.Call(player, cmd, true))
                    {
                        return(true);
                    }
                    else
                    {
                        player.Message("Command was cancelled.");
                    }
                }
            }
            return(false);
        }
示例#6
0
        /// <summary> Parses and calls a specified command. </summary>
        /// <param name="player"> Player who issued the command. </param>
        /// <param name="cmd"> Command to be parsed and executed. </param>
        /// <param name="fromConsole"> Whether this command is being called from a non-player (e.g. Console). </param>
        /// <returns> True if the command was called, false if something prevented it from being called. </returns>
        /// <exception cref="ArgumentNullException"> player or cmd is null. </exception>
        public static bool ParseCommand([NotNull] Player player, [NotNull] CommandReader cmd, bool fromConsole)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player");
            }
            if (cmd == null)
            {
                throw new ArgumentNullException("cmd");
            }
            CommandDescriptor descriptor = cmd.Descriptor;

            if (descriptor == null)
            {
                if (CommandManager.ParseUnknownCommand(player, cmd))
                {
                    return(true);
                }
                player.Message("Unknown command \"{0}\". See &H/Commands", cmd.Name);
                return(false);
            }
            if (!descriptor.IsConsoleSafe && fromConsole)
            {
                player.Message("You cannot use this command from console.");
                return(false);
            }

            if (descriptor.Permissions != null)
            {
                if (!descriptor.CanBeCalledBy(player.Info.Rank))
                {
                    player.MessageNoAccess(descriptor);
                    return(false);
                }

                if (descriptor.MinRank != RankManager.LowestRank && !player.Info.ClassicubeVerified)
                {
                    player.Message("As you had an older minecraft.net account, you must have an admin verify your " +
                                   "new classicube.net account actually is you with /verify before you can use non-guest commands.");
                    return(false);
                }
            }

            if (descriptor.Call(player, cmd, true))
            {
                return(true);
            }
            else
            {
                player.Message("Command was cancelled.");
                return(false);
            }
        }
示例#7
0
        static bool RaiseCommandRegisteringEvent(CommandDescriptor descriptor)
        {
            var h = CommandRegistering;

            if (h == null)
            {
                return(false);
            }
            var e = new CommandRegisteringEventArgs(descriptor);

            h(null, e);
            return(e.Cancel);
        }
示例#8
0
        internal static bool RaiseCommandCallingEvent(CommandReader cmd, CommandDescriptor descriptor, Player player)
        {
            var h = CommandCalling;

            if (h == null)
            {
                return(false);
            }
            var e = new CommandCallingEventArgs(cmd, descriptor, player);

            h(null, e);
            return(e.Cancel);
        }
示例#9
0
 internal CommandRegistrationException([NotNull] CommandDescriptor descriptor,
                                       [NotNull] string message, [NotNull] params object[] args)
     : base(String.Format(message, args))
 {
     if (descriptor == null)
     {
         throw new ArgumentNullException("descriptor");
     }
     if (args == null)
     {
         throw new ArgumentNullException("args");
     }
     Descriptor = descriptor;
 }
示例#10
0
        /// <summary> Prints "This command requires ___+ rank" message. </summary>
        /// <param name="cmd"> Command to check. </param>
        /// <exception cref="ArgumentNullException"> cmd is null. </exception>
        public void MessageNoAccess([NotNull] CommandDescriptor cmd)
        {
            if (cmd == null)
            {
                throw new ArgumentNullException("cmd");
            }
            Rank reqRank = cmd.MinRank;

            if (reqRank == null)
            {
                Message("This command is disabled on the server.");
            }
            else
            {
                Message("This command requires {0}+&S rank.",
                        reqRank.ClassyName);
            }
        }
示例#11
0
        /// <summary> Parses and calls a specified command. </summary>
        /// <param name="player"> Player who issued the command. </param>
        /// <param name="cmd"> Command to be parsed and executed. </param>
        /// <param name="fromConsole"> Whether this command is being called from a non-player (e.g. Console). </param>
        public static void ParseCommand(Player player, Command cmd, bool fromConsole)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player");
            }
            if (cmd == null)
            {
                throw new ArgumentNullException("cmd");
            }
            CommandDescriptor descriptor = GetDescriptor(cmd.Name);

            if (descriptor == null)
            {
                player.Message("Unknown command \"{0}\". See &H/help commands", cmd.Name);
                return;
            }

            if (!descriptor.IsConsoleSafe && fromConsole)
            {
                player.Message("You cannot use this command from console.");
            }
            else
            {
                if (descriptor.Permissions != null)
                {
                    if (player.Can(descriptor.Permissions))
                    {
                        descriptor.Call(player, cmd, true);
                    }
                    else
                    {
                        player.NoAccessMessage(descriptor.Permissions);
                    }
                }
                else
                {
                    descriptor.Call(player, cmd, true);
                }
            }
        }
示例#12
0
 static bool RaiseCommandRegisteringEvent( CommandDescriptor descriptor )
 {
     var h = CommandRegistering;
     if ( h == null ) return false;
     var e = new CommandRegistringEventArgs( descriptor );
     h( null, e );
     return e.Cancel;
 }
示例#13
0
 static void RaiseCommandRegisteredEvent( CommandDescriptor descriptor )
 {
     var h = CommandRegistered;
     if ( h != null ) h( null, new CommandRegisteredEventArgs( descriptor ) );
 }
示例#14
0
 internal static bool RaiseCommandCallingEvent( Command cmd, CommandDescriptor descriptor, Player player )
 {
     var h = CommandCalling;
     if ( h == null ) return false;
     var e = new CommandCallingEventArgs( cmd, descriptor, player );
     h( null, e );
     return e.Cancel;
 }
示例#15
0
 internal static void RaiseCommandCalledEvent( Command cmd, CommandDescriptor descriptor, Player player )
 {
     var h = CommandCalled;
     if ( h != null ) CommandCalled( null, new CommandCalledEventArgs( cmd, descriptor, player ) );
 }
示例#16
0
 internal static void RaiseCommandCalledEvent(CommandReader cmd, CommandDescriptor descriptor, Player player)
 {
     CommandCalledEvent.Raise(new CommandCalledEventArgs(cmd, descriptor, player));
 }
示例#17
0
 static void RaiseCommandRegisteredEvent(CommandDescriptor descriptor)
 {
     CommandRegisteredEvent.Raise(new CommandRegisteredEventArgs(descriptor));
 }
示例#18
0
        internal static void RegisterCommand( CommandDescriptor descriptor ) {
            if( descriptor == null ) throw new ArgumentNullException( "descriptor" );

#if DEBUG
            if( descriptor.Category == CommandCategory.None && !descriptor.IsCustom ) {
                throw new CommandRegistrationException( "Standard commands must have a category set." );
            }
#endif

            if( string.IsNullOrEmpty( descriptor.Name ) || descriptor.Name.Length > 16 ) {
                throw new CommandRegistrationException( "All commands need a name, between 1 and 16 alphanumeric characters long." );
            }

            if( Commands.ContainsKey( descriptor.Name ) ) {
                throw new CommandRegistrationException( "A command with the name \"{0}\" is already registered.", descriptor.Name );
            }

            if( descriptor.Handler == null ) {
                throw new CommandRegistrationException( "All command descriptors are required to provide a handler callback." );
            }

            if( descriptor.Aliases != null ) {
                if( descriptor.Aliases.Any( alias => Commands.ContainsKey( alias ) ) ) {
                    throw new CommandRegistrationException( "One of the aliases for \"{0}\" is using the name of an already-defined command." );
                }
            }

            if( descriptor.Usage == null ) {
                descriptor.Usage = "/" + descriptor.Name;
            }

            if( RaiseCommandRegisteringEvent( descriptor ) ) return;

            if( Aliases.ContainsKey( descriptor.Name ) ) {
                Logger.Log( "Commands.RegisterCommand: \"{0}\" was defined as an alias for \"{1}\", but has been overridden.", LogType.Warning,
                            descriptor.Name, Aliases[descriptor.Name] );
                Aliases.Remove( descriptor.Name );
            }

            if( descriptor.Aliases != null ) {
                foreach( string alias in descriptor.Aliases ) {
                    if( Aliases.ContainsKey( alias ) ) {
                        Logger.Log( "Commands.RegisterCommand: \"{0}\" was defined as an alias for \"{1}\", but has been overridden to resolve to \"{2}\" instead.",
                                    LogType.Warning,
                                    alias, Aliases[alias], descriptor.Name );
                    } else {
                        Aliases.Add( alias, descriptor.Name );
                    }
                }
            }

            Commands.Add( descriptor.Name, descriptor );

            RaiseCommandRegisteredEvent( descriptor );
        }
示例#19
0
        internal static void RegisterCommand(CommandDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

#if DEBUG
            if (descriptor.Category == CommandCategory.None && !descriptor.IsCustom)
            {
                throw new CommandRegistrationException("Standard commands must have a category set.");
            }
#endif

            if (string.IsNullOrEmpty(descriptor.Name) || descriptor.Name.Length > 16)
            {
                throw new CommandRegistrationException("All commands need a name, between 1 and 16 alphanumeric characters long.");
            }

            if (Commands.ContainsKey(descriptor.Name))
            {
                throw new CommandRegistrationException("A command with the name \"{0}\" is already registered.", descriptor.Name);
            }

            if (descriptor.Handler == null)
            {
                throw new CommandRegistrationException("All command descriptors are required to provide a handler callback.");
            }

            if (descriptor.Aliases != null)
            {
                if (descriptor.Aliases.Any(alias => Commands.ContainsKey(alias)))
                {
                    throw new CommandRegistrationException("One of the aliases for \"{0}\" is using the name of an already-defined command.");
                }
            }

            if (descriptor.Usage == null)
            {
                descriptor.Usage = "/" + descriptor.Name;
            }

            if (RaiseCommandRegisteringEvent(descriptor))
            {
                return;
            }

            if (Aliases.ContainsKey(descriptor.Name))
            {
                Logger.Log("Commands.RegisterCommand: \"{0}\" was defined as an alias for \"{1}\", but has been overridden.", LogType.Warning,
                           descriptor.Name, Aliases[descriptor.Name]);
                Aliases.Remove(descriptor.Name);
            }

            if (descriptor.Aliases != null)
            {
                foreach (string alias in descriptor.Aliases)
                {
                    if (Aliases.ContainsKey(alias))
                    {
                        Logger.Log("Commands.RegisterCommand: \"{0}\" was defined as an alias for \"{1}\", but has been overridden to resolve to \"{2}\" instead.",
                                   LogType.Warning,
                                   alias, Aliases[alias], descriptor.Name);
                    }
                    else
                    {
                        Aliases.Add(alias, descriptor.Name);
                    }
                }
            }

            Commands.Add(descriptor.Name, descriptor);

            RaiseCommandRegisteredEvent(descriptor);
        }
示例#20
0
        internal static void RegisterCommand([NotNull] CommandDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

#if DEBUG
            if (descriptor.Category == CommandCategory.None && !descriptor.IsCustom)
            {
                throw new CommandRegistrationException(descriptor, "Standard commands must have a category set.");
            }
#endif

            if (!IsValidCommandName(descriptor.Name))
            {
                throw new CommandRegistrationException(descriptor,
                                                       "All commands need a name, between 1 and 16 alphanumeric characters long.");
            }

            string normalizedName = descriptor.Name.ToLower();

            if (Commands.ContainsKey(normalizedName))
            {
                throw new CommandRegistrationException(descriptor,
                                                       "A command with the name \"{0}\" is already registered.",
                                                       descriptor.Name);
            }

            if (ReservedCommandNames.Contains(normalizedName))
            {
                throw new CommandRegistrationException(descriptor, "The command name is reserved.");
            }

            if (descriptor.Handler == null)
            {
                throw new CommandRegistrationException(descriptor,
                                                       "All command descriptors are required to provide a handler callback.");
            }

            if (descriptor.Aliases != null)
            {
                if (descriptor.Aliases.Any(alias => Commands.ContainsKey(alias.ToLowerInvariant())))
                {
                    throw new CommandRegistrationException(descriptor,
                                                           "One of the aliases for \"{0}\" is using the name of an already-defined command.",
                                                           descriptor.Name);
                }
            }

            if (!Char.IsUpper(descriptor.Name[0]))
            {
                descriptor.Name = descriptor.Name.UppercaseFirst();
            }

            if (descriptor.Usage == null)
            {
                descriptor.Usage = "/" + descriptor.Name;
            }

            if (RaiseCommandRegisteringEvent(descriptor))
            {
                return;
            }

            if (Aliases.ContainsKey(normalizedName))
            {
                Logger.Log(LogType.Warning,
                           "CommandManager.RegisterCommand: \"{0}\" was defined as an alias for \"{1}\", " +
                           "but has now been replaced by a different command of the same name.",
                           descriptor.Name, Aliases[descriptor.Name]);
                Aliases.Remove(normalizedName);
            }

            if (descriptor.Aliases != null)
            {
                foreach (string alias in descriptor.Aliases)
                {
                    string normalizedAlias = alias.ToLowerInvariant();
                    if (ReservedCommandNames.Contains(normalizedAlias) &&
                        !(descriptor.Name == "Cancel" && alias == "Nvm"))
                    { // special case for cancel/nvm aliases
                        Logger.Log(LogType.Warning,
                                   "CommandManager.RegisterCommand: Alias \"{0}\" for \"{1}\" ignored (reserved name).",
                                   alias, descriptor.Name);
                    }
                    else if (Aliases.ContainsKey(normalizedAlias))
                    {
                        Logger.Log(LogType.Warning,
                                   "CommandManager.RegisterCommand: \"{0}\" was defined as an alias for \"{1}\", " +
                                   "but has been overridden to resolve to \"{2}\" instead.",
                                   alias, Aliases[normalizedAlias], descriptor.Name);
                    }
                    else
                    {
                        Aliases.Add(normalizedAlias, normalizedName);
                    }
                }
            }

            Commands.Add(normalizedName, descriptor);

            RaiseCommandRegisteredEvent(descriptor);
        }
示例#21
0
 public static void RegisterCustomCommand( CommandDescriptor descriptor ) {
     if( descriptor == null ) throw new ArgumentNullException( "descriptor" );
     descriptor.IsCustom = true;
     RegisterCommand( descriptor );
 }