Permalink
Browse files

Start work on modularising masks and patterns

  • Loading branch information...
me4502 committed Dec 23, 2018
1 parent d6977ae commit c5d9aadab872e6c09600d3326c7bda983cdee8d3
Showing with 557 additions and 122 deletions.
  1. +2 −1 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/BlockFactory.java
  2. +2 −1 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/ItemFactory.java
  3. +52 −1 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/MaskFactory.java
  4. +6 −3 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java
  5. +3 −3 ...edit-core/src/main/java/com/sk89q/worldedit/extension/factory/{ → parser}/DefaultBlockParser.java
  6. +2 −2 ...dedit-core/src/main/java/com/sk89q/worldedit/extension/factory/{ → parser}/DefaultItemParser.java
  7. +55 −0 ...core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockCategoryMaskParser.java
  8. +6 −82 ...-core/src/main/java/com/sk89q/worldedit/extension/factory/{ → parser/mask}/DefaultMaskParser.java
  9. +51 −0 ...edit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExistingMaskParser.java
  10. +48 −0 ...it-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/LazyRegionMaskParser.java
  11. +47 −0 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NegateMaskParser.java
  12. +45 −0 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/NoiseMaskParser.java
  13. +52 −0 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/RegionMaskParser.java
  14. +51 −0 worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/SolidMaskParser.java
  15. +21 −21 ...ldedit/extension/factory/{HashTagPatternParser.java → parser/pattern/ClipboardPatternParser.java}
  16. +4 −3 .../src/main/java/com/sk89q/worldedit/extension/factory/{ → parser/pattern}/RandomPatternParser.java
  17. +3 −3 ...main/java/com/sk89q/worldedit/extension/factory/{ → parser/pattern}/SingleBlockPatternParser.java
  18. +24 −1 worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/AbstractFactory.java
  19. +12 −1 worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/InputParser.java
  20. +71 −0 worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/SimpleInputParser.java
@@ -21,6 +21,7 @@

import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.factory.parser.DefaultBlockParser;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
@@ -47,7 +48,7 @@
public BlockFactory(WorldEdit worldEdit) {
super(worldEdit);

parsers.add(new DefaultBlockParser(worldEdit));
register(new DefaultBlockParser(worldEdit));
}

/**
@@ -21,6 +21,7 @@

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.extension.factory.parser.DefaultItemParser;
import com.sk89q.worldedit.internal.registry.AbstractFactory;

public class ItemFactory extends AbstractFactory<BaseItem> {
@@ -33,7 +34,7 @@
public ItemFactory(WorldEdit worldEdit) {
super(worldEdit);

parsers.add(new DefaultItemParser(worldEdit));
register(new DefaultItemParser(worldEdit));
}

}
@@ -20,8 +20,24 @@
package com.sk89q.worldedit.extension.factory;

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.factory.parser.mask.BlockCategoryMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.DefaultMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.ExistingMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.LazyRegionMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.NegateMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.NoiseMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.RegionMaskParser;
import com.sk89q.worldedit.extension.factory.parser.mask.SolidMaskParser;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.MaskIntersection;
import com.sk89q.worldedit.internal.registry.AbstractFactory;
import com.sk89q.worldedit.internal.registry.InputParser;

import java.util.ArrayList;
import java.util.List;

/**
* A registry of known {@link Mask}s. Provides methods to instantiate
@@ -40,7 +56,42 @@
public MaskFactory(WorldEdit worldEdit) {
super(worldEdit);

parsers.add(new DefaultMaskParser(worldEdit));
register(new ExistingMaskParser(worldEdit));
register(new SolidMaskParser(worldEdit));
register(new LazyRegionMaskParser(worldEdit));
register(new RegionMaskParser(worldEdit));
register(new BlockCategoryMaskParser(worldEdit));
register(new NoiseMaskParser(worldEdit));
register(new NegateMaskParser(worldEdit));
register(new DefaultMaskParser(worldEdit));
}

@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
List<Mask> masks = new ArrayList<>();

for (String component : input.split(" ")) {
if (component.isEmpty()) {
continue;
}

for (InputParser<Mask> parser : getParsers()) {
Mask match = parser.parseFromInput(component, context);

if (match != null) {
masks.add(match);
}
}
}

switch (masks.size()) {
case 0:
throw new NoMatchException("No match for '" + input + "'");
case 1:
return masks.get(0);
default:
return new MaskIntersection(masks);
}
}

}
@@ -20,6 +20,9 @@
package com.sk89q.worldedit.extension.factory;

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.factory.parser.pattern.ClipboardPatternParser;
import com.sk89q.worldedit.extension.factory.parser.pattern.RandomPatternParser;
import com.sk89q.worldedit.extension.factory.parser.pattern.SingleBlockPatternParser;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.registry.AbstractFactory;

@@ -40,9 +43,9 @@
public PatternFactory(WorldEdit worldEdit) {
super(worldEdit);

parsers.add(new HashTagPatternParser(worldEdit));
parsers.add(new SingleBlockPatternParser(worldEdit));
parsers.add(new RandomPatternParser(worldEdit));
register(new ClipboardPatternParser(worldEdit));
register(new SingleBlockPatternParser(worldEdit));
register(new RandomPatternParser(worldEdit));
}

}
@@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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

import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.NotABlockException;
@@ -52,9 +52,9 @@
/**
* Parses block input strings.
*/
class DefaultBlockParser extends InputParser<BlockStateHolder> {
public class DefaultBlockParser extends InputParser<BlockStateHolder> {

protected DefaultBlockParser(WorldEdit worldEdit) {
public DefaultBlockParser(WorldEdit worldEdit) {
super(worldEdit);
}

@@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem;
@@ -30,7 +30,7 @@

public class DefaultItemParser extends InputParser<BaseItem> {

protected DefaultItemParser(WorldEdit worldEdit) {
public DefaultItemParser(WorldEdit worldEdit) {
super(worldEdit);
}

@@ -0,0 +1,55 @@
/*
* 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.extension.factory.parser.mask;

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.BlockCategoryMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.world.block.BlockCategories;
import com.sk89q.worldedit.world.block.BlockCategory;

public class BlockCategoryMaskParser extends InputParser<Mask> {

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

@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.startsWith("##")) {
Extent extent = Request.request().getEditSession();

// This means it's a tag mask.
BlockCategory category = BlockCategories.get(input.substring(2).toLowerCase());
if (category == null) {
throw new InputParseException("Unrecognised tag '" + input.substring(2) + '\'');
} else {
return new BlockCategoryMask(extent, category);
}
}

return null;
}
}
@@ -17,121 +17,55 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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

import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.BiomeMask2D;
import com.sk89q.worldedit.function.mask.BlockCategoryMask;
import com.sk89q.worldedit.function.mask.BlockMask;
import com.sk89q.worldedit.function.mask.ExistingBlockMask;
import com.sk89q.worldedit.function.mask.ExpressionMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.MaskIntersection;
import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.function.mask.NoiseFilter;
import com.sk89q.worldedit.function.mask.OffsetMask;
import com.sk89q.worldedit.function.mask.RegionMask;
import com.sk89q.worldedit.function.mask.SolidBlockMask;
import com.sk89q.worldedit.internal.expression.Expression;
import com.sk89q.worldedit.internal.expression.ExpressionException;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.math.noise.RandomNoise;
import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment;
import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.session.request.RequestSelection;
import com.sk89q.worldedit.world.biome.BaseBiome;
import com.sk89q.worldedit.world.biome.Biomes;
import com.sk89q.worldedit.world.block.BlockCategories;
import com.sk89q.worldedit.world.block.BlockCategory;
import com.sk89q.worldedit.world.registry.BiomeRegistry;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Parses mask input strings.
*/
class DefaultMaskParser extends InputParser<Mask> {
public class DefaultMaskParser extends InputParser<Mask> {

protected DefaultMaskParser(WorldEdit worldEdit) {
public DefaultMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}

@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
List<Mask> masks = new ArrayList<>();

for (String component : input.split(" ")) {
if (component.isEmpty()) {
continue;
}

Mask current = getBlockMaskComponent(masks, component, context);

masks.add(current);
}

switch (masks.size()) {
case 0:
return null;

case 1:
return masks.get(0);

default:
return new MaskIntersection(masks);
}
}

private Mask getBlockMaskComponent(List<Mask> masks, String component, ParserContext context) throws InputParseException {
public Mask parseFromInput(String component, ParserContext context) throws InputParseException {
Extent extent = Request.request().getEditSession();

final char firstChar = component.charAt(0);
switch (firstChar) {
case '#':
if (component.equalsIgnoreCase("#existing")) {
return new ExistingBlockMask(extent);
} else if (component.equalsIgnoreCase("#solid")) {
return new SolidBlockMask(extent);
} else if (component.equalsIgnoreCase("#dregion")
|| component.equalsIgnoreCase("#dselection")
|| component.equalsIgnoreCase("#dsel")) {
return new RegionMask(new RequestSelection());
} else if (component.equalsIgnoreCase("#selection")
|| component.equalsIgnoreCase("#region")
|| component.equalsIgnoreCase("#sel")) {
try {
return new RegionMask(context.requireSession().getSelection(context.requireWorld()).clone());
} catch (IncompleteRegionException e) {
throw new InputParseException("Please make a selection first.");
}
} else if (component.startsWith("##")) {
// This means it's a tag mask.
BlockCategory category = BlockCategories.get(component.substring(2).toLowerCase());
if (category == null) {
throw new NoMatchException("Unrecognised tag '" + component.substring(2) + '\'');
} else {
return new BlockCategoryMask(extent, category);
}
} else {
throw new NoMatchException("Unrecognized mask '" + component + '\'');
}

case '>':
case '<':
Mask submask;
if (component.length() > 1) {
submask = getBlockMaskComponent(masks, component.substring(1), context);
submask = worldEdit.getMaskFactory().parseFromInput(component.substring(1), context);
} else {
submask = new ExistingBlockMask(extent);
}
@@ -141,8 +75,7 @@ private Mask getBlockMaskComponent(List<Mask> masks, String component, ParserCon
case '$':
Set<BaseBiome> biomes = new HashSet<>();
String[] biomesList = component.substring(1).split(",");
BiomeRegistry biomeRegistry = WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry();
BiomeRegistry biomeRegistry = worldEdit.getPlatformManager().queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry();
List<BaseBiome> knownBiomes = biomeRegistry.getBiomes();
for (String biomeName : biomesList) {
BaseBiome biome = Biomes.findBiomeByName(knownBiomes, biomeName, biomeRegistry);
@@ -154,10 +87,6 @@ private Mask getBlockMaskComponent(List<Mask> masks, String component, ParserCon

return Masks.asMask(new BiomeMask2D(context.requireExtent(), biomes));

case '%':
int i = Integer.parseInt(component.substring(1));
return new NoiseFilter(new RandomNoise(), ((double) i) / 100);

case '=':
try {
Expression exp = Expression.compile(component.substring(1), "x", "y", "z");
@@ -169,11 +98,6 @@ private Mask getBlockMaskComponent(List<Mask> masks, String component, ParserCon
throw new InputParseException("Invalid expression: " + e.getMessage());
}

case '!':
if (component.length() > 1) {
return Masks.negate(getBlockMaskComponent(masks, component.substring(1), context));
}

default:
ParserContext tempContext = new ParserContext(context);
tempContext.setRestricted(false);
Oops, something went wrong.

0 comments on commit c5d9aad

Please sign in to comment.