From 0e633e8111d6524886818d8a2d48a0fddc2a6af0 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Fri, 24 Aug 2018 16:06:52 +1000 Subject: [PATCH] Added a few more commands to the ones in the task queue test. Now it's all liquid commands. --- .../java/com/sk89q/worldedit/EditSession.java | 49 ++++++++++++----- .../worldedit/command/RegionCommands.java | 2 +- .../worldedit/command/SelectionCommands.java | 55 +++++++++---------- .../worldedit/command/UtilityCommands.java | 21 +++---- .../sk89q/worldedit/function/task/Task.java | 14 +++++ 5 files changed, 84 insertions(+), 57 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java index cf762c0826..c4ab0d3d74 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -613,30 +613,30 @@ public int countBlocks(Region region, Set searchBlocks) { /** * Fills an area recursively in the X/Z directions. * + * @param actor the actor, if present * @param origin the location to start from * @param block the block to fill with * @param radius the radius of the spherical area to fill * @param depth the maximum depth, starting from the origin * @param recursive whether a breadth-first search should be performed * @return number of blocks affected - * @throws MaxChangedBlocksException thrown if too many blocks are changed */ - public int fillXZ(Vector origin, BlockStateHolder block, double radius, int depth, boolean recursive) throws MaxChangedBlocksException { - return fillXZ(origin, new BlockPattern(block), radius, depth, recursive); + public Task fillXZ(@Nullable Actor actor, Vector origin, BlockStateHolder block, double radius, int depth, boolean recursive) { + return fillXZ(actor, origin, new BlockPattern(block), radius, depth, recursive); } /** * Fills an area recursively in the X/Z directions. * + * @param actor the actor, if present * @param origin the origin to start the fill from * @param pattern the pattern to fill with * @param radius the radius of the spherical area to fill, with 0 as the smallest radius * @param depth the maximum depth, starting from the origin, with 1 as the smallest depth * @param recursive whether a breadth-first search should be performed * @return number of blocks affected - * @throws MaxChangedBlocksException thrown if too many blocks are changed */ - public int fillXZ(Vector origin, Pattern pattern, double radius, int depth, boolean recursive) throws MaxChangedBlocksException { + public Task fillXZ(@Nullable Actor actor, Vector origin, Pattern pattern, double radius, int depth, boolean recursive) { checkNotNull(origin); checkNotNull(pattern); checkArgument(radius >= 0, "radius >= 0"); @@ -664,9 +664,14 @@ public int fillXZ(Vector origin, Pattern pattern, double radius, int depth, bool visitor.visit(origin); // Execute - Operations.completeLegacy(visitor); + Task task = Operations.completeQueued(visitor, actor, this) + .withSupplier(visitor::getAffected); - return visitor.getAffected(); + if (actor != null) { + task.addActorConsumers(actor); + } + + return task; } /** @@ -1127,8 +1132,7 @@ public Task drainArea(@Nullable Actor actor, Vector origin, double radi .withSupplier(visitor::getAffected); if (actor != null) { - task.withStatusConsumer(actor::print); - task.onExcept(e -> actor.printError(e.getMessage())); + task.addActorConsumers(actor); } return task; @@ -1137,13 +1141,14 @@ public Task drainArea(@Nullable Actor actor, Vector origin, double radi /** * Fix liquids so that they turn into stationary blocks and extend outward. * + * @param actor the actor, if present * @param origin the original position * @param radius the radius to fix * @param fluid the type of the fluid * @return number of blocks affected * @throws MaxChangedBlocksException thrown if too many blocks are changed */ - public int fixLiquid(Vector origin, double radius, BlockType fluid) throws MaxChangedBlocksException { + public Task fixLiquid(@Nullable Actor actor, Vector origin, double radius, BlockType fluid) throws MaxChangedBlocksException { checkNotNull(origin); checkArgument(radius >= 0, "radius >= 0 required"); @@ -1170,9 +1175,14 @@ public int fixLiquid(Vector origin, double radius, BlockType fluid) throws MaxCh } } - Operations.completeLegacy(visitor); + Task task = Operations.completeQueued(visitor, actor, this) + .withSupplier(visitor::getAffected); - return visitor.getAffected(); + if (actor != null) { + task.addActorConsumers(actor); + } + + return task; } /** @@ -1656,14 +1666,23 @@ public int makeForest(Vector basePosition, int size, double density, TreeGenerat /** * Get the block distribution inside a region. * + * @param actor the actor, if present * @param region a region + * @param fuzzy If it should ignore states * @return the results */ - public List> getBlockDistribution(Region region, boolean fuzzy) { + public Task>> getBlockDistribution(@Nullable Actor actor, Region region, boolean fuzzy) { BlockDistributionCounter count = new BlockDistributionCounter(this, fuzzy); RegionVisitor visitor = new RegionVisitor(region, count); - Operations.completeBlindly(visitor); - return count.getDistribution(); + + Task>> task = Operations.>>completeQueued(visitor, actor, this) + .withSupplier(count::getDistribution); + + if (actor != null) { + task.addActorConsumers(actor); + } + + return task; } public int makeShape(final Region region, final Vector zero, final Vector unit, final Pattern pattern, final String expressionString, final boolean hollow) throws ExpressionException, MaxChangedBlocksException { 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 5b5c171335..6fbd63dad9 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 @@ -350,7 +350,7 @@ public void stack(Player player, EditSession editSession, LocalSession session, public void regenerateChunk(Player player, LocalSession session, EditSession editSession, @Selection Region region) throws WorldEditException { Mask mask = session.getMask(); try { - session.setMask((Mask) null); + session.setMask(null); player.getWorld().regenerate(region, editSession); } finally { session.setMask(mask); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java index 5f4fca09c3..34eb5d12e7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java @@ -58,7 +58,6 @@ import com.sk89q.worldedit.util.formatting.component.CommandListBox; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BlockStateHolder; -import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.storage.ChunkStore; @@ -662,40 +661,40 @@ public void distr(Player player, LocalSession session, EditSession editSession, int size; boolean useData = args.hasFlag('d'); - List> distribution; if (args.hasFlag('c')) { // TODO: Update for new clipboard throw new CommandException("Needs to be re-written again"); } else { - distribution = editSession.getBlockDistribution(session.getSelection(player.getWorld()), !useData); size = session.getSelection(player.getWorld()).getArea(); - } - - if (distribution.isEmpty()) { // *Should* always be false - player.printError("No blocks counted."); - return; - } + editSession.getBlockDistribution(player, session.getSelection(player.getWorld()), !useData) + .withConsumer(distribution -> { + if (distribution.isEmpty()) { // *Should* always be false + player.printError("No blocks counted."); + return; + } - player.print("# total blocks: " + size); - - for (Countable c : distribution) { - String name = c.getID().getBlockType().getName(); - String str; - if (useData) { - str = String.format("%-7s (%.3f%%) %s #%s", - String.valueOf(c.getAmount()), - c.getAmount() / (double) size * 100, - name, - c.getID().getAsString()); - } else { - str = String.format("%-7s (%.3f%%) %s #%s", - String.valueOf(c.getAmount()), - c.getAmount() / (double) size * 100, - name, - c.getID().getBlockType().getId()); - } - player.print(str); + player.print("# total blocks: " + size); + + for (Countable c : distribution) { + String name = c.getID().getBlockType().getName(); + String str; + if (useData) { + str = String.format("%-7s (%.3f%%) %s #%s", + String.valueOf(c.getAmount()), + c.getAmount() / (double) size * 100, + name, + c.getID().getAsString()); + } else { + str = String.format("%-7s (%.3f%%) %s #%s", + String.valueOf(c.getAmount()), + c.getAmount() / (double) size * 100, + name, + c.getID().getBlockType().getId()); + } + player.print(str); + } + }); } } 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 0f95e52657..bb8865bd8a 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 @@ -104,8 +104,8 @@ public void fill(Player player, LocalSession session, EditSession editSession, C int depth = args.argsLength() > 2 ? Math.max(1, args.getInteger(2)) : 1; Vector pos = session.getPlacementPosition(player); - int affected = editSession.fillXZ(pos, pattern, radius, depth, false); - player.print(affected + " block(s) have been created."); + editSession.fillXZ(player, pos, pattern, radius, depth, false) + .withConsumer(affected -> player.print(affected + " block(s) have been created.")); } @Command( @@ -130,13 +130,8 @@ public void fillr(Player player, LocalSession session, EditSession editSession, int depth = args.argsLength() > 2 ? Math.max(1, args.getInteger(2)) : Integer.MAX_VALUE; Vector pos = session.getPlacementPosition(player); - int affected = 0; - if (pattern instanceof BlockPattern) { - affected = editSession.fillXZ(pos, ((BlockPattern) pattern).getBlock(), radius, depth, true); - } else { - affected = editSession.fillXZ(pos, pattern, radius, depth, true); - } - player.print(affected + " block(s) have been created."); + editSession.fillXZ(player, pos, pattern, radius, depth, true) + .withConsumer(affected -> player.print(affected + " block(s) have been created.")); } @Command( @@ -170,8 +165,8 @@ public void fixLava(Player player, LocalSession session, EditSession editSession double radius = Math.max(0, args.getDouble(0)); we.checkMaxRadius(radius); - int affected = editSession.fixLiquid(session.getPlacementPosition(player), radius, BlockTypes.LAVA); - player.print(affected + " block(s) have been changed."); + editSession.fixLiquid(player, session.getPlacementPosition(player), radius, BlockTypes.LAVA) + .withConsumer(affected -> player.print(affected + " block(s) have been changed.")); } @Command( @@ -187,8 +182,8 @@ public void fixWater(Player player, LocalSession session, EditSession editSessio double radius = Math.max(0, args.getDouble(0)); we.checkMaxRadius(radius); - int affected = editSession.fixLiquid(session.getPlacementPosition(player), radius, BlockTypes.WATER); - player.print(affected + " block(s) have been changed."); + editSession.fixLiquid(player, session.getPlacementPosition(player), radius, BlockTypes.WATER) + .withConsumer(affected -> player.print(affected + " block(s) have been changed.")); } @Command( diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/task/Task.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/task/Task.java index 1e57adb29c..c835e17b0e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/task/Task.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/task/Task.java @@ -22,6 +22,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.operation.RunContext; @@ -114,6 +115,19 @@ public Task withStatusConsumer(Consumer statusConsumer) { return this; } + /** + * Adds exception and status consumers to this actor. + * + * @param actor The actor + * @return The task, for chaining + */ + public Task addActorConsumers(Actor actor) { + checkNotNull(actor); + onExcept(e -> actor.printError(e.getMessage())); + withStatusConsumer(actor::print); + return this; + } + /** * Resume the task with a given context. *