From ab1e09fdafa29d7e36ffb3652e7caad15f5e13ea Mon Sep 17 00:00:00 2001 From: wizjany Date: Mon, 27 May 2019 00:35:26 -0400 Subject: [PATCH] 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. --- .../sk89q/worldedit/extension/factory/BlockFactory.java | 4 +--- .../com/sk89q/worldedit/extension/factory/ItemFactory.java | 4 +--- .../com/sk89q/worldedit/extension/factory/MaskFactory.java | 4 +--- .../sk89q/worldedit/extension/factory/PatternFactory.java | 5 +---- .../extension/factory/parser/DefaultBlockParser.java | 4 +++- .../sk89q/worldedit/internal/registry/AbstractFactory.java | 7 +++++-- 6 files changed, 12 insertions(+), 16 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/BlockFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/BlockFactory.java index c00583183..4f5a4fdbc 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/BlockFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/BlockFactory.java @@ -45,9 +45,7 @@ public class BlockFactory extends AbstractFactory { * @param worldEdit the WorldEdit instance. */ public BlockFactory(WorldEdit worldEdit) { - super(worldEdit); - - register(new DefaultBlockParser(worldEdit)); + super(worldEdit, new DefaultBlockParser(worldEdit)); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/ItemFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/ItemFactory.java index e2a2bee29..71853f79f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/ItemFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/ItemFactory.java @@ -32,9 +32,7 @@ public class ItemFactory extends AbstractFactory { * @param worldEdit the WorldEdit instance. */ public ItemFactory(WorldEdit worldEdit) { - super(worldEdit); - - register(new DefaultItemParser(worldEdit)); + super(worldEdit, new DefaultItemParser(worldEdit)); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/MaskFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/MaskFactory.java index 31b1ac2df..73df7a52d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/MaskFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/MaskFactory.java @@ -58,7 +58,7 @@ public final class MaskFactory extends AbstractFactory { * @param worldEdit the WorldEdit instance */ public MaskFactory(WorldEdit worldEdit) { - super(worldEdit); + super(worldEdit, new BlocksMaskParser(worldEdit)); register(new ExistingMaskParser(worldEdit)); register(new SolidMaskParser(worldEdit)); @@ -72,8 +72,6 @@ public final class MaskFactory extends AbstractFactory { register(new BlockCategoryMaskParser(worldEdit)); register(new BiomeMaskParser(worldEdit)); - - register(new BlocksMaskParser(worldEdit)); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java index 0df1dc40c..014229a3e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/PatternFactory.java @@ -44,7 +44,7 @@ public final class PatternFactory extends AbstractFactory { * @param worldEdit the WorldEdit instance */ public PatternFactory(WorldEdit worldEdit) { - super(worldEdit); + super(worldEdit, new SingleBlockPatternParser(worldEdit)); // split and parse each sub-pattern register(new RandomPatternParser(worldEdit)); @@ -54,9 +54,6 @@ public final class PatternFactory extends AbstractFactory { register(new TypeOrStateApplyingPatternParser(worldEdit)); register(new RandomStatePatternParser(worldEdit)); register(new BlockCategoryPatternParser(worldEdit)); - - // inner-most pattern: just one block - must be last - register(new SingleBlockPatternParser(worldEdit)); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java index 4a03a5471..0bdcdc9ae 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java @@ -54,6 +54,8 @@ import java.util.Locale; import java.util.Map; import java.util.stream.Stream; +import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix; + /** * Parses block input strings. */ @@ -209,7 +211,7 @@ public class DefaultBlockParser extends InputParser { if (input.contains(",")) { return Stream.empty(); } - return BlockType.REGISTRY.keySet().stream(); + return limitByPrefix(BlockType.REGISTRY.keySet().stream(), input).stream(); } String blockType = input.substring(0, idx); BlockType type = BlockTypes.get(blockType.toLowerCase(Locale.ROOT)); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/AbstractFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/AbstractFactory.java index f1b3e05aa..a5844306d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/AbstractFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/AbstractFactory.java @@ -46,10 +46,13 @@ public abstract class AbstractFactory { * Create a new factory. * * @param worldEdit the WorldEdit instance + * @param defaultParser the parser to fall back to */ - protected AbstractFactory(WorldEdit worldEdit) { + protected AbstractFactory(WorldEdit worldEdit, InputParser defaultParser) { checkNotNull(worldEdit); + checkNotNull(defaultParser); this.worldEdit = worldEdit; + this.parsers.add(defaultParser); } /** @@ -91,6 +94,6 @@ public abstract class AbstractFactory { public void register(InputParser inputParser) { checkNotNull(inputParser); - parsers.add(inputParser); + parsers.add(parsers.size() - 1, inputParser); } }