Include users which inherit the group when running /lp group <group> editor

This commit is contained in:
Luck 2020-05-30 02:13:18 +01:00
parent c5c99abd16
commit 91d75424f4
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 85 additions and 21 deletions

View File

@ -31,19 +31,35 @@ import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.ArgumentPermissions;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.commands.misc.EditorCommand;
import me.lucko.luckperms.common.context.contextset.ImmutableContextSetImpl;
import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.HolderType;
import me.lucko.luckperms.common.model.PermissionHolder;
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.node.types.Inheritance;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
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.verbose.event.MetaCheckEvent;
import me.lucko.luckperms.common.web.WebEditor;
import net.luckperms.api.node.Node;
import net.luckperms.api.query.QueryOptions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class HolderEditor<T extends PermissionHolder> extends ChildCommand<T> {
public HolderEditor(LocaleManager locale, HolderType type) {
@ -57,9 +73,52 @@ public class HolderEditor<T extends PermissionHolder> extends ChildCommand<T> {
return CommandResult.NO_PERMISSION;
}
List<PermissionHolder> holders = new ArrayList<>();
// also include users who are a member of the group
if (holder instanceof Group) {
Group group = (Group) holder;
ConstraintNodeMatcher<Node> matcher = StandardNodeMatchers.key(Inheritance.key(group.getName()));
Map<UUID, User> users = new LinkedHashMap<>(plugin.getUserManager().getAll());
// only include online players who are in the group
users.values().removeIf(user -> user.normalData().immutable().values().stream().noneMatch(matcher));
// fill up with other matching users
if (users.size() < EditorCommand.MAX_USERS) {
plugin.getStorage().searchUserNodes(matcher).join().stream()
.map(NodeEntry::getHolder)
.distinct()
.filter(uuid -> !users.containsKey(uuid))
.sorted()
.limit(EditorCommand.MAX_USERS - users.size())
.forEach(uuid -> {
User user = plugin.getStorage().loadUser(uuid, null).join();
if (user != null) {
users.put(uuid, user);
}
plugin.getUserManager().getHouseKeeper().cleanup(uuid);
});
}
users.values().stream()
.sorted(Comparator
.<User>comparingInt(u -> u.getCachedData().getMetaData(QueryOptions.nonContextual()).getWeight(MetaCheckEvent.Origin.INTERNAL)).reversed()
.thenComparing(User::getFormattedDisplayName, String.CASE_INSENSITIVE_ORDER)
)
.forEach(holders::add);
// remove holders which the sender doesn't have perms to view
holders.removeIf(h -> ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), h) || ArgumentPermissions.checkGroup(plugin, sender, h, ImmutableContextSetImpl.EMPTY));
}
// include the original holder too
holders.add(holder);
Message.EDITOR_START.send(sender);
JsonObject payload = WebEditor.formPayload(Collections.singletonList(holder), Collections.emptyList(), sender, label, plugin);
JsonObject payload = WebEditor.formPayload(holders, Collections.emptyList(), sender, label, plugin);
return WebEditor.post(payload, sender, plugin);
}

View File

@ -60,7 +60,7 @@ import java.util.Map;
import java.util.UUID;
public class EditorCommand extends SingleCommand {
private static final int MAX_USERS = 1000;
public static final int MAX_USERS = 1000;
public EditorCommand(LocaleManager locale) {
super(CommandSpec.EDITOR.localize(locale), "Editor", CommandPermission.EDITOR, Predicates.notInRange(0, 2));
@ -99,29 +99,34 @@ public class EditorCommand extends SingleCommand {
tracks.addAll(plugin.getTrackManager().getAll().values());
}
if (type.includingUsers) {
Map<UUID, User> users;
// include all online players
Map<UUID, User> users = new LinkedHashMap<>(plugin.getUserManager().getAll());
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
users = new LinkedHashMap<>(plugin.getUserManager().getAll());
// then fill up with other users with permissions
// only include online players matching the permission
users.values().removeIf(user -> user.normalData().immutable().values().stream().noneMatch(matcher));
// fill up with other matching users
if (type.includingOffline && users.size() < MAX_USERS) {
plugin.getStorage().searchUserNodes(matcher).join().stream()
.map(NodeEntry::getHolder)
.distinct()
.filter(uuid -> !users.containsKey(uuid))
.sorted()
.limit(MAX_USERS - users.size())
.forEach(uuid -> {
User user = plugin.getStorage().loadUser(uuid, null).join();
if (user != null) {
users.put(uuid, user);
}
plugin.getUserManager().getHouseKeeper().cleanup(uuid);
});
}
} else {
// fill up with other users
if (type.includingOffline && users.size() < MAX_USERS) {
plugin.getStorage().getUniqueUsers().join().stream()
.filter(uuid -> !users.containsKey(uuid))