From 7d032ba69f7fe91c10de4cd3aa88d4272cad922c Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sat, 18 Sep 2021 17:42:40 +0100 Subject: [PATCH] Scattercommand should use the same editsession for all commands, make it "silent" by default and allow players to see output if wanted. --- .../command/tool/brush/ScatterCommand.java | 49 ++++++++++++++++--- .../core/wrappers/SilentPlayerWrapper.java | 1 - .../worldedit/command/BrushCommands.java | 6 ++- .../event/platform/CommandEvent.java | 31 ++++++++++++ .../platform/PlatformCommandManager.java | 11 ++++- 5 files changed, 88 insertions(+), 10 deletions(-) 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 70c5c7598..e5fb0743d 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 @@ -11,20 +11,29 @@ 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 java.util.List; public class ScatterCommand extends ScatterBrush { private final String command; + private final boolean print; - public ScatterCommand(int count, int distance, String command) { + public ScatterCommand(int count, int distance, String command, boolean print) { super(count, distance); this.command = command; + this.print = print; } @Override - public void apply(EditSession editSession, LocalBlockVectorSet placed, BlockVector3 position, Pattern p, double size) throws + public void apply( + EditSession editSession, + LocalBlockVectorSet placed, + BlockVector3 position, + Pattern pattern, + double size + ) throws MaxChangedBlocksException { int radius = getDistance(); CuboidRegionSelector selector = new CuboidRegionSelector( @@ -42,12 +51,40 @@ public class ScatterCommand extends ScatterBrush { player.setSelection(selector); List cmds = StringMan.split(replaced, ';'); for (String cmd : cmds) { - CommandEvent event = new CommandEvent( - new LocationMaskedPlayerWrapper(player, player.getLocation().setPosition(position.toVector3()), false), - cmd - ); + Player p = print ? + new LocationMaskedPlayerWrapper(player, player.getLocation().setPosition(position.toVector3()), false) : + new ScatterCommandPlayerWrapper(player, position); + CommandEvent event = new CommandEvent(p, 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/fastasyncworldedit/core/wrappers/SilentPlayerWrapper.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/wrappers/SilentPlayerWrapper.java index 763b9ee3f..c053ab176 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/wrappers/SilentPlayerWrapper.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/wrappers/SilentPlayerWrapper.java @@ -21,7 +21,6 @@ public class SilentPlayerWrapper extends AsyncPlayer { @Override public void print(Component component) { - super.print(component); } @Override 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 861138d66..9e5699237 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 @@ -694,13 +694,15 @@ public class BrushCommands { @Arg(desc = "double", def = "1") double distance, @Arg(desc = "List of comma-separated commands") - List commandStr + List commandStr, + @Switch(name = 'p', desc = "Show any printed output") + boolean print ) throws WorldEditException { worldEdit.checkMaxBrushRadius(radius); set( context, - new ScatterCommand((int) points, (int) distance, StringMan.join(commandStr, " ")), + new ScatterCommand((int) points, (int) distance, StringMan.join(commandStr, " "), print), "worldedit.brush.scattercommand" ) .setSize(radius); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/event/platform/CommandEvent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/event/platform/CommandEvent.java index 811588aa5..1bcb703d7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/event/platform/CommandEvent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/event/platform/CommandEvent.java @@ -19,10 +19,13 @@ package com.sk89q.worldedit.event.platform; +import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.event.AbstractCancellable; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.PlatformCommandManager; +import javax.annotation.Nullable; + import static com.google.common.base.Preconditions.checkNotNull; /** @@ -32,6 +35,10 @@ public class CommandEvent extends AbstractCancellable { private final Actor actor; private final String arguments; + //FAWE start + @Nullable + private final EditSession session; + //FAWE end /** * Create a new instance. @@ -45,8 +52,32 @@ public class CommandEvent extends AbstractCancellable { this.actor = actor; this.arguments = arguments; + //FAWE start + this.session = null; } + /** + * Create a new instance. + * + * @param actor the player + * @param arguments the arguments + * @param editSession the editsession + */ + public CommandEvent(Actor actor, String arguments, @Nullable EditSession editSession) { + checkNotNull(actor); + checkNotNull(arguments); + + this.actor = actor; + this.arguments = arguments; + this.session = editSession; + } + + @Nullable + public EditSession getSession() { + return session; + } + //FAWE end + /** * Get the actor that issued the command. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index 24bfaf5dd..0e0b87a85 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -795,7 +795,8 @@ public final class PlatformCommandManager { } Optional editSessionOpt = context.injectedValue(Key.of(EditSession.class)); - if (editSessionOpt.isPresent()) { + // Require null CommandEvent#getSession as it means the editsession is being handled somewhere else. + if (editSessionOpt.isPresent() && event.getSession() == null) { EditSession editSession = editSessionOpt.get(); editSession.flushQueue(); session.remember(editSession); @@ -866,6 +867,14 @@ public final class PlatformCommandManager { store.injectValue(Key.of(boolean.class), context -> Optional.of(isSuggestions)); store.injectValue(Key.of(InjectedValueStore.class), ValueProvider.constant(store)); store.injectValue(Key.of(Event.class), ValueProvider.constant(event)); + //FAWE start - allow giving editsessions + if (event instanceof CommandEvent) { + EditSession session = ((CommandEvent) event).getSession(); + if (session != null) { + store.injectValue(Key.of(EditSession.class), context -> Optional.of(session)); + } + } + //FAWE end return MemoizingValueAccess.wrap( MergedValueAccess.of(store, globalInjectedValues) );