Skip to content

Commit

Permalink
Strict mode for BlockStateMask.
Browse files Browse the repository at this point in the history
When true, it will only match blocks that have the desired properties.
When false, it will only fail blocks that have the properties but incorrect values.
  • Loading branch information
wizjany committed Feb 13, 2019
1 parent 2adfc2e commit 4d8da66
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ public int drainArea(BlockVector3 origin, double radius, boolean waterlogged) th
getWorld().createLiquidMask(),
new BlockStateMask(this, new HashMap<String, String>() {{
put("waterlogged", "true");
}}))
}}, true))
: getWorld().createLiquidMask());

BlockReplace replace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ public BlockStateMaskParser(WorldEdit worldEdit) {

@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
if (!input.startsWith("^[") || !input.endsWith("]")) {
if (!(input.startsWith("^[") || input.startsWith("^=[")) || !input.endsWith("]")) {
return null;
}
Extent extent = Request.request().getEditSession();

String states = input.substring(2, input.length() - 1);
boolean strict = input.charAt(1) == '=';
String states = input.substring(2 + (strict ? 1 : 0), input.length() - 1);
try {
return new BlockStateMask(extent,
Splitter.on(',').omitEmptyStrings().trimResults().withKeyValueSeparator('=').split(states));
Splitter.on(',').omitEmptyStrings().trimResults().withKeyValueSeparator('=').split(states),
strict);
} catch (Exception e) {
throw new InputParseException("Invalid states.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public class BlockStateMask extends AbstractExtentMask {
private final boolean strict;
private Map<BlockType, Map<Property<Object>, Object>> cache = Maps.newHashMap();

/**
* Creates a mask that checks if a given block has the desired properties set to the desired value.
*
* @param extent the extent to get blocks from
* @param states the desired states (property -> value) that a block should have to match the mask
* @param strict true to only match blocks that have all properties and values, false to also match blocks that
* do not have the properties (but only fail blocks with the properties but wrong values)
*/
public BlockStateMask(Extent extent, Map<String, String> states, boolean strict) {
super(extent);
this.states = states;
Expand Down

0 comments on commit 4d8da66

Please sign in to comment.