Implement paginated permission listings - closes #80

This commit is contained in:
Luck 2016-12-20 13:55:42 +00:00
parent 8a692200d5
commit 5e68f7b58c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 73 additions and 10 deletions

View File

@ -23,6 +23,7 @@
package me.lucko.luckperms.common.commands.generic.permission; package me.lucko.luckperms.common.commands.generic.permission;
import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.commands.Arg;
import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.generic.SharedSubCommand; import me.lucko.luckperms.common.commands.generic.SharedSubCommand;
@ -34,20 +35,44 @@ import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.PermissionHolder; import me.lucko.luckperms.common.core.model.PermissionHolder;
import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.Predicates;
import io.github.mkremins.fanciful.FancyMessage;
import java.util.List; import java.util.List;
import java.util.Map;
public class PermissionInfo extends SharedSubCommand { public class PermissionInfo extends SharedSubCommand {
public PermissionInfo() { public PermissionInfo() {
super("info", "Lists the permission nodes the object has", Permission.USER_PERM_INFO, Permission.GROUP_PERM_INFO, Predicates.alwaysFalse(), null); super("info", "Lists the permission nodes the object has", Permission.USER_PERM_INFO,
Permission.GROUP_PERM_INFO, Predicates.alwaysFalse(),
Arg.list(
Arg.create("page", false, "the page to view")
)
);
} }
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
Message.LISTNODES.send(sender, holder.getFriendlyName());
if (sender.getUuid().equals(Constants.getConsoleUUID())) { if (sender.getUuid().equals(Constants.getConsoleUUID())) {
Message.LISTNODES.send(sender, holder.getFriendlyName());
sender.sendMessage(Util.color(Util.permNodesToStringConsole(holder.getPermissions(false)))); sender.sendMessage(Util.color(Util.permNodesToStringConsole(holder.getPermissions(false))));
} else { } else {
sender.sendMessage(Util.permNodesToMessage(holder.getPermissions(false), holder, label)); int page = 1;
if (args.size() > 0) {
try {
page = Integer.parseInt(args.get(0));
} catch (NumberFormatException e) {
// ignored
}
}
Map.Entry<FancyMessage, String> ent = Util.permNodesToMessage(holder.getPermissions(false), holder, label, page);
if (ent.getValue() != null) {
Message.LISTNODES_WITH_PAGE.send(sender, holder.getFriendlyName(), ent.getValue());
sender.sendMessage(ent.getKey());
} else {
Message.LISTNODES.send(sender, holder.getFriendlyName());
sender.sendMessage(ent.getKey());
}
} }
Message.LISTNODES_TEMP.send(sender, holder.getFriendlyName(), Util.tempNodesToString(holder.getPermissions(false))); Message.LISTNODES_TEMP.send(sender, holder.getFriendlyName(), Util.tempNodesToString(holder.getPermissions(false)));

View File

@ -24,6 +24,8 @@ package me.lucko.luckperms.common.commands.utils;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import com.google.common.collect.Maps;
import me.lucko.luckperms.api.LocalizedNode; import me.lucko.luckperms.api.LocalizedNode;
import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
@ -40,6 +42,7 @@ import io.github.mkremins.fanciful.FancyMessage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Map; import java.util.Map;
@ -195,19 +198,52 @@ public class Util {
return message; return message;
} }
public static FancyMessage permNodesToMessage(SortedSet<LocalizedNode> nodes, PermissionHolder holder, String label) { public static Map.Entry<FancyMessage, String> permNodesToMessage(SortedSet<LocalizedNode> nodes, PermissionHolder holder, String label, int pageNumber) {
FancyMessage message = new FancyMessage(""); List<Node> l = new ArrayList<>();
boolean found = false;
for (Node node : nodes) { for (Node node : nodes) {
if (node.isTemporary()) continue; if (!node.isTemporary()) {
found = true; l.add(node);
}
}
if (l.isEmpty()) {
return Maps.immutableEntry(new FancyMessage("None").color(ChatColor.getByChar('3')), null);
}
int index = pageNumber - 1;
List<List<Node>> pages = divideList(l, 20);
if ((index < 0 || index >= pages.size())) {
pageNumber = 1;
index = 0;
}
List<Node> page = pages.get(index);
FancyMessage message = new FancyMessage("");
String title = "&7(showing page &f" + pageNumber + "&7 of &f" + pages.size() + "&7 - &f" + nodes.size() + "&7 entries)";
for (Node node : page) {
message = makeFancy(holder, label, node, message.then("> ").color(ChatColor.getByChar('3'))); message = makeFancy(holder, label, node, message.then("> ").color(ChatColor.getByChar('3')));
message = makeFancy(holder, label, node, message.then(Util.color(node.getPermission())).color(node.getValue() ? ChatColor.getByChar('a') : ChatColor.getByChar('c'))); message = makeFancy(holder, label, node, message.then(Util.color(node.getPermission())).color(node.getValue() ? ChatColor.getByChar('a') : ChatColor.getByChar('c')));
message = makeFancy(holder, label, node, appendNodeContextDescription(node, message)); message = makeFancy(holder, label, node, appendNodeContextDescription(node, message));
message = message.then("\n"); message = message.then("\n");
} }
return !found ? new FancyMessage("None").color(ChatColor.getByChar('3')) : message;
return Maps.immutableEntry(message, title);
}
private static <T> List<List<T>> divideList(List<T> source, int size) {
List<List<T>> lists = new ArrayList<>();
Iterator<T> it = source.iterator();
while (it.hasNext()) {
List<T> subList = new ArrayList<>();
for (int i = 0; it.hasNext() && i < size; i++) {
subList.add(it.next());
}
lists.add(subList);
}
return lists;
} }
public static String tempNodesToString(SortedSet<LocalizedNode> nodes) { public static String tempNodesToString(SortedSet<LocalizedNode> nodes) {

View File

@ -161,6 +161,7 @@ public enum Message {
TRACKS_LIST("&aTracks: {0}", true), TRACKS_LIST("&aTracks: {0}", true),
LISTNODES("&b{0}'s Nodes:", true), LISTNODES("&b{0}'s Nodes:", true),
LISTNODES_WITH_PAGE("&b{0}'s Nodes: {1}", true),
LISTNODES_TEMP("&b{0}'s Temporary Nodes:" + "\n" + "{1}", true), LISTNODES_TEMP("&b{0}'s Temporary Nodes:" + "\n" + "{1}", true),
LISTPARENTS("&b{0}'s Parent Groups:" + "\n" + "{1}", true), LISTPARENTS("&b{0}'s Parent Groups:" + "\n" + "{1}", true),
LISTPARENTS_TEMP("&b{0}'s Temporary Parent Groups:" + "\n" + "{1}", true), LISTPARENTS_TEMP("&b{0}'s Temporary Parent Groups:" + "\n" + "{1}", true),

View File

@ -121,6 +121,7 @@ delete-track-error: "There was an error whilst deleting the track."
tracks-list: "&aTracks: {0}" tracks-list: "&aTracks: {0}"
listnodes: "&b{0}'s Nodes:" listnodes: "&b{0}'s Nodes:"
listnodes-with-page: "&b{0}'s Nodes: {1}"
listnodes-temp: "&b{0}'s Temporary Nodes:\n{1}" listnodes-temp: "&b{0}'s Temporary Nodes:\n{1}"
listparents: "&b{0}'s Parent Groups:\n{1}" listparents: "&b{0}'s Parent Groups:\n{1}"
listparents-temp: "&b{0}'s Temporary Parent Groups:\n{1}" listparents-temp: "&b{0}'s Temporary Parent Groups:\n{1}"