Add optional filter argument to editor command

This commit is contained in:
Luck 2020-05-30 01:47:31 +01:00
parent 98c71739f8
commit 6c88f7629e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 37 additions and 11 deletions

View File

@ -39,12 +39,16 @@ import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.PermissionHolder; import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.Track; import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.matcher.ConstraintNodeMatcher;
import me.lucko.luckperms.common.node.matcher.StandardNodeMatchers;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.sender.Sender; import me.lucko.luckperms.common.sender.Sender;
import me.lucko.luckperms.common.storage.misc.NodeEntry;
import me.lucko.luckperms.common.util.Predicates; import me.lucko.luckperms.common.util.Predicates;
import me.lucko.luckperms.common.verbose.event.MetaCheckEvent; import me.lucko.luckperms.common.verbose.event.MetaCheckEvent;
import me.lucko.luckperms.common.web.WebEditor; import me.lucko.luckperms.common.web.WebEditor;
import net.luckperms.api.node.Node;
import net.luckperms.api.query.QueryOptions; import net.luckperms.api.query.QueryOptions;
import java.util.ArrayList; import java.util.ArrayList;
@ -58,7 +62,7 @@ public class EditorCommand extends SingleCommand {
private static final int MAX_USERS = 1000; private static final int MAX_USERS = 1000;
public EditorCommand(LocaleManager locale) { public EditorCommand(LocaleManager locale) {
super(CommandSpec.EDITOR.localize(locale), "Editor", CommandPermission.EDITOR, Predicates.notInRange(0, 1)); super(CommandSpec.EDITOR.localize(locale), "Editor", CommandPermission.EDITOR, Predicates.notInRange(0, 2));
} }
@Override @Override
@ -75,6 +79,8 @@ public class EditorCommand extends SingleCommand {
} }
} }
String filter = ArgumentParser.parseStringOrElse(1, args, null);
// run a sync task // run a sync task
plugin.getSyncTaskBuffer().requestDirectly(); plugin.getSyncTaskBuffer().requestDirectly();
@ -92,8 +98,27 @@ public class EditorCommand extends SingleCommand {
tracks.addAll(plugin.getTrackManager().getAll().values()); tracks.addAll(plugin.getTrackManager().getAll().values());
} }
if (type.includingUsers) { if (type.includingUsers) {
Map<UUID, User> users;
if (filter != null) {
// return users matching the filter
users = new LinkedHashMap<>();
ConstraintNodeMatcher<Node> matcher = StandardNodeMatchers.keyStartsWith(filter);
plugin.getStorage().searchUserNodes(matcher).join().stream()
.map(NodeEntry::getHolder)
.distinct()
.sorted()
.limit(MAX_USERS)
.forEach(uuid -> {
User user = plugin.getStorage().loadUser(uuid, null).join();
if (user != null) {
users.put(uuid, user);
}
plugin.getUserManager().getHouseKeeper().cleanup(uuid);
});
} else {
// include all online players // include all online players
Map<UUID, User> users = new LinkedHashMap<>(plugin.getUserManager().getAll()); users = new LinkedHashMap<>(plugin.getUserManager().getAll());
// then fill up with other users with permissions // then fill up with other users with permissions
if (type.includingOffline && users.size() < MAX_USERS) { if (type.includingOffline && users.size() < MAX_USERS) {
@ -109,11 +134,11 @@ public class EditorCommand extends SingleCommand {
plugin.getUserManager().getHouseKeeper().cleanup(uuid); plugin.getUserManager().getHouseKeeper().cleanup(uuid);
}); });
} }
}
users.values().stream() users.values().stream()
.sorted(Comparator .sorted(Comparator
.<User>comparingInt(u -> u.getCachedData().getMetaData(QueryOptions.nonContextual()).getWeight(MetaCheckEvent.Origin.INTERNAL)) .<User>comparingInt(u -> u.getCachedData().getMetaData(QueryOptions.nonContextual()).getWeight(MetaCheckEvent.Origin.INTERNAL)).reversed()
.reversed()
.thenComparing(User::getFormattedDisplayName, String.CASE_INSENSITIVE_ORDER) .thenComparing(User::getFormattedDisplayName, String.CASE_INSENSITIVE_ORDER)
) )
.forEach(holders::add); .forEach(holders::add);

View File

@ -58,7 +58,8 @@ public enum CommandSpec {
INFO("Prints general information about the active plugin instance.", "/%s info"), INFO("Prints general information about the active plugin instance.", "/%s info"),
EDITOR("Creates a new web editor session", "/%s editor [type]", EDITOR("Creates a new web editor session", "/%s editor [type]",
Argument.list( Argument.list(
Argument.create("type", false, "the types to load into the editor. ('all', 'users' or 'groups')") Argument.create("type", false, "the types to load into the editor. ('all', 'users' or 'groups')"),
Argument.create("filter", false, "permission to filter user entries by")
) )
), ),
DEBUG("Produces a set of internal debugging output", "/%s debug"), DEBUG("Produces a set of internal debugging output", "/%s debug"),