From 91fc5a2404400e5aab589604650f512212a111b7 Mon Sep 17 00:00:00 2001 From: ulumulu1510 Date: Fri, 17 Mar 2017 10:09:57 +0100 Subject: [PATCH] Change Flag show and list commands. --- .../factions/cmd/CmdFactionsFlagList.java | 53 ++++++++--- .../factions/cmd/CmdFactionsFlagShow.java | 45 +++++---- .../massivecraft/factions/entity/MFlag.java | 93 +++++++++---------- 3 files changed, 109 insertions(+), 82 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsFlagList.java b/src/com/massivecraft/factions/cmd/CmdFactionsFlagList.java index 6ada52fe..ec8003e4 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsFlagList.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsFlagList.java @@ -1,12 +1,17 @@ package com.massivecraft.factions.cmd; -import java.util.ArrayList; import java.util.List; +import org.bukkit.Bukkit; + +import com.massivecraft.factions.Factions; import com.massivecraft.factions.entity.MFlag; +import com.massivecraft.factions.entity.MFlagColl; 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; public class CmdFactionsFlagList extends FactionsCommand { @@ -27,20 +32,42 @@ public class CmdFactionsFlagList extends FactionsCommand @Override public void perform() throws MassiveException { - // Args - int page = this.readArg(); + // Parameter + final int page = this.readArg(); - //Create messages - List messages = new ArrayList(); - - for (MFlag flag : MFlag.getAll()) + // Pager create + String title = "Flag List for " + msenderFaction.describeTo(msender); + final Pager pager = new Pager<>(this, title, page, new Stringifier() { - if ( ! flag.isVisible() && ! msender.isOverriding()) continue; - messages.add(flag.getStateDesc(false, false, true, true, true, false)); - } + @Override + public String toString(MFlag mflag, int index) + { + return mflag.getStateDesc(false, false, true, true, true, false); + } + }); - //Send messages - message(Txt.getPage(messages, page, "Available Faction Flags", this)); + Bukkit.getScheduler().runTaskAsynchronously(Factions.get(), new Runnable() + { + @Override + public void run() + { + // Get items + List items = MFlagColl.get().getAll(msender.isOverriding() ? null : new Predicate() + { + @Override + public boolean apply(MFlag mflag) + { + return mflag.isVisible(); + } + }); + + // Pager items + pager.setItems(items); + + // Pager message + pager.message(); + } + }); } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsFlagShow.java b/src/com/massivecraft/factions/cmd/CmdFactionsFlagShow.java index ffe69952..44490fd8 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsFlagShow.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsFlagShow.java @@ -1,16 +1,15 @@ package com.massivecraft.factions.cmd; -import java.util.ArrayList; -import java.util.Collection; import java.util.List; import com.massivecraft.factions.cmd.type.TypeFaction; -import com.massivecraft.factions.cmd.type.TypeMFlag; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.MFlag; import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.command.type.container.TypeSet; -import com.massivecraft.massivecore.util.Txt; +import com.massivecraft.massivecore.collections.MassiveList; +import com.massivecraft.massivecore.command.Parameter; +import com.massivecraft.massivecore.pager.Pager; +import com.massivecraft.massivecore.pager.Stringifier; public class CmdFactionsFlagShow extends FactionsCommand { @@ -22,7 +21,7 @@ public class CmdFactionsFlagShow extends FactionsCommand { // Parameters this.addParameter(TypeFaction.get(), "faction", "you"); - this.addParameter(TypeSet.get(TypeMFlag.get()), "flags", "all", true); + this.addParameter(Parameter.getPage()); } // -------------------------------------------- // @@ -32,20 +31,30 @@ public class CmdFactionsFlagShow extends FactionsCommand @Override public void perform() throws MassiveException { - // Arg: Faction - Faction faction = this.readArg(msenderFaction); - Collection mflags = this.readArg(MFlag.getAll()); + // Parameters + final Faction faction = this.readArg(msenderFaction); + int page = this.readArg(); - // Create messages - List messages = new ArrayList<>(); - messages.add(Txt.titleize("Flag for " + faction.describeTo(msender, true))); - for (MFlag mflag : mflags) + // Pager create + String title = "Flags for " + faction.describeTo(msender); + Pager pager = new Pager<>(this, title, page, MFlag.getAll(), new Stringifier() { - messages.add(mflag.getStateDesc(faction.getFlag(mflag), true, true, true, true, true)); - } - - // Send messages - message(messages); + @Override + public String toString(MFlag mflag, int index) + { + return mflag.getStateDesc(faction.getFlag(mflag), true, true, true, true, true); + } + }); + + // Pager args + List pagerArgs = new MassiveList<>( + faction.getId(), + String.valueOf(page) + ); + pager.setArgs(pagerArgs); + + // Pager message + pager.messageAsync(); } } diff --git a/src/com/massivecraft/factions/entity/MFlag.java b/src/com/massivecraft/factions/entity/MFlag.java index 42de06e6..8dd88b5b 100644 --- a/src/com/massivecraft/factions/entity/MFlag.java +++ b/src/com/massivecraft/factions/entity/MFlag.java @@ -2,6 +2,8 @@ package com.massivecraft.factions.entity; import java.util.List; +import org.bukkit.ChatColor; + import com.massivecraft.factions.event.EventFactionsCreateFlags; import com.massivecraft.massivecore.Named; import com.massivecraft.massivecore.Prioritized; @@ -249,62 +251,51 @@ public class MFlag extends Entity implements Prioritized, Registerable, N public String getStateDesc(boolean value, boolean withValue, boolean monospaceValue, boolean withName, boolean withDesc, boolean specificDesc) { - List parts = new MassiveList(); + // Create + List ret = new MassiveList<>(); - if (withValue) - { - if (monospaceValue) - { - parts.add(Txt.parse(value ? "YES" : "NOO")); - } - else - { - parts.add(Txt.parse(value ? "YES" : "NO")); - } - } + // Fill + if (withValue) ret.add(getStateValue(value, monospaceValue)); + if (withName) ret.add(this.getStateName()); + if (withDesc) ret.add(this.getStateDescription(value, specificDesc)); - if (withName) - { - String nameFormat; - if ( ! this.isVisible()) - { - nameFormat = "%s"; - } - else if (this.isEditable()) - { - nameFormat = "%s"; - } - else - { - nameFormat = "%s"; - } - String name = this.getName(); - String nameDesc = Txt.parse(nameFormat, name); - parts.add(nameDesc); - } - - if (withDesc) - { - String desc; - if (specificDesc) - { - desc = value ? this.getDescYes() : this.getDescNo(); - } - else - { - desc = this.getDesc(); - } - String descDesc = Txt.parse("%s", desc); - parts.add(descDesc); - } - - return Txt.implode(parts, " "); + // Return + return Txt.implode(ret, " "); } - @Deprecated - public String getStateInfo(boolean value, boolean withDesc) + private static String getStateValue(boolean value, boolean monoSpace) { - return this.getStateDesc(value, true, true, true, true, false); + String yes = "YES"; + String no = monoSpace ? "NOO" : "NO"; + + return Txt.parse(value ? yes : no); + } + + private String getStateName() + { + return this.getStateColor().toString() + this.getName(); + } + + private ChatColor getStateColor() + { + // Is special? + if (!this.isVisible()) return ChatColor.GRAY; + if (this.isEditable()) return ChatColor.LIGHT_PURPLE; + + // Return normal + return ChatColor.AQUA; + } + + private String getStateDescription(boolean value, boolean specific) + { + // Create + String desc = this.getDesc(); + + // Is specific? + if (specific) desc = value ? this.getDescYes() : this.getDescNo(); + + // Return + return Txt.parse("%s", desc); } }