Added a snow brush (#1462)

* Added a snow brush

* Re-add the Apply class for deprecation reasons

* Use the context extent rather than an EditSession

* Only freeze water source blocks.

* Fixed the bind message for snow stacker

* Fixed PR issues
This commit is contained in:
Matthew Miller 2020-08-09 14:26:26 +10:00 committed by GitHub
parent 1b91b8f596
commit 5e5f1c21e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 219 additions and 17 deletions

View File

@ -33,7 +33,7 @@
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.factory.Apply;
import com.sk89q.worldedit.function.factory.ApplyRegion;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.annotation.Direction;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
@ -98,7 +98,7 @@ private void setApplyBrush(CommandParameters parameters, Player player, LocalSes
double radius = requireNonNull(RADIUS.value(parameters).asSingle(double.class));
RegionFactory regionFactory = REGION_FACTORY.value(parameters).asSingle(RegionFactory.class);
BrushCommands.setOperationBasedBrush(player, localSession, radius,
new Apply(generatorFactory), regionFactory, "worldedit.brush.apply");
new ApplyRegion(generatorFactory), regionFactory, "worldedit.brush.apply");
}
@Command(

View File

@ -42,9 +42,11 @@
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.factory.Apply;
import com.sk89q.worldedit.function.factory.ApplyLayer;
import com.sk89q.worldedit.function.factory.ApplyRegion;
import com.sk89q.worldedit.function.factory.Deform;
import com.sk89q.worldedit.function.factory.Paint;
import com.sk89q.worldedit.function.factory.Snow;
import com.sk89q.worldedit.function.mask.BlockTypeMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.operation.Operation;
@ -52,6 +54,7 @@
import com.sk89q.worldedit.internal.annotation.ClipboardMask;
import com.sk89q.worldedit.internal.annotation.VertHeight;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.factory.CylinderRegionFactory;
import com.sk89q.worldedit.regions.factory.RegionFactory;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.session.request.RequestExtent;
@ -360,7 +363,7 @@ public void set(Player player, LocalSession localSession,
@Arg(desc = "The pattern of blocks to set")
Pattern pattern) throws WorldEditException {
setOperationBasedBrush(player, localSession, radius,
new Apply(new ReplaceFactory(pattern)), shape, "worldedit.brush.set");
new ApplyRegion(new ReplaceFactory(pattern)), shape, "worldedit.brush.set");
}
@Command(
@ -409,6 +412,27 @@ public void lower(Player player, LocalSession localSession,
new Deform("y+=1"), shape, "worldedit.brush.lower");
}
@Command(
name = "snow",
desc = "Snow brush, sets snow in the area"
)
@CommandPermissions("worldedit.brush.snow")
public void snow(Player player, LocalSession localSession,
@Arg(desc = "The shape of the region")
RegionFactory shape,
@Arg(desc = "The size of the brush", def = "5")
double radius,
@Switch(name = 's', desc = "Whether to stack snow")
boolean stack) throws WorldEditException {
if (shape instanceof CylinderRegionFactory) {
shape = new CylinderRegionFactory(radius);
}
setOperationBasedBrush(player, localSession, radius,
new ApplyLayer(new Snow(stack)), shape, "worldedit.brush.snow");
}
static void setOperationBasedBrush(Player player, LocalSession session, double radius,
Contextual<? extends Operation> factory,
RegionFactory shape,

View File

@ -19,8 +19,8 @@
package com.sk89q.worldedit.function.block;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.LayerFunction;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property;
@ -34,14 +34,15 @@ public class SnowSimulator implements LayerFunction {
private final BlockState snowBlock = BlockTypes.SNOW_BLOCK.getDefaultState();
private final Property<Integer> snowLayersProperty = BlockTypes.SNOW.getProperty("layers");
private final Property<Integer> waterLevelProperty = BlockTypes.WATER.getProperty("level");
private final EditSession editSession;
private final Extent extent;
private final boolean stack;
private int affected;
public SnowSimulator(EditSession editSession, boolean stack) {
this.editSession = editSession;
public SnowSimulator(Extent extent, boolean stack) {
this.extent = extent;
this.stack = stack;
this.affected = 0;
@ -53,7 +54,7 @@ public int getAffected() {
@Override
public boolean isGround(BlockVector3 position) {
BlockState block = this.editSession.getBlock(position);
BlockState block = this.extent.getBlock(position);
// We're returning the first block we can place *on top of*
if (block.getBlockType().getMaterial().isAir() || (stack && block.getBlockType() == BlockTypes.SNOW)) {
@ -77,22 +78,24 @@ public boolean apply(BlockVector3 position, int depth) throws WorldEditException
return false;
}
BlockState block = this.editSession.getBlock(position);
BlockState block = this.extent.getBlock(position);
if (block.getBlockType() == BlockTypes.WATER) {
if (this.editSession.setBlock(position, ice)) {
affected++;
if (block.getState(waterLevelProperty) == 0) {
if (this.extent.setBlock(position, ice)) {
affected++;
}
}
return false;
}
// Can't put snow this far up
if (position.getBlockY() == this.editSession.getMaximumPoint().getBlockY()) {
if (position.getBlockY() == this.extent.getMaximumPoint().getBlockY()) {
return false;
}
BlockVector3 abovePosition = position.add(0, 1, 0);
BlockState above = this.editSession.getBlock(abovePosition);
BlockState above = this.extent.getBlock(abovePosition);
// Can only replace air (or snow in stack mode)
if (!above.getBlockType().getMaterial().isAir() && (!stack || above.getBlockType() != BlockTypes.SNOW)) {
@ -103,17 +106,17 @@ public boolean apply(BlockVector3 position, int depth) throws WorldEditException
int currentHeight = above.getState(snowLayersProperty);
// We've hit the highest layer (If it doesn't contain current + 2 it means it's 1 away from full)
if (!snowLayersProperty.getValues().contains(currentHeight + 2)) {
if (this.editSession.setBlock(abovePosition, snowBlock)) {
if (this.extent.setBlock(abovePosition, snowBlock)) {
this.affected++;
}
} else {
if (this.editSession.setBlock(abovePosition, above.with(snowLayersProperty, currentHeight + 1))) {
if (this.extent.setBlock(abovePosition, above.with(snowLayersProperty, currentHeight + 1))) {
this.affected++;
}
}
return false;
}
if (this.editSession.setBlock(abovePosition, snow)) {
if (this.extent.setBlock(abovePosition, snow)) {
this.affected++;
}
return false;

View File

@ -30,6 +30,13 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.util.GuavaUtil.firstNonNull;
/**
* Creates an operation from a region context.
*
* @deprecated Use {@link ApplyRegion} or {@link ApplyLayer}
* depending on function type.
*/
@Deprecated
public class Apply implements Contextual<Operation> {
private final Region region;

View File

@ -0,0 +1,65 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.factory;
import com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.EditContext;
import com.sk89q.worldedit.function.LayerFunction;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.visitor.LayerVisitor;
import com.sk89q.worldedit.regions.FlatRegion;
import com.sk89q.worldedit.regions.NullRegion;
import com.sk89q.worldedit.regions.Regions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.util.GuavaUtil.firstNonNull;
public class ApplyLayer implements Contextual<Operation> {
private final FlatRegion region;
private final Contextual<? extends LayerFunction> function;
public ApplyLayer(Contextual<? extends LayerFunction> function) {
this(Regions.asFlatRegion(new NullRegion()), function);
}
public ApplyLayer(FlatRegion region, Contextual<? extends LayerFunction> function) {
checkNotNull(region, "region");
checkNotNull(function, "function");
this.region = region;
this.function = function;
}
@Override
public Operation createFromContext(EditContext context) {
FlatRegion localRegion = Regions.asFlatRegion(firstNonNull(context.getRegion(), region));
return new LayerVisitor(localRegion,
localRegion.getMinimumPoint().getY(),
localRegion.getMaximumPoint().getY(),
function.createFromContext(context));
}
@Override
public String toString() {
return "set " + function;
}
}

View File

@ -0,0 +1,59 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.factory;
import com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.EditContext;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.visitor.RegionVisitor;
import com.sk89q.worldedit.regions.NullRegion;
import com.sk89q.worldedit.regions.Region;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.util.GuavaUtil.firstNonNull;
public class ApplyRegion implements Contextual<Operation> {
private final Region region;
private final Contextual<? extends RegionFunction> function;
public ApplyRegion(Contextual<? extends RegionFunction> function) {
this(new NullRegion(), function);
}
public ApplyRegion(Region region, Contextual<? extends RegionFunction> function) {
checkNotNull(region, "region");
checkNotNull(function, "function");
this.region = region;
this.function = function;
}
@Override
public Operation createFromContext(EditContext context) {
return new RegionVisitor(firstNonNull(context.getRegion(), region), function.createFromContext(context));
}
@Override
public String toString() {
return "set " + function;
}
}

View File

@ -0,0 +1,44 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.factory;
import com.sk89q.worldedit.function.Contextual;
import com.sk89q.worldedit.function.EditContext;
import com.sk89q.worldedit.function.LayerFunction;
import com.sk89q.worldedit.function.block.SnowSimulator;
public class Snow implements Contextual<LayerFunction> {
private final boolean stack;
public Snow(boolean stack) {
this.stack = stack;
}
@Override
public LayerFunction createFromContext(EditContext context) {
return new SnowSimulator(context.getDestination(), this.stack);
}
@Override
public String toString() {
return "snow" + (this.stack ? " stacker" : "");
}
}