From 1f29ab3b79451c028d2b3ec2032ea667a5d4090e Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 10 Jul 2023 22:04:33 +0100 Subject: [PATCH] fix: fix command brush (#2347) --- .../core/command/tool/brush/CommandBrush.java | 43 +++++++++++----- .../command/tool/brush/ScatterCommand.java | 51 +++++++------------ .../worldedit/command/BrushCommands.java | 6 ++- 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/command/tool/brush/CommandBrush.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/command/tool/brush/CommandBrush.java index 4072828bf..227960c1d 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/command/tool/brush/CommandBrush.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/command/tool/brush/CommandBrush.java @@ -22,9 +22,27 @@ import java.util.List; public class CommandBrush implements Brush { private final String command; + private final boolean print; + /** + * New instance + * + * @deprecated Use {@link CommandBrush#CommandBrush(String, boolean)} + */ + @Deprecated(forRemoval = true) public CommandBrush(String command) { - this.command = command.charAt(0) == '/' ? "/" + command : command; + this(command, false); + } + + /** + * New instance + * + * @param command command to run, or commands split by ';' + * @param print if output should be printed to the actor for the run commands + */ + public CommandBrush(String command, boolean print) { + this.command = command; + this.print = print; } @Override @@ -36,7 +54,7 @@ public class CommandBrush implements Brush { position.subtract(radius, radius, radius), position.add(radius, radius, radius) ); - String replaced = command.replace("{x}", position.getBlockX() + "") + String replaced = command.replace("{x}", Integer.toString(position.getBlockX())) .replace("{y}", Integer.toString(position.getBlockY())) .replace("{z}", Integer.toString(position.getBlockZ())) .replace("{world}", editSession.getWorld().getName()) @@ -46,21 +64,22 @@ public class CommandBrush implements Brush { if (!(actor instanceof Player player)) { throw FaweCache.PLAYER_ONLY; } - //Use max world height to allow full coverage of the world height - Location face = player.getBlockTraceFace(editSession.getWorld().getMaxY(), true); - if (face == null) { - position = position.add(0, 1, 1); - } else { - position = position.add(face.getDirection().toBlockPoint()); - } player.setSelection(selector); - AsyncPlayer wePlayer = new SilentPlayerWrapper(new LocationMaskedPlayerWrapper( + AsyncPlayer wePlayer = new LocationMaskedPlayerWrapper( player, new Location(player.getExtent(), position.toVector3()) - )); + ); + if (!print) { + wePlayer = new SilentPlayerWrapper(wePlayer); + } List cmds = StringMan.split(replaced, ';'); for (String cmd : cmds) { - CommandEvent event = new CommandEvent(wePlayer, cmd); + if (cmd.isBlank()) { + continue; + } + cmd = cmd.charAt(0) != '/' ? "/" + cmd : cmd; + cmd = cmd.length() >1 && cmd.charAt(1) == '/' ? cmd.substring(1) : cmd; + CommandEvent event = new CommandEvent(wePlayer, cmd, editSession); PlatformCommandManager.getInstance().handleCommandOnCurrentThread(event); } } diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/command/tool/brush/ScatterCommand.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/command/tool/brush/ScatterCommand.java index 81be2640c..1d7841355 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/command/tool/brush/ScatterCommand.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/command/tool/brush/ScatterCommand.java @@ -3,7 +3,9 @@ package com.fastasyncworldedit.core.command.tool.brush; import com.fastasyncworldedit.core.FaweCache; import com.fastasyncworldedit.core.math.LocalBlockVectorSet; import com.fastasyncworldedit.core.util.StringMan; +import com.fastasyncworldedit.core.wrappers.AsyncPlayer; import com.fastasyncworldedit.core.wrappers.LocationMaskedPlayerWrapper; +import com.fastasyncworldedit.core.wrappers.SilentPlayerWrapper; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.MaxChangedBlocksException; import com.sk89q.worldedit.entity.Player; @@ -13,7 +15,7 @@ import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.regions.selector.CuboidRegionSelector; -import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.Location; import java.util.List; @@ -43,7 +45,7 @@ public class ScatterCommand extends ScatterBrush { position.subtract(radius, radius, radius), position.add(radius, radius, radius) ); - String replaced = command.replace("{x}", position.getBlockX() + "") + String replaced = command.replace("{x}", Integer.toString(position.getBlockX())) .replace("{y}", Integer.toString(position.getBlockY())) .replace("{z}", Integer.toString(position.getBlockZ())) .replace("{world}", editSession.getWorld().getName()) @@ -55,41 +57,22 @@ public class ScatterCommand extends ScatterBrush { } player.setSelection(selector); List cmds = StringMan.split(replaced, ';'); + AsyncPlayer wePlayer = new LocationMaskedPlayerWrapper( + player, + new Location(player.getExtent(), position.toVector3()) + ); + if (!print) { + wePlayer = new SilentPlayerWrapper(wePlayer); + } for (String cmd : cmds) { - Player p = print ? - new LocationMaskedPlayerWrapper(player, player.getLocation().setPosition(position.toVector3()), false) : - new ScatterCommandPlayerWrapper(player, position); - CommandEvent event = new CommandEvent(p, cmd, editSession); + if (cmd.isBlank()) { + continue; + } + cmd = cmd.charAt(0) != '/' ? "/" + cmd : cmd; + cmd = cmd.length() >1 && cmd.charAt(1) == '/' ? cmd.substring(1) : cmd; + CommandEvent event = new CommandEvent(wePlayer, cmd, editSession); PlatformCommandManager.getInstance().handleCommandOnCurrentThread(event); } } - private static final class ScatterCommandPlayerWrapper extends LocationMaskedPlayerWrapper { - - ScatterCommandPlayerWrapper(Player player, BlockVector3 position) { - super(player, player.getLocation().setPosition(position.toVector3()), false); - } - - @Override - public void print(String msg) { - } - - @Override - public void print(Component component) { - } - - @Override - public void printDebug(String msg) { - } - - @Override - public void printError(String msg) { - } - - @Override - public void printRaw(String msg) { - } - - } - } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java index 358f75413..76cb60c64 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java @@ -929,11 +929,13 @@ public class BrushCommands { @Arg(desc = "Expression") Expression radius, @Arg(desc = "Command to run") - List input + List input, + @Switch(name = 'p', desc = "Show any printed output") + boolean print ) throws WorldEditException { worldEdit.checkMaxBrushRadius(radius); String cmd = StringMan.join(input, " "); - set(context, new CommandBrush(cmd), "worldedit.brush.command").setSize(radius); + set(context, new CommandBrush(cmd, print), "worldedit.brush.command").setSize(radius); } @Command(