From 52a502a1c6da9ac54664b0d8c4cbd7beb4eeb162 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Thu, 21 Nov 2019 06:50:37 +0000 Subject: [PATCH] Refactor confirmation --- .../worldedit/bukkit/WorldEditPlugin.java | 26 +- .../com/boydti/fawe/command/FaweParser.java | 6 +- .../main/java/com/boydti/fawe/config/BBC.java | 3 +- .../fawe/object/brush/BrushSettings.java | 2 +- .../util/task/InterruptableCondition.java | 34 ++ .../worldedit/command/ClipboardCommands.java | 105 +++--- .../worldedit/command/GenerationCommands.java | 134 ++++---- .../worldedit/command/HistoryCommands.java | 38 +-- .../worldedit/command/RegionCommands.java | 313 ++++++++---------- .../worldedit/command/UtilityCommands.java | 6 +- .../worldedit/command/WorldEditCommands.java | 2 +- .../util/CommandQueuedConditionGenerator.java | 1 + .../command/util/annotation/Confirm.java | 136 ++++++++ .../util/{ => annotation}/SkipQueue.java | 3 +- .../com/sk89q/worldedit/entity/Player.java | 33 -- .../worldedit/event/AbstractCancellable.java | 2 +- .../worldedit/extension/platform/Actor.java | 153 ++++----- .../platform/PlatformCommandManager.java | 76 +++-- .../platform/binding/ConsumeBindings.java | 73 +++- .../internal/command/CommandUtil.java | 145 ++++---- .../internal/command/ConfirmHandler.java | 47 +++ .../internal/command/MethodInjector.java | 19 ++ 22 files changed, 782 insertions(+), 575 deletions(-) create mode 100644 worldedit-core/src/main/java/com/boydti/fawe/util/task/InterruptableCondition.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java rename worldedit-core/src/main/java/com/sk89q/worldedit/command/util/{ => annotation}/SkipQueue.java (73%) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/ConfirmHandler.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/MethodInjector.java diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 743026306..f5f2bd95c 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -341,19 +341,19 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter } } - @Override - public List onTabComplete(CommandSender sender, Command cmd, String commandLabel, String[] args) { - // Add the command to the array because the underlying command handling - // code of WorldEdit expects it - String[] split = new String[args.length + 1]; - System.arraycopy(args, 0, split, 1, args.length); - split[0] = commandLabel; - - String arguments = Joiner.on(" ").join(split); - CommandSuggestionEvent event = new CommandSuggestionEvent(wrapCommandSender(sender), arguments); - getWorldEdit().getEventBus().post(event); - return CommandUtil.fixSuggestions(arguments, event.getSuggestions()); - } +// @Override +// public List onTabComplete(CommandSender sender, Command cmd, String commandLabel, String[] args) { +// // Add the command to the array because the underlying command handling +// // code of WorldEdit expects it +// String[] split = new String[args.length + 1]; +// System.arraycopy(args, 0, split, 1, args.length); +// split[0] = commandLabel; +// +// String arguments = Joiner.on(" ").join(split); +// CommandSuggestionEvent event = new CommandSuggestionEvent(wrapCommandSender(sender), arguments); +// getWorldEdit().getEventBus().post(event); +// return CommandUtil.fixSuggestions(arguments, event.getSuggestions()); +// } private void fail(Runnable run, String message) { try { diff --git a/worldedit-core/src/main/java/com/boydti/fawe/command/FaweParser.java b/worldedit-core/src/main/java/com/boydti/fawe/command/FaweParser.java index 046dd6db3..caafa0c18 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/command/FaweParser.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/command/FaweParser.java @@ -4,9 +4,7 @@ import com.boydti.fawe.util.StringMan; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; -import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.PlatformCommandManager; -import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.internal.registry.InputParser; import org.enginehub.piston.inject.InjectedValueAccess; @@ -29,9 +27,9 @@ public abstract class FaweParser extends InputParser { input = prefix + " " + input; InjectedValueAccess injected = context.getInjected(); if (injected != null) { - return getPlatform().parse(input, injected); + return getPlatform().parseCommand(input, injected); } else { - return getPlatform().parse(input, context.getActor()); + return getPlatform().parseCommand(input, context.getActor()); } } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/config/BBC.java b/worldedit-core/src/main/java/com/boydti/fawe/config/BBC.java index 2872fefb9..012c6e886 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/config/BBC.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/config/BBC.java @@ -265,7 +265,8 @@ public enum BBC { WORLDEDIT_SOME_FAILS_BLOCKBAG("Missing blocks: %s0", "Error"), WORLDEDIT_CANCEL_COUNT("Cancelled %s0 edits.", "Cancel"), - WORLDEDIT_CANCEL_REASON_CONFIRM("Your selection is large (%s0 -> %s1, containing %s3 blocks). Use //confirm to execute %s2", "Cancel"), + WORLDEDIT_CANCEL_REASON_CONFIRM("Use //confirm to execute %s2", "Cancel"), + WORLDEDIT_CANCEL_REASON_CONFIRM_REGION("Your selection is large (%s0 -> %s1, containing %s3 blocks). Use //confirm to execute %s2", "Cancel"), WORLDEDIT_CANCEL_REASON("Your WorldEdit action was cancelled: %s0.", "Cancel"), WORLDEDIT_CANCEL_REASON_MANUAL("Manual cancellation", "Cancel"), WORLDEDIT_CANCEL_REASON_LOW_MEMORY("Low memory", "Cancel"), diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/BrushSettings.java b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/BrushSettings.java index 13ef9dc66..1051f13f4 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/brush/BrushSettings.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/brush/BrushSettings.java @@ -63,7 +63,7 @@ public class BrushSettings { if (constructor == null) { return new BrushSettings(); } - BrushSettings bs = manager.parse(constructor, player); + BrushSettings bs = manager.parseCommand(constructor, player); bs.constructor.put(SettingType.BRUSH, constructor); if (settings.containsKey(SettingType.PERMISSIONS.name())) { bs.permissions.addAll((Collection) settings.get(SettingType.PERMISSIONS.name())); diff --git a/worldedit-core/src/main/java/com/boydti/fawe/util/task/InterruptableCondition.java b/worldedit-core/src/main/java/com/boydti/fawe/util/task/InterruptableCondition.java new file mode 100644 index 000000000..b1aeeb7c3 --- /dev/null +++ b/worldedit-core/src/main/java/com/boydti/fawe/util/task/InterruptableCondition.java @@ -0,0 +1,34 @@ +package com.boydti.fawe.util.task; + +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class InterruptableCondition { + private final Condition condition; + private final Thread thread; + private final Lock lock; + + public InterruptableCondition(Lock lock, Condition condition, Thread thread) { + checkNotNull(condition); + checkNotNull(thread); + checkNotNull(lock); + this.lock = lock; + this.condition = condition; + this.thread = thread; + } + + public void signal() { + try { + lock.lock(); + condition.signalAll(); + } finally { + lock.unlock(); + } + } + + public void interrupt() { + this.thread.interrupt(); + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java index ff2efac91..b3fa826f5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java @@ -30,7 +30,6 @@ import com.boydti.fawe.object.RunnableVal; import com.boydti.fawe.object.clipboard.MultiClipboardHolder; import com.boydti.fawe.object.clipboard.ReadOnlyClipboard; import com.boydti.fawe.object.clipboard.URIClipboardHolder; -import com.boydti.fawe.object.clipboard.WorldCutClipboard; import com.boydti.fawe.object.exception.FaweException; import com.boydti.fawe.object.io.FastByteArrayOutputStream; import com.boydti.fawe.util.ImgurUtility; @@ -44,10 +43,10 @@ import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.command.util.Logging; +import com.sk89q.worldedit.command.util.annotation.Confirm; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.event.extent.PasteEvent; import com.sk89q.worldedit.extension.platform.Actor; -import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat; @@ -74,7 +73,6 @@ import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.RegionSelector; import com.sk89q.worldedit.regions.selector.CuboidRegionSelector; import com.sk89q.worldedit.session.ClipboardHolder; -import com.sk89q.worldedit.session.request.Request; import com.sk89q.worldedit.world.World; import org.enginehub.piston.annotation.Command; import org.enginehub.piston.annotation.CommandContainer; @@ -92,7 +90,6 @@ import java.net.URLEncoder; import java.nio.file.Files; import java.util.HashSet; import java.util.Set; -import java.util.function.Supplier; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION; @@ -113,6 +110,7 @@ public class ClipboardCommands { desc = "Copy the selection to the clipboard" ) @CommandPermissions("worldedit.clipboard.copy") + @Confirm(Confirm.Processor.REGION) public void copy(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Switch(name = 'e', desc = "Skip copy entities") @@ -120,7 +118,7 @@ public class ClipboardCommands { @Switch(name = 'b', desc = "Also copy biomes") boolean copyBiomes, @ArgFlag(name = 'm', desc = "Set the exclude mask, matching blocks become air", def = "") - Mask mask, InjectedValueAccess context) throws WorldEditException { + Mask mask) throws WorldEditException { BlockVector3 min = region.getMinimumPoint(); BlockVector3 max = region.getMaximumPoint(); @@ -130,33 +128,31 @@ public class ClipboardCommands { if (volume >= limit.MAX_CHECKS) { throw FaweCache.MAX_CHECKS; } - actor.checkConfirmationRegion(() -> { - session.setClipboard(null); + session.setClipboard(null); - Clipboard clipboard = new BlockArrayClipboard(region, actor.getUniqueId()); + Clipboard clipboard = new BlockArrayClipboard(region, actor.getUniqueId()); - session.setClipboard(new ClipboardHolder(clipboard)); + session.setClipboard(new ClipboardHolder(clipboard)); - clipboard.setOrigin(session.getPlacementPosition(actor)); - ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint()); - copy.setCopyingEntities(!skipEntities); - copy.setCopyingBiomes(copyBiomes); + clipboard.setOrigin(session.getPlacementPosition(actor)); + ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint()); + copy.setCopyingEntities(!skipEntities); + copy.setCopyingBiomes(copyBiomes); - Mask sourceMask = editSession.getSourceMask(); - if (sourceMask != null) { - new MaskTraverser(sourceMask).reset(editSession); - copy.setSourceMask(sourceMask); - editSession.setSourceMask(null); - } - if (mask != null && mask != Masks.alwaysTrue()) { - copy.setSourceMask(mask); - } - Operations.completeLegacy(copy); - BBC.COMMAND_COPY.send(actor, region.getArea()); - if (!actor.hasPermission("fawe.tips")) { - BBC.TIP_PASTE.or(BBC.TIP_DOWNLOAD, BBC.TIP_ROTATE, BBC.TIP_COPYPASTE, BBC.TIP_REPLACE_MARKER, BBC.TIP_COPY_PATTERN).send(actor); - } - }, "/copy", region, context); + Mask sourceMask = editSession.getSourceMask(); + if (sourceMask != null) { + new MaskTraverser(sourceMask).reset(editSession); + copy.setSourceMask(sourceMask); + editSession.setSourceMask(null); + } + if (mask != null && mask != Masks.alwaysTrue()) { + copy.setSourceMask(mask); + } + Operations.completeLegacy(copy); + BBC.COMMAND_COPY.send(actor, region.getArea()); + if (!actor.hasPermission("fawe.tips")) { + BBC.TIP_PASTE.or(BBC.TIP_DOWNLOAD, BBC.TIP_ROTATE, BBC.TIP_COPYPASTE, BBC.TIP_REPLACE_MARKER, BBC.TIP_COPY_PATTERN).send(actor); + } } @Command( @@ -229,6 +225,7 @@ public class ClipboardCommands { ) @CommandPermissions("worldedit.clipboard.cut") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void cut(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "Pattern to leave in place of the selection", def = "air") @@ -238,8 +235,7 @@ public class ClipboardCommands { @Switch(name = 'b', desc = "Also copy biomes, source biomes are unaffected") boolean copyBiomes, @ArgFlag(name = 'm', desc = "Set the exclude mask, matching blocks become air", def = "") - Mask mask, - InjectedValueAccess context) throws WorldEditException { + Mask mask) throws WorldEditException { BlockVector3 min = region.getMinimumPoint(); BlockVector3 max = region.getMaximumPoint(); @@ -251,35 +247,32 @@ public class ClipboardCommands { if (volume >= limit.MAX_CHANGES) { throw FaweCache.MAX_CHANGES; } - actor.checkConfirmationRegion(() -> { - session.setClipboard(null); + session.setClipboard(null); - BlockArrayClipboard clipboard = new BlockArrayClipboard(region, actor.getUniqueId()); - clipboard.setOrigin(session.getPlacementPosition(actor)); + BlockArrayClipboard clipboard = new BlockArrayClipboard(region, actor.getUniqueId()); + clipboard.setOrigin(session.getPlacementPosition(actor)); - ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint()); - copy.setSourceFunction(new BlockReplace(editSession, leavePattern)); - copy.setCopyingEntities(!skipEntities); - copy.setRemovingEntities(true); - copy.setCopyingBiomes(copyBiomes); - Mask sourceMask = editSession.getSourceMask(); - if (sourceMask != null) { - new MaskTraverser(sourceMask).reset(editSession); - copy.setSourceMask(sourceMask); - editSession.setSourceMask(null); - } - if (mask != null) { - copy.setSourceMask(mask); - } - Operations.completeLegacy(copy); - session.setClipboard(new ClipboardHolder(clipboard)); - - BBC.COMMAND_CUT_SLOW.send(actor, region.getArea()); - if (!actor.hasPermission("fawe.tips")) { - BBC.TIP_LAZYCUT.send(actor); - } - }, "cut", region, context); + ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint()); + copy.setSourceFunction(new BlockReplace(editSession, leavePattern)); + copy.setCopyingEntities(!skipEntities); + copy.setRemovingEntities(true); + copy.setCopyingBiomes(copyBiomes); + Mask sourceMask = editSession.getSourceMask(); + if (sourceMask != null) { + new MaskTraverser(sourceMask).reset(editSession); + copy.setSourceMask(sourceMask); + editSession.setSourceMask(null); + } + if (mask != null) { + copy.setSourceMask(mask); + } + Operations.completeLegacy(copy); + session.setClipboard(new ClipboardHolder(clipboard)); + BBC.COMMAND_CUT_SLOW.send(actor, region.getArea()); + if (!actor.hasPermission("fawe.tips")) { + BBC.TIP_LAZYCUT.send(actor); + } } @Command( diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java index 0f3f5a9df..7786feabd 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java @@ -37,6 +37,7 @@ import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.command.util.Logging; +import com.sk89q.worldedit.command.util.annotation.Confirm; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.function.generator.CavesGen; @@ -46,9 +47,7 @@ import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.visitor.RegionVisitor; -import com.sk89q.worldedit.internal.annotation.Radii; import com.sk89q.worldedit.internal.annotation.Selection; -import com.sk89q.worldedit.internal.expression.Expression; import com.sk89q.worldedit.internal.expression.ExpressionException; import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector3; @@ -95,6 +94,7 @@ public class GenerationCommands { ) @CommandPermissions("worldedit.generation.caves") @Logging(PLACEMENT) + @Confirm(Confirm.Processor.REGION) public void caves(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(name = "size", desc = "TODO", def = "8") int sizeOpt, @Arg(name = "frequency", desc = "TODO", def = "40") int frequencyOpt, @@ -105,12 +105,10 @@ public class GenerationCommands { @Arg(name = "individualRarity", desc = "TODO", def = "25") int individualRarityOpt, @Arg(name = "pocketChance", desc = "TODO", def = "0") int pocketChanceOpt, @Arg(name = "pocketMin", desc = "TODO", def = "0") int pocketMinOpt, - @Arg(name = "pocketMax", desc = "TODO", def = "3") int pocketMaxOpt, InjectedValueAccess context) throws WorldEditException { - actor.checkConfirmationRegion(() -> { - CavesGen gen = new CavesGen(sizeOpt, frequencyOpt, rarityOpt, minYOpt, maxYOpt, systemFrequencyOpt, individualRarityOpt, pocketChanceOpt, pocketMinOpt, pocketMaxOpt); - editSession.generate(region, gen); - BBC.VISITOR_BLOCK.send(actor, editSession.getBlockChangeCount()); - }, "/caves", region, context); + @Arg(name = "pocketMax", desc = "TODO", def = "3") int pocketMaxOpt) throws WorldEditException { + CavesGen gen = new CavesGen(sizeOpt, frequencyOpt, rarityOpt, minYOpt, maxYOpt, systemFrequencyOpt, individualRarityOpt, pocketChanceOpt, pocketMinOpt, pocketMaxOpt); + editSession.generate(region, gen); + BBC.VISITOR_BLOCK.send(actor, editSession.getBlockChangeCount()); } @@ -120,11 +118,10 @@ public class GenerationCommands { ) @CommandPermissions("worldedit.generation.ore") @Logging(PLACEMENT) - public void ores(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "Mask") Mask mask, InjectedValueAccess context) throws WorldEditException { - actor.checkConfirmationRegion(() -> { - editSession.addOres(region, mask); - BBC.VISITOR_BLOCK.send(actor, editSession.getBlockChangeCount()); - }, "/ores", region, context); + @Confirm(Confirm.Processor.REGION) + public void ores(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "Mask") Mask mask) throws WorldEditException { + editSession.addOres(region, mask); + BBC.VISITOR_BLOCK.send(actor, editSession.getBlockChangeCount()); } @Command( @@ -174,11 +171,10 @@ public class GenerationCommands { ) @CommandPermissions("worldedit.generation.ore") @Logging(PLACEMENT) - public void ore(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "Mask") Mask mask, @Arg(desc = "Pattern") Pattern material, @Arg(desc="Ore vein size") @Range(from = 0, to=Integer.MAX_VALUE) int size, int freq, @Range(from=0, to=100) int rarity, @Range(from=0, to=255) int minY, @Range(from=0, to=255) int maxY, InjectedValueAccess context) throws WorldEditException { - actor.checkConfirmationRegion(() -> { - editSession.addOre(region, mask, material, size, freq, rarity, minY, maxY); - BBC.VISITOR_BLOCK.send(actor, editSession.getBlockChangeCount()); - }, "/ore", region, context); + @Confirm(Confirm.Processor.REGION) + public void ore(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "Mask") Mask mask, @Arg(desc = "Pattern") Pattern material, @Arg(desc="Ore vein size") @Range(from = 0, to=Integer.MAX_VALUE) int size, int freq, @Range(from=0, to=100) int rarity, @Range(from=0, to=255) int minY, @Range(from=0, to=255) int maxY) throws WorldEditException { + editSession.addOre(region, mask, material, size, freq, rarity, minY, maxY); + BBC.VISITOR_BLOCK.send(actor, editSession.getBlockChangeCount()); } @Command( @@ -187,14 +183,14 @@ public class GenerationCommands { ) @CommandPermissions("worldedit.generation.cylinder") @Logging(PLACEMENT) - public void hcyl(Actor actor, LocalSession session, EditSession editSession, InjectedValueAccess context, + public void hcyl(Actor actor, LocalSession session, EditSession editSession, @Arg(desc = "The pattern of blocks to generate") Pattern pattern, - @Arg(desc = "The radii of the cylinder. 1st is N/S, 2nd is E/W") + @Confirm(Confirm.Processor.RADIUS) @Arg(desc = "The radii of the cylinder. 1st is N/S, 2nd is E/W") BlockVector2 radii, @Arg(desc = "The height of the cylinder", def = "1") int height) throws WorldEditException { - cyl(actor, session, editSession, pattern, radii, height, true, context); + cyl(actor, session, editSession, pattern, radii, height, true); } @Command( @@ -206,18 +202,16 @@ public class GenerationCommands { public void cyl(Actor actor, LocalSession session, EditSession editSession, @Arg(desc = "The pattern of blocks to generate") Pattern pattern, - @Arg(desc = "The radii of the cylinder. Order is N/S, E/W") BlockVector2 radius, + @Confirm(Confirm.Processor.RADIUS) @Arg(desc = "The radii of the cylinder. Order is N/S, E/W") BlockVector2 radius, @Arg(desc = "The height of the cylinder", def = "1") int height, @Switch(name = 'h', desc = "Make a hollow cylinder") - boolean hollow, InjectedValueAccess context) throws WorldEditException { + boolean hollow) throws WorldEditException { double max = Math.max(radius.getBlockX(), radius.getBlockZ()); worldEdit.checkMaxRadius(max); BlockVector3 pos = session.getPlacementPosition(actor); - actor.checkConfirmationRadius(() -> { - int affected = editSession.makeCylinder(pos, pattern, radius.getX(), radius.getZ(), Math.min(256, height), !hollow); - BBC.VISITOR_BLOCK.send(actor, affected); - }, "/cyl", (int) max, context); + int affected = editSession.makeCylinder(pos, pattern, radius.getX(), radius.getZ(), Math.min(256, height), !hollow); + BBC.VISITOR_BLOCK.send(actor, affected); } @Command( @@ -229,11 +223,10 @@ public class GenerationCommands { public void hsphere(Actor actor, LocalSession session, EditSession editSession, @Arg(desc = "The pattern of blocks to generate") Pattern pattern, - @Arg(desc = "The radii of the sphere. Order is N/S, U/D, E/W") BlockVector3 radii, + @Confirm(Confirm.Processor.RADIUS) @Arg(desc = "The radii of the sphere. Order is N/S, U/D, E/W") BlockVector3 radii, @Switch(name = 'r', desc = "Raise the bottom of the sphere to the placement position") - boolean raised, - InjectedValueAccess context) throws WorldEditException { - sphere(actor, session, editSession, pattern, radii, raised, true, context); + boolean raised) throws WorldEditException { + sphere(actor, session, editSession, pattern, radii, raised, true); } @Command( @@ -245,23 +238,21 @@ public class GenerationCommands { public void sphere(Actor actor, LocalSession session, EditSession editSession, @Arg(desc = "The pattern of blocks to generate") Pattern pattern, - @Arg(desc = "The radii of the sphere. Order is N/S, U/D, E/W") + @Confirm(Confirm.Processor.RADIUS) @Arg(desc = "The radii of the sphere. Order is N/S, U/D, E/W") BlockVector3 radii, @Switch(name = 'r', desc = "Raise the bottom of the sphere to the placement position") boolean raised, @Switch(name = 'h', desc = "Make a hollow sphere") - boolean hollow, InjectedValueAccess context) throws WorldEditException { + boolean hollow) throws WorldEditException { double max = MathMan.max(radii.getBlockX(), radii.getBlockY(), radii.getBlockZ()); worldEdit.checkMaxRadius(max); BlockVector3 pos = session.getPlacementPosition(actor); BlockVector3 finalPos = raised ? pos.add(0, radii.getY(), 0) : pos; - actor.checkConfirmationRadius(() -> { - int affected = editSession.makeSphere(finalPos, pattern, radii.getX(), radii.getY(), radii.getZ(), !hollow); - if (actor instanceof Player) { - ((Player) actor).findFreePosition(); - } - BBC.VISITOR_BLOCK.send(actor, affected); - }, "sphere", (int) max, context); + int affected = editSession.makeSphere(finalPos, pattern, radii.getX(), radii.getY(), radii.getZ(), !hollow); + if (actor instanceof Player) { + ((Player) actor).findFreePosition(); + } + BBC.VISITOR_BLOCK.send(actor, affected); } @Command( @@ -312,9 +303,9 @@ public class GenerationCommands { public void hollowPyramid(Actor actor, LocalSession session, EditSession editSession, @Arg(desc = "The pattern of blocks to set") Pattern pattern, - @Arg(desc = "The size of the pyramid") - int size, InjectedValueAccess context) throws WorldEditException { - pyramid(actor, session, editSession, pattern, size, true, context); + @Confirm(Confirm.Processor.RADIUS) @Arg(desc = "The size of the pyramid") + int size) throws WorldEditException { + pyramid(actor, session, editSession, pattern, size, true); } @Command( @@ -326,20 +317,17 @@ public class GenerationCommands { public void pyramid(Actor actor, LocalSession session, EditSession editSession, @Arg(desc = "The pattern of blocks to set") Pattern pattern, - @Arg(desc = "The size of the pyramid") + @Confirm(Confirm.Processor.RADIUS) @Arg(desc = "The size of the pyramid") int size, @Switch(name = 'h', desc = "Make a hollow pyramid") - boolean hollow, - InjectedValueAccess context) throws WorldEditException { + boolean hollow) throws WorldEditException { BlockVector3 pos = session.getPlacementPosition(actor); worldEdit.checkMaxRadius(size); - actor.checkConfirmationRadius(() -> { - int affected = editSession.makePyramid(pos, pattern, size, !hollow); - if (actor instanceof Player) { - ((Player) actor).findFreePosition(); - } - BBC.VISITOR_BLOCK.send(actor, affected); - }, getArguments(context), size, context); + int affected = editSession.makePyramid(pos, pattern, size, !hollow); + if (actor instanceof Player) { + ((Player) actor).findFreePosition(); + } + BBC.VISITOR_BLOCK.send(actor, affected); } @Command( @@ -350,6 +338,7 @@ public class GenerationCommands { ) @CommandPermissions("worldedit.generation.shape") @Logging(ALL) + @Confirm(Confirm.Processor.REGION) public void generate(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "The pattern of blocks to set") @@ -363,8 +352,7 @@ public class GenerationCommands { @Switch(name = 'o', desc = "Use the placement's coordinate origin") boolean offset, @Switch(name = 'c', desc = "Use the selection's center as origin") - boolean offsetCenter, - InjectedValueAccess context) throws WorldEditException { + boolean offsetCenter) throws WorldEditException { final Vector3 zero; Vector3 unit; @@ -395,17 +383,15 @@ public class GenerationCommands { final Vector3 unit1 = unit; - actor.checkConfirmationRegion(() -> { - try { - final int affected = editSession.makeShape(region, zero, unit1, pattern, String.join(" ", expression), hollow, session.getTimeout()); - if (actor instanceof Player) { - ((Player) actor).findFreePosition(); - } - BBC.VISITOR_BLOCK.send(actor, affected); - } catch (ExpressionException e) { - actor.printError(e.getMessage()); + try { + final int affected = editSession.makeShape(region, zero, unit1, pattern, String.join(" ", expression), hollow, session.getTimeout()); + if (actor instanceof Player) { + ((Player) actor).findFreePosition(); } - }, "/generate", region, context); + BBC.VISITOR_BLOCK.send(actor, affected); + } catch (ExpressionException e) { + actor.printError(e.getMessage()); + } } @Command( @@ -418,6 +404,7 @@ public class GenerationCommands { ) @CommandPermissions("worldedit.generation.shape.biome") @Logging(ALL) + @Confirm(Confirm.Processor.REGION) public void generateBiome(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "The biome type to set") @@ -431,8 +418,7 @@ public class GenerationCommands { @Switch(name = 'o', desc = "Use the placement's coordinate origin") boolean offset, @Switch(name = 'c', desc = "Use the selection's center as origin") - boolean offsetCenter, - InjectedValueAccess context) throws WorldEditException { + boolean offsetCenter) throws WorldEditException { final Vector3 zero; Vector3 unit; @@ -461,14 +447,12 @@ public class GenerationCommands { } final Vector3 unit1 = unit; - actor.checkConfirmationRegion(() -> { - try { - final int affected = editSession.makeBiomeShape(region, zero, unit1, target, String.join(" ", expression), hollow, session.getTimeout()); - BBC.VISITOR_FLAT.send(actor, affected); - } catch (ExpressionException e) { - actor.printError(e.getMessage()); - } - }, "/generatebiome", region, context); + try { + final int affected = editSession.makeBiomeShape(region, zero, unit1, target, String.join(" ", expression), hollow, session.getTimeout()); + BBC.VISITOR_FLAT.send(actor, affected); + } catch (ExpressionException e) { + actor.printError(e.getMessage()); + } } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java index 2f7ac5526..6fdf3a8a7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java @@ -38,6 +38,7 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; +import com.sk89q.worldedit.command.util.annotation.Confirm; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.math.BlockVector3; @@ -225,11 +226,10 @@ public class HistoryCommands { ) @CommandPermissions({"worldedit.history.undo", "worldedit.history.undo.self"}) public void undo(Player player, LocalSession session, - @Arg(desc = "Number of undoes to perform", def = "1") + @Confirm(Confirm.Processor.LIMIT) @Arg(desc = "Number of undoes to perform", def = "1") int times, @Arg(name = "player", desc = "Undo this player's operations", def = "") - String playerName, - InjectedValueAccess context) throws WorldEditException { + String playerName) throws WorldEditException { times = Math.max(1, times); LocalSession undoSession; if (session.hasFastMode()) { @@ -247,22 +247,20 @@ public class HistoryCommands { undoSession = session; } int finalTimes = times; - player.checkConfirmation(() -> { - EditSession undone = null; - int i = 0; - for (; i < finalTimes; ++i) { - undone = undoSession.undo(undoSession.getBlockBag(player), player); - if (undone == null) break; - worldEdit.flushBlockBag(player, undone); - } - if (undone == null) i--; - if (i > 0) { - BBC.COMMAND_UNDO_SUCCESS.send(player, i == 1 ? "" : " x" + i); - } - if (undone == null) { - player.printError(BBC.COMMAND_UNDO_ERROR.s()); - } - }, "undo", times, 50, context); + EditSession undone = null; + int i = 0; + for (; i < finalTimes; ++i) { + undone = undoSession.undo(undoSession.getBlockBag(player), player); + if (undone == null) break; + worldEdit.flushBlockBag(player, undone); + } + if (undone == null) i--; + if (i > 0) { + BBC.COMMAND_UNDO_SUCCESS.send(player, i == 1 ? "" : " x" + i); + } + if (undone == null) { + player.printError(BBC.COMMAND_UNDO_ERROR.s()); + } } @Command( @@ -272,7 +270,7 @@ public class HistoryCommands { ) @CommandPermissions({"worldedit.history.redo", "worldedit.history.redo.self"}) public void redo(Player player, LocalSession session, - @Arg(desc = "Number of redoes to perform", def = "1") + @Confirm(Confirm.Processor.LIMIT) @Arg(desc = "Number of redoes to perform", def = "1") int times, @Arg(name = "player", desc = "Redo this player's operations", def = "") String playerName) throws WorldEditException { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java index 9b785c15c..d17b87a6a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java @@ -19,39 +19,29 @@ package com.sk89q.worldedit.command; -import com.google.common.base.Joiner; - import com.boydti.fawe.FaweAPI; import com.boydti.fawe.FaweCache; -import com.boydti.fawe.beta.implementation.processors.ChunkSendProcessor; -import com.boydti.fawe.beta.implementation.processors.NullProcessor; import com.boydti.fawe.config.BBC; import com.boydti.fawe.object.FaweLimit; -import com.google.common.collect.Lists; import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; -import com.sk89q.worldedit.function.RegionFunction; -import com.sk89q.worldedit.function.block.BlockReplace; +import com.sk89q.worldedit.command.util.annotation.Confirm; import com.sk89q.worldedit.command.util.Logging; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Actor; -import com.sk89q.worldedit.function.FlatRegionFunction; import com.sk89q.worldedit.function.GroundFunction; -import com.sk89q.worldedit.function.biome.BiomeReplace; import com.sk89q.worldedit.function.generator.FloraGenerator; import com.sk89q.worldedit.function.mask.MaskIntersection; import com.sk89q.worldedit.function.mask.ExistingBlockMask; import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.NoiseFilter2D; -import com.sk89q.worldedit.function.visitor.RegionVisitor; import com.sk89q.worldedit.function.mask.SolidBlockMask; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.pattern.Pattern; -import com.sk89q.worldedit.function.visitor.FlatRegionVisitor; import com.sk89q.worldedit.function.visitor.LayerVisitor; import com.sk89q.worldedit.internal.annotation.Direction; import com.sk89q.worldedit.internal.annotation.Selection; @@ -85,13 +75,13 @@ import org.enginehub.piston.inject.InjectedValueAccess; import org.jetbrains.annotations.Range; import java.util.ArrayList; -import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.stream.Stream; -import static com.sk89q.worldedit.command.MethodCommands.getArguments; + import static com.sk89q.worldedit.command.util.Logging.LogMode.ORIENTATION_REGION; import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION; +import static com.sk89q.worldedit.command.util.annotation.Confirm.Processor.ALWAYS; +import static com.sk89q.worldedit.command.util.annotation.Confirm.Processor.RADIUS; import static com.sk89q.worldedit.internal.command.CommandUtil.checkCommandArgument; import static com.sk89q.worldedit.regions.Regions.asFlatRegion; import static com.sk89q.worldedit.regions.Regions.maximumBlockY; @@ -116,19 +106,17 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.set") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public int set(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "The pattern of blocks to set") - Pattern pattern, - InjectedValueAccess context) { - actor.checkConfirmationRegion(() -> { - int affected = editSession.setBlocks(region, pattern); - if (affected != 0) { - BBC.OPERATION.send(actor, affected); - if (!actor.hasPermission("fawe.tips")) - BBC.TIP_FAST.or(BBC.TIP_CANCEL, BBC.TIP_MASK, BBC.TIP_MASK_ANGLE, BBC.TIP_SET_LINEAR, BBC.TIP_SURFACE_SPREAD, BBC.TIP_SET_HAND).send(actor); - } - }, getArguments(context), region, context); + Pattern pattern) { + int affected = editSession.setBlocks(region, pattern); + if (affected != 0) { + BBC.OPERATION.send(actor, affected); + if (!actor.hasPermission("fawe.tips")) + BBC.TIP_FAST.or(BBC.TIP_CANCEL, BBC.TIP_MASK, BBC.TIP_MASK_ANGLE, BBC.TIP_SET_LINEAR, BBC.TIP_SURFACE_SPREAD, BBC.TIP_SET_HAND).send(actor); + } return 0; } @@ -139,10 +127,8 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.set") @Logging(REGION) - public void air(Actor actor, EditSession editSession, - @Selection Region region, - InjectedValueAccess context) throws WorldEditException { - set(actor, editSession, region, BlockTypes.AIR, context); + public void air(Actor actor, EditSession editSession, @Selection Region region) throws WorldEditException { + set(actor, editSession, region, BlockTypes.AIR); } @Command( @@ -151,12 +137,13 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.test") @Logging(REGION) - public void test(Player player, EditSession editSession, @Selection Region region, @Arg(desc = "hello there") BiomeType biome) throws WorldEditException { - editSession.addProcessor(new ChunkSendProcessor(editSession.getWorld(), () -> Collections.singleton(player))); - editSession.addProcessor(NullProcessor.INSTANCE); - FlatRegionFunction replace = new BiomeReplace(editSession, biome); - FlatRegionVisitor visitor = new FlatRegionVisitor(Regions.asFlatRegion(region), replace); - Operations.completeLegacy(visitor); + public void test(Player player, EditSession editSession, @Arg(desc = "test") @Confirm(RADIUS) BlockVector3 radius) throws WorldEditException { + player.print("Run test " + radius); +// editSession.addProcessor(new ChunkSendProcessor(editSession.getWorld(), () -> Collections.singleton(player))); +// editSession.addProcessor(NullProcessor.INSTANCE); +// FlatRegionFunction replace = new BiomeReplace(editSession, biome); +// FlatRegionVisitor visitor = new FlatRegionVisitor(Regions.asFlatRegion(region), replace); +// Operations.completeLegacy(visitor); } @Command( @@ -279,6 +266,7 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.curve") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void curve(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "The pattern of blocks to place") @@ -286,21 +274,19 @@ public class RegionCommands { @Arg(desc = "The thickness of the curve", def = "0") int thickness, @Switch(name = 'h', desc = "Generate only a shell") - boolean shell, InjectedValueAccess context) throws WorldEditException { + boolean shell) throws WorldEditException { if (!(region instanceof ConvexPolyhedralRegion)) { actor.printError("//curve only works with convex polyhedral selections"); return; } checkCommandArgument(thickness >= 0, "Thickness must be >= 0"); - actor.checkConfirmationRegion(() -> { ConvexPolyhedralRegion cpregion = (ConvexPolyhedralRegion) region; List vectors = new ArrayList<>(cpregion.getVertices()); int blocksChanged = editSession.drawSpline(pattern, vectors, 0, 0, 0, 10, thickness, !shell); actor.print(blocksChanged + " block(s) have been changed."); - }, getArguments(context), region, context); } @Command( @@ -310,19 +296,18 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.replace") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void replace(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "The mask representing blocks to replace", def = "") Mask from, @Arg(desc = "The pattern of blocks to replace with") - Pattern to, InjectedValueAccess context) throws WorldEditException { + Pattern to) throws WorldEditException { if (from == null) { from = new ExistingBlockMask(editSession); } Mask finalFrom = from; - actor.checkConfirmationRegion(() -> { int affected = editSession.replaceBlocks(region, finalFrom, to); actor.print(affected + " block(s) have been replaced."); - }, getArguments(context), region, context); } @Command( @@ -331,13 +316,12 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.overlay") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void overlay(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "The pattern of blocks to overlay") - Pattern pattern, InjectedValueAccess context) throws WorldEditException { - actor.checkConfirmationRegion(() -> { + Pattern pattern) throws WorldEditException { int affected = editSession.overlayCuboidBlocks(region, pattern); actor.print(affected + " block(s) have been overlaid."); - }, getArguments(context), region, context); } @Command( @@ -346,24 +330,23 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.overlay") @Logging(REGION) - public void lay(Player player, EditSession editSession, @Selection Region region, @Arg(name = "pattern", desc = "The pattern of blocks to lay") Pattern patternArg, InjectedValueAccess context) throws WorldEditException { - player.checkConfirmationRegion(() -> { - BlockVector3 max = region.getMaximumPoint(); - int maxY = max.getBlockY(); - Iterable flat = Regions.asFlatRegion(region).asFlatRegion(); - Iterator iter = flat.iterator(); - int y = 0; - int affected = 0; - while (iter.hasNext()) { - BlockVector2 pos = iter.next(); - int x = pos.getBlockX(); - int z = pos.getBlockZ(); - y = editSession.getNearestSurfaceTerrainBlock(x, z, y, 0, maxY); - editSession.setBlock(x, y, z, patternArg); - affected++; - } - BBC.VISITOR_BLOCK.send(player, affected); - }, getArguments(context), region, context); + @Confirm(Confirm.Processor.REGION) + public void lay(Player player, EditSession editSession, @Selection Region region, @Arg(name = "pattern", desc = "The pattern of blocks to lay") Pattern patternArg) throws WorldEditException { + BlockVector3 max = region.getMaximumPoint(); + int maxY = max.getBlockY(); + Iterable flat = Regions.asFlatRegion(region).asFlatRegion(); + Iterator iter = flat.iterator(); + int y = 0; + int affected = 0; + while (iter.hasNext()) { + BlockVector2 pos = iter.next(); + int x = pos.getBlockX(); + int z = pos.getBlockZ(); + y = editSession.getNearestSurfaceTerrainBlock(x, z, y, 0, maxY); + editSession.setBlock(x, y, z, patternArg); + affected++; + } + BBC.VISITOR_BLOCK.send(player, affected); } @Command( @@ -387,11 +370,10 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.naturalize") @Logging(REGION) - public void naturalize(Actor actor, EditSession editSession, @Selection Region region, InjectedValueAccess context) throws WorldEditException { - actor.checkConfirmationRegion(() -> { - int affected = editSession.naturalizeCuboidBlocks(region); - actor.print(affected + " block(s) have been made to look more natural."); - }, getArguments(context), region, context); + @Confirm(Confirm.Processor.REGION) + public void naturalize(Actor actor, EditSession editSession, @Selection Region region) throws WorldEditException { + int affected = editSession.naturalizeCuboidBlocks(region); + actor.print(affected + " block(s) have been made to look more natural."); } @Command( @@ -400,13 +382,12 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.walls") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void walls(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "The pattern of blocks to set") - Pattern pattern, InjectedValueAccess context) throws WorldEditException { - actor.checkConfirmationRegion(() -> { - int affected = editSession.makeWalls(region, pattern); - actor.print(affected + " block(s) have been changed."); - }, getArguments(context), region, context); + Pattern pattern) throws WorldEditException { + int affected = editSession.makeWalls(region, pattern); + actor.print(affected + " block(s) have been changed."); } @Command( @@ -416,13 +397,12 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.faces") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void faces(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "The pattern of blocks to set") - Pattern pattern, InjectedValueAccess context) throws WorldEditException { - actor.checkConfirmationRegion(() -> { - int affected = editSession.makeCuboidFaces(region, pattern); - actor.print(affected + " block(s) have been changed."); - }, getArguments(context), region, context); + Pattern pattern) throws WorldEditException { + int affected = editSession.makeCuboidFaces(region, pattern); + actor.print(affected + " block(s) have been changed."); } @Command( @@ -432,12 +412,13 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.smooth") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void smooth(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "# of iterations to perform", def = "1") int iterations, @Arg(desc = "The mask of blocks to use as the height map", def = "") Mask mask, - @Switch(name = 's', desc = "TODO") boolean snow, InjectedValueAccess context) throws WorldEditException { + @Switch(name = 's', desc = "TODO") boolean snow) throws WorldEditException { BlockVector3 min = region.getMinimumPoint(); BlockVector3 max = region.getMaximumPoint(); long volume = (((long) max.getX() - (long) min.getX() + 1) * ((long) max.getY() - (long) min.getY() + 1) * ((long) max.getZ() - (long) min.getZ() + 1)); @@ -445,16 +426,14 @@ public class RegionCommands { if (volume >= limit.MAX_CHECKS) { throw FaweCache.MAX_CHECKS; } - actor.checkConfirmationRegion(() -> { - try { - HeightMap heightMap = new HeightMap(editSession, region, mask, snow); - HeightMapFilter filter = new HeightMapFilter(new GaussianKernel(5, 1.0)); - int affected = heightMap.applyFilter(filter, iterations); - actor.print("Terrain's height map smoothed. " + affected + " block(s) changed."); - } catch (Throwable e) { - throw new RuntimeException(e); - } - }, getArguments(context), region, context); + try { + HeightMap heightMap = new HeightMap(editSession, region, mask, snow); + HeightMapFilter filter = new HeightMapFilter(new GaussianKernel(5, 1.0)); + int affected = heightMap.applyFilter(filter, iterations); + actor.print("Terrain's height map smoothed. " + affected + " block(s) changed."); + } catch (Throwable e) { + throw new RuntimeException(e); + } } @Command( @@ -497,6 +476,7 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.move") @Logging(ORIENTATION_REGION) + @Confirm(Confirm.Processor.REGION) public void move(Actor actor, World world, EditSession editSession, LocalSession session, @Selection Region region, @Arg(desc = "# of blocks to move", def = "1") @@ -515,8 +495,7 @@ public class RegionCommands { @Switch(name = 'b', desc = "Also copy biomes") boolean copyBiomes, @ArgFlag(name = 'm', desc = "Set the include mask, non-matching blocks become air", def = "") - Mask mask, - InjectedValueAccess context) throws WorldEditException { + Mask mask) throws WorldEditException { checkCommandArgument(count >= 1, "Count must be >= 1"); Mask combinedMask; @@ -530,22 +509,20 @@ public class RegionCommands { combinedMask = mask; } - actor.checkConfirmationRegion(() -> { - int affected = editSession.moveRegion(region, direction, count, !skipEntities, copyBiomes, combinedMask, replace); + int affected = editSession.moveRegion(region, direction, count, !skipEntities, copyBiomes, combinedMask, replace); - if (moveSelection) { - try { - region.shift(direction.multiply(count)); + if (moveSelection) { + try { + region.shift(direction.multiply(count)); - session.getRegionSelector(world).learnChanges(); - session.getRegionSelector(world).explainRegionAdjust(actor, session); - } catch (RegionOperationException e) { - actor.printError(e.getMessage()); - } + session.getRegionSelector(world).learnChanges(); + session.getRegionSelector(world).explainRegionAdjust(actor, session); + } catch (RegionOperationException e) { + actor.printError(e.getMessage()); } + } - BBC.VISITOR_BLOCK.send(actor, affected); - }, getArguments(context), region, context); + BBC.VISITOR_BLOCK.send(actor, affected); } @Command( @@ -556,15 +533,13 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.fall") @Logging(ORIENTATION_REGION) + @Confirm(Confirm.Processor.REGION) public void fall(Player player, EditSession editSession, LocalSession session, @Selection Region region, @Arg(desc = "BlockStateHolder", def = "air") BlockStateHolder replace, - @Switch(name = 'm', desc = "TODO") boolean notFullHeight, - InjectedValueAccess context) throws WorldEditException { - player.checkConfirmationRegion(() -> { - int affected = editSession.fall(region, !notFullHeight, replace); - BBC.VISITOR_BLOCK.send(player, affected); - }, getArguments(context), region, context); + @Switch(name = 'm', desc = "TODO") boolean notFullHeight) throws WorldEditException { + int affected = editSession.fall(region, !notFullHeight, replace); + BBC.VISITOR_BLOCK.send(player, affected); } @Command( @@ -576,7 +551,7 @@ public class RegionCommands { public void stack(Actor actor, World world, EditSession editSession, LocalSession session, @Selection Region region, @Arg(desc = "# of copies to stack", def = "1") - int count, + @Confirm(Confirm.Processor.REGION) int count, @Arg(desc = "The direction to stack", def = Direction.AIM) @Direction(includeDiagonals = true) BlockVector3 direction, @@ -589,8 +564,7 @@ public class RegionCommands { @Switch(name = 'b', desc = "Also copy biomes") boolean copyBiomes, @ArgFlag(name = 'm', desc = "Source mask", def="") - Mask mask, - InjectedValueAccess context) throws WorldEditException { + Mask mask) throws WorldEditException { Mask combinedMask; if (ignoreAirBlocks) { @@ -603,25 +577,23 @@ public class RegionCommands { combinedMask = mask; } - actor.checkConfirmationStack(() -> { - int affected = editSession.stackCuboidRegion(region, direction, count, !skipEntities, copyBiomes, combinedMask); + int affected = editSession.stackCuboidRegion(region, direction, count, !skipEntities, copyBiomes, combinedMask); - if (moveSelection) { - try { - final BlockVector3 size = region.getMaximumPoint().subtract(region.getMinimumPoint()).add(1, 1, 1); + if (moveSelection) { + try { + final BlockVector3 size = region.getMaximumPoint().subtract(region.getMinimumPoint()).add(1, 1, 1); - final BlockVector3 shiftVector = direction.multiply(size).multiply(count); - region.shift(shiftVector); + final BlockVector3 shiftVector = direction.multiply(size).multiply(count); + region.shift(shiftVector); - session.getRegionSelector(world).learnChanges(); - session.getRegionSelector(world).explainRegionAdjust(actor, session); - } catch (RegionOperationException e) { - actor.printError(e.getMessage()); - } + session.getRegionSelector(world).learnChanges(); + session.getRegionSelector(world).explainRegionAdjust(actor, session); + } catch (RegionOperationException e) { + actor.printError(e.getMessage()); } + } - BBC.VISITOR_BLOCK.send(actor, affected); - }, getArguments(context), region, count, context); + BBC.VISITOR_BLOCK.send(actor, affected); } @Command( @@ -633,7 +605,8 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.deform") @Logging(ALL) - public void deform(Actor actor, LocalSession session, EditSession editSession, InjectedValueAccess context, + @Confirm(Confirm.Processor.REGION) + public void deform(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "The expression to use", variable = true) List expression, @@ -663,17 +636,15 @@ public class RegionCommands { } final Vector3 unit1 = unit; - actor.checkConfirmationRegion(() -> { - try { - final int affected = editSession.deformRegion(region, zero, unit1, String.join(" ", expression), session.getTimeout()); - if (actor instanceof Player) { - ((Player) actor).findFreePosition(); - } - actor.print(affected + " block(s) have been deformed."); - } catch (ExpressionException e) { - actor.printError(e.getMessage()); + try { + final int affected = editSession.deformRegion(region, zero, unit1, String.join(" ", expression), session.getTimeout()); + if (actor instanceof Player) { + ((Player) actor).findFreePosition(); } - }, getArguments(context), region, context); + actor.print(affected + " block(s) have been deformed."); + } catch (ExpressionException e) { + actor.printError(e.getMessage()); + } } @Command( @@ -686,31 +657,27 @@ public class RegionCommands { ) @CommandPermissions("worldedit.regen") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void regenerateChunk(Player player, LocalSession session, EditSession editSession, @Selection Region region, @Arg(def = "", desc = "Regenerate with biome") BiomeType biome, - @Arg(def = "", desc = "Regenerate with seed") Long seed, - InjectedValueAccess context) throws WorldEditException { - player.checkConfirmationRegion(() -> { - Mask mask = session.getMask(); - session.setMask((Mask) null); - session.setSourceMask((Mask) null); - editSession.regenerate(region, biome, seed); - session.setMask(mask); - session.setSourceMask(mask); - if (!player.hasPermission("fawe.tips")) { - player.print(BBC.COMMAND_REGEN_2.s()); - } else if (biome == null) { - BBC.COMMAND_REGEN_0.send(player); - if (!player.hasPermission("fawe.tips")) player.print(BBC.TIP_REGEN_0.s()); - } else if (seed == null) { - player.print(BBC.COMMAND_REGEN_1.s()); - if (!player.hasPermission("fawe.tips")) BBC.TIP_REGEN_1.send(player); - } else { - player.print(BBC.COMMAND_REGEN_2.s()); - } - }, getArguments(context), region, context); - - + @Arg(def = "", desc = "Regenerate with seed") Long seed) throws WorldEditException { + Mask mask = session.getMask(); + session.setMask((Mask) null); + session.setSourceMask((Mask) null); + editSession.regenerate(region, biome, seed); + session.setMask(mask); + session.setSourceMask(mask); + if (!player.hasPermission("fawe.tips")) { + player.print(BBC.COMMAND_REGEN_2.s()); + } else if (biome == null) { + BBC.COMMAND_REGEN_0.send(player); + if (!player.hasPermission("fawe.tips")) player.print(BBC.TIP_REGEN_0.s()); + } else if (seed == null) { + player.print(BBC.COMMAND_REGEN_1.s()); + if (!player.hasPermission("fawe.tips")) BBC.TIP_REGEN_1.send(player); + } else { + player.print(BBC.COMMAND_REGEN_2.s()); + } } @Command( @@ -723,20 +690,18 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.hollow") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void hollow(Actor actor, EditSession editSession, @Selection Region region, @Range(from=0, to=Integer.MAX_VALUE) @Arg(desc = "Thickness of the shell to leave", def = "0") int thickness, @Arg(desc = "The pattern of blocks to replace the hollowed area with", def = "air") Pattern pattern, - @ArgFlag(name = 'm', desc = "Mask to hollow with") Mask mask, - InjectedValueAccess context) throws WorldEditException { + @ArgFlag(name = 'm', desc = "Mask to hollow with") Mask mask) throws WorldEditException { checkCommandArgument(thickness >= 0, "Thickness must be >= 0"); Mask finalMask = mask == null ? new SolidBlockMask(editSession) : mask; - actor.checkConfirmationRegion(() -> { - int affected = editSession.hollowOutRegion(region, thickness, pattern, finalMask); - actor.print(affected + " block(s) have been changed."); - }, getArguments(context), region, context); + int affected = editSession.hollowOutRegion(region, thickness, pattern, finalMask); + actor.print(affected + " block(s) have been changed."); } @Command( @@ -745,6 +710,7 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.forest") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public int forest(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "The type of tree to place", def = "tree") TreeType type, @@ -762,20 +728,19 @@ public class RegionCommands { ) @CommandPermissions("worldedit.region.flora") @Logging(REGION) + @Confirm(Confirm.Processor.REGION) public void flora(Actor actor, EditSession editSession, @Selection Region region, @Arg(desc = "The density of the forest", def = "5") - double density, InjectedValueAccess context) throws WorldEditException { + double density) throws WorldEditException { checkCommandArgument(0 <= density && density <= 100, "Density must be in [0, 100]"); - actor.checkConfirmationRegion(() -> { - FloraGenerator generator = new FloraGenerator(editSession); - GroundFunction ground = new GroundFunction(new ExistingBlockMask(editSession), generator); - LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground); - visitor.setMask(new NoiseFilter2D(new RandomNoise(), density / 100)); - Operations.completeLegacy(visitor); + FloraGenerator generator = new FloraGenerator(editSession); + GroundFunction ground = new GroundFunction(new ExistingBlockMask(editSession), generator); + LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground); + visitor.setMask(new NoiseFilter2D(new RandomNoise(), density / 100)); + Operations.completeLegacy(visitor); - int affected = ground.getAffected(); - actor.print(affected + " flora created."); - }, "/flora", region, context); + int affected = ground.getAffected(); + actor.print(affected + " flora created."); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index f74aafad6..5fd7323cb 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -39,11 +39,12 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; +import com.sk89q.worldedit.command.util.CommandQueuedConditionGenerator; import com.sk89q.worldedit.command.util.CreatureButcher; import com.sk89q.worldedit.command.util.EntityRemover; import com.sk89q.worldedit.command.util.Logging; import com.sk89q.worldedit.command.util.PrintCommandHelp; -import com.sk89q.worldedit.command.util.SkipQueue; +import com.sk89q.worldedit.command.util.annotation.SkipQueue; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Actor; @@ -102,7 +103,7 @@ import org.jetbrains.annotations.Range; /** * Utility commands. */ -@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class) +@CommandContainer(superTypes = {CommandPermissionsConditionGenerator.Registration.class, CommandQueuedConditionGenerator.Registration.class}) public class UtilityCommands { private final WorldEdit we; @@ -700,6 +701,7 @@ public class UtilityCommands { name = "/confirm", desc = "Confirm a command" ) + @SkipQueue @CommandPermissions("fawe.confirm") public void confirm(Player fp) throws WorldEditException { if (!fp.confirm()) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java index 5ba3612f3..a12f83783 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/WorldEditCommands.java @@ -29,7 +29,7 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; -import com.sk89q.worldedit.command.util.SkipQueue; +import com.sk89q.worldedit.command.util.annotation.SkipQueue; import com.sk89q.worldedit.command.util.CommandQueuedConditionGenerator; import com.sk89q.worldedit.command.util.PrintCommandHelp; import com.sk89q.worldedit.entity.Player; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/CommandQueuedConditionGenerator.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/CommandQueuedConditionGenerator.java index 1708c45db..5f72a31f1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/CommandQueuedConditionGenerator.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/CommandQueuedConditionGenerator.java @@ -1,5 +1,6 @@ package com.sk89q.worldedit.command.util; +import com.sk89q.worldedit.command.util.annotation.SkipQueue; import org.enginehub.piston.Command; import org.enginehub.piston.gen.CommandConditionGenerator; import org.enginehub.piston.util.NonnullByDefault; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java new file mode 100644 index 000000000..2aa23d08f --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/Confirm.java @@ -0,0 +1,136 @@ +package com.sk89q.worldedit.command.util.annotation; + +import com.boydti.fawe.Fawe; +import com.boydti.fawe.config.BBC; +import com.boydti.fawe.util.task.InterruptableCondition; +import com.sk89q.worldedit.IncompleteRegionException; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.command.argument.Arguments; +import com.sk89q.worldedit.event.Event; +import com.sk89q.worldedit.event.platform.CommandEvent; +import com.sk89q.worldedit.extension.platform.Actor; +import com.sk89q.worldedit.internal.annotation.Selection; +import com.sk89q.worldedit.internal.command.CommandArgParser; +import com.sk89q.worldedit.internal.util.Substring; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import org.enginehub.piston.exception.StopExecutionException; +import org.enginehub.piston.inject.InjectAnnotation; +import org.enginehub.piston.inject.InjectedValueAccess; +import org.enginehub.piston.inject.Key; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.Stream; + +/** + * Indicates how the affected blocks should be hinted at in the log. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.PARAMETER, ElementType.METHOD}) +@InjectAnnotation +public @interface Confirm { + Processor value() default Processor.ALWAYS; + + enum Processor { + REGION { + @Override + public boolean passes(Actor actor, InjectedValueAccess context, double value) { + Region region = context.injectedValue(Key.of(Region.class, Selection.class)).orElseThrow(IncompleteRegionException::new); + BlockVector3 min = region.getMinimumPoint(); + BlockVector3 max = region.getMaximumPoint(); + long area = (max.getX() - min.getX()) * (max.getZ() - min.getZ() + 1) * (int) value; + if (area > 2 << 18) { + BBC.WORLDEDIT_CANCEL_REASON_CONFIRM_REGION.send(actor, min, max, getArgs(context)); + return confirm(actor, context); + } + return true; + } + }, + RADIUS { + @Override + public boolean passes(Actor actor, InjectedValueAccess context, double value) { + int max = WorldEdit.getInstance().getConfiguration().maxRadius; + if (value > max) { + BBC.WORLDEDIT_CANCEL_REASON_CONFIRM_REGION.send(actor, value, max, getArgs(context)); + return Processor.confirm(actor, context); + } + return true; + } + }, + LIMIT { + @Override + public boolean passes(Actor actor, InjectedValueAccess context, double value) { + int max = 50;// TODO configurable, get Key.of(Method.class) @Limit + if (value > max) { + BBC.WORLDEDIT_CANCEL_REASON_CONFIRM_REGION.send(actor, value, max, getArgs(context)); + return Processor.confirm(actor, context); + } + return true; + } + }, + ALWAYS { + @Override + public boolean passes(Actor actor, InjectedValueAccess context, double value) { + BBC.WORLDEDIT_CANCEL_REASON_CONFIRM.send(actor); + return confirm(actor, context); + } + } + ; + + public boolean passes(Actor actor, InjectedValueAccess context, double value){return true;} + + public T check(Actor actor, InjectedValueAccess context, T value) { + if (!passes(actor, context, value.doubleValue())) { + throw new StopExecutionException(TextComponent.empty()); + } + return value; + } + + private static String getArgs(InjectedValueAccess context) { + return context.injectedValue(Key.of(Arguments.class)).map(Arguments::get).get(); + } + + private static String getArg(InjectedValueAccess context, String def) { + Stream split = CommandArgParser.forArgString(getArgs(context)).parseArgs(); + Substring first = split.findFirst().orElse(null); + if (first == null && def == null) { + throw new StopExecutionException(TextComponent.of("No arguments")); + } + return first != null ? first.getSubstring() : def; + } + + private static int getInt(InjectedValueAccess context, String def) { + return Integer.parseInt(getArg(context, def)); + } + + private static boolean confirm(Actor actor, InjectedValueAccess context) { + Event event = context.injectedValue(Key.of(Event.class)).orElse(null); + if (!(event instanceof CommandEvent)) { + return true; + } + ReentrantLock lock = new ReentrantLock(); + Condition condition = lock.newCondition(); + InterruptableCondition wait = new InterruptableCondition(lock, condition, Thread.currentThread()); + try { + lock.lock(); + actor.setMeta("cmdConfirm", wait); + if (condition.await(15, TimeUnit.SECONDS)) { + return true; + } + } catch (InterruptedException e) {} + finally { + if (actor.getMeta("cmdConfirm") == wait) { + actor.deleteMeta("cmdConfirm"); + } + } + return false; + } + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SkipQueue.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/SkipQueue.java similarity index 73% rename from worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SkipQueue.java rename to worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/SkipQueue.java index c67f82248..13ce8ae1f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/SkipQueue.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/annotation/SkipQueue.java @@ -1,5 +1,6 @@ -package com.sk89q.worldedit.command.util; +package com.sk89q.worldedit.command.util.annotation; +import com.sk89q.worldedit.command.util.CommandQueuedConditionGenerator; import org.enginehub.piston.annotation.CommandCondition; import java.lang.annotation.Retention; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java b/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java index 30c03771f..74d94b96e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/entity/Player.java @@ -375,39 +375,6 @@ public interface Player extends Entity, Actor { } } - default int cancel(boolean close) { - int cancelled = 0; - - for (Request request : Request.getAll()) { - EditSession editSession = request.getEditSession(); - if (editSession != null) { - Player player = editSession.getPlayer(); - if (equals(player)) { - editSession.cancel(); - cancelled++; - } - } - } - VirtualWorld world = getSession().getVirtualWorld(); - if (world != null) { - if (close) { - try { - world.close(false); - } catch (IOException e) { - e.printStackTrace(); - } - } - else { - try { - world.close(false); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - return cancelled; - } - void sendTitle(String title, String sub); /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/event/AbstractCancellable.java b/worldedit-core/src/main/java/com/sk89q/worldedit/event/AbstractCancellable.java index 673b251c5..fe54fe0ef 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/event/AbstractCancellable.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/event/AbstractCancellable.java @@ -23,7 +23,7 @@ package com.sk89q.worldedit.event; * An abstract implementation of {@link Cancellable} that has all * of {@link Cancellable}'s methods implemented. */ -public abstract class AbstractCancellable implements Cancellable { +public abstract class AbstractCancellable extends Event implements Cancellable { private boolean cancelled; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java index a8b6c9a50..f7b9fb54b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java @@ -23,13 +23,18 @@ import com.boydti.fawe.Fawe; import com.boydti.fawe.config.BBC; import com.boydti.fawe.config.Settings; import com.boydti.fawe.object.FaweLimit; +import com.boydti.fawe.object.brush.visualization.VirtualWorld; +import com.boydti.fawe.util.task.InterruptableCondition; +import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.entity.MapMetadatable; +import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.event.platform.CommandEvent; import com.sk89q.worldedit.internal.cui.CUIEvent; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.RegionOperationException; import com.sk89q.worldedit.session.SessionOwner; +import com.sk89q.worldedit.session.request.Request; import com.sk89q.worldedit.util.Identifiable; import com.sk89q.worldedit.util.auth.Subject; import com.sk89q.worldedit.util.formatting.text.Component; @@ -37,7 +42,9 @@ import org.enginehub.piston.inject.InjectedValueAccess; import org.jetbrains.annotations.NotNull; import java.io.File; +import java.io.IOException; import java.text.NumberFormat; +import java.util.concurrent.locks.Condition; /** * An object that can perform actions in WorldEdit. @@ -134,100 +141,30 @@ public interface Actor extends Identifiable, SessionOwner, Subject, MapMetadatab boolean runAction(Runnable ifFree, boolean checkFree, boolean async); - default void checkConfirmationStack(@NotNull Runnable task, @NotNull String command, - Region region, int times, InjectedValueAccess context) throws RegionOperationException { - if (!getMeta("cmdConfirmRunning", false)) { - if (region != null) { - BlockVector3 min = region.getMinimumPoint(); - BlockVector3 max = region.getMaximumPoint(); - long area = - (long) ((max.getX() - min.getX()) * (max.getZ() - min.getZ() + 1)) * times; - if (area > 2 << 18) { - setConfirmTask(task, context, command); - BlockVector3 base = max.subtract(min).add(BlockVector3.ONE); - long volume = (long) base.getX() * base.getZ() * base.getY() * times; - throw new RegionOperationException(BBC.WORLDEDIT_CANCEL_REASON_CONFIRM - .format(min, max, command, - NumberFormat.getNumberInstance().format(volume))); - } - } + /** + * Decline any pending actions + * @return true if an action was pending + */ + default boolean decline() { + InterruptableCondition confirm = deleteMeta("cmdConfirm"); + if (confirm != null) { + confirm.interrupt(); + return true; } - task.run(); - } - - default void checkConfirmationRegion(@NotNull Runnable task, @NotNull String command, - Region region, InjectedValueAccess context) throws RegionOperationException { - if (!getMeta("cmdConfirmRunning", false)) { - if (region != null) { - BlockVector3 min = region.getMinimumPoint(); - BlockVector3 max = region.getMaximumPoint(); - long area = (max.getX() - min.getX()) * (max.getZ() - min.getZ() + 1); - if (area > 2 << 18) { - setConfirmTask(task, context, command); - BlockVector3 base = max.subtract(min).add(BlockVector3.ONE); - long volume = (long) base.getX() * base.getZ() * base.getY(); - throw new RegionOperationException(BBC.WORLDEDIT_CANCEL_REASON_CONFIRM - .format(min, max, command, - NumberFormat.getNumberInstance().format(volume))); - } - } - } - task.run(); - } - - default void setConfirmTask(@NotNull Runnable task, InjectedValueAccess context, - @NotNull String command) { - CommandEvent event = new CommandEvent(this, command); - Runnable newTask = () -> PlatformCommandManager.getInstance().handleCommandTask(() -> { - task.run(); - return null; - }, context, getSession(), event); - setMeta("cmdConfirm", newTask); - } - - default void checkConfirmation(@NotNull Runnable task, @NotNull String command, int times, - int limit, InjectedValueAccess context) throws RegionOperationException { - if (!getMeta("cmdConfirmRunning", false)) { - if (times > limit) { - setConfirmTask(task, context, command); - String volume = ""; - throw new RegionOperationException( - BBC.WORLDEDIT_CANCEL_REASON_CONFIRM.format(0, times, command, volume)); - } - } - task.run(); - } - - default void checkConfirmationRadius(@NotNull Runnable task, String command, int radius, - InjectedValueAccess context) throws RegionOperationException { - if (command != null && !getMeta("cmdConfirmRunning", false)) { - if (radius > 0) { - if (radius > 448) { - setConfirmTask(task, context, command); - long volume = (long) (Math.PI * ((double) radius * radius)); - throw new RegionOperationException(BBC.WORLDEDIT_CANCEL_REASON_CONFIRM - .format(0, radius, command, - NumberFormat.getNumberInstance().format(volume))); - } - } - } - task.run(); + return false; } + /** + * Confirm any pending actions + * @return true if an action was pending + */ default boolean confirm() { - Runnable confirm = deleteMeta("cmdConfirm"); - if (confirm == null) { - return false; + InterruptableCondition confirm = deleteMeta("cmdConfirm"); + if (confirm != null) { + confirm.signal();; + return true; } - queueAction(() -> { - setMeta("cmdConfirmRunning", true); - try { - confirm.run(); - } finally { - setMeta("cmdConfirmRunning", false); - } - }); - return true; + return false; } /** @@ -257,4 +194,42 @@ public interface Actor extends Identifiable, SessionOwner, Subject, MapMetadatab default boolean runIfFree(Runnable r) { return runAction(r, true, false); } + + /** + * Attempt to cancel all pending and running actions + * @param close if Extents are closed + * @return number of cancelled actions + */ + default int cancel(boolean close) { + int cancelled = decline() ? 1 : 0; + + for (Request request : Request.getAll()) { + EditSession editSession = request.getEditSession(); + if (editSession != null) { + Player player = editSession.getPlayer(); + if (equals(player)) { + editSession.cancel(); + cancelled++; + } + } + } + VirtualWorld world = getSession().getVirtualWorld(); + if (world != null) { + if (close) { + try { + world.close(false); + } catch (IOException e) { + e.printStackTrace(); + } + } + else { + try { + world.close(false); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return cancelled; + } } 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 564d67b5d..db15ad5b6 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 @@ -111,6 +111,7 @@ import com.sk89q.worldedit.command.util.PrintCommandHelp; import com.sk89q.worldedit.command.util.SubCommandPermissionCondition; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.event.Event; import com.sk89q.worldedit.event.platform.CommandEvent; import com.sk89q.worldedit.event.platform.CommandSuggestionEvent; import com.sk89q.worldedit.extension.platform.binding.Bindings; @@ -122,6 +123,8 @@ import com.sk89q.worldedit.internal.annotation.Selection; import com.sk89q.worldedit.internal.command.CommandArgParser; import com.sk89q.worldedit.internal.command.CommandLoggingHandler; import com.sk89q.worldedit.internal.command.CommandRegistrationHandler; +import com.sk89q.worldedit.internal.command.ConfirmHandler; +import com.sk89q.worldedit.internal.command.MethodInjector; import com.sk89q.worldedit.internal.command.exception.ExceptionConverter; import com.sk89q.worldedit.internal.command.exception.WorldEditExceptionConverter; import com.sk89q.worldedit.internal.util.Substring; @@ -130,6 +133,7 @@ import com.sk89q.worldedit.session.SessionKey; import com.sk89q.worldedit.session.request.Request; import com.sk89q.worldedit.util.auth.AuthorizationException; import com.sk89q.worldedit.util.eventbus.Subscribe; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; @@ -138,12 +142,12 @@ import com.sk89q.worldedit.util.logging.LogFormat; import com.sk89q.worldedit.world.World; import org.enginehub.piston.Command; import org.enginehub.piston.CommandManager; -import org.enginehub.piston.config.TextConfig; +import org.enginehub.piston.converter.ArgumentConverter; import org.enginehub.piston.converter.ArgumentConverters; +import org.enginehub.piston.converter.ConversionResult; import org.enginehub.piston.exception.CommandException; import org.enginehub.piston.exception.CommandExecutionException; import org.enginehub.piston.exception.ConditionFailedException; -import org.enginehub.piston.exception.StopExecutionException; import org.enginehub.piston.exception.UsageException; import org.enginehub.piston.gen.CommandRegistration; import org.enginehub.piston.impl.CommandManagerServiceImpl; @@ -165,6 +169,8 @@ import java.io.File; import java.io.IOException; import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; @@ -221,7 +227,10 @@ public final class PlatformCommandManager { this.globalInjectedValues = MapBackedValueStore.create(); this.registration = new CommandRegistrationHandler( ImmutableList.of( - new CommandLoggingHandler(worldEdit, COMMAND_LOG) + new CommandLoggingHandler(worldEdit, COMMAND_LOG), + new MethodInjector(), + new ConfirmHandler() + )); // setup separate from main constructor // ensures that everything is definitely assigned @@ -262,7 +271,7 @@ public final class PlatformCommandManager { WorldConverter.register(commandManager); ExpressionConverter.register(commandManager); - registerBindings(new ConsumeBindings(worldEdit)); + registerBindings(new ConsumeBindings(worldEdit, this)); registerBindings(new PrimitiveBindings(worldEdit)); registerBindings(new ProvideBindings(worldEdit)); } @@ -611,17 +620,29 @@ public final class PlatformCommandManager { return CommandArgParser.forArgString(input).parseArgs(); } - public T parse(String args, Actor actor) { + public T parseCommand(String args, Actor actor) { InjectedValueAccess context; if (actor == null) { context = globalInjectedValues; } else { - context = initializeInjectedValues(args::toString, actor); + context = initializeInjectedValues(args::toString, actor, null); } - return parse(args, context); + return parseCommand(args, context); } - public T parse(String args, InjectedValueAccess access) { + public T parseConverter(String args, InjectedValueAccess access, Class clazz) { + ArgumentConverter converter = commandManager.getConverter(Key.of(clazz)).orElse(null); + if (converter != null) { + ConversionResult result = converter.convert(args, access); + Collection values = result.orElse(Collections.emptyList()); + if (!values.isEmpty()) { + return values.iterator().next(); + } + } + return null; + } + + public T parseCommand(String args, InjectedValueAccess access) { if (args.isEmpty()) return null; String[] split = parseArgs(args) .map(Substring::getSubstring) @@ -639,8 +660,6 @@ public final class PlatformCommandManager { String arg0 = space0 == -1 ? args : args.substring(0, space0); Optional optional = commandManager.getCommand(arg0); if (!optional.isPresent()) { - System.out.println("No command for '" + arg0 + "' " + StringMan.getString(commandManager.getAllCommands().map( - Command::getName).collect(Collectors.toList()))); return; } Command cmd = optional.get(); @@ -648,6 +667,8 @@ public final class PlatformCommandManager { if (queued != null && !queued.isQueued()) { handleCommandOnCurrentThread(event); return; + } else { + actor.decline(); } LocalSession session = worldEdit.getSessionManager().get(actor); synchronized (session) { @@ -679,7 +700,7 @@ public final class PlatformCommandManager { } } - MemoizingValueAccess context = initializeInjectedValues(event::getArguments, actor); + MemoizingValueAccess context = initializeInjectedValues(event::getArguments, actor, event); ThrowableSupplier task = () -> commandManager.execute(context, ImmutableList.copyOf(split)); @@ -717,10 +738,6 @@ public final class PlatformCommandManager { } catch (FaweException e) { actor.printError("Edit cancelled: " + e.getMessage()); } catch (UsageException e) { - actor.print(TextComponent.builder("") - .color(TextColor.RED) - .append(e.getRichMessage()) - .build()); ImmutableList cmd = e.getCommands(); if (!cmd.isEmpty()) { actor.print(TextComponent.builder("Usage: ") @@ -728,22 +745,27 @@ public final class PlatformCommandManager { .append(HelpGenerator.create(e.getCommandParseResult()).getUsage()) .build()); } - } catch (CommandExecutionException e) { - handleUnknownException(actor, e.getCause()); - } catch (CommandException e) { actor.print(TextComponent.builder("") .color(TextColor.RED) .append(e.getRichMessage()) .build()); - List argList = parseArgs(event.getArguments()).map(Substring::getSubstring).collect(Collectors.toList()); - printUsage(actor, argList); + } catch (CommandExecutionException e) { + handleUnknownException(actor, e.getCause()); + } catch (CommandException e) { + Component msg = e.getRichMessage(); + if (msg != TextComponent.empty()) { + actor.print(TextComponent.builder("") + .color(TextColor.RED) + .append(msg) + .build()); + List argList = parseArgs(event.getArguments()).map(Substring::getSubstring).collect(Collectors.toList()); + printUsage(actor, argList); + } } catch (Throwable t) { handleUnknownException(actor, t); } finally { if (context instanceof MemoizingValueAccess) { context = ((MemoizingValueAccess) context).snapshotMemory(); - } else { - System.out.println("Invalid context " + context); } Optional editSessionOpt = context.injectedValue(Key.of(EditSession.class)); @@ -783,7 +805,7 @@ public final class PlatformCommandManager { getCommandManager(), actor, "//help"); } - private MemoizingValueAccess initializeInjectedValues(Arguments arguments, Actor tmp) { + private MemoizingValueAccess initializeInjectedValues(Arguments arguments, Actor tmp, Event event) { InjectedValueStore store = MapBackedValueStore.create(); Actor actor = wrapActor(tmp, store); store.injectValue(Key.of(Actor.class), ValueProvider.constant(actor)); @@ -801,9 +823,10 @@ public final class PlatformCommandManager { localSession.tellVersion(actor); return Optional.of(localSession); }); - + store.injectValue(Key.of(InjectedValueStore.class), ValueProvider.constant(store)); + store.injectValue(Key.of(Event.class), ValueProvider.constant(event)); return MemoizingValueAccess.wrap( - MergedValueAccess.of(store, globalInjectedValues) + MergedValueAccess.of(store, globalInjectedValues) ); } @@ -821,7 +844,7 @@ public final class PlatformCommandManager { List argStrings = split.stream() .map(Substring::getSubstring) .collect(Collectors.toList()); - MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor()); + MemoizingValueAccess access = initializeInjectedValues(() -> arguments, event.getActor(), event); ImmutableSet suggestions; try { suggestions = commandManager.getSuggestions(access, argStrings); @@ -832,7 +855,6 @@ public final class PlatformCommandManager { } throw t; } - event.setSuggestions(suggestions.stream() .map(suggestion -> { int noSlashLength = arguments.length() - 1; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/binding/ConsumeBindings.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/binding/ConsumeBindings.java index 8a99ff387..65e328e59 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/binding/ConsumeBindings.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/binding/ConsumeBindings.java @@ -2,20 +2,24 @@ package com.sk89q.worldedit.extension.platform.binding; import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.image.ImageUtil; -import com.sk89q.worldedit.UnknownDirectionException; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.command.util.annotation.Confirm; import com.sk89q.worldedit.entity.Entity; -import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.NoMatchException; import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Capability; +import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.extent.Extent; -import com.sk89q.worldedit.internal.annotation.Direction; +import com.sk89q.worldedit.internal.annotation.Selection; +import com.sk89q.worldedit.internal.expression.Expression; +import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.util.TreeGenerator; +import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeTypes; @@ -26,14 +30,73 @@ import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.registry.BiomeRegistry; import org.enginehub.piston.CommandManager; -import org.enginehub.piston.inject.InjectedValueStore; +import org.enginehub.piston.converter.ConversionResult; +import org.enginehub.piston.exception.StopExecutionException; +import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.Key; import java.util.Collection; public class ConsumeBindings extends Bindings { - public ConsumeBindings(WorldEdit worldEdit) { + private final PlatformCommandManager manager; + + public ConsumeBindings(WorldEdit worldEdit, PlatformCommandManager manager) { super(worldEdit); + this.manager = manager; + } + + @Binding + @Confirm + @Selection + public int regionMultiple(Actor actor, InjectedValueAccess context, @Selection Region region, String argument) { + int times = (int) Expression.compile(argument).evaluate(); + return Confirm.Processor.REGION.check(actor, context, times); + } + + @Binding + @Confirm(Confirm.Processor.RADIUS) + public Integer radiusInteger(Actor actor, InjectedValueAccess context, String argument) { + int times = (int) Expression.compile(argument).evaluate(); + return Confirm.Processor.RADIUS.check(actor, context, times); + } + + @Binding + @Confirm(Confirm.Processor.LIMIT) + public Integer limitInteger(Actor actor, InjectedValueAccess context, String argument) { + int times = (int) Expression.compile(argument).evaluate(); + return Confirm.Processor.LIMIT.check(actor, context, times); + } + + @Binding + @Confirm(Confirm.Processor.RADIUS) + public Double radiusDouble(Actor actor, InjectedValueAccess context, String argument) { + double times = Expression.compile(argument).evaluate(); + return Confirm.Processor.RADIUS.check(actor, context, times); + } + + @Binding + @Confirm(Confirm.Processor.LIMIT) + public Double limitDouble(Actor actor, InjectedValueAccess context, String argument) { + double times = Expression.compile(argument).evaluate(); + return Confirm.Processor.LIMIT.check(actor, context, times); + } + + @Binding + @Confirm(Confirm.Processor.RADIUS) + public BlockVector2 radiusVec2(Actor actor, InjectedValueAccess context, String argument) { + BlockVector2 radius = manager.parseConverter(argument, context, BlockVector2.class); + double length = radius.length(); + Confirm.Processor.RADIUS.check(actor, context, length); + return radius; + } + + @Binding + @Confirm(Confirm.Processor.RADIUS) + public BlockVector3 radiusVec3(Actor actor, InjectedValueAccess context, String argument) { + BlockVector3 radius = manager.parseConverter(argument, context, BlockVector3.class); + double length = radius.length(); + Confirm.Processor.RADIUS.check(actor, context, length); + return radius; } @Binding diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java index 2b6d9cc04..2b06f539b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/CommandUtil.java @@ -19,9 +19,6 @@ package com.sk89q.worldedit.internal.command; -import static com.google.common.base.Preconditions.checkState; -import static java.util.stream.Collectors.toList; - import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.sk89q.worldedit.extension.platform.Actor; @@ -32,13 +29,6 @@ import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.event.ClickEvent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; import com.sk89q.worldedit.util.formatting.text.format.TextDecoration; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.stream.Collectors; import org.enginehub.piston.Command; import org.enginehub.piston.CommandParameters; import org.enginehub.piston.NoInputCommandParameters; @@ -47,19 +37,30 @@ import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.Key; import org.enginehub.piston.part.SubCommandPart; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +import static com.google.common.base.Preconditions.checkState; +import static java.util.stream.Collectors.toList; + public class CommandUtil { private static final Component DEPRECATION_MARKER = TextComponent.of("This command is deprecated."); private static Component makeDeprecatedFooter(String reason, Component newCommand) { return TextComponent.builder() - .append(DEPRECATION_MARKER) - .append(" " + reason + ".") - .append(TextComponent.newline()) - .append(TextComponent.of("Use ", TextColor.GOLD, TextDecoration.ITALIC)) - .append(newCommand) - .append(TextComponent.of(" instead.", TextColor.GOLD, TextDecoration.ITALIC)) - .build(); + .append(DEPRECATION_MARKER) + .append(" " + reason + ".") + .append(TextComponent.newline()) + .append(TextComponent.of("Use ", TextColor.GOLD, TextDecoration.ITALIC)) + .append(newCommand) + .append(TextComponent.of(" instead.", TextColor.GOLD, TextDecoration.ITALIC)) + .build(); } public interface NewCommandGenerator { @@ -71,45 +72,45 @@ public class CommandUtil { public static Command deprecate(Command command, String reason, NewCommandGenerator newCommandGenerator) { Component deprecatedWarning = makeDeprecatedFooter( - reason, - newCommandSuggestion(newCommandGenerator, - NoInputCommandParameters.builder().build(), - command) + reason, + newCommandSuggestion(newCommandGenerator, + NoInputCommandParameters.builder().build(), + command) ); return command.toBuilder() - .action(parameters -> - deprecatedCommandWarning(parameters, command, reason, newCommandGenerator)) - .footer(command.getFooter() - .map(existingFooter -> existingFooter - .append(TextComponent.newline()).append(deprecatedWarning)) - .orElse(deprecatedWarning)) - .build(); + .action(parameters -> + deprecatedCommandWarning(parameters, command, reason, newCommandGenerator)) + .footer(command.getFooter() + .map(existingFooter -> existingFooter + .append(TextComponent.newline()).append(deprecatedWarning)) + .orElse(deprecatedWarning)) + .build(); } public static Optional footerWithoutDeprecation(Command command) { return command.getFooter() - .filter(footer -> anyComponent(footer, Predicate.isEqual(DEPRECATION_MARKER))) - .map(footer -> Optional.of( - replaceDeprecation(footer) - )) - .orElseGet(command::getFooter); + .filter(footer -> anyComponent(footer, Predicate.isEqual(DEPRECATION_MARKER))) + .map(footer -> Optional.of( + replaceDeprecation(footer) + )) + .orElseGet(command::getFooter); } public static Optional deprecationWarning(Command command) { return command.getFooter() - .map(CommandUtil::extractDeprecation) - .orElseGet(command::getFooter); + .map(CommandUtil::extractDeprecation) + .orElseGet(command::getFooter); } public static boolean isDeprecated(Command command) { return command.getFooter() - .filter(footer -> anyComponent(footer, Predicate.isEqual(DEPRECATION_MARKER))) - .isPresent(); + .filter(footer -> anyComponent(footer, Predicate.isEqual(DEPRECATION_MARKER))) + .isPresent(); } private static boolean anyComponent(Component component, Predicate test) { return test.test(component) || component.children().stream() - .anyMatch(x -> anyComponent(x, test)); + .anyMatch(x -> anyComponent(x, test)); } private static Component replaceDeprecation(Component component) { @@ -117,9 +118,9 @@ public class CommandUtil { return TextComponent.empty(); } return component.children( - component.children().stream() - .map(CommandUtil::replaceDeprecation) - .collect(toList()) + component.children().stream() + .map(CommandUtil::replaceDeprecation) + .collect(toList()) ); } @@ -128,26 +129,26 @@ public class CommandUtil { return Optional.of(component); } return component.children().stream() - .map(CommandUtil::extractDeprecation) - .filter(Optional::isPresent) - .map(Optional::get) - .findAny(); + .map(CommandUtil::extractDeprecation) + .filter(Optional::isPresent) + .map(Optional::get) + .findAny(); } private static Object deprecatedCommandWarning( - CommandParameters parameters, - Command command, - String reason, - NewCommandGenerator generator + CommandParameters parameters, + Command command, + String reason, + NewCommandGenerator generator ) throws Exception { parameters.injectedValue(Key.of(Actor.class)) - .ifPresent(actor -> { - Component suggestion = newCommandSuggestion(generator, parameters, command); - actor.print(TextComponent.of(reason + ". Please use ", TextColor.GOLD) - .append(suggestion) - .append(TextComponent.of(" instead.")) - ); - }); + .ifPresent(actor -> { + Component suggestion = newCommandSuggestion(generator, parameters, command); + actor.print(TextComponent.of(reason + ". Please use ", TextColor.GOLD) + .append(suggestion) + .append(TextComponent.of(" instead.")) + ); + }); return command.getAction().run(parameters); } @@ -156,15 +157,15 @@ public class CommandUtil { Command command) { String suggestedCommand = generator.newCommand(command, parameters); return TextComponent.of(suggestedCommand) - .decoration(TextDecoration.UNDERLINED, true) - .clickEvent(ClickEvent.suggestCommand(suggestedCommand)); + .decoration(TextDecoration.UNDERLINED, true) + .clickEvent(ClickEvent.suggestCommand(suggestedCommand)); } public static Map getSubCommands(Command currentCommand) { return currentCommand.getParts().stream() - .filter(p -> p instanceof SubCommandPart) - .flatMap(p -> ((SubCommandPart) p).getCommands().stream()) - .collect(Collectors.toMap(Command::getName, Function.identity())); + .filter(p -> p instanceof SubCommandPart) + .flatMap(p -> ((SubCommandPart) p).getCommands().stream()) + .collect(Collectors.toMap(Command::getName, Function.identity())); } private static String clean(String input) { @@ -172,7 +173,7 @@ public class CommandUtil { } private static final Comparator BY_CLEAN_NAME = - Comparator.comparing(c -> clean(c.getName())); + Comparator.comparing(c -> clean(c.getName())); public static Comparator byCleanName() { return BY_CLEAN_NAME; @@ -183,15 +184,15 @@ public class CommandUtil { */ public static List fixSuggestions(String arguments, List suggestions) { Substring lastArg = Iterables.getLast( - CommandArgParser.spaceSplit(arguments) + CommandArgParser.spaceSplit(arguments) ); return suggestions.stream() - // Re-map suggestions to only operate on the last non-quoted word - .map(suggestion -> onlyOnLastQuotedWord(lastArg, suggestion)) - .map(suggestion -> suggestLast(lastArg, suggestion)) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(toList()); + // Re-map suggestions to only operate on the last non-quoted word + .map(suggestion -> onlyOnLastQuotedWord(lastArg, suggestion)) + .map(suggestion -> suggestLast(lastArg, suggestion)) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(toList()); } private static Substring onlyOnLastQuotedWord(Substring lastArg, Substring suggestion) { @@ -227,7 +228,7 @@ public class CommandUtil { return Optional.empty(); } checkState(end <= builder.length(), - "Suggestion ends too late, last=%s, suggestion=", last, suggestion); + "Suggestion ends too late, last=%s, suggestion=", last, suggestion); builder.replace(start, end, suggestion.getSubstring()); return Optional.of(builder.toString()); } @@ -258,10 +259,10 @@ public class CommandUtil { public static T requireIV(Key type, String name, InjectedValueAccess injectedValueAccess) { return injectedValueAccess.injectedValue(type).orElseThrow(() -> - new IllegalStateException("No injected value for " + name + " (type " + type + ")") + new IllegalStateException("No injected value for " + name + " (type " + type + ")") ); } private CommandUtil() { } -} +} \ No newline at end of file diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/ConfirmHandler.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/ConfirmHandler.java new file mode 100644 index 000000000..0ed6eee07 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/ConfirmHandler.java @@ -0,0 +1,47 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.internal.command; + +import com.sk89q.worldedit.command.util.annotation.Confirm; +import com.sk89q.worldedit.extension.platform.Actor; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import org.enginehub.piston.CommandParameters; +import org.enginehub.piston.exception.StopExecutionException; +import org.enginehub.piston.gen.CommandCallListener; +import org.enginehub.piston.inject.Key; + +import java.lang.reflect.Method; + +/** + * Logs called commands to a logger. + */ +public class ConfirmHandler implements CommandCallListener { + @Override + public void beforeCall(Method method, CommandParameters parameters) { + Confirm confirmAnnotation = method.getAnnotation(Confirm.class); + if (confirmAnnotation == null) { + return; + } + Actor actor = parameters.injectedValue(Key.of(Actor.class)).get(); + if (!confirmAnnotation.value().passes(actor, parameters, 1)) { + throw new StopExecutionException(TextComponent.empty()); + } + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/MethodInjector.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/MethodInjector.java new file mode 100644 index 000000000..f0ead981f --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/MethodInjector.java @@ -0,0 +1,19 @@ +package com.sk89q.worldedit.internal.command; + +import org.enginehub.piston.CommandParameters; +import org.enginehub.piston.gen.CommandCallListener; +import org.enginehub.piston.inject.InjectedValueAccess; +import org.enginehub.piston.inject.InjectedValueStore; +import org.enginehub.piston.inject.Key; +import org.enginehub.piston.util.ValueProvider; + +import java.lang.reflect.Method; +import java.util.Optional; + +public class MethodInjector implements CommandCallListener { + @Override + public void beforeCall(Method commandMethod, CommandParameters parameters) { + InjectedValueStore store = parameters.injectedValue(Key.of(InjectedValueStore.class)).get(); + store.injectValue(Key.of(Method.class), ValueProvider.constant(commandMethod)); + } +}