From a5746dfa21afb9591762e7bef5167b1574e2b475 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 7 May 2017 22:52:45 +0100 Subject: [PATCH] Support sponge selector resolution - closes #275 --- .../lucko/luckperms/sponge/SpongeCommand.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCommand.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCommand.java index 0f4dcb9b4..03b5de84a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCommand.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCommand.java @@ -35,13 +35,16 @@ import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandResult; import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.selector.Selector; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; import co.aikar.timings.Timing; import java.util.List; +import java.util.ListIterator; import java.util.Optional; import javax.annotation.Nullable; @@ -58,11 +61,29 @@ class SpongeCommand extends CommandManager implements CommandCallable { @Override public CommandResult process(CommandSource source, String s) throws CommandException { try (Timing ignored = plugin.getTimings().time(LPTiming.ON_COMMAND)) { - onCommand( - plugin.getSenderFactory().wrap(source), - "lp", - Util.stripQuotes(Splitter.on(COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(s)) - ); + List args = Util.stripQuotes(Splitter.on(COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(s)); + + // resolve selectors + ListIterator it = args.listIterator(); + while (it.hasNext()) { + String element = it.next(); + if (element.startsWith("@")) { + try { + Player ret = Selector.parse(element).resolve(source).stream() + .filter(e -> e instanceof Player) + .map(e -> ((Player) e)) + .findFirst().orElse(null); + + if (ret != null) { + it.set(ret.getUniqueId().toString()); + } + } catch (IllegalArgumentException e) { + // ignored + } + } + } + + onCommand(plugin.getSenderFactory().wrap(source), "lp", args); return CommandResult.success(); } }