From e2017b3fd2f129cab650bdaf71058eb66ce475fe Mon Sep 17 00:00:00 2001 From: speed <43330808+speedxx@users.noreply.github.com> Date: Sun, 29 Nov 2020 20:46:21 -0500 Subject: [PATCH] page /g list --- .idea/TFGuilds.iml | 19 ++---- pom.xml | 6 +- .../tfguilds/command/ListSubcommand.java | 42 +++++++++++- .../tfguilds/util/PaginationList.java | 66 +++++++++++++++++++ 4 files changed, 112 insertions(+), 21 deletions(-) create mode 100644 src/main/java/me/totalfreedom/tfguilds/util/PaginationList.java diff --git a/.idea/TFGuilds.iml b/.idea/TFGuilds.iml index d0c8bd9..4742ffe 100644 --- a/.idea/TFGuilds.iml +++ b/.idea/TFGuilds.iml @@ -25,20 +25,7 @@ - - - - - - - - - - - - - - + @@ -68,6 +55,7 @@ + @@ -87,6 +75,7 @@ + @@ -105,6 +94,6 @@ - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index eb5071b..645f53b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.totalfreedom TFGuilds - 0.2.17 + 0.2.18 jar TFGuilds @@ -83,13 +83,13 @@ me.totalfreedom TotalFreedomMod - 2020.9 + 2020.11.5 provided org.projectlombok lombok - 1.18.14 + 1.18.16 provided diff --git a/src/main/java/me/totalfreedom/tfguilds/command/ListSubcommand.java b/src/main/java/me/totalfreedom/tfguilds/command/ListSubcommand.java index 463b765..c400888 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/ListSubcommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/ListSubcommand.java @@ -3,7 +3,7 @@ package me.totalfreedom.tfguilds.command; import java.util.List; import me.totalfreedom.tfguilds.Common; import me.totalfreedom.tfguilds.guild.Guild; -import org.apache.commons.lang.StringUtils; +import me.totalfreedom.tfguilds.util.PaginationList; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -14,7 +14,14 @@ public class ListSubcommand extends Common implements CommandExecutor @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length > 2) + { + sender.sendMessage(tl(PREFIX + "Proper usage: /g list [page]")); + return true; + } + List guilds = Guild.getGuildList(); + PaginationList paged = new PaginationList<>(10); if (guilds.isEmpty()) { @@ -22,8 +29,37 @@ public class ListSubcommand extends Common implements CommandExecutor return true; } - sender.sendMessage(tl(PREFIX + "%s%Guild List (%p%" + guilds.size() + " total%s%)")); - sender.sendMessage(tl("%s%- %p%" + StringUtils.join(guilds, "\n%s%- %p%"))); + paged.addAll(guilds); + + int pageIndex = 1; + + if (args.length >= 2) + { + try + { + pageIndex = Integer.parseInt(args[1]); + } + catch (NumberFormatException e) + { + sender.sendMessage(ChatColor.RED + "Invalid number"); + } + } + + if (pageIndex > paged.getPageCount() || pageIndex + 1 < paged.getPageCount()) + { + sender.sendMessage(ChatColor.RED + "Not a valid page number"); + return true; + } + + paged.getPage(pageIndex); + List page = paged.getPage(pageIndex); + + sender.sendMessage(tl(PREFIX + "%s%Guild List (%p%" + guilds.size() + " total%s%) [%p%Page " + pageIndex + "%s%/%p%" + paged.getPageCount() + "%s%]")); + + for (String guild : page) + { + sender.sendMessage(tl("%s%- %p%" + guild)); + } return true; } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/tfguilds/util/PaginationList.java b/src/main/java/me/totalfreedom/tfguilds/util/PaginationList.java new file mode 100644 index 0000000..1060caa --- /dev/null +++ b/src/main/java/me/totalfreedom/tfguilds/util/PaginationList.java @@ -0,0 +1,66 @@ +package me.totalfreedom.tfguilds.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class PaginationList extends ArrayList +{ + private final int epp; + + /** + * Defines a new Pagination List. + * @param epp Elements per page - how many elements will be included on a page of the list. + */ + public PaginationList(int epp) + { + super(); + this.epp = epp; + } + + /** + * Defines a new Pagination List. + * @param epp Elements per page - how many elements will be included on a page of the list. + * @param elements Elements to add to the list immediately. + */ + @SafeVarargs + public PaginationList(int epp, T... elements) + { + super(Arrays.asList(elements)); + this.epp = epp; + } + + /** + * @return The number of pages this list holds. + */ + public int getPageCount() + { + return (int) Math.ceil((double) size() / (double) epp); + } + + /** + * Get a page from the list. + * @param page Page you want to access. + * @return A sublist of only the elements from that page. + */ + public List getPage(int page) + { + if (page < 1 || page > getPageCount()) return null; + int startIndex = (page - 1) * epp; + int endIndex = Math.min(startIndex + (epp - 1), this.size() - 1); + return subList(startIndex, endIndex + 1); + } + + /*@Override + public String toString() + { + StringBuilder res = new StringBuilder(); + for (int i = 1; i <= getPageCount(); i++) + { + res.append("Page ").append(i).append(": ").append("\n"); + for (T element : getPage(i)) + res.append(" - ").append(element).append("\n"); + } + return res.toString(); + }*/ +} \ No newline at end of file