Added a few new things using block states.

* `//set ##*tag` sets all states in the tag (not just default state per type)
* `//set ^type` is a pattern changing block type but copying all valid existing states
* `//set ^[prop=val,...]` sets the property `prop` to `val` wherever the existing block has that property
* `//set ^type[prop=val,...]` does both of the above
Those work anywhere a pattern is taken, of course.

* The mask syntax `^[prop=val]` matches blocks with the property `prop` set to `val`, or blocks that don't have the property at all.
* The mask syntax `^=[prop=val]` only matches blocks that have the property.
Those work anywhere a mask is taken, of course. (`//mask`, `//gmask`, `//replace`, etc)

The `//drain` command now takes `-w` flag that removes the waterlogged state from blocks (in addition to removing water, as before).
This commit is contained in:
wizjany
2019-02-12 16:39:09 -05:00
parent 1ae0e88b63
commit 88014b18a3
19 changed files with 711 additions and 49 deletions

View File

@ -0,0 +1,39 @@
/*
* 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.pattern;
import com.sk89q.worldedit.extent.Extent;
import static com.google.common.base.Preconditions.checkNotNull;
public abstract class AbstractExtentPattern extends AbstractPattern implements ExtentPattern {
private final Extent extent;
public AbstractExtentPattern(Extent extent) {
this.extent = extent;
checkNotNull(extent);
}
@Override
public Extent getExtent() {
return extent;
}
}

View File

@ -0,0 +1,66 @@
/*
* 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.pattern;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.extent.buffer.ExtentBuffer;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;
import static com.google.common.base.Preconditions.checkArgument;
/**
* A pattern that composes multiple patterns consecutively, ensuring that changes from one
* pattern are realized by the subsequent one(s). For best results, use an {@link ExtentBuffer}
* to avoid changing blocks in an underlying extent (e.g. the world).
*/
public class ExtentBufferedCompositePattern extends AbstractExtentPattern {
private final Pattern[] patterns;
/**
* Construct a new instance of this pattern.
*
* <p>Note that all patterns passed which are ExtentPatterns should use the same extent as the one
* passed to this constructor, or block changes may not be realized by those patterns.</p>
*
* @param extent the extent to buffer changes to
* @param patterns the patterns to apply, in order
*/
public ExtentBufferedCompositePattern(Extent extent, Pattern... patterns) {
super(extent);
checkArgument(patterns.length != 0, "patterns cannot be empty");
this.patterns = patterns;
}
@Override
public BaseBlock apply(BlockVector3 position) {
BaseBlock lastBlock = null;
for (Pattern pattern : patterns) {
lastBlock = pattern.apply(position);
try {
getExtent().setBlock(position, lastBlock);
} catch (WorldEditException ignored) { // buffer doesn't throw
}
}
return lastBlock;
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.pattern;
import com.sk89q.worldedit.extent.Extent;
public interface ExtentPattern extends Pattern {
/**
* Get the extent associated with this pattern.
*
* @return the extent for this pattern
*/
public Extent getExtent();
}

View File

@ -28,10 +28,9 @@ import com.sk89q.worldedit.world.block.BaseBlock;
/**
* Returns the blocks from {@link Extent}, repeating when out of bounds.
*/
public class RepeatingExtentPattern extends AbstractPattern {
public class RepeatingExtentPattern extends AbstractExtentPattern {
private final BlockVector3 size;
private Extent extent;
private BlockVector3 origin;
private BlockVector3 offset;
@ -42,31 +41,12 @@ public class RepeatingExtentPattern extends AbstractPattern {
* @param offset the offset
*/
public RepeatingExtentPattern(Extent extent, BlockVector3 origin, BlockVector3 offset) {
setExtent(extent);
super(extent);
setOrigin(origin);
setOffset(offset);
size = extent.getMaximumPoint().subtract(extent.getMinimumPoint()).add(1, 1, 1);
}
/**
* Get the extent.
*
* @return the extent
*/
public Extent getExtent() {
return extent;
}
/**
* Set the extent.
*
* @param extent the extent
*/
public void setExtent(Extent extent) {
checkNotNull(extent);
this.extent = extent;
}
/**
* Get the offset.
*
@ -111,7 +91,7 @@ public class RepeatingExtentPattern extends AbstractPattern {
int x = Math.abs(base.getBlockX()) % size.getBlockX();
int y = Math.abs(base.getBlockY()) % size.getBlockY();
int z = Math.abs(base.getBlockZ()) % size.getBlockZ();
return extent.getFullBlock(BlockVector3.at(x, y, z).add(origin));
return getExtent().getFullBlock(BlockVector3.at(x, y, z).add(origin));
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.pattern;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import java.util.Map;
import java.util.Map.Entry;
import static com.sk89q.worldedit.blocks.Blocks.resolveProperties;
public class StateApplyingPattern extends AbstractExtentPattern {
private final Map<String, String> states;
private Map<BlockType, Map<Property<Object>, Object>> cache = Maps.newHashMap();
public StateApplyingPattern(Extent extent, Map<String, String> statesToSet) {
super(extent);
this.states = statesToSet;
}
@Override
public BaseBlock apply(BlockVector3 position) {
BlockState block = getExtent().getBlock(position);
for (Entry<Property<Object>, Object> entry : cache
.computeIfAbsent(block.getBlockType(), (b -> resolveProperties(states, b))).entrySet()) {
block = block.with(entry.getKey(), entry.getValue());
}
return block.toBaseBlock();
}
}

View File

@ -0,0 +1,52 @@
/*
* 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.pattern;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import java.util.Map.Entry;
/**
* Applies a block type while retaining all possible states.
*/
public class TypeApplyingPattern extends AbstractExtentPattern {
private final BlockState blockState;
public TypeApplyingPattern(Extent extent, BlockState blockState) {
super(extent);
this.blockState = blockState;
}
@Override
public BaseBlock apply(BlockVector3 position) {
BlockState oldBlock = getExtent().getBlock(position);
BlockState newBlock = blockState;
for (Entry<Property<?>, Object> entry : oldBlock.getStates().entrySet()) {
@SuppressWarnings("unchecked")
Property<Object> prop = (Property<Object>) entry.getKey();
newBlock = newBlock.with(prop, entry.getValue());
}
return newBlock.toBaseBlock();
}
}

View File

@ -0,0 +1,47 @@
/*
* 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.pattern;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes;
/**
* Removes the waterlogged state from blocks if possible. If not possible, returns air.
*/
public class WaterloggedRemover extends AbstractExtentPattern {
public WaterloggedRemover(Extent extent) {
super(extent);
}
@Override
public BaseBlock apply(BlockVector3 position) {
BaseBlock block = getExtent().getFullBlock(position);
@SuppressWarnings("unchecked")
Property<Object> prop = (Property<Object>) block.getBlockType().getPropertyMap().getOrDefault("waterlogged", null);
if (prop != null) {
return block.with(prop, false);
}
return BlockTypes.AIR.getDefaultState().toBaseBlock();
}
}