Modularise command handling code somewhat.

This commit is contained in:
UnknownShadow200 2016-06-22 23:07:24 +10:00
parent a928ed94c8
commit 85c4ae4199
3 changed files with 77 additions and 69 deletions

View File

@ -40,7 +40,7 @@ namespace MCGalaxy.Commands
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/botsummon <name>"); Player.Message(p, "%T/botsummon <name>");
Player.Message(p, "%Hummons a bot to your position."); Player.Message(p, "%HSummons a bot to your position.");
} }
} }
} }

View File

@ -53,7 +53,7 @@ namespace MCGalaxy.Commands.World {
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/main %H- Sends you to the main level."); Player.Message(p, "%T/main %H- Sends you to the main level.");
Player.Message(p, "%T/main [level]%H- Sets the main level to that level."); Player.Message(p, "%T/main [level] %H- Sets the main level to that level.");
} }
} }
} }

View File

@ -1190,43 +1190,54 @@ return;
public void HandleCommand(string cmd, string message) { public void HandleCommand(string cmd, string message) {
cmd = cmd.ToLower(); cmd = cmd.ToLower();
try { try {
if (cmd == "") { SendMessage("No command entered."); return; } if (!CheckCommand(cmd)) return;
Command command = GetCommand(ref cmd, ref message);
if (command != null) UseCommand(command, cmd, message);
} catch (Exception e) {
Server.ErrorLog(e); SendMessage("Command failed.");
}
}
bool CheckCommand(string cmd) {
if (cmd == "") { SendMessage("No command entered."); return false; }
if (Server.agreetorulesonentry && !agreed && !(cmd == "agree" || cmd == "rules" || cmd == "disagree")) { if (Server.agreetorulesonentry && !agreed && !(cmd == "agree" || cmd == "rules" || cmd == "disagree")) {
SendMessage("You must read /rules then agree to them with /agree!"); return; SendMessage("You must read /rules then agree to them with /agree!"); return false;
} }
if (jailed) { if (jailed) {
SendMessage("You cannot use any commands while jailed."); return; SendMessage("You cannot use any commands while jailed."); return false;
} }
if (Server.verifyadmins && adminpen && !(cmd == "pass" || cmd == "setpass")) { if (Server.verifyadmins && adminpen && !(cmd == "pass" || cmd == "setpass")) {
SendMessage("&cYou must use &a/pass [Password]&c to verify!"); return; SendMessage("&cYou must use &a/pass [Password]&c to verify!"); return false;
} }
//DO NOT REMOVE THE TWO COMMANDS BELOW, /PONY AND /RAINBOWDASHLIKESCOOLTHINGS. -EricKilla //DO NOT REMOVE THE TWO COMMANDS BELOW, /PONY AND /RAINBOWDASHLIKESCOOLTHINGS. -EricKilla
if (cmd == "pony") { if (cmd == "pony") {
if ( ponycount < 2 ) { if (ponycount < 2) {
GlobalMessage(color + DisplayName + " %Sjust so happens to be a proud brony! Everyone give " + color + name + " %Sa brohoof!"); GlobalMessage(color + DisplayName + " %Sjust so happens to be a proud brony! Everyone give " + color + cmd + " %Sa brohoof!");
ponycount += 1; ponycount++;
} else { } else {
SendMessage("You have used this command 2 times. You cannot use it anymore! Sorry, Brony!"); SendMessage("You have used this command 2 times. You cannot use it anymore! Sorry, Brony!");
} }
return; return false;
} } else if (cmd == "rainbowdashlikescoolthings") {
if (cmd == "rainbowdashlikescoolthings") { if (rdcount < 2) {
if ( rdcount < 2 ) {
GlobalMessage("&1T&2H&3I&4S &5S&6E&7R&8V&9E&aR &bJ&cU&dS&eT &fG&0O&1T &22&30 &4P&CE&7R&DC&EE&9N&1T &5C&6O&7O&8L&9E&aR&b!"); GlobalMessage("&1T&2H&3I&4S &5S&6E&7R&8V&9E&aR &bJ&cU&dS&eT &fG&0O&1T &22&30 &4P&CE&7R&DC&EE&9N&1T &5C&6O&7O&8L&9E&aR&b!");
rdcount += 1; rdcount++;
} else { } else {
SendMessage("You have used this command 2 times. You cannot use it anymore! Sorry, Brony!"); SendMessage("You have used this command 2 times. You cannot use it anymore! Sorry, Brony!");
} }
return; return false;
}
return true;
} }
Command GetCommand(ref string cmd, ref string message) {
string shortcut = Command.all.FindShort(cmd); string shortcut = Command.all.FindShort(cmd);
if (shortcut != "") cmd = shortcut; if (shortcut != "") cmd = shortcut;
byte bindIndex; byte bindIndex;
if (byte.TryParse(cmd, out bindIndex) && bindIndex < 10) { if (byte.TryParse(cmd, out bindIndex) && bindIndex < 10) {
if (messageBind[bindIndex] == null) { SendMessage("No command is bound to: /" + cmd); return; } if (messageBind[bindIndex] == null) { SendMessage("No command is bound to: /" + cmd); return null; }
cmd = cmdBind[bindIndex]; cmd = cmdBind[bindIndex];
message = messageBind[bindIndex] + " " + message; message = messageBind[bindIndex] + " " + message;
message = message.TrimEnd(' '); message = message.TrimEnd(' ');
@ -1244,21 +1255,18 @@ return;
if (OnCommand != null) OnCommand(cmd, this, message); if (OnCommand != null) OnCommand(cmd, this, message);
if (PlayerCommand != null) PlayerCommand(cmd, this, message); if (PlayerCommand != null) PlayerCommand(cmd, this, message);
OnPlayerCommandEvent.Call(cmd, this, message); OnPlayerCommandEvent.Call(cmd, this, message);
if (cancelcommand) { if (cancelcommand) { cancelcommand = false; return null; }
cancelcommand = false; return;
}
Command command = Command.all.Find(cmd); Command command = Command.all.Find(cmd);
if (command != null) { if (command != null) return command;
UseCommand(command, cmd, message); if (Block.Byte(cmd) != Block.Zero) {
} else if (Block.Byte(cmd.ToLower()) != Block.Zero) { message = cmd.ToLower(); cmd = "mode";
HandleCommand("mode", cmd.ToLower()); return Command.all.Find("mode");
} else { } else {
SendMessage("Unknown command \"" + cmd + "\"!"); SendMessage("Unknown command \"" + cmd + "\"!");
return null;
} }
} }
catch ( Exception e ) { Server.ErrorLog(e); SendMessage("Command failed."); }
}
void UseCommand(Command command, string cmd, string message) { void UseCommand(Command command, string cmd, string message) {
if (!group.CanExecute(command)) { command.MessageCannotUse(this); return; } if (!group.CanExecute(command)) { command.MessageCannotUse(this); return; }