Start work on modularising masks and patterns

This commit is contained in:
Matthew Miller 2018-12-23 18:56:26 +10:00
parent d6977aeae4
commit c5d9aadab8
20 changed files with 557 additions and 122 deletions

View File

@ -21,6 +21,7 @@ package com.sk89q.worldedit.extension.factory;
import com.sk89q.util.StringUtil; import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.factory.parser.DefaultBlockParser;
import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.ParserContext;
@ -47,7 +48,7 @@ public class BlockFactory extends AbstractFactory<BlockStateHolder> {
public BlockFactory(WorldEdit worldEdit) { public BlockFactory(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit);
parsers.add(new DefaultBlockParser(worldEdit)); register(new DefaultBlockParser(worldEdit));
} }
/** /**

View File

@ -21,6 +21,7 @@ package com.sk89q.worldedit.extension.factory;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.extension.factory.parser.DefaultItemParser;
import com.sk89q.worldedit.internal.registry.AbstractFactory; import com.sk89q.worldedit.internal.registry.AbstractFactory;
public class ItemFactory extends AbstractFactory<BaseItem> { public class ItemFactory extends AbstractFactory<BaseItem> {
@ -33,7 +34,7 @@ public class ItemFactory extends AbstractFactory<BaseItem> {
public ItemFactory(WorldEdit worldEdit) { public ItemFactory(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit);
parsers.add(new DefaultItemParser(worldEdit)); register(new DefaultItemParser(worldEdit));
} }
} }

View File

@ -20,8 +20,24 @@
package com.sk89q.worldedit.extension.factory; package com.sk89q.worldedit.extension.factory;
import com.sk89q.worldedit.WorldEdit; 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.Mask;
import com.sk89q.worldedit.function.mask.MaskIntersection;
import com.sk89q.worldedit.internal.registry.AbstractFactory; 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 * A registry of known {@link Mask}s. Provides methods to instantiate
@ -40,7 +56,42 @@ public final class MaskFactory extends AbstractFactory<Mask> {
public MaskFactory(WorldEdit worldEdit) { public MaskFactory(WorldEdit worldEdit) {
super(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);
}
} }
} }

View File

@ -20,6 +20,9 @@
package com.sk89q.worldedit.extension.factory; package com.sk89q.worldedit.extension.factory;
import com.sk89q.worldedit.WorldEdit; 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.function.pattern.Pattern;
import com.sk89q.worldedit.internal.registry.AbstractFactory; import com.sk89q.worldedit.internal.registry.AbstractFactory;
@ -40,9 +43,9 @@ public final class PatternFactory extends AbstractFactory<Pattern> {
public PatternFactory(WorldEdit worldEdit) { public PatternFactory(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit);
parsers.add(new HashTagPatternParser(worldEdit)); register(new ClipboardPatternParser(worldEdit));
parsers.add(new SingleBlockPatternParser(worldEdit)); register(new SingleBlockPatternParser(worldEdit));
parsers.add(new RandomPatternParser(worldEdit)); register(new RandomPatternParser(worldEdit));
} }
} }

View File

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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.IncompleteRegionException;
import com.sk89q.worldedit.NotABlockException; import com.sk89q.worldedit.NotABlockException;
@ -52,9 +52,9 @@ import java.util.Map;
/** /**
* Parses block input strings. * 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); super(worldEdit);
} }

View File

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItem;
@ -30,7 +30,7 @@ import com.sk89q.worldedit.world.registry.LegacyMapper;
public class DefaultItemParser extends InputParser<BaseItem> { public class DefaultItemParser extends InputParser<BaseItem> {
protected DefaultItemParser(WorldEdit worldEdit) { public DefaultItemParser(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit);
} }

View File

@ -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;
}
}

View File

@ -17,43 +17,32 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException; 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.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.BiomeMask2D; 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.BlockMask;
import com.sk89q.worldedit.function.mask.ExistingBlockMask; import com.sk89q.worldedit.function.mask.ExistingBlockMask;
import com.sk89q.worldedit.function.mask.ExpressionMask; import com.sk89q.worldedit.function.mask.ExpressionMask;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.MaskIntersection; import com.sk89q.worldedit.function.mask.MaskIntersection;
import com.sk89q.worldedit.function.mask.Masks; 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.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.Expression;
import com.sk89q.worldedit.internal.expression.ExpressionException; import com.sk89q.worldedit.internal.expression.ExpressionException;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3; import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.math.noise.RandomNoise;
import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment; import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment;
import com.sk89q.worldedit.session.request.Request; 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.BaseBiome;
import com.sk89q.worldedit.world.biome.Biomes; 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 com.sk89q.worldedit.world.registry.BiomeRegistry;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -61,77 +50,22 @@ import java.util.Set;
/** /**
* Parses mask input strings. * 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); super(worldEdit);
} }
@Override public Mask parseFromInput(String component, ParserContext context) throws InputParseException {
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 {
Extent extent = Request.request().getEditSession(); Extent extent = Request.request().getEditSession();
final char firstChar = component.charAt(0); final char firstChar = component.charAt(0);
switch (firstChar) { 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 '>':
case '<': case '<':
Mask submask; Mask submask;
if (component.length() > 1) { if (component.length() > 1) {
submask = getBlockMaskComponent(masks, component.substring(1), context); submask = worldEdit.getMaskFactory().parseFromInput(component.substring(1), context);
} else { } else {
submask = new ExistingBlockMask(extent); submask = new ExistingBlockMask(extent);
} }
@ -141,8 +75,7 @@ class DefaultMaskParser extends InputParser<Mask> {
case '$': case '$':
Set<BaseBiome> biomes = new HashSet<>(); Set<BaseBiome> biomes = new HashSet<>();
String[] biomesList = component.substring(1).split(","); String[] biomesList = component.substring(1).split(",");
BiomeRegistry biomeRegistry = WorldEdit.getInstance().getPlatformManager() BiomeRegistry biomeRegistry = worldEdit.getPlatformManager().queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry();
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry();
List<BaseBiome> knownBiomes = biomeRegistry.getBiomes(); List<BaseBiome> knownBiomes = biomeRegistry.getBiomes();
for (String biomeName : biomesList) { for (String biomeName : biomesList) {
BaseBiome biome = Biomes.findBiomeByName(knownBiomes, biomeName, biomeRegistry); BaseBiome biome = Biomes.findBiomeByName(knownBiomes, biomeName, biomeRegistry);
@ -154,10 +87,6 @@ class DefaultMaskParser extends InputParser<Mask> {
return Masks.asMask(new BiomeMask2D(context.requireExtent(), biomes)); 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 '=': case '=':
try { try {
Expression exp = Expression.compile(component.substring(1), "x", "y", "z"); Expression exp = Expression.compile(component.substring(1), "x", "y", "z");
@ -169,11 +98,6 @@ class DefaultMaskParser extends InputParser<Mask> {
throw new InputParseException("Invalid expression: " + e.getMessage()); throw new InputParseException("Invalid expression: " + e.getMessage());
} }
case '!':
if (component.length() > 1) {
return Masks.negate(getBlockMaskComponent(masks, component.substring(1), context));
}
default: default:
ParserContext tempContext = new ParserContext(context); ParserContext tempContext = new ParserContext(context);
tempContext.setRestricted(false); tempContext.setRestricted(false);

View File

@ -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 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.google.common.collect.Lists;
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.ExistingBlockMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import com.sk89q.worldedit.session.request.Request;
import java.util.List;
public class ExistingMaskParser extends SimpleInputParser<Mask> {
public ExistingMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public List<String> getMatchedAliases() {
return Lists.newArrayList("#existing");
}
@Override
public Mask parseFromSimpleInput(String input, ParserContext context) throws InputParseException {
Extent extent = Request.request().getEditSession();
return new ExistingBlockMask(extent);
}
}

View File

@ -0,0 +1,48 @@
/*
* 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.google.common.collect.Lists;
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.Mask;
import com.sk89q.worldedit.function.mask.RegionMask;
import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import com.sk89q.worldedit.session.request.RequestSelection;
import java.util.List;
public class LazyRegionMaskParser extends SimpleInputParser<Mask> {
public LazyRegionMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public List<String> getMatchedAliases() {
return Lists.newArrayList("#dregion", "#dselection", "#dsel");
}
@Override
public Mask parseFromSimpleInput(String input, ParserContext context) throws InputParseException {
return new RegionMask(new RequestSelection());
}
}

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.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.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.internal.registry.InputParser;
public class NegateMaskParser extends InputParser<Mask> {
public NegateMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
if (!input.startsWith("!")) {
return null;
}
if (input.length() > 1) {
return Masks.negate(worldEdit.getMaskFactory().parseFromInput(input.substring(1), context));
} else {
throw new InputParseException("Can't negate nothing!");
}
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.function.mask.Mask;
import com.sk89q.worldedit.function.mask.NoiseFilter;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.math.noise.RandomNoise;
public class NoiseMaskParser extends InputParser<Mask> {
public NoiseMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
if (!input.startsWith("%")) {
return null;
}
int i = Integer.parseInt(input.substring(1));
return new NoiseFilter(new RandomNoise(), ((double) i) / 100);
}
}

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.extension.factory.parser.mask;
import com.google.common.collect.Lists;
import com.sk89q.worldedit.IncompleteRegionException;
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.Mask;
import com.sk89q.worldedit.function.mask.RegionMask;
import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import java.util.List;
public class RegionMaskParser extends SimpleInputParser<Mask> {
public RegionMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public List<String> getMatchedAliases() {
return Lists.newArrayList("#region", "#selection", "#sel");
}
@Override
public Mask parseFromSimpleInput(String input, ParserContext context) throws InputParseException {
try {
return new RegionMask(context.requireSession().getSelection(context.requireWorld()).clone());
} catch (IncompleteRegionException e) {
throw new InputParseException("Please make a selection first.");
}
}
}

View File

@ -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 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.google.common.collect.Lists;
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.Mask;
import com.sk89q.worldedit.function.mask.SolidBlockMask;
import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import com.sk89q.worldedit.session.request.Request;
import java.util.List;
public class SolidMaskParser extends SimpleInputParser<Mask> {
public SolidMaskParser(WorldEdit worldEdit) {
super(worldEdit);
}
@Override
public List<String> getMatchedAliases() {
return Lists.newArrayList("#solid");
}
@Override
public Mask parseFromSimpleInput(String input, ParserContext context) throws InputParseException {
Extent extent = Request.request().getEditSession();
return new SolidBlockMask(extent);
}
}

View File

@ -17,8 +17,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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.pattern;
import com.google.common.collect.Lists;
import com.sk89q.worldedit.EmptyClipboardException; import com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
@ -27,22 +28,24 @@ import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.pattern.ClipboardPattern; import com.sk89q.worldedit.function.pattern.ClipboardPattern;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
class HashTagPatternParser extends InputParser<Pattern> { import java.util.List;
HashTagPatternParser(WorldEdit worldEdit) { public class ClipboardPatternParser extends SimpleInputParser<Pattern> {
public ClipboardPatternParser(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit);
} }
@Override @Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException { public List<String> getMatchedAliases() {
if (input.charAt(0) == '#') { return Lists.newArrayList("#clipboard", "#copy");
if (!input.equals("#clipboard") && !input.equals("#copy")) {
throw new InputParseException("#clipboard or #copy is acceptable for patterns starting with #");
} }
@Override
public Pattern parseFromSimpleInput(String input, ParserContext context) throws InputParseException {
LocalSession session = context.requireSession(); LocalSession session = context.requireSession();
if (session != null) { if (session != null) {
@ -56,9 +59,6 @@ class HashTagPatternParser extends InputParser<Pattern> {
} else { } else {
throw new InputParseException("No session is available, so no clipboard is available"); throw new InputParseException("No session is available, so no clipboard is available");
} }
} else {
return null;
}
} }
} }

View File

@ -17,10 +17,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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.pattern;
import com.sk89q.util.StringUtil; import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.factory.BlockFactory;
import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.pattern.BlockPattern; import com.sk89q.worldedit.function.pattern.BlockPattern;
@ -29,9 +30,9 @@ import com.sk89q.worldedit.function.pattern.RandomPattern;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockStateHolder;
class RandomPatternParser extends InputParser<Pattern> { public class RandomPatternParser extends InputParser<Pattern> {
RandomPatternParser(WorldEdit worldEdit) { public RandomPatternParser(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit);
} }

View File

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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.pattern;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.InputParseException;
@ -26,9 +26,9 @@ import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.InputParser;
class SingleBlockPatternParser extends InputParser<Pattern> { public class SingleBlockPatternParser extends InputParser<Pattern> {
SingleBlockPatternParser(WorldEdit worldEdit) { public SingleBlockPatternParser(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit);
} }

View File

@ -21,12 +21,14 @@ package com.sk89q.worldedit.internal.registry;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Lists;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.NoMatchException; import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.ParserContext;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -38,7 +40,7 @@ import java.util.List;
public abstract class AbstractFactory<E> { public abstract class AbstractFactory<E> {
protected final WorldEdit worldEdit; protected final WorldEdit worldEdit;
protected final List<InputParser<E>> parsers = new ArrayList<>(); private final List<InputParser<E>> parsers = new ArrayList<>();
/** /**
* Create a new factory. * Create a new factory.
@ -50,6 +52,17 @@ public abstract class AbstractFactory<E> {
this.worldEdit = worldEdit; this.worldEdit = worldEdit;
} }
/**
* Gets an immutable list of parsers.
*
* To add parsers, use the register method.
*
* @return the parsers
*/
public List<InputParser<E>> getParsers() {
return Collections.unmodifiableList(parsers);
}
public E parseFromInput(String input, ParserContext context) throws InputParseException { public E parseFromInput(String input, ParserContext context) throws InputParseException {
E match; E match;
@ -64,4 +77,14 @@ public abstract class AbstractFactory<E> {
throw new NoMatchException("No match for '" + input + "'"); throw new NoMatchException("No match for '" + input + "'");
} }
/**
* Registers an InputParser to this factory
*
* @param inputParser The input parser
*/
public void register(InputParser<E> inputParser) {
checkNotNull(inputParser);
parsers.add(inputParser);
}
} }

View File

@ -19,10 +19,13 @@
package com.sk89q.worldedit.internal.registry; package com.sk89q.worldedit.internal.registry;
import com.google.common.collect.Lists;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.ParserContext;
import java.util.List;
/** /**
* Input parser interface for {@link AbstractFactory}. * Input parser interface for {@link AbstractFactory}.
* *
@ -33,10 +36,18 @@ public abstract class InputParser<E> {
protected final WorldEdit worldEdit; protected final WorldEdit worldEdit;
protected InputParser(WorldEdit worldEdit) { public InputParser(WorldEdit worldEdit) {
this.worldEdit = worldEdit; this.worldEdit = worldEdit;
} }
public abstract E parseFromInput(String input, ParserContext context) throws InputParseException; public abstract E parseFromInput(String input, ParserContext context) throws InputParseException;
/**
* Gets a list of suggestions of input to this parser.
*
* @return a list of suggestions
*/
public List<String> getSuggestions() {
return Lists.newArrayList();
}
} }

View File

@ -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 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.internal.registry;
import com.google.common.collect.Lists;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import java.util.List;
/**
* An input parser that only performs a single function from aliases.
*
* @param <E> the element
*/
public abstract class SimpleInputParser<E> extends InputParser<E> {
public SimpleInputParser(WorldEdit worldEdit) {
super(worldEdit);
}
/**
* The strings this parser matches
*
* @return the matching aliases
*/
public abstract List<String> getMatchedAliases();
@Override
public E parseFromInput(String input, ParserContext context) throws InputParseException {
if (!getMatchedAliases().contains(input)) {
return null;
}
return parseFromSimpleInput(input, context);
}
public abstract E parseFromSimpleInput(String input, ParserContext context) throws InputParseException;
/**
* Gets the primary name of this matcher
*
* @return the primary match
*/
public String getPrimaryMatcher() {
return getMatchedAliases().get(0);
}
@Override
public List<String> getSuggestions() {
return Lists.newArrayList(getPrimaryMatcher());
}
}