Skip to content

Commit

Permalink
Allow using ReplaceBrush as a /brush (Thanks 123DMWM)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Oct 21, 2022
1 parent 2152efb commit 74e5ea3
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 115 deletions.
41 changes: 14 additions & 27 deletions MCGalaxy/Commands/building/CmdReplaceBrush.cs
Expand Up @@ -20,65 +20,52 @@
using MCGalaxy.Drawing.Ops;
using BlockID = System.UInt16;

namespace MCGalaxy.Commands.Building {
public class CmdReplaceBrush : DrawCmd {
namespace MCGalaxy.Commands.Building
{
public class CmdReplaceBrush : DrawCmd
{
public override string name { get { return "ReplaceBrush"; } }
public override string shortcut { get { return "rb"; } }
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
protected virtual bool ReplaceNot { get { return false; } }

protected override DrawOp GetDrawOp(DrawArgs dArgs) {
string[] args = dArgs.Message.SplitSpaces(3);
Player p = dArgs.Player;
if (args.Length < 2) { Help(p); return null; }

string replaceCmd = ReplaceNot ? "ReplaceNot" : "Replace";
if (!p.CanUse(replaceCmd) || !p.CanUse("Brush")) {
p.Message("You cannot use &T/Brush &Sand/or &T/" + replaceCmd +
"&S, so therefore cannot use this command."); return null;
}


BlockID target;
if (!CommandParser.GetBlockIfAllowed(p, args[0], "replace", out target)) return null;

BrushFactory factory = BrushFactory.Find(args[1]);
if (factory == null) {
p.Message("No brush found with name \"{0}\".", args[1]);
CmdBrush.List(p);
return null;
}

DrawOp op = null;
if (ReplaceNot) op = new ReplaceNotDrawOp(target);
else op = new ReplaceDrawOp(target);

DrawOp op = new CuboidDrawOp();
op.AffectedByTransform = false;
return op;
}

protected override void GetBrush(DrawArgs dArgs) {
string[] args = dArgs.Message.SplitSpaces(3);
dArgs.BrushName = args[1];
dArgs.BrushArgs = args.Length > 2 ? args[2] : "";
dArgs.BrushName = ReplaceNot ? "ReplaceNotBrush" : "ReplaceBrush";
dArgs.BrushArgs = dArgs.Message;
}

public override void Help(Player p) {
p.Message("&T/ReplaceBrush [block] [brush name] <brush args>");
p.Message("&HReplaces all blocks of the given type, " +
"in the specified area with the output of the given brush.");
"in the specified area with the output of the given brush.");
p.Message(BrushHelpLine);
}
}

public class CmdReplaceNotBrush : CmdReplaceBrush {
public class CmdReplaceNotBrush : CmdReplaceBrush
{
public override string name { get { return "ReplaceNotBrush"; } }
public override string shortcut { get { return "rnb"; } }

public override string shortcut { get { return "rnb"; } }
protected override bool ReplaceNot { get { return true; } }

public override void Help(Player p) {
p.Message("&T/ReplaceNotBrush [block] [brush name] <brush args>");
p.Message("&HReplaces all blocks (except for the given block), " +
"in the specified area with the output of the given brush.");
"in the specified area with the output of the given brush.");
p.Message(BrushHelpLine);
}
}
Expand Down
1 change: 1 addition & 0 deletions MCGalaxy/Commands/building/ReplaceCmds.cs
Expand Up @@ -48,6 +48,7 @@ public class CmdReplace : DrawCmd {
public sealed class CmdReplaceNot : CmdReplace {
public override string name { get { return "ReplaceNot"; } }
public override string shortcut { get { return "rn"; } }

protected override void GetBrush(DrawArgs dArgs) {
dArgs.BrushName = "ReplaceNot";
dArgs.BrushArgs = dArgs.Message;
Expand Down
13 changes: 7 additions & 6 deletions MCGalaxy/Drawing/BrushFactories/BrushFactory.cs
Expand Up @@ -34,12 +34,13 @@ public abstract class BrushFactory
public virtual bool Validate(BrushArgs args) { return Construct(args) != null; }

public static List<BrushFactory> Brushes = new List<BrushFactory>() {
new SolidBrushFactory(), new CheckeredBrushFactory(),
new StripedBrushFactory(), new PasteBrushFactory(),
new ReplaceBrushFactory(), new ReplaceNotBrushFactory(),
new RainbowBrushFactory(), new BWRainbowBrushFactory(),
new RandomBrushFactory(), new CloudyBrushFactory(),
new GradientBrushFactory(),
new SolidBrushFactory(), new CheckeredBrushFactory(),
new StripedBrushFactory(), new PasteBrushFactory(),
new ReplaceBrushFactory(), new ReplaceNotBrushFactory(),
new RainbowBrushFactory(), new BWRainbowBrushFactory(),
new RandomBrushFactory(), new CloudyBrushFactory(),
new GradientBrushFactory(), new ReplaceBrushBrushFactory(),
new ReplaceNotBrushBrushFactory(),
};

public static BrushFactory Find(string name) {
Expand Down
80 changes: 80 additions & 0 deletions MCGalaxy/Drawing/BrushFactories/ReplaceBrushBrushes.cs
@@ -0,0 +1,80 @@
/*
Copyright 2015 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.Brushes;
using MCGalaxy.Commands;
using MCGalaxy.Commands.Building;
using BlockID = System.UInt16;

namespace MCGalaxy.Drawing.Brushes
{
public class ReplaceBrushBrushFactory : BrushFactory
{
public override string Name { get { return "ReplaceBrush"; } }
public override string[] Help { get { return HelpString; } }

static string[] HelpString = new string[] {
"&TArguments: [block] [brush name] <brush args>",
"&HDraws by replacing existing blocks that are the given [block] with the output of the given brush"
};

public override Brush Construct(BrushArgs args) {
BlockID include = 0;
Brush brush = ParseArguments(args, ref include);

if (brush == null) return null;
return new ReplaceBrushBrush(include, brush);
}

protected Brush ParseArguments(BrushArgs args, ref BlockID target) {
string[] parts = args.Message.SplitSpaces(3);
Player p = args.Player;

if (parts.Length < 2) { p.MessageLines(Help); return null; }
if (!CommandParser.GetBlockIfAllowed(p, parts[0], "replace", out target)) return null;

BrushFactory factory = BrushFactory.Find(parts[1]);
if (factory == null) {
p.Message("No brush found with name \"{0}\".", parts[1]);
CmdBrush.List(p); return null;
}

args.Message = parts.Length > 2 ? parts[2] : "";
return factory.Construct(args);
}
}

public class ReplaceNotBrushBrushFactory : ReplaceBrushBrushFactory
{
public override string Name { get { return "ReplaceNotBrush"; } }
public override string[] Help { get { return HelpString; } }

static string[] HelpString = new string[] {
"&TArguments: [block] [brush name] <brush args>",
"&HDraws by replacing existing blocks that not the given [block] with the output of the given brush"
};

public override Brush Construct(BrushArgs args) {
BlockID exclude = 0;
Brush brush = ParseArguments(args, ref exclude);

if (brush == null) return null;
return new ReplaceNotBrushBrush(exclude, brush);
}
}
}
72 changes: 72 additions & 0 deletions MCGalaxy/Drawing/Brushes/ReplaceBrushBrushes.cs
@@ -0,0 +1,72 @@
/*
Copyright 2015 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.DB;
using MCGalaxy.Drawing.Ops;
using BlockID = System.UInt16;

namespace MCGalaxy.Drawing.Brushes
{
public sealed class ReplaceBrushBrush : Brush
{
readonly BlockID include;
readonly Brush replacement;

public ReplaceBrushBrush(BlockID include, Brush replacement) {
this.include = include; this.replacement = replacement;
}

public override string Name { get { return "ReplaceBrush"; } }

public override void Configure(DrawOp op, Player p) {
op.Flags = BlockDBFlags.Replaced;
}

public override BlockID NextBlock(DrawOp op) {
ushort x = op.Coords.X, y = op.Coords.Y, z = op.Coords.Z;
BlockID block = op.Level.GetBlock(x, y, z); // TODO FastGetBlock

if (block != include) return Block.Invalid;
return replacement.NextBlock(op);
}
}

public sealed class ReplaceNotBrushBrush : Brush
{
readonly BlockID exclude;
readonly Brush replacement;

public ReplaceNotBrushBrush(BlockID exclude, Brush replacement) {
this.exclude = exclude; this.replacement = replacement;
}

public override string Name { get { return "ReplaceNotBrush"; } }

public override void Configure(DrawOp op, Player p) {
op.Flags = BlockDBFlags.Replaced;
}

public override BlockID NextBlock(DrawOp op) {
ushort x = op.Coords.X, y = op.Coords.Y, z = op.Coords.Z;
BlockID block = op.Level.GetBlock(x, y, z); // TODO FastGetBlock

if (block == exclude) return Block.Invalid;
return replacement.NextBlock(op);
}
}
}
81 changes: 0 additions & 81 deletions MCGalaxy/Drawing/DrawOps/ReplaceDrawOp.cs

This file was deleted.

3 changes: 2 additions & 1 deletion MCGalaxy/MCGalaxy_.csproj
Expand Up @@ -390,11 +390,13 @@
<Compile Include="Drawing\Brushes\PasteBrush.cs" />
<Compile Include="Drawing\Brushes\RainbowBrush.cs" />
<Compile Include="Drawing\Brushes\RandomBrush.cs" />
<Compile Include="Drawing\Brushes\ReplaceBrushBrushes.cs" />
<Compile Include="Drawing\Brushes\ReplaceBrushes.cs" />
<Compile Include="Drawing\Brushes\SimpleBrushes.cs" />
<Compile Include="Drawing\BrushFactories\BrushFactory.cs" />
<Compile Include="Drawing\BrushFactories\CloudyBrush.cs" />
<Compile Include="Drawing\BrushFactories\FrequencyBrushes.cs" />
<Compile Include="Drawing\BrushFactories\ReplaceBrushBrushes.cs" />
<Compile Include="Drawing\BrushFactories\ReplaceBrushes.cs" />
<Compile Include="Drawing\BrushFactories\SimpleBrushes.cs" />
<Compile Include="Drawing\CopyState.cs" />
Expand All @@ -413,7 +415,6 @@
<Compile Include="Drawing\DrawOps\MazeDrawOp.cs" />
<Compile Include="Drawing\DrawOps\PasteDrawOp.cs" />
<Compile Include="Drawing\DrawOps\RedoDrawOp.cs" />
<Compile Include="Drawing\DrawOps\ReplaceDrawOp.cs" />
<Compile Include="Drawing\DrawOps\RestoreSelectionDrawOp.cs" />
<Compile Include="Drawing\DrawOps\SpheroidDrawOps.cs" />
<Compile Include="Drawing\DrawOps\PyramidDrawOp.cs" />
Expand Down

0 comments on commit 74e5ea3

Please sign in to comment.