From 3bb6f9aa45ba188f217130601b2915d6fa4d3f8f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 10 Mar 2016 13:23:12 +1100 Subject: [PATCH] Add /replacebrush and /replacenotbrush. --- Commands/Command.All.cs | 2 + Commands/World/CmdCopyLVL.cs | 2 +- Commands/World/CmdGoto.cs | 2 +- Commands/building/CmdReplaceBrush.cs | 124 ++++++++++++++++++++------- Commands/building/CmdUndo.cs | 2 +- Commands/building/CmdWrite.cs | 2 +- Drawing/DrawOps/DrawOp.cs | 8 +- Drawing/DrawOps/ReplaceDrawOp.cs | 38 ++++---- Levels/Level.Blocks.cs | 3 +- MCGalaxy_.csproj | 1 + Player/Group/Group.cs | 9 +- 11 files changed, 128 insertions(+), 65 deletions(-) diff --git a/Commands/Command.All.cs b/Commands/Command.All.cs index 496eef525..403d50303 100644 --- a/Commands/Command.All.cs +++ b/Commands/Command.All.cs @@ -221,7 +221,9 @@ namespace MCGalaxy all.Add(new CmdRepeat()); all.Add(new CmdReplace()); all.Add(new CmdReplaceAll()); + all.Add(new CmdReplaceBrush()); all.Add(new CmdReplaceNot()); + all.Add(new CmdReplaceNotBrush()); all.Add(new CmdReport()); all.Add(new CmdResetBot()); all.Add(new CmdResetPass()); diff --git a/Commands/World/CmdCopyLVL.cs b/Commands/World/CmdCopyLVL.cs index c64600e75..ed36ce562 100644 --- a/Commands/World/CmdCopyLVL.cs +++ b/Commands/World/CmdCopyLVL.cs @@ -38,7 +38,7 @@ namespace MCGalaxy.Commands { } string src = args[0], dst = args[1]; - if (!p.group.CanExecute(Command.all.Find("newlvl"))) { + if (!p.group.CanExecute("newlvl")) { Player.SendMessage(p, "You cannot use /copylvl as you cannot use /newlvl."); return; } if (!Player.ValidName(src)) { Player.SendMessage(p, "\"" + src + "\" is not a valid level name."); return; } diff --git a/Commands/World/CmdGoto.cs b/Commands/World/CmdGoto.cs index 933580175..0d08ba582 100644 --- a/Commands/World/CmdGoto.cs +++ b/Commands/World/CmdGoto.cs @@ -105,7 +105,7 @@ namespace MCGalaxy.Commands { if (!p.ignorePermission && p.group.Permission < lvl.permissionvisit) { Player.SendMessage(p, "You're not allowed to go to " + lvl.name + "."); return false; } - if (!p.ignorePermission && p.group.Permission > lvl.pervisitmax && !p.group.CanExecute(Command.all.Find("pervisitmax"))) { + if (!p.ignorePermission && p.group.Permission > lvl.pervisitmax && !p.group.CanExecute("pervisitmax")) { Player.SendMessage(p, "Your rank must be " + lvl.pervisitmax + " or lower to go there!"); return false; } if (File.Exists("text/lockdown/map/" + message.ToLower())) { diff --git a/Commands/building/CmdReplaceBrush.cs b/Commands/building/CmdReplaceBrush.cs index 9b3983e12..40fe629d7 100644 --- a/Commands/building/CmdReplaceBrush.cs +++ b/Commands/building/CmdReplaceBrush.cs @@ -17,38 +17,98 @@ */ using System; using MCGalaxy; +using MCGalaxy.Drawing; using MCGalaxy.Drawing.Brushes; - -namespace MCGalaxy.Commands { - - public sealed class CmdReplaceBrush : Command { - public override string name { get { return "replacebrush"; } } - public override string shortcut { get { return "rb"; } } - public override string type { get { return CommandTypes.Building; } } - public override bool museumUsable { get { return false; } } - public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } } - static char[] trimChars = {' '}; +using MCGalaxy.Drawing.Ops; - public override void Use(Player p, string message) { - // TODO: make sure can use or brush first. - if (message == "") { Help(p); return; } - if (p == null) { MessageInGameOnly(p); return; } - string[] args = message.Split(trimChars, 3); - if (args.Length < 2) { Help(p); return } - - byte extType = 0; - byte type = DrawCmd.GetBlock(p, args[0], out extType); - string brush = CmdBrush.FindBrush(args[1]); - if (brush == null) { - Player.SendMessage(p, "No brush found with name \"" + message + "\"."); - Player.SendMessage(p, "Available brushes: " + CmdBrush.AvailableBrushes); - return; - } - } - - public override void Help(Player p) { - Player.SendMessage(p, "/replace [block] [block2].. [new] - replace block with new inside a selected cuboid"); - Player.SendMessage(p, "If more than one [block] is specified, they will all be replaced."); - } - } +namespace MCGalaxy.Commands { + + public class CmdReplaceBrush : Command { + public override string name { get { return "replacebrush"; } } + public override string shortcut { get { return "rb"; } } + public override string type { get { return CommandTypes.Building; } } + public override bool museumUsable { get { return false; } } + public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } } + static char[] trimChars = {' '}; + + public override void Use(Player p, string message) { + if (message == "") { Help(p); return; } + if (p == null) { MessageInGameOnly(p); return; } + string replaceCmd = ReplaceNot ? "replacenot" : "replace"; + if (!p.group.CanExecute(replaceCmd) || !p.group.CanExecute("brush")) { + Player.SendMessage(p, "You cannot use /brush and/or /" + replaceCmd + + ", so therefore cannot use this command."); return; + } + + CatchPos cpos = default(CatchPos); + cpos.message = message.ToLower(); + p.blockchangeObject = cpos; + + Player.SendMessage(p, "Place two blocks to determine the edges."); + p.ClearBlockchange(); + p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1); + } + + void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType) { + RevertAndClearState(p, x, y, z); + CatchPos bp = (CatchPos)p.blockchangeObject; + bp.x = x; bp.y = y; bp.z = z; + p.blockchangeObject = bp; + p.Blockchange += new Player.BlockchangeEventHandler(Blockchange2); + } + + void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type, byte extType) { + RevertAndClearState(p, x, y, z); + CatchPos cpos = (CatchPos)p.blockchangeObject; + type = type < 128 ? p.bindings[type] : type; + + string[] parts = cpos.message.Split(trimChars, 3); + if (parts.Length < 2) { Help(p); return; } + + byte extTile = 0; + byte tile = DrawCmd.GetBlock(p, parts[0], out extTile); + if (tile == Block.Zero) return; + string brushName = CmdBrush.FindBrush(parts[1]); + if (brushName == null) { + Player.SendMessage(p, "No brush found with name \"" + parts[1] + "\"."); + Player.SendMessage(p, "Available brushes: " + CmdBrush.AvailableBrushes); + return; + } + + string brushMessage = parts.Length > 2 ? parts[2].ToLower() : ""; + BrushArgs args = new BrushArgs(p, brushMessage, type, extType); + Brush brush = Brush.Brushes[brushName](args); + if (brush == null) return; + + DrawOp drawOp = null; + if (ReplaceNot) drawOp = new ReplaceNotDrawOp(tile, extTile); + else drawOp = new ReplaceDrawOp(tile, extTile); + + if (!DrawOp.DoDrawOp(drawOp, brush, p, cpos.x, cpos.y, cpos.z, x, y, z)) + return; + if (p.staticCommands) + p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1); + } + + protected virtual bool ReplaceNot { get { return false; } } + + struct CatchPos { public ushort x, y, z; public string message; } + + public override void Help(Player p) { + Player.SendMessage(p, "/replace [block] [block2].. [new] - replace block with new inside a selected cuboid"); + Player.SendMessage(p, "If more than one [block] is specified, they will all be replaced."); + } + } + + public class CmdReplaceNotBrush : CmdReplaceBrush { + public override string name { get { return "replacenotbrush"; } } + public override string shortcut { get { return "r b"; } } + + protected override bool ReplaceNot { get { return true; } } + + public override void Help(Player p) { + Player.SendMessage(p, "/replace [block] [block2].. [new] - replace block with new inside a selected cuboid"); + Player.SendMessage(p, "If more than one [block] is specified, they will all be replaced."); + } + } } diff --git a/Commands/building/CmdUndo.cs b/Commands/building/CmdUndo.cs index d8f32edca..27d381a10 100644 --- a/Commands/building/CmdUndo.cs +++ b/Commands/building/CmdUndo.cs @@ -127,7 +127,7 @@ namespace MCGalaxy.Commands if (p != null && (int)p.group.Permission < CommandOtherPerms.GetPerm(this, 2)) { Player.SendMessage(p, "Reserved for " + Group.findPermInt(CommandOtherPerms.GetPerm(this, 2)).name + "+"); return; } - if (p != null && !p.group.CanExecute(Command.all.Find("physics"))) { + if (p != null && !p.group.CanExecute("physics")) { Player.SendMessage(p, "You can only undo physics if you can use /physics."); return; } Command.all.Find("physics").Use(p, "0"); diff --git a/Commands/building/CmdWrite.cs b/Commands/building/CmdWrite.cs index c553196f1..6e2154b26 100644 --- a/Commands/building/CmdWrite.cs +++ b/Commands/building/CmdWrite.cs @@ -31,7 +31,7 @@ namespace MCGalaxy.Commands { public override void Use(Player p, string message) { if (p == null) { MessageInGameOnly(p); return; } - if (!p.group.CanExecute(Command.all.Find("write"))) { + if (!p.group.CanExecute("write")) { Player.SendMessage(p, "You must be able to use /write to use /writetext."); return; } diff --git a/Drawing/DrawOps/DrawOp.cs b/Drawing/DrawOps/DrawOp.cs index 0249fe00c..35a73d4db 100644 --- a/Drawing/DrawOps/DrawOp.cs +++ b/Drawing/DrawOps/DrawOp.cs @@ -23,7 +23,13 @@ namespace MCGalaxy { public struct FillPos { public ushort X, Y, Z; } - public struct ExtBlock { public byte Type, ExtType; } + public struct ExtBlock { + public byte Type, ExtType; + + public ExtBlock(byte type, byte extType) { + Type = type; ExtType = extType; + } + } } namespace MCGalaxy.Drawing.Ops { diff --git a/Drawing/DrawOps/ReplaceDrawOp.cs b/Drawing/DrawOps/ReplaceDrawOp.cs index 1befdf6da..0c237876d 100644 --- a/Drawing/DrawOps/ReplaceDrawOp.cs +++ b/Drawing/DrawOps/ReplaceDrawOp.cs @@ -22,8 +22,11 @@ namespace MCGalaxy.Drawing.Ops { public class ReplaceDrawOp : DrawOp { - public ExtBlock[] ToReplace; - public ExtBlock Target; + public ExtBlock Include; + + public ReplaceDrawOp(byte type, byte extType) { + Include = new ExtBlock(type, extType); + } public override string Name { get { return "Replace"; } } @@ -33,8 +36,6 @@ namespace MCGalaxy.Drawing.Ops { public override void Perform(ushort x1, ushort y1, ushort z1, ushort x2, ushort y2, ushort z2, Player p, Level lvl, Brush brush) { - ExtBlock[] toReplace = ToReplace; - ExtBlock target = Target; for (ushort y = y1; y <= y2; y++) for (ushort z = z1; z <= z2; z++) for (ushort x = x1; x <= x2; x++) @@ -42,20 +43,19 @@ namespace MCGalaxy.Drawing.Ops { byte tile = lvl.GetTile(x, y, z), extTile = 0; if (tile == Block.custom_block) extTile = lvl.GetExtTile(x, y, z); - for (int i = 0; i < toReplace.Length; i++) { - ExtBlock block = toReplace[i]; - if (tile == block.Type && (tile != Block.custom_block || extTile == block.ExtType)) { - PlaceBlock(p, lvl, x, y, z, target.Type, target.ExtType); break; - } - } + if (tile == Include.Type && (tile != Block.custom_block || extTile == Include.ExtType)) + PlaceBlock(p, lvl, x, y, z, brush); } } } public class ReplaceNotDrawOp : DrawOp { - public ExtBlock[] ToReplace; - public ExtBlock Target; + public ExtBlock Exclude; + + public ReplaceNotDrawOp(byte type, byte extType) { + Exclude = new ExtBlock(type, extType); + } public override string Name { get { return "ReplaceNot"; } } @@ -65,8 +65,6 @@ namespace MCGalaxy.Drawing.Ops { public override void Perform(ushort x1, ushort y1, ushort z1, ushort x2, ushort y2, ushort z2, Player p, Level lvl, Brush brush) { - ExtBlock[] toReplace = ToReplace; - ExtBlock target = Target; for (ushort y = y1; y <= y2; y++) for (ushort z = z1; z <= z2; z++) for (ushort x = x1; x <= x2; x++) @@ -74,16 +72,10 @@ namespace MCGalaxy.Drawing.Ops { byte tile = lvl.GetTile(x, y, z), extTile = 0; if (tile == Block.custom_block) extTile = lvl.GetExtTile(x, y, z); - bool place = true; - for (int i = 0; i < toReplace.Length; i++) { - ExtBlock block = toReplace[i]; - if (tile == block.Type || (tile == Block.custom_block && extTile == block.ExtType)) { - place = false; break; - } + if (tile != Exclude.Type || (tile == Block.custom_block && extTile != Exclude.ExtType)) { + PlaceBlock(p, lvl, x, y, z, brush); } - if (place) - PlaceBlock(p, lvl, x, y, z, target.Type, target.ExtType); } } } -} +} \ No newline at end of file diff --git a/Levels/Level.Blocks.cs b/Levels/Level.Blocks.cs index e4f379e94..e536f8695 100644 --- a/Levels/Level.Blocks.cs +++ b/Levels/Level.Blocks.cs @@ -273,8 +273,7 @@ namespace MCGalaxy { return false; } - if (p.group.Permission > perbuildmax && (!inZone || !AllowBuild) && - !p.group.CanExecute(Command.all.Find("perbuildmax"))) { + if (p.group.Permission > perbuildmax && (!inZone || !AllowBuild) && !p.group.CanExecute("perbuildmax")) { if (p.ZoneSpam.AddSeconds(2) <= DateTime.UtcNow) { Player.SendMessage(p, "Your rank must be " + perbuildmax + " or lower to build here!"); p.ZoneSpam = DateTime.UtcNow; diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index fe76a2d5c..520b2a160 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -127,6 +127,7 @@ + diff --git a/Player/Group/Group.cs b/Player/Group/Group.cs index 1e7bbaa38..0cad254e3 100644 --- a/Player/Group/Group.cs +++ b/Player/Group/Group.cs @@ -99,9 +99,12 @@ namespace MCGalaxy GrpCommands.AddCommands(out _commands, Permission); commands = _commands; } - /// - /// Check to see if this group can excute cmd - /// + + public bool CanExecute(string cmdName) { + return commands.Contains(Command.all.Find(cmdName)); + } + + /// Check to see if this group can excute cmd /// The command object to check /// True if this group can use it, false if they cant public bool CanExecute(Command cmd) { return commands.Contains(cmd); }