Reduce code duplication in /mb.

This commit is contained in:
UnknownShadow200 2016-01-24 22:49:52 +11:00
parent 8cad8ad507
commit 21d527e741
4 changed files with 55 additions and 122 deletions

View File

@ -23,7 +23,7 @@ namespace MCGalaxy.Commands
{ {
public override string name { get { return "aka"; } } public override string name { get { return "aka"; } }
public override string shortcut { get { return ""; } } public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Moderation; } } public override string type { get { return CommandTypes.Games; } }
public override bool museumUsable { get { return true; } } public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Banned; } } public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
public CmdAka() { } public CmdAka() { }

View File

@ -1,7 +1,7 @@
/* /*
Copyright 2011 MCGalaxy Copyright 2011 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at obtain a copy of the Licenses at
@ -14,119 +14,58 @@
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.Data; using System.Data;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using MCGalaxy.SQL; using MCGalaxy.SQL;
namespace MCGalaxy.Commands namespace MCGalaxy.Commands {
{
public sealed class CmdMessageBlock : Command public sealed class CmdMessageBlock : Command {
{
public override string name { get { return "mb"; } } public override string name { get { return "mb"; } }
public override string shortcut { get { return ""; } } public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Building; } } public override string type { get { return CommandTypes.Building; } }
public override bool museumUsable { get { return false; } } public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } } public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
public CmdMessageBlock() { } public CmdMessageBlock() { }
static char[] trimChars = { ' ' };
public override void Use(Player p, string message) public override void Use(Player p, string message) {
{
if (message == "") { Help(p); return; } if (message == "") { Help(p); return; }
CatchPos cpos; CatchPos cpos;
cpos.message = ""; cpos.message = null;
string[] args = message.Split(trimChars, 2);
try switch (args[0].ToLower()) {
{
switch (message.Split(' ')[0])
{
case "air": cpos.type = Block.MsgAir; break; case "air": cpos.type = Block.MsgAir; break;
case "water": cpos.type = Block.MsgWater; break; case "water": cpos.type = Block.MsgWater; break;
case "lava": cpos.type = Block.MsgLava; break; case "lava": cpos.type = Block.MsgLava; break;
case "black": cpos.type = Block.MsgBlack; break; case "black": cpos.type = Block.MsgBlack; break;
case "white": cpos.type = Block.MsgWhite; break; case "white": cpos.type = Block.MsgWhite; break;
case "show": showMBs(p); return; case "show": ShowMessageBlocks(p); return;
default: cpos.type = Block.MsgWhite; cpos.message = message; break; default: cpos.type = Block.MsgWhite; cpos.message = message; break;
}
if (cpos.message == null)
cpos.message = args[1];
foreach (Command com in Command.all.commands) {
if (com.defaultRank <= p.group.Permission && !com.type.Contains("mod")) continue;
if (CaselessStarts(cpos.message, "/" + com.name)) {
p.SendMessage("You cannot use that command in a messageblock."); return;
}
if (com.shortcut != "" && CaselessStarts(cpos.message, "/" + com.shortcut)) {
p.SendMessage("You cannot use that command in a messageblock."); return;
} }
} }
catch { cpos.type = Block.MsgWhite; cpos.message = message; }
string current = "";
string cmdparts = "";
/*Fix by alecdent*/
try
{
foreach (var com in Command.all.commands)
{
if (com.type.Contains("mod"))
{
current = "/" + com.name;
cmdparts = message.Split(' ')[0].ToLower().ToString();
if (cmdparts[0] == '/')
{
if (current == cmdparts.ToLower())
{
p.SendMessage("You can't use that command in your messageblock!");
return;
}
}
cmdparts = message.Split(' ')[1].ToLower().ToString();
if (cmdparts[0] == '/')
{
if (current == cmdparts.ToLower())
{
p.SendMessage("You can't use that command in your messageblock!");
return;
}
}
if (com.shortcut != "")
{
current = "/" + com.name;
cmdparts = message.Split(' ')[0].ToLower().ToString();
if (cmdparts[0] == '/')
{
if (current == cmdparts.ToLower())
{
p.SendMessage("You can't use that command in your messageblock!");
return;
}
}
cmdparts = message.Split(' ')[1].ToLower().ToString();
if (cmdparts[0] == '/')
{
if (current == cmdparts.ToLower())
{
p.SendMessage("You can't use that command in your messageblock!");
return;
}
}
}
}
}
}
catch { }
if (cpos.message == "") cpos.message = message.Substring(message.IndexOf(' ') + 1);
p.blockchangeObject = cpos; p.blockchangeObject = cpos;
Player.SendMessage(p, "Place where you wish the message block to go."); p.ClearBlockchange(); Player.SendMessage(p, "Place where you wish the message block to go."); p.ClearBlockchange();
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1); p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
} }
public override void Help(Player p)
{
Player.SendMessage(p, "/mb [block] [message] - Places a message in your next block.");
Player.SendMessage(p, "Valid blocks: white, black, air, water, lava");
Player.SendMessage(p, "/mb show shows or hides MBs");
}
public void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType) public void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType) {
{
p.ClearBlockchange(); p.ClearBlockchange();
CatchPos cpos = (CatchPos)p.blockchangeObject; CatchPos cpos = (CatchPos)p.blockchangeObject;
@ -135,52 +74,47 @@ namespace MCGalaxy.Commands
//safe against SQL injections because no user input is given here //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=" + (int)x + " AND Y=" + (int)y + " AND Z=" + (int)z);
Database.AddParams("@Message", cpos.message); Database.AddParams("@Message", cpos.message);
if (Messages.Rows.Count == 0) if (Messages.Rows.Count == 0)
{
Database.executeQuery("INSERT INTO `Messages" + p.level.name + "` (X, Y, Z, Message) VALUES (" + (int)x + ", " + (int)y + ", " + (int)z + ", @Message)"); Database.executeQuery("INSERT INTO `Messages" + p.level.name + "` (X, Y, Z, Message) VALUES (" + (int)x + ", " + (int)y + ", " + (int)z + ", @Message)");
}
else else
{
Database.executeQuery("UPDATE `Messages" + p.level.name + "` SET Message=@Message WHERE X=" + (int)x + " AND Y=" + (int)y + " AND Z=" + (int)z); Database.executeQuery("UPDATE `Messages" + p.level.name + "` SET Message=@Message WHERE X=" + (int)x + " AND Y=" + (int)y + " AND Z=" + (int)z);
}
Messages.Dispose(); Messages.Dispose();
Player.SendMessage(p, "Message block placed."); Player.SendMessage(p, "Message block placed.");
p.level.Blockchange(p, x, y, z, cpos.type); p.level.Blockchange(p, x, y, z, cpos.type);
p.SendBlockchange(x, y, z, cpos.type); p.SendBlockchange(x, y, z, cpos.type);
if (p.staticCommands) p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1); if (p.staticCommands)
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
} }
struct CatchPos { public string message; public byte type; } struct CatchPos { public string message; public byte type; }
static bool CaselessStarts(string a, string b) {
return a.StartsWith(b, StringComparison.OrdinalIgnoreCase);
}
public void showMBs(Player p) void ShowMessageBlocks(Player p) {
{ p.showMBs = !p.showMBs;
try //safe against SQL injections because no user input is given here
{ using (DataTable Messages = Database.fillData("SELECT * FROM `Messages" + p.level.name + "`")) {
p.showMBs = !p.showMBs; if (p.showMBs) {
//safe against SQL injections because no user input is given here for (int i = 0; i < Messages.Rows.Count; i++)
using (DataTable Messages = Database.fillData("SELECT * FROM `Messages" + p.level.name + "`")) p.SendBlockchange(ushort.Parse(Messages.Rows[i]["X"].ToString()), ushort.Parse(Messages.Rows[i]["Y"].ToString()), ushort.Parse(Messages.Rows[i]["Z"].ToString()), Block.MsgWhite);
{ Player.SendMessage(p, "Now showing &a" + Messages.Rows.Count + " %SMBs.");
int i; } else {
for (int i = 0; i < Messages.Rows.Count; i++)
if (p.showMBs) p.SendBlockchange(ushort.Parse(Messages.Rows[i]["X"].ToString()), ushort.Parse(Messages.Rows[i]["Y"].ToString()), ushort.Parse(Messages.Rows[i]["Z"].ToString()), p.level.GetTile(ushort.Parse(Messages.Rows[i]["X"].ToString()), ushort.Parse(Messages.Rows[i]["Y"].ToString()), ushort.Parse(Messages.Rows[i]["Z"].ToString())));
{ Player.SendMessage(p, "Now hiding MBs.");
for (i = 0; i < Messages.Rows.Count; i++)
p.SendBlockchange(ushort.Parse(Messages.Rows[i]["X"].ToString()), ushort.Parse(Messages.Rows[i]["Y"].ToString()), ushort.Parse(Messages.Rows[i]["Z"].ToString()), Block.MsgWhite);
Player.SendMessage(p, "Now showing &a" + i.ToString() + Server.DefaultColor + " MBs.");
}
else
{
for (i = 0; i < Messages.Rows.Count; i++)
p.SendBlockchange(ushort.Parse(Messages.Rows[i]["X"].ToString()), ushort.Parse(Messages.Rows[i]["Y"].ToString()), ushort.Parse(Messages.Rows[i]["Z"].ToString()), p.level.GetTile(ushort.Parse(Messages.Rows[i]["X"].ToString()), ushort.Parse(Messages.Rows[i]["Y"].ToString()), ushort.Parse(Messages.Rows[i]["Z"].ToString())));
Player.SendMessage(p, "Now hiding MBs.");
}
} }
} }
catch (Exception e) }
{
Server.ErrorLog(e); public override void Help(Player p) {
} Player.SendMessage(p, "/mb [block] [message] - Places a message in your next block.");
Player.SendMessage(p, "Valid blocks: white, black, air, water, lava");
Player.SendMessage(p, "/mb show shows or hides MBs");
} }
} }
} }

View File

@ -170,6 +170,7 @@
<Compile Include="Commands\Economy\CmdGive.cs" /> <Compile Include="Commands\Economy\CmdGive.cs" />
<Compile Include="Commands\Economy\CmdMoney.cs" /> <Compile Include="Commands\Economy\CmdMoney.cs" />
<Compile Include="Commands\Economy\CmdPay.cs" /> <Compile Include="Commands\Economy\CmdPay.cs" />
<Compile Include="Commands\Fun\CmdAka.cs" />
<Compile Include="Commands\Fun\CmdAlive.cs" /> <Compile Include="Commands\Fun\CmdAlive.cs" />
<Compile Include="Commands\Fun\CmdCountdown.cs" /> <Compile Include="Commands\Fun\CmdCountdown.cs" />
<Compile Include="Commands\Fun\CmdCtf.cs" /> <Compile Include="Commands\Fun\CmdCtf.cs" />
@ -222,7 +223,6 @@
<Compile Include="Commands\Information\CmdWhois.cs" /> <Compile Include="Commands\Information\CmdWhois.cs" />
<Compile Include="Commands\Information\CmdWhoNick.cs" /> <Compile Include="Commands\Information\CmdWhoNick.cs" />
<Compile Include="Commands\Information\CmdWhowas.cs" /> <Compile Include="Commands\Information\CmdWhowas.cs" />
<Compile Include="Commands\Moderation\CmdAka.cs" />
<Compile Include="Commands\Moderation\CmdAllowGuns.cs" /> <Compile Include="Commands\Moderation\CmdAllowGuns.cs" />
<Compile Include="Commands\Moderation\CmdBan.cs" /> <Compile Include="Commands\Moderation\CmdBan.cs" />
<Compile Include="Commands\Moderation\CmdBanEdit.cs" /> <Compile Include="Commands\Moderation\CmdBanEdit.cs" />

View File

@ -640,15 +640,14 @@ namespace MCGalaxy {
} }
void SendHackControl( byte allowflying, byte allownoclip, byte allowspeeding, byte allowrespawning, void SendHackControl( byte allowflying, byte allownoclip, byte allowspeeding, byte allowrespawning,
byte allowthirdperson, byte allowchangingweather, short maxjumpheight ) { byte allowthirdperson, short maxjumpheight ) {
byte[] buffer = new byte[7]; byte[] buffer = new byte[7];
buffer[0] = allowflying; buffer[0] = allowflying;
buffer[1] = allownoclip; buffer[1] = allownoclip;
buffer[2] = allowspeeding; buffer[2] = allowspeeding;
buffer[3] = allowrespawning; buffer[3] = allowrespawning;
buffer[4] = allowthirdperson; buffer[4] = allowthirdperson;
buffer[5] = allowchangingweather; NetUtils.WriteI16(maxjumpheight, buffer, 5);
NetUtils.WriteI16(maxjumpheight, buffer, 6);
SendRaw( Opcode.CpeHackControl, buffer ); SendRaw( Opcode.CpeHackControl, buffer );
} }