From 2e40557b39b5da74a1407917e7bcbde44b923d84 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 4 Apr 2017 16:07:41 +0100 Subject: [PATCH] Add '/lp group listmembers' command - closes #203 --- .../commands/impl/group/GroupListMembers.java | 210 ++++++++++++++++++ .../commands/impl/group/GroupMainCommand.java | 1 + .../luckperms/common/constants/Message.java | 1 + .../common/constants/Permission.java | 1 + 4 files changed, 213 insertions(+) create mode 100644 common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupListMembers.java diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupListMembers.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupListMembers.java new file mode 100644 index 00000000..69b6edcf --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupListMembers.java @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2017 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.commands.impl.group; + +import com.google.common.collect.Maps; + +import me.lucko.luckperms.api.HeldPermission; +import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.abstraction.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Message; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.core.NodeFactory; +import me.lucko.luckperms.common.core.model.Group; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.utils.DateUtil; +import me.lucko.luckperms.common.utils.Predicates; + +import io.github.mkremins.fanciful.ChatColor; +import io.github.mkremins.fanciful.FancyMessage; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class GroupListMembers extends SubCommand { + public GroupListMembers() { + super("listmembers", "Show the users/groups who inherit from this group", + Permission.GROUP_LISTMEMBERS, Predicates.notInRange(0, 1), + Arg.list( + Arg.create("page", false, "the page to view") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { + String query = "group." + group.getName(); + int page = ArgumentUtils.handleIntOrElse(0, args, 1); + + Message.SEARCH_SEARCHING_MEMBERS.send(sender, group.getName()); + + List> matchedUsers = plugin.getStorage().getUsersWithPermission(query).join(); + List> matchedGroups = plugin.getStorage().getGroupsWithPermission(query).join(); + + int users = matchedUsers.size(); + int groups = matchedGroups.size(); + + Message.SEARCH_RESULT.send(sender, users + groups, users, groups); + + Map uuidLookups = new HashMap<>(); + Function lookupFunc = uuid -> uuidLookups.computeIfAbsent(uuid, u -> { + String s = plugin.getStorage().getName(u).join(); + if (s == null || s.isEmpty() || s.equals("null")) { + s = u.toString(); + } + return s; + }); + + Map.Entry msgUsers = searchUserResultToMessage(matchedUsers, lookupFunc, label, page); + Map.Entry msgGroups = searchGroupResultToMessage(matchedGroups, label, page); + + if (msgUsers.getValue() != null) { + Message.SEARCH_SHOWING_USERS_WITH_PAGE.send(sender, msgUsers.getValue()); + sender.sendMessage(msgUsers.getKey()); + } else { + Message.SEARCH_SHOWING_USERS.send(sender); + sender.sendMessage(msgUsers.getKey()); + } + + if (msgGroups.getValue() != null) { + Message.SEARCH_SHOWING_GROUPS_WITH_PAGE.send(sender, msgGroups.getValue()); + sender.sendMessage(msgGroups.getKey()); + } else { + Message.SEARCH_SHOWING_GROUPS.send(sender); + sender.sendMessage(msgGroups.getKey()); + } + + return CommandResult.SUCCESS; + } + + private static Map.Entry searchUserResultToMessage(List> results, Function uuidLookup, String label, int pageNumber) { + if (results.isEmpty()) { + return Maps.immutableEntry(new FancyMessage("None").color(ChatColor.getByChar('3')), null); + } + + List> sorted = new ArrayList<>(results); + sorted.sort(Comparator.comparing(HeldPermission::getHolder)); + + int index = pageNumber - 1; + List>> pages = Util.divideList(sorted, 15); + + if (index < 0 || index >= pages.size()) { + pageNumber = 1; + index = 0; + } + + List> page = pages.get(index); + List>> uuidMappedPage = page.stream() + .map(hp -> Maps.immutableEntry(uuidLookup.apply(hp.getHolder()), hp)) + .collect(Collectors.toList()); + + FancyMessage message = new FancyMessage(""); + String title = "&7(page &f" + pageNumber + "&7 of &f" + pages.size() + "&7 - &f" + sorted.size() + "&7 entries)"; + + for (Map.Entry> ent : uuidMappedPage) { + message.then("> ").color(ChatColor.getByChar('3')).apply(m -> makeFancy(m, ent.getKey(), false, label, ent.getValue())) + .then(ent.getKey() + " ").color(ChatColor.getByChar('b')).apply(m -> makeFancy(m, ent.getKey(), false, label, ent.getValue())) + .apply(ent.getValue().asNode(), GroupListMembers::appendNodeExpiry) + .apply(ent.getValue().asNode(), Util::appendNodeContextDescription) + .then("\n"); + } + + return Maps.immutableEntry(message, title); + } + + private static Map.Entry searchGroupResultToMessage(List> results, String label, int pageNumber) { + if (results.isEmpty()) { + return Maps.immutableEntry(new FancyMessage("None").color(ChatColor.getByChar('3')), null); + } + + List> sorted = new ArrayList<>(results); + sorted.sort(Comparator.comparing(HeldPermission::getHolder)); + + int index = pageNumber - 1; + List>> pages = Util.divideList(sorted, 15); + + if (index < 0 || index >= pages.size()) { + pageNumber = 1; + index = 0; + } + + List> page = pages.get(index); + + FancyMessage message = new FancyMessage(""); + String title = "&7(page &f" + pageNumber + "&7 of &f" + pages.size() + "&7 - &f" + sorted.size() + "&7 entries)"; + + for (HeldPermission ent : page) { + message.then("> ").color(ChatColor.getByChar('3')).apply(m -> makeFancy(m, ent.getHolder(), true, label, ent)) + .then(ent.getHolder() + " ").color(ChatColor.getByChar('b')).apply(m -> makeFancy(m, ent.getHolder(), true, label, ent)) + .apply(ent.asNode(), GroupListMembers::appendNodeExpiry) + .apply(ent.asNode(), Util::appendNodeContextDescription) + .then("\n"); + } + + return Maps.immutableEntry(message, title); + } + + private static void appendNodeExpiry(FancyMessage message, Node node) { + if (!node.isTemporary()) { + return; + } + + message.then(" (").color(ChatColor.getByChar('8')) + .then("expires in " + DateUtil.formatDateDiff(node.getExpiryUnixTime())).color(ChatColor.getByChar('7')) + .then(")").color(ChatColor.getByChar('8')); + } + + private static void makeFancy(FancyMessage message, String holderName, boolean group, String label, HeldPermission perm) { + Node node = perm.asNode(); + + message = message.formattedTooltip( + new FancyMessage("> ") + .color(ChatColor.getByChar('3')) + .then(node.getPermission()) + .color(node.getValue() ? ChatColor.getByChar('a') : ChatColor.getByChar('c')), + new FancyMessage(" "), + new FancyMessage("Click to remove this parent from " + holderName).color(ChatColor.getByChar('7')) + ); + + String command = NodeFactory.nodeAsCommand(node, holderName, group) + .replace("/luckperms", "/" + label) + .replace("permission set", "permission unset") + .replace("parent add", "parent remove") + .replace(" true", "") + .replace(" false", ""); + + message.suggest(command); + } +} diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupMainCommand.java index 2d861592..f11bd128 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/group/GroupMainCommand.java @@ -46,6 +46,7 @@ public class GroupMainCommand extends MainCommand { .add(new CommandPermission<>(false)) .add(new CommandParent<>(false)) .add(new CommandMeta<>(false)) + .add(new GroupListMembers()) .add(new GroupSetWeight()) .add(new HolderShowTracks<>(false)) .add(new GroupBulkChange()) diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Message.java b/common/src/main/java/me/lucko/luckperms/common/constants/Message.java index cc6260bd..ba494336 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Message.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Message.java @@ -112,6 +112,7 @@ public enum Message { TREE_URL("&aPermission Tree URL:", true), SEARCH_SEARCHING("&aSearching for users and groups with &b{0}&a...", true), + SEARCH_SEARCHING_MEMBERS("&aSearching for users and groups who inherit from &b{0}&a...", true), SEARCH_RESULT("&aFound &b{0}&a entries from &b{1}&a users and &b{2}&a groups.", true), SEARCH_SHOWING_USERS("&bShowing user entries:", true), SEARCH_SHOWING_GROUPS("&bShowing group entries:", true), diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java b/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java index c6cef555..22fd43e3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java @@ -120,6 +120,7 @@ public enum Permission { GROUP_META_REMOVETEMP_PREFIX(list("meta.removetempprefix", "removetempprefix"), Type.GROUP), GROUP_META_REMOVETEMP_SUFFIX(list("meta.removetempsuffix", "removetempsuffix"), Type.GROUP), GROUP_META_CLEAR(list("meta.clear", "clearmeta"), Type.GROUP), + GROUP_LISTMEMBERS(list("listmembers"), Type.GROUP), GROUP_SHOWTRACKS(list("showtracks"), Type.GROUP), GROUP_SETWEIGHT(list("setweight"), Type.GROUP), GROUP_BULKCHANGE(list("bulkchange"), Type.GROUP),