Handle all internal MCC commands in one place

- MCC internal commands for command prompt, remote control and scripts
are handled in one place, thus it's no more needed to add them in 3
different places.
- "exit" command in scripts is not equivalent to "/quit"
- removed "disconnect" command in scripts /!\
- bots can now easily perform internal MCC commands.
This commit is contained in:
ORelio 2014-06-14 13:20:15 +02:00
parent 283074bb63
commit 9456e82923
4 changed files with 144 additions and 86 deletions

View file

@ -75,11 +75,34 @@ namespace MinecraftClient
/// Send text to the server. Can be anything such as chat messages or commands /// Send text to the server. Can be anything such as chat messages or commands
/// </summary> /// </summary>
/// <param name="text">Text to send to the server</param> /// <param name="text">Text to send to the server</param>
/// <returns>True if the text was sent with no error</returns>
protected void SendText(string text) protected bool SendText(string text)
{ {
ConsoleIO.WriteLineFormatted("§8BOT:" + text, false); ConsoleIO.WriteLineFormatted("§8BOT:" + text, false);
handler.SendChatMessage(text); return handler.SendChatMessage(text);
}
/// <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>
protected bool isInternalCommand(string command)
{
return handler.isInternalCommand(command);
}
/// <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>
protected bool performInternalCommand(string command)
{
return handler.performInternalCommand(command);
} }
/// <summary> /// <summary>

View file

@ -20,27 +20,6 @@ namespace MinecraftClient.ChatBots
string cmd_name = command.Split(' ')[0]; string cmd_name = command.Split(' ')[0];
switch (cmd_name.ToLower()) switch (cmd_name.ToLower())
{ {
case "exit":
DisconnectAndExit();
break;
case "reco":
ReconnectToTheServer();
break;
case "script":
if (command.Length >= 8)
RunScript(command.Substring(7), sender);
break;
case "send":
if (command.Length >= 6)
SendText(command.Substring(5));
break;
case "connect":
if (command.Length >= 9)
{
Settings.setServerIP(command.Substring(8));
ReconnectToTheServer();
}
break;
case "help": case "help":
if (command.Length >= 6) if (command.Length >= 6)
{ {
@ -59,7 +38,11 @@ namespace MinecraftClient.ChatBots
else SendPrivateMessage(sender, "help <cmdname>. Available commands: exit, reco, script, send, connect."); else SendPrivateMessage(sender, "help <cmdname>. Available commands: exit, reco, script, send, connect.");
break; break;
default: default:
SendPrivateMessage(sender, "Unknown command '" + cmd_name + "'. Use 'help' for help."); if (isInternalCommand(command))
{
performInternalCommand(command);
}
else SendPrivateMessage(sender, "Unknown command '" + cmd_name + "'. Use 'help' for help.");
break; break;
} }
} }

View file

@ -90,9 +90,6 @@ namespace MinecraftClient.ChatBots
string instruction_name = instruction_line.Split(' ')[0]; string instruction_name = instruction_line.Split(' ')[0];
switch (instruction_name.ToLower()) switch (instruction_name.ToLower())
{ {
case "send":
SendText(instruction_line.Substring(5, instruction_line.Length - 5));
break;
case "wait": case "wait":
int ticks = 10; int ticks = 10;
try try
@ -102,21 +99,12 @@ namespace MinecraftClient.ChatBots
catch { } catch { }
sleepticks = ticks; sleepticks = ticks;
break; break;
case "disconnect":
DisconnectAndExit();
break;
case "exit": //Exit bot & stay connected to the server
UnloadBot();
break;
case "connect":
if (instruction_line.Length >= 9)
{
Settings.setServerIP(instruction_line.Substring(8));
ReconnectToTheServer();
}
break;
default: default:
sleepticks = 0; Update(); //Unknown command : process next line immediately if (isInternalCommand(instruction_line))
{
performInternalCommand(instruction_line);
}
else sleepticks = 0; Update(); //Unknown command : process next line immediately
break; break;
} }
} }

View file

@ -171,60 +171,103 @@ namespace MinecraftClient
else else
{ {
text = text.Trim(); text = text.Trim();
if (text.ToLower() == "/quit" || text.ToLower() == "/reco") if (text.Length > 0)
{ {
break; if (text[0] == '/')
}
else if (text.ToLower() == "/respawn")
{
handler.SendRespawnPacket();
ConsoleIO.WriteLine("You have respawned.");
}
else if (text.ToLower().StartsWith("/script "))
{
BotLoad(new ChatBots.Script(text.Substring(8)));
}
else if (text.ToLower().StartsWith("/connect "))
{
Settings.setServerIP(text.Substring(9));
Program.Restart();
}
else if (text != "")
{
//Message is too long
if (text.Length > 100)
{ {
if (text[0] == '/') string command = text.Substring(1);
if (isInternalCommand(command))
{ {
//Send the first 100 chars of the command performInternalCommand(command, true);
text = text.Substring(0, 100);
handler.SendChatMessage(text);
}
else
{
//Send the message splitted into several messages
while (text.Length > 100)
{
handler.SendChatMessage(text.Substring(0, 100));
text = text.Substring(100, text.Length - 100);
}
handler.SendChatMessage(text);
} }
else SendChatMessage(text);
} }
else handler.SendChatMessage(text);
} }
} }
} }
switch (text.ToLower())
{
case "/quit": Program.Exit(); break;
case "/reco": Program.Restart(); break;
}
} }
catch (IOException) { } 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>
public bool performInternalCommand(string command, bool interactive_mode = false)
{
string[] command_args = command.Split(' ');
string command_name = command_args[0].ToLower();
switch (command_name)
{
case "exit":
case "quit":
Program.Exit();
break;
case "reco":
Program.Restart();
break;
case "respawn":
handler.SendRespawnPacket();
if (interactive_mode)
ConsoleIO.WriteLine("You have respawned.");
break;
case "send":
if (command.Length > 5)
{
string text = command.Substring(5);
SendChatMessage(text);
}
break;
case "script":
if (command.Length > 8)
BotLoad(new ChatBots.Script(command.Substring(8)));
break;
case "connect":
if (command_args.Length > 1)
{
Settings.setServerIP(command_args[1]);
Program.Restart();
}
break;
default:
return false;
}
return true;
}
/// <summary> /// <summary>
/// Disconnect the client from the server /// Disconnect the client from the server
/// </summary> /// </summary>
@ -306,12 +349,33 @@ namespace MinecraftClient
} }
/// <summary> /// <summary>
/// Send a chat message to the server /// Send a chat message or command to the server
/// </summary> /// </summary>
/// <param name="text">Text to send to the server</param>
/// <returns>True if the text was sent with no error</returns>
public void SendChatMessage(string message) public bool SendChatMessage(string text)
{ {
handler.SendChatMessage(message); if (text.Length > 100) //Message is too long?
{
if (text[0] == '/')
{
//Send the first 100 chars of the command
text = text.Substring(0, 100);
return handler.SendChatMessage(text);
}
else
{
//Send the message splitted into several messages
while (text.Length > 100)
{
handler.SendChatMessage(text.Substring(0, 100));
text = text.Substring(100, text.Length - 100);
}
return handler.SendChatMessage(text);
}
}
else return handler.SendChatMessage(text);
} }
} }
} }