From 8e13c1c13f55eda72e3bdbdf39b7e57c7a00dcf6 Mon Sep 17 00:00:00 2001 From: ulumulu1510 Date: Sun, 25 Jan 2015 00:13:19 +0100 Subject: [PATCH] Implement f status --- plugin.yml | 3 + src/com/massivecraft/factions/Perm.java | 5 +- .../factions/PlayerInactivityComparator.java | 43 +++++++ .../factions/PlayerPowerComparator.java | 44 +++++++ .../factions/PlayerRoleComparator.java | 15 +-- .../factions/cmd/CmdFactions.java | 2 + .../factions/cmd/CmdFactionsStatus.java | 119 ++++++++++++++++++ .../factions/cmd/arg/ARSortMPlayer.java | 64 ++++++++++ .../massivecraft/factions/entity/MPerm.java | 4 + 9 files changed, 288 insertions(+), 11 deletions(-) create mode 100644 src/com/massivecraft/factions/PlayerInactivityComparator.java create mode 100644 src/com/massivecraft/factions/PlayerPowerComparator.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsStatus.java create mode 100644 src/com/massivecraft/factions/cmd/arg/ARSortMPlayer.java diff --git a/plugin.yml b/plugin.yml index d60971cf..9cc69d13 100644 --- a/plugin.yml +++ b/plugin.yml @@ -68,6 +68,7 @@ permissions: factions.seechunkold: {description: see the chunk you stand in, default: false} factions.sethome: {description: set the faction home, default: false} factions.setpower: {description: set power, default: false} + factions.status: {description: show faction status, default: false} factions.name: {description: set faction name, default: false} factions.title: {description: set player title, default: false} factions.title.color: {description: set player title with color, default: false} @@ -146,6 +147,7 @@ permissions: factions.seechunkold: true factions.sethome: true factions.setpower: true + factions.status: true factions.name: true factions.title: true factions.title.color: true @@ -245,6 +247,7 @@ permissions: factions.seechunk: true factions.seechunkold: true factions.sethome: true + factions.status: true factions.name: true factions.title: true factions.title.color: true diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index 6f89229f..cc1de352 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -65,9 +65,10 @@ public enum Perm RANK_ACTION("rank.action"), RELATION("relation"), SEECHUNK("seechunk"), - SEECHUNKOLD("seechunkold"), - SETHOME("sethome"), + SEECHUNKOLD("seechunkold"), + SETHOME("sethome"), SETPOWER("setpower"), + STATUS("status"), NAME("name"), TITLE("title"), TITLE_COLOR("title.color"), diff --git a/src/com/massivecraft/factions/PlayerInactivityComparator.java b/src/com/massivecraft/factions/PlayerInactivityComparator.java new file mode 100644 index 00000000..513e7717 --- /dev/null +++ b/src/com/massivecraft/factions/PlayerInactivityComparator.java @@ -0,0 +1,43 @@ +package com.massivecraft.factions; + +import java.util.Comparator; + +import com.massivecraft.factions.entity.MPlayer; + +public class PlayerInactivityComparator implements Comparator +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static PlayerInactivityComparator i = new PlayerInactivityComparator(); + public static PlayerInactivityComparator get() { return i; } + + // -------------------------------------------- // + // OVERRIDE: COMPARATOR + // -------------------------------------------- // + + @Override + public int compare(MPlayer m1, MPlayer m2) + { + // Null + if (m1 == null && m2 == null) return 0; + else if (m1 == null) return -1; + else if (m2 == null) return +1; + + // Online + boolean o1 = m1.isOnline(); + boolean o2 = m2.isOnline(); + + if (o1 && o2) return 0; + else if (o1) return -1; + else if (o2) return +1; + + // Inactivity Time + long r1 = m1.getLastActivityMillis(); + long r2 = m2.getLastActivityMillis(); + + return (int) (r1 - r2); + } + +} diff --git a/src/com/massivecraft/factions/PlayerPowerComparator.java b/src/com/massivecraft/factions/PlayerPowerComparator.java new file mode 100644 index 00000000..763737f2 --- /dev/null +++ b/src/com/massivecraft/factions/PlayerPowerComparator.java @@ -0,0 +1,44 @@ +package com.massivecraft.factions; + +import java.util.Comparator; + +import com.massivecraft.factions.entity.MPlayer; + +public class PlayerPowerComparator implements Comparator +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static PlayerPowerComparator i = new PlayerPowerComparator(); + public static PlayerPowerComparator get() { return i; } + + // -------------------------------------------- // + // OVERRIDE: COMPARATOR + // -------------------------------------------- // + + @Override + public int compare(MPlayer m1, MPlayer m2) + { + int ret = 0; + + // Null + if (m1 == null && m2 == null) return 0; + else if (m1 == null) return -1; + else if (m2 == null) return +1; + + // Power + int p1 = m1.getPowerRounded(); + int p2 = m2.getPowerRounded(); + ret = p1 - p2; + if (ret != 0) return ret; + + // MaxPower + int max1 = m1.getPowerMaxRounded(); + int max2 = m2.getPowerMaxRounded(); + ret = max1 - max2; + + return ret; + } + +} diff --git a/src/com/massivecraft/factions/PlayerRoleComparator.java b/src/com/massivecraft/factions/PlayerRoleComparator.java index 9ae4165a..9c8d1206 100644 --- a/src/com/massivecraft/factions/PlayerRoleComparator.java +++ b/src/com/massivecraft/factions/PlayerRoleComparator.java @@ -18,19 +18,16 @@ public class PlayerRoleComparator implements Comparator // -------------------------------------------- // @Override - public int compare(MPlayer o1, MPlayer o2) + public int compare(MPlayer m1, MPlayer m2) { - int ret = 0; - // Null - if (o1 == null && o2 == null) ret = 0; - if (o1 == null) ret = -1; - if (o2 == null) ret = +1; - if (ret != 0) return ret; + if (m1 == null && m2 == null) return 0; + else if (m1 == null) return -1; + else if (m2 == null) return +1; // Rank - Rel r1 = o1.getRole(); - Rel r2 = o2.getRole(); + Rel r1 = m1.getRole(); + Rel r2 = m2.getRole(); return r2.getValue() - r1.getValue(); } diff --git a/src/com/massivecraft/factions/cmd/CmdFactions.java b/src/com/massivecraft/factions/cmd/CmdFactions.java index 2394273c..04555ad4 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactions.java +++ b/src/com/massivecraft/factions/cmd/CmdFactions.java @@ -38,6 +38,7 @@ public class CmdFactions extends FactionsCommand public CmdFactionsMoney cmdFactionsMoney = new CmdFactionsMoney(); public CmdFactionsSeeChunk cmdFactionsSeeChunk = new CmdFactionsSeeChunk(); public CmdFactionsSeeChunkOld cmdFactionsSeeChunkOld = new CmdFactionsSeeChunkOld(); + public CmdFactionsStatus cmdFactionsStatus = new CmdFactionsStatus(); public CmdFactionsClaim cmdFactionsClaim = new CmdFactionsClaim(); public CmdFactionsUnclaim cmdFactionsUnclaim = new CmdFactionsUnclaim(); public CmdFactionsAccess cmdFactionsAccess = new CmdFactionsAccess(); @@ -67,6 +68,7 @@ public class CmdFactions extends FactionsCommand this.addSubCommand(this.cmdFactionsList); this.addSubCommand(this.cmdFactionsFaction); this.addSubCommand(this.cmdFactionsPlayer); + this.addSubCommand(this.cmdFactionsStatus); this.addSubCommand(this.cmdFactionsJoin); this.addSubCommand(this.cmdFactionsLeave); this.addSubCommand(this.cmdFactionsHome); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsStatus.java b/src/com/massivecraft/factions/cmd/CmdFactionsStatus.java new file mode 100644 index 00000000..83f4c01e --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsStatus.java @@ -0,0 +1,119 @@ +package com.massivecraft.factions.cmd; + +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.List; + +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.PlayerInactivityComparator; +import com.massivecraft.factions.cmd.arg.ARFaction; +import com.massivecraft.factions.cmd.arg.ARSortMPlayer; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.cmd.MassiveCommandException; +import com.massivecraft.massivecore.cmd.arg.ARInteger; +import com.massivecraft.massivecore.cmd.req.ReqHasPerm; +import com.massivecraft.massivecore.pager.PagerSimple; +import com.massivecraft.massivecore.pager.Stringifier; +import com.massivecraft.massivecore.util.TimeDiffUtil; +import com.massivecraft.massivecore.util.TimeUnit; +import com.massivecraft.massivecore.util.Txt; + + +public class CmdFactionsStatus extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsStatus() + { + // Aliases + this.addAliases("s", "status"); + + // Args + this.addOptionalArg("page", "1"); + this.addOptionalArg("faction", "you"); + this.addOptionalArg("sort by", "time"); + + // Requirements + this.addRequirements(ReqHasPerm.get(Perm.STATUS.node)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveCommandException + { + // Args + Integer pageHumanBased = this.arg(0, ARInteger.get(), 1); + Faction faction = this.arg(1, ARFaction.get(), msenderFaction); + Comparator sortedBy = this.arg(2, ARSortMPlayer.get(), PlayerInactivityComparator.get()); + + // MPerm + if ( ! MPerm.getPermStatus().has(msender, faction, true)) return; + + // Sort list + final List mplayers = faction.getMPlayers(); + Collections.sort(mplayers, sortedBy); + + // Create Pager + final PagerSimple pager = new PagerSimple(mplayers, sender); + String pagerTitle = Txt.parse("Status of %s.", faction.describeTo(msender, true)); + + // Use Pager + List messages = pager.getPageTxt(pageHumanBased, pagerTitle, new Stringifier(){ + + @Override + public String toString(MPlayer mplayer) + { + // Name + String displayName = mplayer.getNameAndSomething(msender.getColorTo(mplayer).toString(), ""); + int length = 15 - displayName.length(); + length = length <= 0 ? 1 : length; + String whiteSpace = Txt.repeat(" ", length); + + // Power + double currentPower = mplayer.getPower(); + double maxPower = mplayer.getPowerMax(); + String color; + double percent = currentPower / maxPower; + + if (percent > 0.75) + { + color = ""; + } + else if (percent > 0.5) + { + color = ""; + } + else if (percent > 0.25) + { + color = ""; + } + else + { + color = ""; + } + + String power = Txt.parse("Power: %s%.0f/%.0f", Txt.parse(color), currentPower, maxPower); + + // Time + long lastActiveMillis = mplayer.getLastActivityMillis() - System.currentTimeMillis(); + LinkedHashMap activeTimes = TimeDiffUtil.limit(TimeDiffUtil.unitcounts(lastActiveMillis, TimeUnit.getAllButMillis()), 3); + String lastActive = mplayer.isOnline() ? Txt.parse("Online right now.") : Txt.parse("Last active: " + TimeDiffUtil.formatedMinimal(activeTimes, "")); + + return Txt.parse("%s%s %s %s", displayName, whiteSpace, power, lastActive); + } + + }); + + // Send message + sendMessage(messages); + } + +} diff --git a/src/com/massivecraft/factions/cmd/arg/ARSortMPlayer.java b/src/com/massivecraft/factions/cmd/arg/ARSortMPlayer.java new file mode 100644 index 00000000..07e1c370 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/arg/ARSortMPlayer.java @@ -0,0 +1,64 @@ +package com.massivecraft.factions.cmd.arg; + +import java.util.Collection; +import java.util.Comparator; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.factions.PlayerInactivityComparator; +import com.massivecraft.factions.PlayerPowerComparator; +import com.massivecraft.factions.PlayerRoleComparator; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.cmd.arg.ARAbstractSelect; +import com.massivecraft.massivecore.util.MUtil; + +public class ARSortMPlayer extends ARAbstractSelect> +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static ARSortMPlayer i = new ARSortMPlayer(); + public static ARSortMPlayer get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public String typename() + { + return "player sorter"; + } + + @Override + public Comparator select(String sortedBy, CommandSender sender) + { + sortedBy = sortedBy.toLowerCase(); + + if (sortedBy.startsWith("r")) + { + // Sort by rank + return PlayerRoleComparator.get(); + } + else if (sortedBy.startsWith("p")) + { + // Sort by power + return PlayerPowerComparator.get(); + } + else if (sortedBy.startsWith("t")) + { + // Sort by time + return PlayerInactivityComparator.get(); + } + + return null; + } + + @Override + public Collection altNames(CommandSender sender) + { + return MUtil.list("rank", "power", "time"); + } + +} diff --git a/src/com/massivecraft/factions/entity/MPerm.java b/src/com/massivecraft/factions/entity/MPerm.java index 59e13108..eca1179f 100644 --- a/src/com/massivecraft/factions/entity/MPerm.java +++ b/src/com/massivecraft/factions/entity/MPerm.java @@ -50,6 +50,7 @@ public class MPerm extends Entity implements Prioritized, Registerable public final static transient String ID_DISBAND = "disband"; public final static transient String ID_FLAGS = "flags"; public final static transient String ID_PERMS = "perms"; + public final static transient String ID_STATUS = "status"; public final static transient int PRIORITY_BUILD = 1000; public final static transient int PRIORITY_PAINBUILD = 2000; @@ -75,6 +76,7 @@ public class MPerm extends Entity implements Prioritized, Registerable public final static transient int PRIORITY_DISBAND = 21000; public final static transient int PRIORITY_FLAGS = 22000; public final static transient int PRIORITY_PERMS = 23000; + public final static transient int PRIORITY_STATUS = 24000; // -------------------------------------------- // // META: CORE @@ -114,6 +116,7 @@ public class MPerm extends Entity implements Prioritized, Registerable getPermKick(); getPermTitle(); getPermHome(); + getPermStatus(); getPermSethome(); getPermDeposit(); getPermWithdraw(); @@ -137,6 +140,7 @@ public class MPerm extends Entity implements Prioritized, Registerable public static MPerm getPermDesc() { return getCreative(PRIORITY_DESC, ID_DESC, ID_DESC, "set description", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } public static MPerm getPermMotd() { return getCreative(PRIORITY_MOTD, ID_MOTD, ID_MOTD, "set motd", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } public static MPerm getPermInvite() { return getCreative(PRIORITY_INVITE, ID_INVITE, ID_INVITE, "invite players", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } + public static MPerm getPermStatus() { return getCreative(PRIORITY_STATUS, ID_STATUS, ID_STATUS, "show status", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } public static MPerm getPermKick() { return getCreative(PRIORITY_KICK, ID_KICK, ID_KICK, "kick members", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } public static MPerm getPermTitle() { return getCreative(PRIORITY_TITLE, ID_TITLE, ID_TITLE, "set titles", MUtil.set(Rel.LEADER, Rel.OFFICER), false, true, true); } public static MPerm getPermHome() { return getCreative(PRIORITY_HOME, ID_HOME, ID_HOME, "teleport home", MUtil.set(Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), false, true, true); }