From 31823f531157edf634183e33862819b6a7b69efd Mon Sep 17 00:00:00 2001 From: TheComputerGeek2 Date: Sun, 16 Apr 2017 19:31:14 -0700 Subject: [PATCH] Use Pager to list faction perms --- .../factions/cmd/CmdFactionsPermList.java | 58 +++++++++++++++---- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermList.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermList.java index 8cdb06b0..cd2e3243 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPermList.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermList.java @@ -1,15 +1,32 @@ package com.massivecraft.factions.cmd; +import com.massivecraft.factions.Factions; import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPermColl; import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.command.Parameter; -import com.massivecraft.massivecore.util.Txt; +import com.massivecraft.massivecore.pager.Pager; +import com.massivecraft.massivecore.pager.Stringifier; +import com.massivecraft.massivecore.predicate.Predicate; +import org.bukkit.Bukkit; -import java.util.ArrayList; import java.util.List; public class CmdFactionsPermList extends FactionsCommand { + // -------------------------------------------- // + // REUSABLE PREDICATE + // -------------------------------------------- // + + private static final Predicate PREDICATE_MPERM_VISIBLE = new Predicate() + { + @Override + public boolean apply(MPerm mperm) + { + return mperm.isVisible(); + } + }; + // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // @@ -27,20 +44,37 @@ public class CmdFactionsPermList extends FactionsCommand @Override public void perform() throws MassiveException { - // Args + // Parameter int page = this.readArg(); - // Create messages - List messages = new ArrayList<>(); - - for (MPerm perm : MPerm.getAll()) + // Pager create + String title = String.format("Perms for %s", msenderFaction.describeTo(msender)); + final Pager pager = new Pager<>(this, title, page, new Stringifier() { - if ( ! perm.isVisible() && ! msender.isOverriding()) continue; - messages.add(perm.getDesc(true, true)); - } + @Override + public String toString(MPerm mperm, int index) + { + return mperm.getDesc(true, true); + } + }); - // Send messages - message(Txt.getPage(messages, page, "Available Faction Perms", this)); + final Predicate predicate = msender.isOverriding() ? null : PREDICATE_MPERM_VISIBLE; + + Bukkit.getScheduler().runTaskAsynchronously(Factions.get(), new Runnable() + { + @Override + public void run() + { + // Get items + List items = MPermColl.get().getAll(predicate); + + // Pager items + pager.setItems(items); + + // Pager message + pager.message(); + } + }); } }