From 3e6bd4e88a9c7a301ffa947f27d1bf0ff8437917 Mon Sep 17 00:00:00 2001 From: drtshock Date: Fri, 23 Jan 2015 09:07:18 -0600 Subject: [PATCH] Add f top command functionality. Resolves #71. Gives the ability to see top Factions on the server sorted by certain criteria: Balance: sorts by faction bank balance and all faction member balances. Power: total power. Land: total land claimed. Online: members currently online. Members: total members, online and offline. Future criteria can be added of course. --- .../com/massivecraft/factions/cmd/CmdTop.java | 180 ++++++++++++++++++ .../massivecraft/factions/cmd/FCmdRoot.java | 2 + .../factions/struct/Permission.java | 1 + .../massivecraft/factions/zcore/util/TL.java | 5 + src/main/resources/plugin.yml | 5 +- 5 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/massivecraft/factions/cmd/CmdTop.java diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdTop.java b/src/main/java/com/massivecraft/factions/cmd/CmdTop.java new file mode 100644 index 00000000..296faf44 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/cmd/CmdTop.java @@ -0,0 +1,180 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.integration.Econ; +import com.massivecraft.factions.struct.Permission; +import com.massivecraft.factions.zcore.util.TL; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class CmdTop extends FCommand { + + public CmdTop() { + super(); + this.aliases.add("top"); + this.aliases.add("t"); + + //this.requiredArgs.add(""); + this.requiredArgs.add("criteria"); + this.optionalArgs.put("page", "1"); + + this.permission = Permission.TOP.node; + this.disableOnLock = false; + + senderMustBePlayer = false; + senderMustBeMember = false; + senderMustBeModerator = false; + senderMustBeAdmin = false; + } + + @Override + public void perform() { + // Can sort by: money, members, online, allies, enemies, power, land. + // Get all Factions and remove non player ones. + ArrayList factionList = Factions.getInstance().getAllFactions(); + factionList.remove(Factions.getInstance().getNone()); + factionList.remove(Factions.getInstance().getSafeZone()); + factionList.remove(Factions.getInstance().getWarZone()); + + String criteria = argAsString(0); + + // TODO: Better way to sort? + if (criteria.equalsIgnoreCase("members")) { + Collections.sort(factionList, new Comparator() { + @Override + public int compare(Faction f1, Faction f2) { + int f1Size = f1.getFPlayers().size(); + int f2Size = f2.getFPlayers().size(); + if (f1Size < f2Size) { + return 1; + } else if (f1Size > f2Size) { + return -1; + } + return 0; + } + }); + } else if (criteria.equalsIgnoreCase("power")) { + Collections.sort(factionList, new Comparator() { + @Override + public int compare(Faction f1, Faction f2) { + int f1Size = f1.getPowerRounded(); + int f2Size = f2.getPowerRounded(); + if (f1Size < f2Size) { + return 1; + } else if (f1Size > f2Size) { + return -1; + } + return 0; + } + }); + } else if (criteria.equalsIgnoreCase("land")) { + Collections.sort(factionList, new Comparator() { + @Override + public int compare(Faction f1, Faction f2) { + int f1Size = f1.getLandRounded(); + int f2Size = f2.getLandRounded(); + if (f1Size < f2Size) { + return 1; + } else if (f1Size > f2Size) { + return -1; + } + return 0; + } + }); + } else if (criteria.equalsIgnoreCase("online")) { + Collections.sort(factionList, new Comparator() { + @Override + public int compare(Faction f1, Faction f2) { + int f1Size = f1.getFPlayersWhereOnline(true).size(); + int f2Size = f2.getFPlayersWhereOnline(true).size(); + if (f1Size < f2Size) { + return 1; + } else if (f1Size > f2Size) { + return -1; + } + return 0; + } + }); + } else if (criteria.equalsIgnoreCase("money") || criteria.equalsIgnoreCase("balance") || criteria.equalsIgnoreCase("bal")) { + Collections.sort(factionList, new Comparator() { + @Override + public int compare(Faction f1, Faction f2) { + double f1Size = Econ.getBalance(f1.getAccountId()); + // Lets get the balance of /all/ the players in the Faction. + for(FPlayer fp : f1.getFPlayers()) { + f1Size = f1Size + Econ.getBalance(fp.getAccountId()); + } + double f2Size = Econ.getBalance(f2.getAccountId()); + for(FPlayer fp : f1.getFPlayers()) { + f2Size = f2Size + Econ.getBalance(fp.getAccountId()); + } + if (f1Size < f2Size) { + return 1; + } else if (f1Size > f2Size) { + return -1; + } + return 0; + } + }); + } else { + msg(TL.COMMAND_TOP_INVALID, criteria); + } + + ArrayList lines = new ArrayList(); + factionList.add(0, Factions.getInstance().getNone()); + + final int pageheight = 9; + int pagenumber = this.argAsInt(1, 1); + int pagecount = (factionList.size() / pageheight) + 1; + if (pagenumber > pagecount) { + pagenumber = pagecount; + } else if (pagenumber < 1) { + pagenumber = 1; + } + int start = (pagenumber - 1) * pageheight; + int end = start + pageheight; + if (end > factionList.size()) { + end = factionList.size(); + } + + lines.add(TL.COMMAND_TOP_TOP.format(criteria.toUpperCase(), pagenumber, pagecount)); + + int rank = 1; + for (Faction faction : factionList.subList(start, end)) { + // Get the relation color if player is executing this. + String fac = sender instanceof Player ? faction.getRelationTo(fme).getColor() + faction.getTag() : faction.getTag(); + lines.add(TL.COMMAND_TOP_LINE.format(rank, fac, getValue(faction, criteria))); + rank++; + } + + sendMessage(lines); + } + + private String getValue(Faction faction, String criteria) { + if (criteria.equalsIgnoreCase("online")) { + return String.valueOf(faction.getFPlayersWhereOnline(true).size()); + } else if (criteria.equalsIgnoreCase("members")) { + return String.valueOf(faction.getFPlayers().size()); + } else if (criteria.equalsIgnoreCase("land")) { + return String.valueOf(faction.getLandRounded()); + } else if (criteria.equalsIgnoreCase("power")) { + return String.valueOf(faction.getPowerRounded()); + } else { // Last one is balance, and it has 3 different things it could be. + double balance = Econ.getBalance(faction.getAccountId()); + for(FPlayer fp : faction.getFPlayers()) { + balance = balance + Econ.getBalance(fp.getAccountId()); + } + return String.valueOf(balance); + } + } + + @Override + public TL getUsageTranslation() { + return TL.COMMAND_TOP_DESCRIPTION; + } +} diff --git a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java index 19af82b5..a16c4863 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java @@ -64,6 +64,7 @@ public class FCmdRoot extends FCommand { public CmdModifyPower cmdModifyPower = new CmdModifyPower(); public CmdLogins cmdLogins = new CmdLogins(); public CmdClaimLine cmdClaimLine = new CmdClaimLine(); + public CmdTop cmdTop = new CmdTop(); public FCmdRoot() { super(); @@ -143,6 +144,7 @@ public class FCmdRoot extends FCommand { this.addSubCommand(this.cmdModifyPower); this.addSubCommand(this.cmdLogins); this.addSubCommand(this.cmdClaimLine); + this.addSubCommand(this.cmdTop); } @Override diff --git a/src/main/java/com/massivecraft/factions/struct/Permission.java b/src/main/java/com/massivecraft/factions/struct/Permission.java index c40a5b61..2b87b515 100644 --- a/src/main/java/com/massivecraft/factions/struct/Permission.java +++ b/src/main/java/com/massivecraft/factions/struct/Permission.java @@ -73,6 +73,7 @@ public enum Permission { SCOREBOARD("scoreboard"), SEECHUNK("seechunk"), SETWARP("setwarp"), + TOP("top"), WARP("warp"); public final String node; diff --git a/src/main/java/com/massivecraft/factions/zcore/util/TL.java b/src/main/java/com/massivecraft/factions/zcore/util/TL.java index c6a284f5..5fa8570e 100644 --- a/src/main/java/com/massivecraft/factions/zcore/util/TL.java +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -453,6 +453,11 @@ public enum TL { COMMAND_TITLE_CHANGED("%1$s changed a title: %2$s"), COMMAND_TITLE_DESCRIPTION("Set or remove a players title"), + COMMAND_TOP_DESCRIPTION("Sort Factions to see the top of some criteria."), + COMMAND_TOP_TOP("Top Factions by %s. Page %d/%d"), + COMMAND_TOP_LINE("%d. &6%s: &c%s"), // Rank. Faction: Value + COMMAND_TOP_INVALID("Could not sort by %s. Try balance, online, members, power or land."), + COMMAND_UNCLAIM_SAFEZONE_SUCCESS("Safe zone was unclaimed."), COMMAND_UNCLAIM_SAFEZONE_NOPERM("This is a safe zone. You lack permissions to unclaim."), COMMAND_UNCLAIM_WARZONE_SUCCESS("War zone was unclaimed."), diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ef9d53e3..2f19a6e3 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -92,6 +92,7 @@ permissions: factions.showinvites: true factions.seechunk: true factions.monitorlogins: true + factions.top: true factions.admin: description: hand over your admin rights factions.admin.any: @@ -246,4 +247,6 @@ permissions: factions.monitorlogins: description: monitor join and leaves of faction members factions.claim.line: - description: claim in a line \ No newline at end of file + description: claim in a line + factions.top: + description: sort factions \ No newline at end of file