mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Account list file support
Accounts can be stored in a file and used while (re)connecting + Check that the server IP is valid (avoid /connect <player>) + Fix command prompt not exiting while exiting the server + SendChatMessage() -> SendText() method name change
This commit is contained in:
parent
36690b8b34
commit
2907b9c587
7 changed files with 108 additions and 26 deletions
|
|
@ -81,7 +81,7 @@ namespace MinecraftClient
|
|||
protected bool SendText(string text)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8BOT:" + text, false);
|
||||
return handler.SendChatMessage(text);
|
||||
return handler.SendText(text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -8,15 +8,27 @@ namespace MinecraftClient.Commands
|
|||
public class Connect : Command
|
||||
{
|
||||
public override string CMDName { get { return "connect"; } }
|
||||
public override string CMDDesc { get { return "connect <serverip>: connect to the specified server."; } }
|
||||
public override string CMDDesc { get { return "connect <serverip> [account]: connect to the specified server."; } }
|
||||
|
||||
public override string Run(McTcpClient handler, string command)
|
||||
{
|
||||
if (hasArg(command))
|
||||
{
|
||||
Settings.setServerIP(getArgs(command)[0]);
|
||||
Program.Restart();
|
||||
return "";
|
||||
string[] args = getArgs(command);
|
||||
if (args.Length > 1)
|
||||
{
|
||||
if (!Settings.setAccount(args[1]))
|
||||
{
|
||||
return "Unknown account '" + args[1] + "'.";
|
||||
}
|
||||
}
|
||||
|
||||
if (Settings.setServerIP(args[0]))
|
||||
{
|
||||
Program.Restart();
|
||||
return "";
|
||||
}
|
||||
else return "Invalid server IP '" + args[0] + "'.";
|
||||
}
|
||||
else return CMDDesc;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,10 +8,18 @@ namespace MinecraftClient.Commands
|
|||
public class Reco : Command
|
||||
{
|
||||
public override string CMDName { get { return "reco"; } }
|
||||
public override string CMDDesc { get { return "reco: restart and reconnect to the server."; } }
|
||||
public override string CMDDesc { get { return "reco [account]: restart and reconnect to the server."; } }
|
||||
|
||||
public override string Run(McTcpClient handler, string command)
|
||||
{
|
||||
string[] args = getArgs(command);
|
||||
if (args.Length > 0)
|
||||
{
|
||||
if (!Settings.setAccount(args[0]))
|
||||
{
|
||||
return "Unknown account '" + args[0] + "'.";
|
||||
}
|
||||
}
|
||||
Program.Restart();
|
||||
return "";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace MinecraftClient.Commands
|
|||
{
|
||||
if (hasArg(command))
|
||||
{
|
||||
handler.SendChatMessage(getArg(command));
|
||||
handler.SendText(getArg(command));
|
||||
return "";
|
||||
}
|
||||
else return CMDDesc;
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ namespace MinecraftClient
|
|||
|
||||
TcpClient client;
|
||||
IMinecraftCom handler;
|
||||
Thread cmdprompt;
|
||||
|
||||
/// <summary>
|
||||
/// Starts the main chat client
|
||||
|
|
@ -126,10 +127,14 @@ namespace MinecraftClient
|
|||
{
|
||||
foreach (ChatBot bot in scripts_on_hold) { bots.Add(bot); }
|
||||
scripts_on_hold.Clear();
|
||||
|
||||
Console.WriteLine("Server was successfully joined.\nType '"
|
||||
+ (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar)
|
||||
+ "quit' to leave the server.");
|
||||
StartTalk();
|
||||
|
||||
cmdprompt = new Thread(new ThreadStart(CommandPrompt));
|
||||
cmdprompt.Name = "MCC Command prompt";
|
||||
cmdprompt.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -149,7 +154,7 @@ namespace MinecraftClient
|
|||
/// Allows the user to send chat messages, commands, and to leave the server.
|
||||
/// </summary>
|
||||
|
||||
private void StartTalk()
|
||||
private void CommandPrompt()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -183,14 +188,14 @@ namespace MinecraftClient
|
|||
string command = Settings.internalCmdChar == ' ' ? text : text.Substring(1);
|
||||
if (!performInternalCommand(Settings.expandVars(command), ref response_msg) && Settings.internalCmdChar == '/')
|
||||
{
|
||||
SendChatMessage(text);
|
||||
SendText(text);
|
||||
}
|
||||
else if (response_msg.Length > 0)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8MCC: " + response_msg);
|
||||
}
|
||||
}
|
||||
else SendChatMessage(text);
|
||||
else SendText(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -199,7 +204,7 @@ namespace MinecraftClient
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform an internal MCC command (not a server command, use SendChatMessage() instead for that!)
|
||||
/// Perform an internal MCC command (not a server command, use SendText() instead for that!)
|
||||
/// </summary>
|
||||
/// <param name="command">The command</param>
|
||||
/// <param name="interactive_mode">Set to true if command was sent by the user using the command prompt</param>
|
||||
|
|
@ -277,6 +282,10 @@ namespace MinecraftClient
|
|||
|
||||
handler.Disconnect();
|
||||
handler.Dispose();
|
||||
|
||||
if (cmdprompt != null)
|
||||
cmdprompt.Abort();
|
||||
|
||||
Thread.Sleep(1000);
|
||||
|
||||
if (client != null) { client.Close(); }
|
||||
|
|
@ -351,7 +360,7 @@ namespace MinecraftClient
|
|||
/// <param name="text">Text to send to the server</param>
|
||||
/// <returns>True if the text was sent with no error</returns>
|
||||
|
||||
public bool SendChatMessage(string text)
|
||||
public bool SendText(string text)
|
||||
{
|
||||
if (text.Length > 100) //Message is too long?
|
||||
{
|
||||
|
|
|
|||
|
|
@ -84,8 +84,9 @@ namespace MinecraftClient
|
|||
//Remote Control
|
||||
public static bool RemoteCtrl_Enabled = false;
|
||||
|
||||
//Custom app variables
|
||||
//Custom app variables and Minecraft accounts
|
||||
private static Dictionary<string, string> AppVars = new Dictionary<string, string>();
|
||||
private static Dictionary<string, KeyValuePair<string, string>> Accounts = new Dictionary<string, KeyValuePair<string, string>>();
|
||||
|
||||
private enum ParseMode { Default, Main, AppVars, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl };
|
||||
|
||||
|
|
@ -157,7 +158,19 @@ namespace MinecraftClient
|
|||
case "backslash": internalCmdChar = '\\'; break;
|
||||
}
|
||||
break;
|
||||
|
||||
case "accountsfile":
|
||||
if (File.Exists(argValue))
|
||||
{
|
||||
foreach (string account_line in File.ReadAllLines(argValue))
|
||||
{
|
||||
//Each line contains account data: 'Alias,Login,Password'
|
||||
string[] account_data = account_line.Split('#')[0].Trim().Split(',');
|
||||
if (account_data.Length == 3)
|
||||
Accounts[account_data[0].ToLower()]
|
||||
= new KeyValuePair<string, string>(account_data[1], account_data[2]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -283,13 +296,14 @@ namespace MinecraftClient
|
|||
+ "consoletitle=%username% - Minecraft Console Client\r\n"
|
||||
+ "internalcmdchar=slash #use 'none', 'slash' or 'backslash'\r\n"
|
||||
+ "mcversion=auto #use 'auto' or '1.X.X' values\r\n"
|
||||
+ "accountsfile=accounts.txt\r\n"
|
||||
+ "exitonfailure=false\r\n"
|
||||
+ "timestamps=false\r\n"
|
||||
+ "\r\n"
|
||||
+ "[AppVars]\r\n"
|
||||
+ "#yourvar=yourvalue\r\n"
|
||||
+ "#can be used in other fields as %yourvar%\r\n"
|
||||
+ "#%username% and %serverip% are reserved variable names.\r\n"
|
||||
+ "#can be used in some other fields as %yourvar%\r\n"
|
||||
+ "#%username% and %serverip% are reserved variables.\r\n"
|
||||
+ "\r\n"
|
||||
+ "[Proxy]\r\n"
|
||||
+ "enabled=false\r\n"
|
||||
|
|
@ -341,26 +355,51 @@ namespace MinecraftClient
|
|||
public static int str2int(string str) { try { return Convert.ToInt32(str); } catch { return 0; } }
|
||||
public static bool str2bool(string str) { return str == "true" || str == "1"; }
|
||||
|
||||
/// <summary>
|
||||
/// Load login/password using an account alias
|
||||
/// </summary>
|
||||
/// <returns>True if the account was found and loaded</returns>
|
||||
|
||||
public static bool setAccount(string accountAlias)
|
||||
{
|
||||
accountAlias = accountAlias.ToLower();
|
||||
if (Accounts.ContainsKey(accountAlias))
|
||||
{
|
||||
Settings.Login = Accounts[accountAlias].Key;
|
||||
Settings.Password = Accounts[accountAlias].Value;
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a "serverip:port" couple and store the values in ServerIP and ServerPort variables
|
||||
/// </summary>
|
||||
/// <returns>True if the server IP was valid and loaded, false otherwise</returns>
|
||||
|
||||
public static void setServerIP(string serverIP)
|
||||
public static bool setServerIP(string serverIP)
|
||||
{
|
||||
string[] sip = serverIP.Split(':');
|
||||
ServerIP = sip[0];
|
||||
if (sip.Length == 1)
|
||||
{
|
||||
ServerPort = 25565;
|
||||
}
|
||||
else
|
||||
string host = sip[0];
|
||||
short port = 25565;
|
||||
|
||||
if (sip.Length > 1)
|
||||
{
|
||||
try
|
||||
{
|
||||
ServerPort = Convert.ToInt16(sip[1]);
|
||||
port = Convert.ToInt16(sip[1]);
|
||||
}
|
||||
catch (FormatException) { ServerPort = 25565; }
|
||||
catch (FormatException) { return false; }
|
||||
}
|
||||
|
||||
if (host == "localhost" || host.Contains('.'))
|
||||
{
|
||||
ServerIP = host;
|
||||
ServerPort = port;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
14
MinecraftClient/config/sample-accounts.txt
Normal file
14
MinecraftClient/config/sample-accounts.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# Minecraft Console Client
|
||||
# Account list file
|
||||
|
||||
# Put account data as comma separated values
|
||||
# Values are: Alias,Login,Password
|
||||
# It allows a fast account switching
|
||||
# without directly using the credentials
|
||||
|
||||
# Usage examples:
|
||||
# /tell <mybot> reco Player2
|
||||
# /connect <serverip> Player1
|
||||
|
||||
Player1,playerone@email.com,thepassword
|
||||
Player2,TestBot,-
|
||||
Loading…
Add table
Add a link
Reference in a new issue