From cf8852524ed561701d6639accccbcab681172599 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Sun, 27 Jan 2019 20:29:52 +0100 Subject: [PATCH] Add factions top --- plugin.yml | 3 + src/com/massivecraft/factions/Perm.java | 1 + .../factions/cmd/CmdFactions.java | 1 + .../factions/cmd/CmdFactionsTop.java | 112 ++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsTop.java diff --git a/plugin.yml b/plugin.yml index 2b295930..d103cdaa 100644 --- a/plugin.yml +++ b/plugin.yml @@ -92,6 +92,7 @@ permissions: factions.title: {description: set player title, default: false} factions.title.color: {description: set player title with color, default: false} factions.territorytitles: {description: toggle territory titles, default: false} + factions.top: {description: show faction top, default: false} factions.unclaim: {description: unclaim faction territory, default: false} factions.unclaim.one: {description: unclaim a single chunk, default: false} factions.unclaim.auto: {description: unclaim as you walk around, default: false} @@ -202,6 +203,7 @@ permissions: factions.title: true factions.title.color: true factions.territorytitles: true + factions.top: true factions.unclaim: true factions.unclaim.one: true factions.unclaim.auto: true @@ -333,6 +335,7 @@ permissions: factions.title: true factions.title.color: true factions.territorytitles: true + factions.top: true factions.unclaim: true factions.unclaim.one: true factions.unclaim.auto: true diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index adbf03ec..33394b0e 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -93,6 +93,7 @@ public enum Perm implements Identified TITLE, TITLE_COLOR, TERRITORYTITLES, + TOP, UNCLAIM, UNCLAIM_ONE, UNCLAIM_AUTO, diff --git a/src/com/massivecraft/factions/cmd/CmdFactions.java b/src/com/massivecraft/factions/cmd/CmdFactions.java index 318c9479..ab9c5d22 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactions.java +++ b/src/com/massivecraft/factions/cmd/CmdFactions.java @@ -40,6 +40,7 @@ public class CmdFactions extends FactionsCommand public CmdFactionsTitle cmdFactionsTitle = new CmdFactionsTitle(); public CmdFactionsRank cmdFactionsRank = new CmdFactionsRank(); public CmdFactionsMoney cmdFactionsMoney = new CmdFactionsMoney(); + public CmdFactionsTop cmdFactionsTop = new CmdFactionsTop(); public CmdFactionsSeeChunk cmdFactionsSeeChunk = new CmdFactionsSeeChunk(); public CmdFactionsSeeChunkOld cmdFactionsSeeChunkOld = new CmdFactionsSeeChunkOld(); public CmdFactionsTerritorytitles cmdFactionsTerritorytitles = new CmdFactionsTerritorytitles(); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsTop.java b/src/com/massivecraft/factions/cmd/CmdFactionsTop.java new file mode 100644 index 00000000..edce63f1 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsTop.java @@ -0,0 +1,112 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.FactionColl; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.integration.Econ; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.Parameter; +import com.massivecraft.massivecore.command.type.enumeration.TypeEnum; +import com.massivecraft.massivecore.money.Money; +import com.massivecraft.massivecore.pager.Pager; +import com.massivecraft.massivecore.pager.Stringifier; +import com.massivecraft.massivecore.util.TimeDiffUtil; +import com.massivecraft.massivecore.util.TimeUnit; +import com.massivecraft.massivecore.util.Txt; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +import java.util.LinkedHashMap; +import java.util.List; + +public class CmdFactionsTop extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsTop() + { + // Parameters + this.addParameter(new TypeEnum<>(TopCategory.class)); + this.addParameter(Parameter.getPage()); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Args + TopCategory category = this.readArg(); + int page = this.readArg(); + final CommandSender sender = this.sender; + final MPlayer msender = this.msender; + + if (category == TopCategory.MONEY && !Econ.isEnabled()) + { + throw new MassiveException().addMsg("Economy is not enabled."); + } + + Pager pager = new Pager<>(this, "Faction top", page); + pager.setMsonifier((Stringifier) (f, i) -> getValue(category, f, msender)); + + // NOTE: The faction list is quite slow and mostly thread safe. + // We run it asynchronously to spare the primary server thread. + + Bukkit.getScheduler().runTaskAsynchronously(Factions.get(), new Runnable() + { + @Override + public void run() + { + // Pager Items + List factions = FactionColl.get().getAll((f1, f2) -> (int) (getNumber(category, f2) - getNumber(category, f1))); + pager.setItems(factions); + + // Pager Message + pager.message(); + } + }); + } + + private static double getNumber(TopCategory category, Faction faction) + { + switch(category) + { + case MONEY: return Money.get(faction); + case MEMBERS: return faction.getMPlayers().size(); + case TERRITORY: return faction.getLandCount(); + case AGE: return faction.getAge(); + } + throw new RuntimeException(); + } + + private static String getValue(TopCategory category, Faction faction, MPlayer mplayer) + { + String ret = Txt.parse("%s: ", faction.getName(mplayer)); + switch(category) + { + case MONEY: ret += Money.format(Money.get(faction), true); break; + case MEMBERS: ret += faction.getMPlayers().size() + " members"; break; + case TERRITORY: ret += faction.getLandCount() + " chunks"; break; + case AGE: + long ageMillis = faction.getAge(); + LinkedHashMap ageUnitcounts = TimeDiffUtil.limit(TimeDiffUtil.unitcounts(ageMillis, TimeUnit.getAllButMillis()), 3); + ret += TimeDiffUtil.formatedVerboose(ageUnitcounts); + break; + } + return ret; + } + + public enum TopCategory + { + MONEY, + MEMBERS, + TERRITORY, + AGE, + } + +}