Cleanup /replace, /replacenot, and /replaceadd commands to be more modular.

This commit is contained in:
UnknownShadow200 2016-03-10 12:49:06 +11:00
parent 8485ed8727
commit bcaf2e43d3
9 changed files with 91 additions and 243 deletions

View File

@ -43,7 +43,8 @@ namespace MCGalaxy.Commands
case DrawMode.walls:
drawOp = new CuboidWallsDrawOp(); break;
case DrawMode.holes:
drawOp = new CuboidHolesDrawOp(); break;
drawOp = new CuboidDrawOp();
brush = new CheckeredBrush(cpos.type, cpos.extType, 0, 0); break;
case DrawMode.wire:
drawOp = new CuboidWireframeDrawOp(); break;
case DrawMode.random:

View File

@ -17,6 +17,7 @@
*/
using System;
using MCGalaxy.Drawing;
using MCGalaxy.Drawing.Brushes;
using MCGalaxy.Drawing.Ops;
namespace MCGalaxy.Commands {
@ -58,9 +59,9 @@ namespace MCGalaxy.Commands {
((PasteDrawOp)op).CopyState = p.CopyBuffer;
string[] args = cpos.message.Split(' ');
if (args[0].ToLower() == "not")
((PasteDrawOp)op).Exclude = ReplaceCmd.GetBlocks(p, 1, args.Length, args);
((PasteDrawOp)op).Exclude = ReplaceBrush.GetBlocks(p, 1, args.Length, args);
else
((PasteDrawOp)op).Include = ReplaceCmd.GetBlocks(p, 0, args.Length, args);
((PasteDrawOp)op).Include = ReplaceBrush.GetBlocks(p, 0, args.Length, args);
}
if (!DrawOp.DoDrawOp(op, null, p, (ushort)offX, (ushort)offY, (ushort)offZ, 0, 0, 0))

View File

@ -1,68 +0,0 @@
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using MCGalaxy.Drawing.Ops;
namespace MCGalaxy.Commands {
public sealed class CmdReplace : ReplaceCmd {
public override string name { get { return "replace"; } }
public override string shortcut { get { return "r"; } }
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
protected override void BeginReplace(Player p, ExtBlock[] toAffect, ExtBlock target) {
CatchPos cpos = default(CatchPos);
cpos.toAffect = toAffect;
cpos.target = target;
p.blockchangeObject = cpos;
Player.SendMessage(p, "Place two blocks to determine the edges.");
p.ClearBlockchange();
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
}
public 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);
}
public void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type, byte extType) {
RevertAndClearState(p, x, y, z);
CatchPos cpos = (CatchPos)p.blockchangeObject;
ReplaceDrawOp drawOp = new ReplaceDrawOp();
drawOp.ToReplace = cpos.toAffect;
drawOp.Target = cpos.target;
if (!DrawOp.DoDrawOp(drawOp, null, p, cpos.x, cpos.y, cpos.z, x, y, z))
return;
if (p.staticCommands)
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
}
struct CatchPos { public ushort x, y, z; public ExtBlock[] toAffect; public ExtBlock target; }
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.");
}
}
}

View File

@ -1,47 +0,0 @@
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using MCGalaxy.Drawing.Ops;
namespace MCGalaxy.Commands {
public sealed class CmdReplaceAll : ReplaceCmd {
public override string name { get { return "replaceall"; } }
public override string shortcut { get { return "ra"; } }
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
protected override void BeginReplace(Player p, ExtBlock[] toAffect, ExtBlock target) {
ushort x2 = (ushort)(p.level.Width - 1);
ushort y2 = (ushort)(p.level.Height - 1);
ushort z2 = (ushort)(p.level.Length - 1);
ReplaceDrawOp drawOp = new ReplaceDrawOp();
drawOp.ToReplace = toAffect;
drawOp.Target = target;
if (!DrawOp.DoDrawOp(drawOp, null, p, 0, 0, 0, x2, y2, z2))
return;
Player.SendMessage(p, "&4/replaceall finished!");
}
public override void Help(Player p) {
Player.SendMessage(p, "/ra [block] [block2].. [new] - Replaces all of [block] with [new] in a map");
Player.SendMessage(p, "If more than one [block] is specified, they will all be replaced.");
}
}
}

View File

@ -1,67 +0,0 @@
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using MCGalaxy.Drawing.Ops;
namespace MCGalaxy.Commands {
public sealed class CmdReplaceNot : ReplaceCmd {
public override string name { get { return "replacenot"; } }
public override string shortcut { get { return "rn"; } }
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
protected override void BeginReplace(Player p, ExtBlock[] toAffect, ExtBlock target) {
CatchPos cpos = default(CatchPos);
cpos.toAffect = toAffect;
cpos.target = target;
p.blockchangeObject = cpos;
Player.SendMessage(p, "Place two blocks to determine the edges.");
p.ClearBlockchange();
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
}
public 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);
}
public void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type, byte extType) {
RevertAndClearState(p, x, y, z);
CatchPos cpos = (CatchPos)p.blockchangeObject;
ReplaceNotDrawOp drawOp = new ReplaceNotDrawOp();
drawOp.ToReplace = cpos.toAffect;
drawOp.Target = cpos.target;
if (!DrawOp.DoDrawOp(drawOp, null, p, cpos.x, cpos.y, cpos.z, x, y, z))
return;
if (p.staticCommands)
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
}
struct CatchPos { public ushort x, y, z; public ExtBlock[] toAffect; public ExtBlock target; }
public override void Help(Player p) {
Player.SendMessage(p, "/rn [block] [block2].. [new] - replace everything but [block] with [new] inside a selected cuboid");
Player.SendMessage(p, "If multiple [block]s are specified they will all be ignored.");
}
}
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2015 MCGalaxy team
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
@ -16,42 +16,100 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using MCGalaxy.Drawing.Brushes;
using MCGalaxy.Drawing.Ops;
namespace MCGalaxy.Commands {
public abstract class ReplaceCmd : Command {
public class CmdReplace : Command {
public override string name { get { return "replace"; } }
public override string shortcut { get { return "r"; } }
public override string type { get { return CommandTypes.Building; } }
public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
public override void Use(Player p, string message) {
string[] parts = message.Split(' ');
for (int i = 0; i < parts.Length; i++)
parts[i] = parts[i].ToLower();
if (parts.Length < 2) { Help(p); 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;
BrushArgs args = new BrushArgs(p, cpos.message, type, extType);
Brush brush = ReplaceNot ? ReplaceNotBrush.Process(args) : ReplaceBrush.Process(args);
if (brush == null) return;
DrawOp drawOp = new CuboidDrawOp();
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 sealed class CmdReplaceNot : CmdReplace {
public override string name { get { return "replacenot"; } }
public override string shortcut { get { return "rn"; } }
ExtBlock[] toAffect = GetBlocks(p, 0, parts.Length - 1, parts);
ExtBlock target;
target.Type = DrawCmd.GetBlock(p, parts[parts.Length - 1], out target.ExtType);
if (target.Type == Block.Zero) return;
BeginReplace(p, toAffect, target);
}
protected override bool ReplaceNot { get { return true; } }
internal static ExtBlock[] GetBlocks(Player p, int start, int max, string[] parts) {
ExtBlock[] blocks = new ExtBlock[max - start];
for (int j = 0; j < blocks.Length; j++)
blocks[j].Type = Block.Zero;
for (int j = 0; start < max; start++, j++ ) {
byte extType = 0;
byte type = DrawCmd.GetBlock(p, parts[start], out extType);
if (type == Block.Zero) continue;
blocks[j].Type = type; blocks[j].ExtType = extType;
}
return blocks;
public override void Help(Player p) {
Player.SendMessage(p, "/rn [block] [block2].. [new] - replace everything but [block] with [new] inside a selected cuboid");
Player.SendMessage(p, "If multiple [block]s are specified they will all be ignored.");
}
}
public sealed class CmdReplaceAll : Command {
protected abstract void BeginReplace(Player p, ExtBlock[] toAffect, ExtBlock target);
public override string name { get { return "replaceall"; } }
public override string shortcut { get { return "ra"; } }
public override string type { get { return CommandTypes.Building; } }
public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
public override void Use(Player p, string message) {
ushort x2 = (ushort)(p.level.Width - 1);
ushort y2 = (ushort)(p.level.Height - 1);
ushort z2 = (ushort)(p.level.Length - 1);
BrushArgs args = new BrushArgs(p, message.ToLower(), 0, 0);
Brush brush = ReplaceBrush.Process(args);
if (brush == null) return;
DrawOp drawOp = new CuboidDrawOp();
if (!DrawOp.DoDrawOp(drawOp, brush, p, 0, 0, 0, x2, y2, z2))
return;
Player.SendMessage(p, "&4/replaceall finished!");
}
public override void Help(Player p) {
Player.SendMessage(p, "/ra [block] [block2].. [new] - Replaces all of [block] with [new] in a map");
Player.SendMessage(p, "If more than one [block] is specified, they will all be replaced.");
}
}
}

View File

@ -84,7 +84,7 @@ namespace MCGalaxy.Drawing.Brushes {
this.exclude = include; this.target = target;
}
public override string Name { get { return "Replace"; } }
public override string Name { get { return "ReplaceNot"; } }
public static Brush Process(BrushArgs args) {
string[] parts = args.Message.Split(' ');

View File

@ -39,33 +39,6 @@ namespace MCGalaxy.Drawing.Ops {
}
}
public class CuboidHolesDrawOp : DrawOp {
public override string Name { get { return "Cuboid Holes"; } }
public override int GetBlocksAffected(Level lvl, ushort x1, ushort y1, ushort z1, ushort x2, ushort y2, ushort z2) {
return (x2 - x1 + 1) * (y2 - y1 + 1) * (z2 - z1 + 1);
}
public override void Perform(ushort x1, ushort y1, ushort z1, ushort x2,
ushort y2, ushort z2, Player p, Level lvl, Brush brush) {
for (ushort y = y1; y <= y2; y++)
for (ushort z = z1; z <= z2; z++)
{
int i = (y & 1) == 0 ? 0 : 1;
if ((z & 1) == 0) i++;
for (ushort x = x1; x <= x2; x++) {
if ((i & 1) == 0)
PlaceBlock(p, lvl, x, y, z, brush);
else
PlaceBlock(p, lvl, x, y, z, Block.air, 0);
i++;
}
}
}
}
public class CuboidHollowsDrawOp : DrawOp {
public override string Name { get { return "Cuboid Hollow"; } }

View File

@ -127,9 +127,7 @@
<Compile Include="Commands\Building\CmdPortal.cs" />
<Compile Include="Commands\Building\CmdPyramid.cs" />
<Compile Include="Commands\Building\CmdRedo.cs" />
<Compile Include="Commands\Building\CmdReplace.cs" />
<Compile Include="Commands\Building\CmdReplaceAll.cs" />
<Compile Include="Commands\Building\CmdReplaceNot.cs" />
<Compile Include="Commands\building\ReplaceCmd.cs" />
<Compile Include="Commands\Building\CmdRestartPhysics.cs" />
<Compile Include="Commands\Building\CmdSpheroid.cs" />
<Compile Include="Commands\Building\CmdSpin.cs" />
@ -141,7 +139,6 @@
<Compile Include="Commands\Building\CmdWrite.cs" />
<Compile Include="Commands\building\CustomBlockCommand.cs" />
<Compile Include="Commands\building\DrawCmd.cs" />
<Compile Include="Commands\building\ReplaceCmd.cs" />
<Compile Include="Commands\Chat\CmdAdminChat.cs" />
<Compile Include="Commands\Chat\CmdChatRoom.cs" />
<Compile Include="Commands\Chat\CmdEmote.cs" />