Support sponge selector resolution - closes #275

This commit is contained in:
Luck 2017-05-07 22:52:45 +01:00
parent cedc5108fc
commit a5746dfa21
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -35,13 +35,16 @@ import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult; import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource; 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.Text;
import org.spongepowered.api.text.selector.Selector;
import org.spongepowered.api.world.Location; import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World; import org.spongepowered.api.world.World;
import co.aikar.timings.Timing; import co.aikar.timings.Timing;
import java.util.List; import java.util.List;
import java.util.ListIterator;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -58,11 +61,29 @@ class SpongeCommand extends CommandManager implements CommandCallable {
@Override @Override
public CommandResult process(CommandSource source, String s) throws CommandException { public CommandResult process(CommandSource source, String s) throws CommandException {
try (Timing ignored = plugin.getTimings().time(LPTiming.ON_COMMAND)) { try (Timing ignored = plugin.getTimings().time(LPTiming.ON_COMMAND)) {
onCommand( List<String> args = Util.stripQuotes(Splitter.on(COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(s));
plugin.getSenderFactory().wrap(source),
"lp", // resolve selectors
Util.stripQuotes(Splitter.on(COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(s)) ListIterator<String> 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(); return CommandResult.success();
} }
} }