Give factories a default parser.

Later registered parsers will always come before the default, ensuring
that the default parser is used when no other parser can match the input,
and that errors may be thrown by it to signify the end of the line.
This commit is contained in:
wizjany 2019-05-27 00:35:26 -04:00
parent 6962b2e7b6
commit ab1e09fdaf
6 changed files with 12 additions and 16 deletions

View File

@ -45,9 +45,7 @@ public class BlockFactory extends AbstractFactory<BaseBlock> {
* @param worldEdit the WorldEdit instance. * @param worldEdit the WorldEdit instance.
*/ */
public BlockFactory(WorldEdit worldEdit) { public BlockFactory(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit, new DefaultBlockParser(worldEdit));
register(new DefaultBlockParser(worldEdit));
} }
/** /**

View File

@ -32,9 +32,7 @@ public class ItemFactory extends AbstractFactory<BaseItem> {
* @param worldEdit the WorldEdit instance. * @param worldEdit the WorldEdit instance.
*/ */
public ItemFactory(WorldEdit worldEdit) { public ItemFactory(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit, new DefaultItemParser(worldEdit));
register(new DefaultItemParser(worldEdit));
} }
} }

View File

@ -58,7 +58,7 @@ public final class MaskFactory extends AbstractFactory<Mask> {
* @param worldEdit the WorldEdit instance * @param worldEdit the WorldEdit instance
*/ */
public MaskFactory(WorldEdit worldEdit) { public MaskFactory(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit, new BlocksMaskParser(worldEdit));
register(new ExistingMaskParser(worldEdit)); register(new ExistingMaskParser(worldEdit));
register(new SolidMaskParser(worldEdit)); register(new SolidMaskParser(worldEdit));
@ -72,8 +72,6 @@ public final class MaskFactory extends AbstractFactory<Mask> {
register(new BlockCategoryMaskParser(worldEdit)); register(new BlockCategoryMaskParser(worldEdit));
register(new BiomeMaskParser(worldEdit)); register(new BiomeMaskParser(worldEdit));
register(new BlocksMaskParser(worldEdit));
} }
@Override @Override

View File

@ -44,7 +44,7 @@ public final class PatternFactory extends AbstractFactory<Pattern> {
* @param worldEdit the WorldEdit instance * @param worldEdit the WorldEdit instance
*/ */
public PatternFactory(WorldEdit worldEdit) { public PatternFactory(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit, new SingleBlockPatternParser(worldEdit));
// split and parse each sub-pattern // split and parse each sub-pattern
register(new RandomPatternParser(worldEdit)); register(new RandomPatternParser(worldEdit));
@ -54,9 +54,6 @@ public final class PatternFactory extends AbstractFactory<Pattern> {
register(new TypeOrStateApplyingPatternParser(worldEdit)); register(new TypeOrStateApplyingPatternParser(worldEdit));
register(new RandomStatePatternParser(worldEdit)); register(new RandomStatePatternParser(worldEdit));
register(new BlockCategoryPatternParser(worldEdit)); register(new BlockCategoryPatternParser(worldEdit));
// inner-most pattern: just one block - must be last
register(new SingleBlockPatternParser(worldEdit));
} }
} }

View File

@ -54,6 +54,8 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix;
/** /**
* Parses block input strings. * Parses block input strings.
*/ */
@ -209,7 +211,7 @@ public class DefaultBlockParser extends InputParser<BaseBlock> {
if (input.contains(",")) { if (input.contains(",")) {
return Stream.empty(); return Stream.empty();
} }
return BlockType.REGISTRY.keySet().stream(); return limitByPrefix(BlockType.REGISTRY.keySet().stream(), input).stream();
} }
String blockType = input.substring(0, idx); String blockType = input.substring(0, idx);
BlockType type = BlockTypes.get(blockType.toLowerCase(Locale.ROOT)); BlockType type = BlockTypes.get(blockType.toLowerCase(Locale.ROOT));

View File

@ -46,10 +46,13 @@ public abstract class AbstractFactory<E> {
* Create a new factory. * Create a new factory.
* *
* @param worldEdit the WorldEdit instance * @param worldEdit the WorldEdit instance
* @param defaultParser the parser to fall back to
*/ */
protected AbstractFactory(WorldEdit worldEdit) { protected AbstractFactory(WorldEdit worldEdit, InputParser<E> defaultParser) {
checkNotNull(worldEdit); checkNotNull(worldEdit);
checkNotNull(defaultParser);
this.worldEdit = worldEdit; this.worldEdit = worldEdit;
this.parsers.add(defaultParser);
} }
/** /**
@ -91,6 +94,6 @@ public abstract class AbstractFactory<E> {
public void register(InputParser<E> inputParser) { public void register(InputParser<E> inputParser) {
checkNotNull(inputParser); checkNotNull(inputParser);
parsers.add(inputParser); parsers.add(parsers.size() - 1, inputParser);
} }
} }