Exemplo n.º 1
0
        internal static void RaiseCommandCalledEvent(Command cmd, CommandDescriptor descriptor, Player player)
        {
            var h = CommandCalled;

            if (h != null)
            {
                CommandCalled(null, new CommandCalledEventArgs(cmd, descriptor, player));
            }
        }
Exemplo n.º 2
0
        private static void RaiseCommandRegisteredEvent(CommandDescriptor descriptor)
        {
            var h = CommandRegistered;

            if (h != null)
            {
                h(null, new CommandRegisteredEventArgs(descriptor));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Registers a custom command with fCraft.
        /// CommandRegistrationException may be thrown if the given descriptor does not meet all the requirements.
        /// </summary>
        public static void RegisterCustomCommand([NotNull] CommandDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            descriptor.IsCustom = true;

            RegisterCommand(descriptor);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        private static bool RaiseCommandRegisteringEvent(CommandDescriptor descriptor)
        {
            var h = CommandRegistering;

            if (h == null)
            {
                return(false);
            }

            var e = new CommandRegistringEventArgs(descriptor);

            h(null, e);

            return(e.Cancel);
        }
Exemplo n.º 6
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("Standard commands must have a category set.");
            }
                        #endif

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

            string normalizedName = descriptor.Name.ToLower();

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

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

            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.", descriptor.ToString());
                }
            }

            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.ToLower();

                    if (ReservedCommandNames.Contains(normalizedAlias))
                    {
                        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);
        }