Scattercommand should use the same editsession for all commands, make it "silent" by default and allow players to see output if wanted.

This commit is contained in:
dordsor21 2021-09-18 17:42:40 +01:00
parent 34301b446a
commit 7d032ba69f
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
5 changed files with 88 additions and 10 deletions

View File

@ -11,20 +11,29 @@ import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector; import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import com.sk89q.worldedit.util.formatting.text.Component;
import java.util.List; import java.util.List;
public class ScatterCommand extends ScatterBrush { public class ScatterCommand extends ScatterBrush {
private final String command; 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); super(count, distance);
this.command = command; this.command = command;
this.print = print;
} }
@Override @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 { MaxChangedBlocksException {
int radius = getDistance(); int radius = getDistance();
CuboidRegionSelector selector = new CuboidRegionSelector( CuboidRegionSelector selector = new CuboidRegionSelector(
@ -42,12 +51,40 @@ public class ScatterCommand extends ScatterBrush {
player.setSelection(selector); player.setSelection(selector);
List<String> cmds = StringMan.split(replaced, ';'); List<String> cmds = StringMan.split(replaced, ';');
for (String cmd : cmds) { for (String cmd : cmds) {
CommandEvent event = new CommandEvent( Player p = print ?
new LocationMaskedPlayerWrapper(player, player.getLocation().setPosition(position.toVector3()), false), new LocationMaskedPlayerWrapper(player, player.getLocation().setPosition(position.toVector3()), false) :
cmd new ScatterCommandPlayerWrapper(player, position);
); CommandEvent event = new CommandEvent(p, cmd, editSession);
PlatformCommandManager.getInstance().handleCommandOnCurrentThread(event); 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) {
}
}
} }

View File

@ -21,7 +21,6 @@ public class SilentPlayerWrapper extends AsyncPlayer {
@Override @Override
public void print(Component component) { public void print(Component component) {
super.print(component);
} }
@Override @Override

View File

@ -694,13 +694,15 @@ public class BrushCommands {
@Arg(desc = "double", def = "1") @Arg(desc = "double", def = "1")
double distance, double distance,
@Arg(desc = "List of comma-separated commands") @Arg(desc = "List of comma-separated commands")
List<String> commandStr List<String> commandStr,
@Switch(name = 'p', desc = "Show any printed output")
boolean print
) )
throws WorldEditException { throws WorldEditException {
worldEdit.checkMaxBrushRadius(radius); worldEdit.checkMaxBrushRadius(radius);
set( set(
context, context,
new ScatterCommand((int) points, (int) distance, StringMan.join(commandStr, " ")), new ScatterCommand((int) points, (int) distance, StringMan.join(commandStr, " "), print),
"worldedit.brush.scattercommand" "worldedit.brush.scattercommand"
) )
.setSize(radius); .setSize(radius);

View File

@ -19,10 +19,13 @@
package com.sk89q.worldedit.event.platform; package com.sk89q.worldedit.event.platform;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.event.AbstractCancellable; import com.sk89q.worldedit.event.AbstractCancellable;
import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
@ -32,6 +35,10 @@ public class CommandEvent extends AbstractCancellable {
private final Actor actor; private final Actor actor;
private final String arguments; private final String arguments;
//FAWE start
@Nullable
private final EditSession session;
//FAWE end
/** /**
* Create a new instance. * Create a new instance.
@ -45,8 +52,32 @@ public class CommandEvent extends AbstractCancellable {
this.actor = actor; this.actor = actor;
this.arguments = arguments; 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. * Get the actor that issued the command.
* *

View File

@ -795,7 +795,8 @@ public final class PlatformCommandManager {
} }
Optional<EditSession> editSessionOpt = context.injectedValue(Key.of(EditSession.class)); Optional<EditSession> 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 editSession = editSessionOpt.get();
editSession.flushQueue(); editSession.flushQueue();
session.remember(editSession); 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(boolean.class), context -> Optional.of(isSuggestions));
store.injectValue(Key.of(InjectedValueStore.class), ValueProvider.constant(store)); store.injectValue(Key.of(InjectedValueStore.class), ValueProvider.constant(store));
store.injectValue(Key.of(Event.class), ValueProvider.constant(event)); 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( return MemoizingValueAccess.wrap(
MergedValueAccess.of(store, globalInjectedValues) MergedValueAccess.of(store, globalInjectedValues)
); );