/// <summary>
 /// Shuts down an exists.
 /// </summary>
 /// <param name="sender">Sending entity</param>
 /// <param name="args">Arguments sent with command</param>
 public static void ExitNoSave(ISender sender, ArgumentList args)
 {
     Tools.NotifyAllOps("Exiting without saving...");
     Netplay.disconnect = true;
 }
예제 #2
0
        public void ParseAndProcess(ISender sender, string line)
        {
            var ctx = new HookContext
            {
                Sender = sender,
#if Full_API
                Player = sender as Player
#endif
            };

#if Full_API
            ctx.Connection = ctx.Player != null ? ctx.Player.Connection.Socket : null;
#endif

            var hargs = new HookArgs.Command();

            try
            {
                CommandInfo info;

                var firstSpace = line.IndexOf(' ');

                if (firstSpace < 0)
                {
                    firstSpace = line.Length;
                }

                var prefix = line.Substring(0, firstSpace);

                hargs.Prefix = prefix;

                if (FindStringCommand(prefix, out info))
                {
                    hargs.ArgumentString = (firstSpace < line.Length - 1 ? line.Substring(firstSpace + 1, line.Length - firstSpace - 1) : "").Trim();

                    HookPoints.Command.Invoke(ref ctx, ref hargs);

                    if (ctx.CheckForKick() || ctx.Result == HookResult.IGNORE)
                    {
                        return;
                    }

                    if (ctx.Result != HookResult.CONTINUE && !CheckAccessLevel(info, sender))
                    {
                        sender.SendMessage("Permissions error", 255, 238, 130, 238);
                        return;
                    }

                    try
                    {
                        info.Run(sender, hargs.ArgumentString);
                    }
                    catch (NLua.Exceptions.LuaScriptException e)
                    {
                        if (e.IsNetException)
                        {
                            var ex = e.GetBaseException();
                            if (ex != null)
                            {
                                if (ex is CommandError)
                                {
                                    sender.SendMessage(prefix + ": " + ex.Message);
                                    info.ShowHelp(sender);
                                }
                            }
                        }
                    }
                    catch (ExitException e)
                    {
                        throw e;
                    }
                    catch (CommandError e)
                    {
                        sender.SendMessage(prefix + ": " + e.Message);
                        info.ShowHelp(sender);
                    }
                    return;
                }

                var args    = new ArgumentList();
                var command = Tokenize(line, args);

                if (command != null)
                {
                    if (FindTokenCommand(command, out info))
                    {
                        hargs.Arguments      = args;
                        hargs.ArgumentString = args.ToString();

                        foreach (BasePlugin plg in PluginManager._plugins.Values)
                        {
                            if (plg.commands.ContainsKey(command))
                            {
                                args.Plugin = plg;
                            }
                        }

                        HookPoints.Command.Invoke(ref ctx, ref hargs);

                        if (ctx.CheckForKick() || ctx.Result == HookResult.IGNORE)
                        {
                            return;
                        }

                        if (ctx.Result != HookResult.CONTINUE && !CheckAccessLevel(info, sender))
                        {
                            sender.SendMessage("Permissions error", 255, 238, 130, 238);
                            return;
                        }

                        try
                        {
                            info.Run(sender, hargs.Arguments);
                        }
                        catch (NLua.Exceptions.LuaScriptException e)
                        {
                            if (e.IsNetException)
                            {
                                var ex = e.GetBaseException();
                                if (ex != null)
                                {
                                    if (ex is CommandError)
                                    {
                                        sender.SendMessage(command + ": " + ex.Message);
                                        info.ShowHelp(sender);
                                    }
                                }
                            }
                        }
                        catch (ExitException e)
                        {
                            throw e;
                        }
                        catch (CommandError e)
                        {
                            sender.SendMessage(command + ": " + e.Message);
                            info.ShowHelp(sender);
                        }
                        return;
                    }
                    else
                    {
                        sender.SendMessage(String.Format("No such command '{0}'.", command));
                    }
                }
            }
            catch (ExitException e)
            {
                throw e;
            }
            catch (TokenizerException e)
            {
                sender.SendMessage(e.Message);
            }
        }
        /// <summary>
        /// Shows the help.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="args">Arguments.</param>
        public static void ShowHelp(ISender sender, ArgumentList args)
        {
            var commands = sender.GetAvailableCommands();

            if (commands != null && commands.Count > 0)
            {
                int page = 0;
                if (!args.TryGetInt(0, out page))
                {
                    if (args.Count > 0)
                    {
                        var command = args.GetString(0);
                        if (commands.ContainsKey(command))
                        {
                            sender.SendMessage(commands[command].description);
                            commands[command].ShowHelp(sender, true);
                            return;
                        }
                        else
                        {
                            throw new CommandError("No such command: " + command);
                        }
                    }
                }
                else
                {
                    page--;
                }

                //				const Int32 MaxLines = 5;
                var maxLines   = sender is Player ? 5 : 15;
                var lineOffset = page * maxLines;
                var maxPages   = (int)Math.Ceiling(commands.Count / (double)maxLines);

                if (page >= 0 && page < maxPages)
                {
                    var cmds   = new List <CommandInfo>();
                    var sorted = commands
                                 .OrderBy(x => x.Key.ToLower())
                                 .Select(x => x.Value)
                                 .ToArray();
                    for (var i = lineOffset; i < lineOffset + maxLines; i++)
                    {
                        if (i < sorted.Length)
                        {
                            cmds.Add(sorted[i]);
                        }
                    }

                    var prefixMax = cmds
                                    .Select(x => x.Prefix.Length)
                                    .OrderByDescending(x => x)
                                    .First();
                    foreach (var cmd in cmds)
                    {
                        cmd.ShowDescription(sender, prefixMax);
                    }

                    sender.SendMessage(String.Format("[Page {0} / {1}]", page + 1, maxPages));
                }
                else
                {
                    sender.SendMessage("Usage:");
                    sender.SendMessage("    help <command> - Get help for a command.");
                    sender.SendMessage("    help <page> - View a list of commands. Valid page numbers are 1 to " + maxPages + ".");
                    sender.SendMessage("Examples:");
                    sender.SendMessage("    help oplogin");
                    sender.SendMessage("    help 1");
                }
            }
            else
            {
                sender.SendMessage("You have no available commands.");
            }
        }
 /// <summary>
 /// Saves to world
 /// </summary>
 /// <param name="sender">Sending entity</param>
 /// <param name="args">Arguments sent with command</param>
 public static void Save(ISender sender, ArgumentList args)
 {
     Terraria.IO.WorldFile.saveWorld(false);
 }
 /// <summary>
 /// Displays the current port
 /// </summary>
 /// <param name="sender">Sending entity</param>
 /// <param name="args">Arguments sent with command</param>
 public static void Port(ISender sender, ArgumentList args)
 {
     sender.Message("Port: " + Netplay.ListenPort);
 }
 /// <summary>
 /// Displays the player limit
 /// </summary>
 /// <param name="sender">Sending entity</param>
 /// <param name="args">Arguments sent with command</param>
 public static void MaxPlayers(ISender sender, ArgumentList args)
 {
     sender.Message("Player limit: " + Main.maxNetPlayers);
 }
 /// <summary>
 /// Displays the current Terraria version
 /// </summary>
 /// <param name="sender">Sending entity</param>
 /// <param name="args">Arguments sent with command</param>
 public static void Version(ISender sender, ArgumentList args)
 {
     sender.Message("Terraria Server " + Main.versionNumber);
     sender.Message("TDSM API Version " + Globals.Build + Globals.PhaseToSuffix(Globals.BuildPhase));
 }