Ejemplo n.º 1
0
        /// <summary>
        /// Pause the program, usually when an error or a kick occured, letting the user press Enter to quit OR type /reconnect
        /// </summary>

        public static void OfflineCommandPrompt()
        {
            if (!Settings.exitOnFailure && offlinePrompt == null)
            {
                offlinePrompt = new Thread(new ThreadStart(delegate
                {
                    string command = " ";
                    ConsoleIO.WriteLineFormatted("Not connected to any server. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help.");
                    ConsoleIO.WriteLineFormatted("Or press Enter to exit Minecraft Console Client.");
                    while (command.Length > 0)
                    {
                        if (!ConsoleIO.basicIO)
                        {
                            ConsoleIO.Write('>');
                        }
                        command = Console.ReadLine().Trim();
                        if (command.Length > 0)
                        {
                            if (Settings.internalCmdChar != ' ' && command[0] == Settings.internalCmdChar)
                            {
                                string message = "";
                                command        = command.Substring(1);
                                if (command.StartsWith("reco"))
                                {
                                    message = new Commands.Reco().Run(null, Settings.expandVars(command));
                                }
                                else if (command.StartsWith("connect"))
                                {
                                    message = new Commands.Connect().Run(null, Settings.expandVars(command));
                                }
                                else if (command.StartsWith("exit") || command.StartsWith("quit"))
                                {
                                    message = new Commands.Exit().Run(null, Settings.expandVars(command));
                                }
                                else if (command.StartsWith("help"))
                                {
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().CMDDesc);
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().CMDDesc);
                                }
                                else
                                {
                                    ConsoleIO.WriteLineFormatted("§8Unknown command '" + command.Split(' ')[0] + "'.");
                                }
                                if (message != "")
                                {
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + message);
                                }
                            }
                            else
                            {
                                ConsoleIO.WriteLineFormatted("§8Please type a command or press Enter to exit.");
                            }
                        }
                    }
                }));
                offlinePrompt.Start();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handle fatal errors such as ping failure, login failure, server disconnection, and so on.
        /// Allows AutoRelog to perform on fatal errors, prompt for server version, and offline commands.
        /// </summary>
        /// <param name="errorMessage">Error message to display and optionally pass to AutoRelog bot</param>
        /// <param name="versionError">Specify if the error is related to an incompatible or unkown server version</param>
        /// <param name="disconnectReason">If set, the error message will be processed by the AutoRelog bot</param>
        public static void HandleFailure(string errorMessage = null, bool versionError = false, ChatBots.AutoRelog.DisconnectReason?disconnectReason = null)
        {
            if (!String.IsNullOrEmpty(errorMessage))
            {
                ConsoleIO.Reset();
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(true);
                }
                Console.WriteLine(errorMessage);

                if (disconnectReason.HasValue)
                {
                    if (ChatBots.AutoRelog.OnDisconnectStatic(disconnectReason.Value, errorMessage))
                    {
                        return; //AutoRelog is triggering a restart of the client
                    }
                }
            }

            if (Settings.interactiveMode)
            {
                if (versionError)
                {
                    Console.Write("Server version : ");
                    Settings.ServerVersion = Console.ReadLine();
                    if (Settings.ServerVersion != "")
                    {
                        useMcVersionOnce = true;
                        Restart();
                        return;
                    }
                }

                if (offlinePrompt == null)
                {
                    offlinePrompt = new Thread(new ThreadStart(delegate
                    {
                        string command = " ";
                        ConsoleIO.WriteLineFormatted("Not connected to any server. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help.");
                        ConsoleIO.WriteLineFormatted("Or press Enter to exit Minecraft Console Client.");
                        while (command.Length > 0)
                        {
                            if (!ConsoleIO.basicIO)
                            {
                                ConsoleIO.Write('>');
                            }
                            command = Console.ReadLine().Trim();
                            if (command.Length > 0)
                            {
                                string message = "";

                                if (Settings.internalCmdChar != ' ' &&
                                    command[0] == Settings.internalCmdChar)
                                {
                                    command = command.Substring(1);
                                }

                                if (command.StartsWith("reco"))
                                {
                                    message = new Commands.Reco().Run(null, Settings.ExpandVars(command));
                                }
                                else if (command.StartsWith("connect"))
                                {
                                    message = new Commands.Connect().Run(null, Settings.ExpandVars(command));
                                }
                                else if (command.StartsWith("exit") || command.StartsWith("quit"))
                                {
                                    message = new Commands.Exit().Run(null, Settings.ExpandVars(command));
                                }
                                else if (command.StartsWith("help"))
                                {
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().CMDDesc);
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().CMDDesc);
                                }
                                else
                                {
                                    ConsoleIO.WriteLineFormatted("§8Unknown command '" + command.Split(' ')[0] + "'.");
                                }

                                if (message != "")
                                {
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + message);
                                }
                            }
                        }
                    }));
                    offlinePrompt.Start();
                }
            }
            else
            {
                Exit();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Write a Minecraft-Like formatted string to the standard output, using §c color codes
        /// See minecraft.gamepedia.com/Classic_server_protocol#Color_Codes for more info
        /// </summary>
        /// <param name="str">String to write</param>
        /// <param name="acceptnewlines">If false, space are printed instead of newlines</param>
        /// <param name="displayTimestamp">
        /// If false, no timestamp is prepended.
        /// If true, "hh-mm-ss" timestamp will be prepended.
        /// If unspecified, value is retrieved from EnableTimestamps.
        /// </param>
        public static void WriteLineFormatted(string str, bool acceptnewlines = true, bool?displayTimestamp = null)
        {
            if (!String.IsNullOrEmpty(str))
            {
                if (!acceptnewlines)
                {
                    str = str.Replace('\n', ' ');
                }
                if (displayTimestamp == null)
                {
                    displayTimestamp = EnableTimestamps;
                }
                if (displayTimestamp.Value)
                {
                    int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
                    ConsoleIO.Write(String.Format("{0}:{1}:{2} ", hour.ToString("00"), minute.ToString("00"), second.ToString("00")));
                }
                if (BasicIO)
                {
                    if (BasicIO_NoColor)
                    {
                        str = ChatBot.GetVerbatim(str);
                    }
                    Console.WriteLine(str);
                    return;
                }
                string[] parts = str.Split(new char[] { '§' });
                if (parts[0].Length > 0)
                {
                    ConsoleIO.Write(parts[0]);
                }
                for (int i = 1; i < parts.Length; i++)
                {
                    if (parts[i].Length > 0)
                    {
                        switch (parts[i][0])
                        {
                        case '0': Console.ForegroundColor = ConsoleColor.Gray; break;     //Should be Black but Black is non-readable on a black background

                        case '1': Console.ForegroundColor = ConsoleColor.DarkBlue; break;

                        case '2': Console.ForegroundColor = ConsoleColor.DarkGreen; break;

                        case '3': Console.ForegroundColor = ConsoleColor.DarkCyan; break;

                        case '4': Console.ForegroundColor = ConsoleColor.DarkRed; break;

                        case '5': Console.ForegroundColor = ConsoleColor.DarkMagenta; break;

                        case '6': Console.ForegroundColor = ConsoleColor.DarkYellow; break;

                        case '7': Console.ForegroundColor = ConsoleColor.Gray; break;

                        case '8': Console.ForegroundColor = ConsoleColor.DarkGray; break;

                        case '9': Console.ForegroundColor = ConsoleColor.Blue; break;

                        case 'a': Console.ForegroundColor = ConsoleColor.Green; break;

                        case 'b': Console.ForegroundColor = ConsoleColor.Cyan; break;

                        case 'c': Console.ForegroundColor = ConsoleColor.Red; break;

                        case 'd': Console.ForegroundColor = ConsoleColor.Magenta; break;

                        case 'e': Console.ForegroundColor = ConsoleColor.Yellow; break;

                        case 'f': Console.ForegroundColor = ConsoleColor.White; break;

                        case 'r': Console.ForegroundColor = ConsoleColor.Gray; break;
                        }

                        if (parts[i].Length > 1)
                        {
                            ConsoleIO.Write(parts[i].Substring(1, parts[i].Length - 1));
                        }
                    }
                }
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            ConsoleIO.Write('\n');
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handle fatal errors such as ping failure, login failure, server disconnection, and so on.
        /// Allows AutoRelog to perform on fatal errors, prompt for server version, and offline commands.
        /// </summary>
        /// <param name="errorMessage">Error message to display and optionally pass to AutoRelog bot</param>
        /// <param name="versionError">Specify if the error is related to an incompatible or unkown server version</param>
        /// <param name="disconnectReason">If set, the error message will be processed by the AutoRelog bot</param>
        public static void HandleFailure(string errorMessage = null, bool versionError = false, ChatBots.AutoRelog.DisconnectReason?disconnectReason = null)
        {
            if (!String.IsNullOrEmpty(errorMessage))
            {
                ConsoleIO.Reset();
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(true);
                }
                Console.WriteLine(errorMessage);

                if (disconnectReason.HasValue)
                {
                    if (ChatBots.AutoRelog.OnDisconnectStatic(disconnectReason.Value, errorMessage))
                    {
                        return; //AutoRelog is triggering a restart of the client
                    }
                }
            }

            if (Settings.interactiveMode)
            {
                if (versionError)
                {
                    Translations.Write("mcc.server_version");
                    Settings.ServerVersion = Console.ReadLine();
                    if (Settings.ServerVersion != "")
                    {
                        useMcVersionOnce = true;
                        Restart();
                        return;
                    }
                }

                if (offlinePrompt == null)
                {
                    offlinePrompt = new Thread(new ThreadStart(delegate
                    {
                        string command = " ";
                        ConsoleIO.WriteLineFormatted(Translations.Get("mcc.disconnected", (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar)));
                        Translations.WriteLineFormatted("mcc.press_exit");
                        while (command.Length > 0)
                        {
                            if (!ConsoleIO.BasicIO)
                            {
                                ConsoleIO.Write('>');
                            }
                            command = Console.ReadLine().Trim();
                            if (command.Length > 0)
                            {
                                string message = "";

                                if (Settings.internalCmdChar != ' ' &&
                                    command[0] == Settings.internalCmdChar)
                                {
                                    command = command.Substring(1);
                                }

                                if (command.StartsWith("reco"))
                                {
                                    message = new Commands.Reco().Run(null, Settings.ExpandVars(command), null);
                                }
                                else if (command.StartsWith("connect"))
                                {
                                    message = new Commands.Connect().Run(null, Settings.ExpandVars(command), null);
                                }
                                else if (command.StartsWith("exit") || command.StartsWith("quit"))
                                {
                                    message = new Commands.Exit().Run(null, Settings.ExpandVars(command), null);
                                }
                                else if (command.StartsWith("help"))
                                {
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().GetCmdDescTranslated());
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().GetCmdDescTranslated());
                                }
                                else
                                {
                                    ConsoleIO.WriteLineFormatted(Translations.Get("icmd.unknown", command.Split(' ')[0]));
                                }

                                if (message != "")
                                {
                                    ConsoleIO.WriteLineFormatted("§8MCC: " + message);
                                }
                            }
                        }
                    }));
                    offlinePrompt.Start();
                }
            }
            else
            {
                // Not in interactive mode, just exit and let the calling script handle the failure
                if (disconnectReason.HasValue)
                {
                    // Return distinct exit codes for known failures.
                    if (disconnectReason.Value == ChatBot.DisconnectReason.UserLogout)
                    {
                        Exit(1);
                    }
                    if (disconnectReason.Value == ChatBot.DisconnectReason.InGameKick)
                    {
                        Exit(2);
                    }
                    if (disconnectReason.Value == ChatBot.DisconnectReason.ConnectionLost)
                    {
                        Exit(3);
                    }
                    if (disconnectReason.Value == ChatBot.DisconnectReason.LoginRejected)
                    {
                        Exit(4);
                    }
                }
                Exit();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Handle fatal errors such as ping failure, login failure, server disconnection, and so on.
        /// Allows AutoRelog to perform on fatal errors, prompt for server version, and offline commands.
        /// </summary>
        /// <param name="errorMessage">Error message to display and optionally pass to AutoRelog bot</param>
        /// <param name="versionError">Specify if the error is related to an incompatible or unkown server version</param>
        /// <param name="disconnectReason">If set, the error message will be processed by the AutoRelog bot</param>
        public static void HandleFailure(string errorMessage = null, bool versionError = false, ChatBots.AutoRelog.DisconnectReason?disconnectReason = null)
        {
            if (!String.IsNullOrEmpty(errorMessage))
            {
                ConsoleIO.Reset();
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(true);
                }
                Console.WriteLine(errorMessage);

                if (disconnectReason.HasValue)
                {
                    if (ChatBots.AutoRelog.OnDisconnectStatic(disconnectReason.Value, errorMessage))
                    {
                        return; //AutoRelog is triggering a restart of the client
                    }
                }
            }

            if (Settings.interactiveMode)
            {
                if (versionError)
                {
                    Console.Write("Versão do servidor : ");
                    Settings.ServerVersion = Console.ReadLine();
                    if (Settings.ServerVersion != "")
                    {
                        useMcVersionOnce = true;
                        Restart();
                        return;
                    }
                }

                if (doReconnect)
                {
                    McTcpClient.ReconnectionAttemptsLeft = 999;
                    Program.Restart(30);
                    vars.loggedIn = false;
                }
                else
                {
                    if (offlinePrompt == null)
                    {
                        offlinePrompt = new Thread(new ThreadStart(delegate
                        {
                            string command = " ";
                            ConsoleIO.WriteLineFormatted("O HtBot está desconectado. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' para ajuda.");
                            ConsoleIO.WriteLineFormatted("Ou aperte Enter para fechar.");
                            while (command.Length > 0)
                            {
                                if (!ConsoleIO.BasicIO)
                                {
                                    ConsoleIO.Write('>');
                                }
                                command = Console.ReadLine().Trim();
                                if (command.Length > 0)
                                {
                                    string message = "";

                                    if (Settings.internalCmdChar != ' ' &&
                                        command[0] == Settings.internalCmdChar)
                                    {
                                        command = command.Substring(1);
                                    }

                                    if (command.StartsWith("reco"))
                                    {
                                        message = new Commands.Reco().Run(null, Settings.ExpandVars(command));
                                    }
                                    else if (command.StartsWith("connect"))
                                    {
                                        message = new Commands.Connect().Run(null, Settings.ExpandVars(command));
                                    }
                                    else if (command.StartsWith("exit") || command.StartsWith("quit"))
                                    {
                                        message = new Commands.Exit().Run(null, Settings.ExpandVars(command));
                                    }
                                    else if (command.StartsWith("help"))
                                    {
                                        ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().CMDDesc);
                                        ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().CMDDesc);
                                    }
                                    else
                                    {
                                        ConsoleIO.WriteLineFormatted("§8Comando desconhecido -> '" + command.Split(' ')[0] + "'.");
                                    }

                                    if (message != "")
                                    {
                                        ConsoleIO.WriteLineFormatted("§8MCC: " + message);
                                    }
                                }
                            }
                        }));
                        offlinePrompt.Start();
                    }
                }
            }
            else
            {
                McTcpClient.ReconnectionAttemptsLeft = 999;
                Program.Restart(2);
                vars.loggedIn = false;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Write a Minecraft-Formatted string to the standard output, using §c color codes
        /// </summary>
        /// <param name="str">String to write</param>
        /// <param name="acceptnewlines">If false, space are printed instead of newlines</param>

        public static void WriteLineFormatted(string str, bool acceptnewlines = true)
        {
            if (basicIO)
            {
                Console.WriteLine(str); return;
            }
            if (!String.IsNullOrEmpty(str))
            {
                if (Settings.chatTimeStamps)
                {
                    int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
                    ConsoleIO.Write(hour.ToString("00") + ':' + minute.ToString("00") + ':' + second.ToString("00") + ' ');
                }
                if (!acceptnewlines)
                {
                    str = str.Replace('\n', ' ');
                }
                if (ConsoleIO.basicIO)
                {
                    ConsoleIO.WriteLine(str); return;
                }
                string[] subs = str.Split(new char[] { '§' });
                if (subs[0].Length > 0)
                {
                    ConsoleIO.Write(subs[0]);
                }
                for (int i = 1; i < subs.Length; i++)
                {
                    if (subs[i].Length > 0)
                    {
                        switch (subs[i][0])
                        {
                        case '0': Console.ForegroundColor = ConsoleColor.Gray; break;     //Should be Black but Black is non-readable on a black background

                        case '1': Console.ForegroundColor = ConsoleColor.DarkBlue; break;

                        case '2': Console.ForegroundColor = ConsoleColor.DarkGreen; break;

                        case '3': Console.ForegroundColor = ConsoleColor.DarkCyan; break;

                        case '4': Console.ForegroundColor = ConsoleColor.DarkRed; break;

                        case '5': Console.ForegroundColor = ConsoleColor.DarkMagenta; break;

                        case '6': Console.ForegroundColor = ConsoleColor.DarkYellow; break;

                        case '7': Console.ForegroundColor = ConsoleColor.Gray; break;

                        case '8': Console.ForegroundColor = ConsoleColor.DarkGray; break;

                        case '9': Console.ForegroundColor = ConsoleColor.Blue; break;

                        case 'a': Console.ForegroundColor = ConsoleColor.Green; break;

                        case 'b': Console.ForegroundColor = ConsoleColor.Cyan; break;

                        case 'c': Console.ForegroundColor = ConsoleColor.Red; break;

                        case 'd': Console.ForegroundColor = ConsoleColor.Magenta; break;

                        case 'e': Console.ForegroundColor = ConsoleColor.Yellow; break;

                        case 'f': Console.ForegroundColor = ConsoleColor.White; break;

                        case 'r': Console.ForegroundColor = ConsoleColor.White; break;
                        }

                        if (subs[i].Length > 1)
                        {
                            ConsoleIO.Write(subs[i].Substring(1, subs[i].Length - 1));
                        }
                    }
                }
                ConsoleIO.Write('\n');
            }
            Console.ForegroundColor = ConsoleColor.Gray;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Translate the key and write the result to the standard output, without newline character
 /// </summary>
 /// <param name="key">Translation key</param>
 public static void Write(string key)
 {
     ConsoleIO.Write(Get(key));
 }