Add set varname=value MCC command

- Allow to use vars declared in scripts or directly by the user
- Commands my now use %variable% as well
- Moved "help" command from RemoteControl to inner MCC command manager
- The only special command is "wait", which is only available in scripts
Todo: Solve ambiguity between MCC help and Server help commands
Note: Auto accept tpa suggested by MousePak
Note: Manually choosing MC version suggested by ZizzyDizzyMC
This commit is contained in:
ORelio 2014-06-14 18:48:43 +02:00
parent 8b5ce567a6
commit 87d4687394
5 changed files with 96 additions and 76 deletions

View file

@ -84,25 +84,27 @@ namespace MinecraftClient
}
/// <summary>
/// Check if the given command is a valid internal MCC command
/// Perform an internal MCC command (not a server command, use SendText() instead for that!)
/// </summary>
/// <param name="command">The command or command name</param>
/// <returns>TRUE if this is an internal command</returns>
/// <param name="command">The command to process</param>
/// <returns>TRUE if the command was indeed an internal MCC command</returns>
protected bool isInternalCommand(string command)
protected bool performInternalCommand(string command)
{
return handler.isInternalCommand(command);
string temp = "";
return handler.performInternalCommand(command, ref temp);
}
/// <summary>
/// Perform an internal MCC command (not a server command, use SendText() instead for that!)
/// </summary>
/// <param name="command">The command</param>
/// <returns>TRUE if the command was successfully recognized and performed</returns>
/// <param name="command">The command to process</param>
/// <param name="response_msg">May contain a confirmation or error message after processing the command, or "" otherwise.</param>
/// <returns>TRUE if the command was indeed an internal MCC command</returns>
protected bool performInternalCommand(string command)
protected bool performInternalCommand(string command, ref string response_msg)
{
return handler.performInternalCommand(command);
return handler.performInternalCommand(command, ref response_msg);
}
/// <summary>

View file

@ -17,33 +17,11 @@ namespace MinecraftClient.ChatBots
string command = "", sender = "";
if (isPrivateMessage(text, ref command, ref sender) && Settings.Bots_Owners.Contains(sender.ToLower()))
{
string cmd_name = command.Split(' ')[0];
switch (cmd_name.ToLower())
string response = "";
performInternalCommand(command, ref response);
if (response.Length > 0)
{
case "help":
if (command.Length >= 6)
{
string help_cmd_name = command.Substring(5).ToLower();
switch (help_cmd_name)
{
case "exit": SendPrivateMessage(sender, "exit: disconnect from the server."); break;
case "reco": SendPrivateMessage(sender, "reco: restart and reconnct to the server."); break;
case "script": SendPrivateMessage(sender, "script <scriptname>: run a script file."); break;
case "send": SendPrivateMessage(sender, "send <text>: send a chat message or command."); break;
case "connect": SendPrivateMessage(sender, "connect <serverip>: connect to the specified server."); break;
case "help": SendPrivateMessage(sender, "help <cmdname>: show brief help about a command."); break;
default: SendPrivateMessage(sender, "help: unknown command '" + help_cmd_name + "'."); break;
}
}
else SendPrivateMessage(sender, "help <cmdname>. Available commands: exit, reco, script, send, connect.");
break;
default:
if (isInternalCommand(command))
{
performInternalCommand(command);
}
else SendPrivateMessage(sender, "Unknown command '" + cmd_name + "'. Use 'help' for help.");
break;
SendPrivateMessage(sender, response);
}
}
else if (isTeleportRequest(text, ref sender) && Settings.Bots_Owners.Contains(sender.ToLower()))

View file

@ -87,6 +87,7 @@ namespace MinecraftClient.ChatBots
{
if (instruction_line[0] != '#' && instruction_line[0] != '/' && instruction_line[1] != '/')
{
instruction_line = Settings.expandVars(instruction_line);
string instruction_name = instruction_line.Split(' ')[0];
switch (instruction_name.ToLower())
{
@ -100,11 +101,10 @@ namespace MinecraftClient.ChatBots
sleepticks = ticks;
break;
default:
if (isInternalCommand(instruction_line))
if (!performInternalCommand(instruction_line))
{
performInternalCommand(instruction_line);
sleepticks = 0; Update(); //Unknown command : process next line immediately
}
else sleepticks = 0; Update(); //Unknown command : process next line immediately
break;
}
}

View file

@ -175,12 +175,16 @@ namespace MinecraftClient
{
if (text[0] == '/')
{
string response_msg = "";
string command = text.Substring(1);
if (isInternalCommand(command))
if (!performInternalCommand(Settings.expandVars(command), ref response_msg))
{
performInternalCommand(command, true);
SendChatMessage(text);
}
else if (response_msg.Length > 0)
{
ConsoleIO.WriteLineFormatted("§8" + response_msg);
}
else SendChatMessage(text);
}
}
}
@ -189,39 +193,17 @@ namespace MinecraftClient
catch (IOException) { }
}
/// <summary>
/// Check if the given command is a valid internal MCC command
/// </summary>
/// <param name="command">The command or command name</param>
/// <returns>TRUE if this is an internal command</returns>
public bool isInternalCommand(string command)
{
string command_name = command.Split(' ')[0].ToLower();
switch (command_name)
{
case "exit":
case "quit":
case "reco":
case "respawn":
case "send":
case "script":
case "connect":
return true;
default:
return false;
}
}
/// <summary>
/// Perform an internal MCC command (not a server command, use SendChatMessage() 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>
/// <returns>TRUE if the command was successfully recognized and performed</returns>
/// <param name="response_msg">May contain a confirmation or error message after processing the command, or "" otherwise.</param>
/// <returns>TRUE if the command was indeed an internal MCC command</returns>
public bool performInternalCommand(string command, bool interactive_mode = false)
public bool performInternalCommand(string command, ref string response_msg)
{
response_msg = "";
string[] command_args = command.Split(' ');
string command_name = command_args[0].ToLower();
switch (command_name)
@ -237,8 +219,7 @@ namespace MinecraftClient
case "respawn":
handler.SendRespawnPacket();
if (interactive_mode)
ConsoleIO.WriteLine("You have respawned.");
response_msg = "You have respawned.";
break;
case "send":
@ -247,22 +228,65 @@ namespace MinecraftClient
string text = command.Substring(5);
SendChatMessage(text);
}
else response_msg = "send <text>: send a chat message or command.";
break;
case "set":
if (command.Length > 3)
{
string[] temp = command.Substring(3).Split('=');
if (temp.Length > 1)
{
if (!Settings.setVar(temp[0], command.Substring(temp[0].Length + 5)))
{
response_msg = "variable name must be A-Za-z0-9.";
}
}
else response_msg = "set varname=value: set a custom %variable%.";
}
else response_msg = "set varname=value: set a custom %variable%.";
break;
case "script":
if (command.Length > 8)
{
BotLoad(new ChatBots.Script(command.Substring(8)));
break;
}
else response_msg = "script <scriptname>: run a script file.";
break;
case "connect":
if (command_args.Length > 1)
if (command_args.Length > 1)
{
Settings.setServerIP(command_args[1]);
Program.Restart();
}
else response_msg = "connect <serverip>: connect to the specified server.";
break;
case "help":
if (command.Length >= 6)
{
string help_cmd_name = command.Substring(5).ToLower();
switch (help_cmd_name)
{
Settings.setServerIP(command_args[1]);
Program.Restart();
case "quit": response_msg = "quit: disconnect from the server."; break;
case "exit": response_msg = "exit: disconnect from the server."; break;
case "reco": response_msg = "reco: restart and reconnct to the server."; break;
case "respawn": response_msg = "respawn: respawn after death."; break;
case "send": response_msg = "send <text>: send a chat message or command."; break;
case "set": response_msg = "set varname=value: set a custom %variable%."; break;
case "script": response_msg = "script <scriptname>: run a script file."; break;
case "connect": response_msg = "connect <serverip>: connect to the specified server."; break;
case "help": response_msg = "help <cmdname>: show brief help about a command."; break;
default: response_msg = "help: unknown command '" + help_cmd_name + "'."; break;
}
}
else response_msg = "help <cmdname>. Available commands: exit, reco, script, send, connect.";
break;
default:
response_msg = "Unknown command '" + command_name + "'. Use 'help' for help.";
return false;
}
return true;

View file

@ -233,9 +233,7 @@ namespace MinecraftClient
break;
case ParseMode.AppVars:
string varName = new string(argName.TakeWhile(char.IsLetterOrDigit).ToArray()).ToLower();
if (varName.Length > 0)
AppVars[varName] = argValue;
setVar(argName, argValue);
break;
}
}
@ -353,6 +351,24 @@ namespace MinecraftClient
}
}
/// <summary>
/// Set a custom %variable% which will be available through expandVars()
/// </summary>
/// <param name="varName">Name of the variable</param>
/// <param name="varData">Value of the variable</param>
/// <returns>True if the parameters were valid</returns>
public static bool setVar(string varName, string varData)
{
varName = new string(varName.TakeWhile(char.IsLetterOrDigit).ToArray()).ToLower();
if (varName.Length > 0)
{
AppVars[varName] = varData;
return true;
}
else return false;
}
/// <summary>
/// Replace %variables% with their value
/// </summary>