From a3ffb91917fac74219622bedc6bbb97b344b8219 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Sat, 18 May 2019 16:47:08 +1000 Subject: [PATCH] Added suggestions to a lot of factory-related commands --- .../worldedit/command/argument/FactoryConverter.java | 8 ++++++++ .../extension/factory/parser/DefaultBlockParser.java | 7 +++++++ .../extension/factory/parser/DefaultItemParser.java | 9 +++++++++ .../extension/factory/parser/mask/BiomeMaskParser.java | 6 ++++++ .../factory/parser/mask/BlockCategoryMaskParser.java | 6 ++++++ .../extension/factory/parser/mask/BlocksMaskParser.java | 7 +++++++ .../factory/parser/mask/ExpressionMaskParser.java | 4 ++-- .../parser/pattern/BlockCategoryPatternParser.java | 7 +++---- .../factory/parser/pattern/ClipboardPatternParser.java | 7 +++---- .../factory/parser/pattern/SingleBlockPatternParser.java | 8 ++++++++ .../sk89q/worldedit/function/pattern/ExtentPattern.java | 2 +- .../worldedit/internal/registry/AbstractFactory.java | 7 +++++++ .../sk89q/worldedit/internal/registry/InputParser.java | 9 +++++---- .../worldedit/internal/registry/SimpleInputParser.java | 5 +++-- 14 files changed, 75 insertions(+), 17 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java index b3b2b8108..826e3a1cd 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/FactoryConverter.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.command.argument; +import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix; + import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.blocks.BaseItem; @@ -41,6 +43,7 @@ import org.enginehub.piston.converter.SuccessfulConversion; import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.Key; +import java.util.List; import java.util.function.Function; public class FactoryConverter implements ArgumentConverter { @@ -92,6 +95,11 @@ public class FactoryConverter implements ArgumentConverter { } } + @Override + public List getSuggestions(String input) { + return limitByPrefix(factoryExtractor.apply(worldEdit).getSuggestions(), input); + } + @Override public Component describeAcceptableArguments() { return TextComponent.of("any " + description); 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 d2212a40e..5a962a9a3 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 @@ -52,6 +52,7 @@ import com.sk89q.worldedit.world.registry.LegacyMapper; import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.stream.Stream; /** * Parses block input strings. @@ -199,6 +200,12 @@ public class DefaultBlockParser extends InputParser { return blockStates; } + @Override + public Stream getSuggestions() { + // TODO Include states + return BlockType.REGISTRY.keySet().stream(); + } + private BaseBlock parseLogic(String input, ParserContext context) throws InputParseException { BlockType blockType = null; Map, Object> blockStates = new HashMap<>(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultItemParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultItemParser.java index 13c382cac..52658327e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultItemParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultItemParser.java @@ -28,7 +28,11 @@ import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.registry.LegacyMapper; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class DefaultItemParser extends InputParser { @@ -36,6 +40,11 @@ public class DefaultItemParser extends InputParser { super(worldEdit); } + @Override + public Stream getSuggestions() { + return ItemType.REGISTRY.keySet().stream(); + } + @Override public BaseItem parseFromInput(String input, ParserContext context) throws InputParseException { BaseItem item = null; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BiomeMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BiomeMaskParser.java index 11b8bb54c..af887e432 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BiomeMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BiomeMaskParser.java @@ -36,6 +36,7 @@ import com.sk89q.worldedit.world.registry.BiomeRegistry; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; public class BiomeMaskParser extends InputParser { @@ -43,6 +44,11 @@ public class BiomeMaskParser extends InputParser { super(worldEdit); } + @Override + public Stream getSuggestions() { + return BiomeType.REGISTRY.keySet().stream().map(biomeType -> "$" + biomeType); + } + @Override public Mask parseFromInput(String input, ParserContext context) throws InputParseException { if (!input.startsWith("$")) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockCategoryMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockCategoryMaskParser.java index 8745e60ca..7c20d9ee2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockCategoryMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlockCategoryMaskParser.java @@ -29,6 +29,7 @@ import com.sk89q.worldedit.session.request.RequestExtent; import com.sk89q.worldedit.world.block.BlockCategory; import java.util.Locale; +import java.util.stream.Stream; public class BlockCategoryMaskParser extends InputParser { @@ -36,6 +37,11 @@ public class BlockCategoryMaskParser extends InputParser { super(worldEdit); } + @Override + public Stream getSuggestions() { + return BlockCategory.REGISTRY.keySet().stream().map(str -> "##" + str); + } + @Override public Mask parseFromInput(String input, ParserContext context) throws InputParseException { if (!input.startsWith("##")) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlocksMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlocksMaskParser.java index e67a7e8b1..85c414383 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlocksMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/BlocksMaskParser.java @@ -28,8 +28,10 @@ import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.session.request.RequestExtent; import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.world.block.BlockCategory; import java.util.Set; +import java.util.stream.Stream; /** * Parses mask input strings. @@ -40,6 +42,11 @@ public class BlocksMaskParser extends InputParser { super(worldEdit); } + @Override + public Stream getSuggestions() { + return worldEdit.getBlockFactory().getSuggestions(); + } + @Override public Mask parseFromInput(String component, ParserContext context) throws InputParseException { ParserContext tempContext = new ParserContext(context); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExpressionMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExpressionMaskParser.java index 4e254109c..3087610be 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExpressionMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/ExpressionMaskParser.java @@ -52,8 +52,8 @@ public class ExpressionMaskParser extends InputParser { WorldEditExpressionEnvironment env = new WorldEditExpressionEnvironment( new RequestExtent(), Vector3.ONE, Vector3.ZERO); exp.setEnvironment(env); - if (context.getActor() instanceof SessionOwner) { - SessionOwner owner = (SessionOwner) context.getActor(); + if (context.getActor() != null) { + SessionOwner owner = context.getActor(); IntSupplier timeout = () -> WorldEdit.getInstance().getSessionManager().get(owner).getTimeout(); return new ExpressionMask(exp, timeout); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BlockCategoryPatternParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BlockCategoryPatternParser.java index 769394146..6c83b569f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BlockCategoryPatternParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BlockCategoryPatternParser.java @@ -29,10 +29,9 @@ import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.world.block.BlockCategory; import com.sk89q.worldedit.world.block.BlockType; -import java.util.List; import java.util.Locale; import java.util.Set; -import java.util.stream.Collectors; +import java.util.stream.Stream; public class BlockCategoryPatternParser extends InputParser { @@ -41,8 +40,8 @@ public class BlockCategoryPatternParser extends InputParser { } @Override - public List getSuggestions() { - return BlockCategory.REGISTRY.keySet().stream().map(str -> "##" + str).collect(Collectors.toList()); + public Stream getSuggestions() { + return BlockCategory.REGISTRY.keySet().stream().map(str -> "##" + str); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/ClipboardPatternParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/ClipboardPatternParser.java index f0939ed60..1f88aecbd 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/ClipboardPatternParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/ClipboardPatternParser.java @@ -19,7 +19,6 @@ package com.sk89q.worldedit.extension.factory.parser.pattern; -import com.google.common.collect.Lists; import com.sk89q.worldedit.EmptyClipboardException; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; @@ -32,7 +31,7 @@ import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.session.ClipboardHolder; -import java.util.List; +import java.util.stream.Stream; public class ClipboardPatternParser extends InputParser { @@ -41,8 +40,8 @@ public class ClipboardPatternParser extends InputParser { } @Override - public List getSuggestions() { - return Lists.newArrayList("#clipboard", "#copy"); + public Stream getSuggestions() { + return Stream.of("#clipboard", "#copy"); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/SingleBlockPatternParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/SingleBlockPatternParser.java index e71530a2e..8e2b99bae 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/SingleBlockPatternParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/SingleBlockPatternParser.java @@ -25,6 +25,9 @@ import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.function.pattern.BlockPattern; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.internal.registry.InputParser; +import com.sk89q.worldedit.world.block.BlockType; + +import java.util.stream.Stream; public class SingleBlockPatternParser extends InputParser { @@ -32,6 +35,11 @@ public class SingleBlockPatternParser extends InputParser { super(worldEdit); } + @Override + public Stream getSuggestions() { + return worldEdit.getBlockFactory().getSuggestions(); + } + @Override public Pattern parseFromInput(String input, ParserContext context) throws InputParseException { return new BlockPattern(worldEdit.getBlockFactory().parseFromInput(input, context)); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/ExtentPattern.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/ExtentPattern.java index c0dec41ee..94ab5ea77 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/ExtentPattern.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/ExtentPattern.java @@ -28,5 +28,5 @@ public interface ExtentPattern extends Pattern { * * @return the extent for this pattern */ - public Extent getExtent(); + Extent getExtent(); } 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 fcee6abcc..6fd8523c0 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 @@ -27,8 +27,11 @@ import com.sk89q.worldedit.extension.input.NoMatchException; import com.sk89q.worldedit.extension.input.ParserContext; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * An abstract implementation of a factory for internal usage. @@ -76,6 +79,10 @@ public abstract class AbstractFactory { throw new NoMatchException("No match for '" + input + "'"); } + public Stream getSuggestions() { + return parsers.stream().flatMap(InputParser::getSuggestions); + } + /** * Registers an InputParser to this factory * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/InputParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/InputParser.java index 575bb9550..a5dc1e992 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/InputParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/InputParser.java @@ -25,6 +25,7 @@ import com.sk89q.worldedit.extension.input.ParserContext; import java.util.Collections; import java.util.List; +import java.util.stream.Stream; /** * Input parser interface for {@link AbstractFactory}. @@ -43,11 +44,11 @@ public abstract class InputParser { public abstract E parseFromInput(String input, ParserContext context) throws InputParseException; /** - * Gets a list of suggestions of input to this parser. + * Gets a stream of suggestions of input to this parser. * - * @return a list of suggestions + * @return a stream of suggestions */ - public List getSuggestions() { - return Collections.emptyList(); + public Stream getSuggestions() { + return Stream.empty(); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/SimpleInputParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/SimpleInputParser.java index d0647f9cc..8ed5f338c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/SimpleInputParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/registry/SimpleInputParser.java @@ -25,6 +25,7 @@ import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; import java.util.List; +import java.util.stream.Stream; /** * An input parser that only performs a single function from aliases. @@ -65,7 +66,7 @@ public abstract class SimpleInputParser extends InputParser { } @Override - public List getSuggestions() { - return Lists.newArrayList(getPrimaryMatcher()); + public Stream getSuggestions() { + return Stream.of(getPrimaryMatcher()); } }