Fix parsing for injected command types

This commit is contained in:
Jesse Boyd 2019-11-11 08:55:53 +00:00
parent 10a6619853
commit 9d6f2df908
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
7 changed files with 39 additions and 10 deletions

View File

@ -8,21 +8,31 @@ import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
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;
import org.enginehub.piston.inject.InjectedValueAccess;
import java.util.*; import java.util.*;
public abstract class FaweParser<T> extends InputParser<T> { public abstract class FaweParser<T> extends InputParser<T> {
protected FaweParser(WorldEdit worldEdit) { private final String prefix;
protected FaweParser(WorldEdit worldEdit, String prefix) {
super(worldEdit); super(worldEdit);
this.prefix = prefix;
} }
public PlatformCommandManager getPlatform() { public PlatformCommandManager getPlatform() {
return PlatformCommandManager.getInstance(); return PlatformCommandManager.getInstance();
} }
public T parse(String input, Actor actor) { public T parse(String input, ParserContext context) {
return getPlatform().parse("pattern " + input, actor); 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 { public T catchSuggestion(String currentInput, String nextInput, ParserContext context) throws InputParseException {

View File

@ -116,6 +116,7 @@ public class FactoryConverter<T> implements ArgumentConverter<T> {
} }
parserContext.setSession(session); parserContext.setSession(session);
parserContext.setRestricted(true); parserContext.setRestricted(true);
parserContext.setInjected(context);
if (contextTweaker != null) { if (contextTweaker != null) {
contextTweaker.accept(parserContext); contextTweaker.accept(parserContext);

View File

@ -23,7 +23,7 @@ import java.util.Map;
public class DefaultTransformParser extends FaweParser<ResettableExtent> { public class DefaultTransformParser extends FaweParser<ResettableExtent> {
public DefaultTransformParser(WorldEdit worldEdit) { public DefaultTransformParser(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit, "transforms");
} }
@Override @Override
@ -53,7 +53,7 @@ public class DefaultTransformParser extends FaweParser<ResettableExtent> {
List<String> args = entry.getValue(); List<String> args = entry.getValue();
String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " ")); String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " "));
try { try {
transform = parse(cmdArgs, actor); transform = parse(command + cmdArgs, context);
} catch (SuggestInputParseException rethrow) { } catch (SuggestInputParseException rethrow) {
throw rethrow; throw rethrow;
} catch (Throwable e) { } catch (Throwable e) {

View File

@ -49,7 +49,7 @@ import java.util.stream.Stream;
public class DefaultMaskParser extends FaweParser<Mask> { public class DefaultMaskParser extends FaweParser<Mask> {
public DefaultMaskParser(WorldEdit worldEdit) { public DefaultMaskParser(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit, "masks");
} }
@Override @Override
@ -80,7 +80,7 @@ public class DefaultMaskParser extends FaweParser<Mask> {
List<String> args = entry.getValue(); List<String> args = entry.getValue();
String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " ")); String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " "));
try { try {
mask = parse(cmdArgs, actor); mask = parse(command + cmdArgs, context);
} catch (SuggestInputParseException rethrow) { } catch (SuggestInputParseException rethrow) {
throw rethrow; throw rethrow;
} catch (Throwable e) { } catch (Throwable e) {

View File

@ -30,6 +30,7 @@ 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 com.sk89q.worldedit.extension.platform.Actor; 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.Pattern;
import com.sk89q.worldedit.function.pattern.RandomPattern; import com.sk89q.worldedit.function.pattern.RandomPattern;
import com.sk89q.worldedit.internal.expression.Expression; import com.sk89q.worldedit.internal.expression.Expression;
@ -44,7 +45,7 @@ import java.util.stream.Stream;
public class DefaultPatternParser extends FaweParser<Pattern> { public class DefaultPatternParser extends FaweParser<Pattern> {
public DefaultPatternParser(WorldEdit worldEdit) { public DefaultPatternParser(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit, "patterns");
} }
@Override @Override
@ -72,7 +73,7 @@ public class DefaultPatternParser extends FaweParser<Pattern> {
List<String> args = entry.getValue(); List<String> args = entry.getValue();
String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " ")); String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " "));
try { try {
pattern = parse(cmdArgs, actor); pattern = parse(command + cmdArgs, context);
} catch (SuggestInputParseException rethrow) { } catch (SuggestInputParseException rethrow) {
throw rethrow; throw rethrow;
} catch (Throwable e) { } catch (Throwable e) {

View File

@ -24,6 +24,7 @@ import com.sk89q.worldedit.extension.factory.MaskFactory;
import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import org.enginehub.piston.inject.InjectedValueAccess;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -42,6 +43,7 @@ public class ParserContext {
private boolean restricted = true; private boolean restricted = true;
private boolean tryLegacy = true; private boolean tryLegacy = true;
private boolean preferringWildcard; private boolean preferringWildcard;
private InjectedValueAccess injected;
/** /**
* Create a new instance. * Create a new instance.
@ -248,4 +250,12 @@ public class ParserContext {
public boolean isTryingLegacy() { public boolean isTryingLegacy() {
return tryLegacy; return tryLegacy;
} }
public void setInjected(InjectedValueAccess injected) {
this.injected = injected;
}
public InjectedValueAccess getInjected() {
return injected;
}
} }

View File

@ -609,10 +609,17 @@ public final class PlatformCommandManager {
} }
public <T> T parse(String args, Actor actor) { public <T> 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> T parse(String args, InjectedValueAccess access) { public <T> T parse(String args, InjectedValueAccess access) {
if (args.isEmpty()) return null;
String[] split = parseArgs(args) String[] split = parseArgs(args)
.map(Substring::getSubstring) .map(Substring::getSubstring)
.toArray(String[]::new); .toArray(String[]::new);