From 9d6f2df908dfc49525879bee7081220f6d2a9936 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Mon, 11 Nov 2019 08:55:53 +0000 Subject: [PATCH] Fix parsing for injected command types --- .../java/com/boydti/fawe/command/FaweParser.java | 16 +++++++++++++--- .../command/argument/FactoryConverter.java | 1 + .../factory/DefaultTransformParser.java | 4 ++-- .../factory/parser/mask/DefaultMaskParser.java | 4 ++-- .../parser/pattern/DefaultPatternParser.java | 5 +++-- .../worldedit/extension/input/ParserContext.java | 10 ++++++++++ .../platform/PlatformCommandManager.java | 9 ++++++++- 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/worldedit-core/src/main/java/com/boydti/fawe/command/FaweParser.java b/worldedit-core/src/main/java/com/boydti/fawe/command/FaweParser.java index 273eb873c..046dd6db3 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/command/FaweParser.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/command/FaweParser.java @@ -8,21 +8,31 @@ import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.internal.registry.InputParser; +import org.enginehub.piston.inject.InjectedValueAccess; import java.util.*; public abstract class FaweParser extends InputParser { - protected FaweParser(WorldEdit worldEdit) { + private final String prefix; + + protected FaweParser(WorldEdit worldEdit, String prefix) { super(worldEdit); + this.prefix = prefix; } public PlatformCommandManager getPlatform() { return PlatformCommandManager.getInstance(); } - public T parse(String input, Actor actor) { - return getPlatform().parse("pattern " + input, actor); + public T parse(String input, ParserContext context) { + input = prefix + " " + input; + InjectedValueAccess injected = context.getInjected(); + if (injected != null) { + return getPlatform().parse(input, injected); + } else { + return getPlatform().parse(input, context.getActor()); + } } public T catchSuggestion(String currentInput, String nextInput, ParserContext context) throws InputParseException { 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 8e5b0d9e8..d475c387c 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 @@ -116,6 +116,7 @@ public class FactoryConverter implements ArgumentConverter { } parserContext.setSession(session); parserContext.setRestricted(true); + parserContext.setInjected(context); if (contextTweaker != null) { contextTweaker.accept(parserContext); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultTransformParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultTransformParser.java index 3124196f7..fb4c09be9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultTransformParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultTransformParser.java @@ -23,7 +23,7 @@ import java.util.Map; public class DefaultTransformParser extends FaweParser { public DefaultTransformParser(WorldEdit worldEdit) { - super(worldEdit); + super(worldEdit, "transforms"); } @Override @@ -53,7 +53,7 @@ public class DefaultTransformParser extends FaweParser { List args = entry.getValue(); String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " ")); try { - transform = parse(cmdArgs, actor); + transform = parse(command + cmdArgs, context); } catch (SuggestInputParseException rethrow) { throw rethrow; } catch (Throwable e) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/DefaultMaskParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/DefaultMaskParser.java index 2c4975001..adff91132 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/DefaultMaskParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/mask/DefaultMaskParser.java @@ -49,7 +49,7 @@ import java.util.stream.Stream; public class DefaultMaskParser extends FaweParser { public DefaultMaskParser(WorldEdit worldEdit) { - super(worldEdit); + super(worldEdit, "masks"); } @Override @@ -80,7 +80,7 @@ public class DefaultMaskParser extends FaweParser { List args = entry.getValue(); String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " ")); try { - mask = parse(cmdArgs, actor); + mask = parse(command + cmdArgs, context); } catch (SuggestInputParseException rethrow) { throw rethrow; } catch (Throwable e) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/DefaultPatternParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/DefaultPatternParser.java index ae0bdde3b..63fdebf0b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/DefaultPatternParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/DefaultPatternParser.java @@ -30,6 +30,7 @@ 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.Actor; +import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.RandomPattern; import com.sk89q.worldedit.internal.expression.Expression; @@ -44,7 +45,7 @@ import java.util.stream.Stream; public class DefaultPatternParser extends FaweParser { public DefaultPatternParser(WorldEdit worldEdit) { - super(worldEdit); + super(worldEdit, "patterns"); } @Override @@ -72,7 +73,7 @@ public class DefaultPatternParser extends FaweParser { List args = entry.getValue(); String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " ")); try { - pattern = parse(cmdArgs, actor); + pattern = parse(command + cmdArgs, context); } catch (SuggestInputParseException rethrow) { throw rethrow; } catch (Throwable e) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java index d6fced5eb..ecf63e6fc 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java @@ -24,6 +24,7 @@ import com.sk89q.worldedit.extension.factory.MaskFactory; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.world.World; +import org.enginehub.piston.inject.InjectedValueAccess; import javax.annotation.Nullable; @@ -42,6 +43,7 @@ public class ParserContext { private boolean restricted = true; private boolean tryLegacy = true; private boolean preferringWildcard; + private InjectedValueAccess injected; /** * Create a new instance. @@ -248,4 +250,12 @@ public class ParserContext { public boolean isTryingLegacy() { return tryLegacy; } + + public void setInjected(InjectedValueAccess injected) { + this.injected = injected; + } + + public InjectedValueAccess getInjected() { + return injected; + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index 73cee647e..6c4760da2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -609,10 +609,17 @@ public final class PlatformCommandManager { } public T parse(String args, Actor actor) { - return parse(args, actor); + InjectedValueAccess context; + if (actor == null) { + context = globalInjectedValues; + } else { + context = initializeInjectedValues(args::toString, actor); + } + return parse(args, context); } public T parse(String args, InjectedValueAccess access) { + if (args.isEmpty()) return null; String[] split = parseArgs(args) .map(Substring::getSubstring) .toArray(String[]::new);