Skip to content

Commit

Permalink
Added a few more commands to the ones in the task queue test. Now it'…
Browse files Browse the repository at this point in the history
…s all liquid commands.
  • Loading branch information
me4502 committed Aug 24, 2018
1 parent 1a20105 commit d7ceef3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 57 deletions.
49 changes: 34 additions & 15 deletions worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java
Expand Up @@ -613,30 +613,30 @@ public int countBlocks(Region region, Set<BlockStateHolder> 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<Integer> 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<Integer> fillXZ(@Nullable Actor actor, Vector origin, Pattern pattern, double radius, int depth, boolean recursive) {
checkNotNull(origin);
checkNotNull(pattern);
checkArgument(radius >= 0, "radius >= 0");
Expand Down Expand Up @@ -664,9 +664,14 @@ public int fillXZ(Vector origin, Pattern pattern, double radius, int depth, bool
visitor.visit(origin);

// Execute
Operations.completeLegacy(visitor);
Task<Integer> task = Operations.<Integer>completeQueued(visitor, actor, this)
.withSupplier(visitor::getAffected);

return visitor.getAffected();
if (actor != null) {
task.addActorConsumers(actor);
}

return task;
}

/**
Expand Down Expand Up @@ -1127,8 +1132,7 @@ public Task<Integer> 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;
Expand All @@ -1137,13 +1141,14 @@ public Task<Integer> 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<Integer> fixLiquid(@Nullable Actor actor, Vector origin, double radius, BlockType fluid) throws MaxChangedBlocksException {
checkNotNull(origin);
checkArgument(radius >= 0, "radius >= 0 required");

Expand All @@ -1170,9 +1175,14 @@ public int fixLiquid(Vector origin, double radius, BlockType fluid) throws MaxCh
}
}

Operations.completeLegacy(visitor);
Task<Integer> task = Operations.<Integer>completeQueued(visitor, actor, this)
.withSupplier(visitor::getAffected);

return visitor.getAffected();
if (actor != null) {
task.addActorConsumers(actor);
}

return task;
}

/**
Expand Down Expand Up @@ -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<Countable<BlockStateHolder>> getBlockDistribution(Region region, boolean fuzzy) {
public Task<List<Countable<BlockStateHolder>>> 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<List<Countable<BlockStateHolder>>> task = Operations.<List<Countable<BlockStateHolder>>>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 {
Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -662,40 +661,40 @@ public void distr(Player player, LocalSession session, EditSession editSession,

int size;
boolean useData = args.hasFlag('d');
List<Countable<BlockStateHolder>> 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<BlockStateHolder> 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<BlockStateHolder> 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);
}
});
}
}

Expand Down
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -114,6 +115,19 @@ public Task<T> withStatusConsumer(Consumer<String> statusConsumer) {
return this;
}

/**
* Adds exception and status consumers to this actor.
*
* @param actor The actor
* @return The task, for chaining
*/
public Task<T> addActorConsumers(Actor actor) {
checkNotNull(actor);
onExcept(e -> actor.printError(e.getMessage()));
withStatusConsumer(actor::print);
return this;
}

/**
* Resume the task with a given context.
*
Expand Down

0 comments on commit d7ceef3

Please sign in to comment.