Initial work on multiple commands in a message block. (Thanks whiteraven1000)

This commit is contained in:
UnknownShadow200 2016-06-21 19:21:21 +10:00
parent 8fb745c434
commit 7174fc253e
2 changed files with 59 additions and 21 deletions

View File

@ -16,6 +16,7 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Data;
using MCGalaxy.SQL;
@ -28,7 +29,8 @@ namespace MCGalaxy.BlockBehaviour {
p.RevertBlock(x, y, z);
try {
//safe against SQL injections because no user input is given here
DataTable Portals = Database.fillData("SELECT * FROM `Portals" + p.level.name + "` WHERE EntryX=" + (int)x + " AND EntryY=" + (int)y + " AND EntryZ=" + (int)z);
DataTable Portals = Database.fillData("SELECT * FROM `Portals" + p.level.name +
"` WHERE EntryX=" + x + " AND EntryY=" + y + " AND EntryZ=" + z);
int last = Portals.Rows.Count - 1;
if (last == -1) { Portals.Dispose(); return true; }
byte rotX = p.rot[0], rotY = p.rot[1];
@ -64,18 +66,20 @@ namespace MCGalaxy.BlockBehaviour {
p.RevertBlock(x, y, z);
try {
//safe against SQL injections because no user input is given here
DataTable Messages = Database.fillData("SELECT * FROM `Messages" + p.level.name + "` WHERE X=" + (int)x + " AND Y=" + (int)y + " AND Z=" + (int)z);
DataTable Messages = Database.fillData("SELECT * FROM `Messages" + p.level.name +
"` WHERE X=" + x + " AND Y=" + y + " AND Z=" + z);
int last = Messages.Rows.Count - 1;
if (last == -1) { Messages.Dispose(); return true; }
string message = Messages.Rows[last]["Message"].ToString().Trim();
message = message.Replace("\\'", "\'");
if ( message != p.prevMsg || Server.repeatMessage ) {
if ( message.StartsWith("/") ) {
string[] parts = message.Remove(0, 1).SplitSpaces(2);
if (message != p.prevMsg || Server.repeatMessage) {
string text;
List<string> cmds = ParseMB(message, out text);
if (text != null) Player.Message(p, text);
foreach (string cmd in cmds) {
string[] parts = cmd.SplitSpaces(2);
p.HandleCommand(parts[0], parts.Length > 1 ? parts[1] : "");
} else {
Player.Message(p, message);
}
p.prevMsg = message;
}
@ -103,5 +107,29 @@ namespace MCGalaxy.BlockBehaviour {
p.level.Blockchange(x, y, z, Block.DoorAirs(block));
return true;
}
static string[] sep = { " |/" };
const StringSplitOptions opts = StringSplitOptions.RemoveEmptyEntries;
static List<string> empty = new List<string>();
internal static List<string> ParseMB(string message, out string text) {
if (message.IndexOf('|') == -1) return ParseSingle(message, out text);
string[] parts = message.Split(sep, opts);
List<string> cmds = ParseSingle(parts[0], out text);
if (parts.Length == 1) return cmds;
if (text != null) cmds = new List<string>();
for (int i = 1; i < parts.Length; i++)
cmds.Add(parts[i]);
return cmds;
}
static List<string> ParseSingle(string message, out string text) {
if (message[0] == '/') {
text = null; return new List<string>(){ message.Substring(1) };
} else {
text = message; return empty;
}
}
}
}

View File

@ -16,7 +16,9 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Data;
using MCGalaxy.BlockBehaviour;
using MCGalaxy.SQL;
namespace MCGalaxy.Commands.Building {
@ -49,19 +51,12 @@ namespace MCGalaxy.Commands.Building {
if (args.Length == 1) {
Player.Message(p, "You need to provide text to put in the messageblock."); return;
}
if (cpos.message == null)
cpos.message = args[1];
bool allCmds = CheckExtraPerm(p);
foreach (Command com in Command.all.commands) {
if (com.defaultRank <= p.group.Permission && (allCmds || !com.type.Contains("mod"))) continue;
if (IsCommand(cpos.message, "/" + com.name)) {
p.SendMessage("You cannot use that command in a messageblock."); return;
}
if (com.shortcut != "" && IsCommand(cpos.message, "/" + com.shortcut)) {
p.SendMessage("You cannot use that command in a messageblock."); return;
}
if (cpos.message == null) cpos.message = args[1];
string text;
List<string> cmds = WalkthroughBehaviour.ParseMB(cpos.message, out text);
foreach (string cmd in cmds) {
if (!CheckCommand(p, cmd)) return;
}
p.blockchangeObject = cpos;
@ -69,6 +64,21 @@ namespace MCGalaxy.Commands.Building {
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
}
bool CheckCommand(Player p, string message) {
bool allCmds = CheckExtraPerm(p);
foreach (Command cmd in Command.all.commands) {
if (cmd.defaultRank <= p.group.Permission && (allCmds || !cmd.type.Contains("mod"))) continue;
if (IsCommand(message, cmd.name)) {
p.SendMessage("You cannot use that command in a messageblock."); return false;
}
if (cmd.shortcut != "" && IsCommand(message, cmd.shortcut)) {
p.SendMessage("You cannot use that command in a messageblock."); return false;
}
}
return true;
}
bool IsCommand(string message, string cmd) {
return message.CaselessEq(cmd) || message.CaselessStarts(cmd + " ");
}