Skip to content

Commit

Permalink
Added an AdjacentMask, with a parser for an Exposed/Surface mask
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Sep 10, 2020
1 parent 15467e9 commit 65cd1da
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
Expand Up @@ -26,6 +26,7 @@
import com.sk89q.worldedit.extension.factory.parser.mask.BlockStateMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.BlocksMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.ExistingMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.ExposedMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.ExpressionMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.LazyRegionMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.NegateMaskParser;
Expand Down Expand Up @@ -66,6 +67,7 @@ public MaskFactory(WorldEdit worldEdit) {

register(new ExistingMaskParser(worldEdit));
register(new AirMaskParser(worldEdit));
register(new ExposedMaskParser(worldEdit));
register(new SolidMaskParser(worldEdit));
register(new LazyRegionMaskParser(worldEdit));
register(new RegionMaskParser(worldEdit));
Expand Down
@@ -0,0 +1,51 @@
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.extension.factory.parser.mask;

import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.mask.AdjacentMask;
import com.sk89q.worldedit.function.mask.ExistingBlockMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.internal.registry.SimpleInputParser;

import java.util.List;

public class ExposedMaskParser extends SimpleInputParser<Mask> {

private final List<String> aliases = ImmutableList.of("#exposed", "#surface");

public ExposedMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}

@Override
public List<String> getMatchedAliases() {
return this.aliases;
}

@Override
public Mask parseFromSimpleInput(String input, ParserContext context) throws InputParseException {
return new AdjacentMask(Masks.negate(new ExistingBlockMask(context.requireExtent())), true);
}
}
@@ -0,0 +1,71 @@
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.function.mask;

import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.util.Direction;

import java.util.List;
import javax.annotation.Nullable;

/**
* Checks whether any face of the given position is adjacent to a block matching a given mask.
*/
public class AdjacentMask extends AbstractMask {

private static final List<Direction> DIRECTION_LIST = ImmutableList.copyOf(
Direction.valuesOf(Direction.Flag.CARDINAL | Direction.Flag.UPRIGHT)
);

private final Mask mask;
private final boolean excludeSelf;

/**
* Create a new instance.
*
* @param mask the mask to test against
* @param excludeSelf excludes blocks where the mask matches itself
*/
public AdjacentMask(Mask mask, boolean excludeSelf) {
this.mask = mask;
this.excludeSelf = excludeSelf;
}

@Override
public boolean test(BlockVector3 vector) {
if (excludeSelf && mask.test(vector)) {
return false;
}
for (Direction direction : DIRECTION_LIST) {
if (mask.test(vector.add(direction.toBlockVector()))) {
return true;
}
}

return false;
}

@Nullable
@Override
public Mask2D toMask2D() {
return null;
}
}

0 comments on commit 65cd1da

Please sign in to comment.