From bc02fe5949fd457c2ea40d5468f856fe6ac344c8 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 25 Jan 2016 09:49:43 +0100 Subject: [PATCH] Check player visibility in CmdFactionsList --- .../massivecraft/factions/PredicateRole.java | 35 ++++++++++ .../factions/cmd/CmdFactionsList.java | 7 +- .../massivecraft/factions/entity/Faction.java | 68 +++++++++---------- 3 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 src/com/massivecraft/factions/PredicateRole.java diff --git a/src/com/massivecraft/factions/PredicateRole.java b/src/com/massivecraft/factions/PredicateRole.java new file mode 100644 index 00000000..79735b2e --- /dev/null +++ b/src/com/massivecraft/factions/PredicateRole.java @@ -0,0 +1,35 @@ +package com.massivecraft.factions; + +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.Predicate; + +public class PredicateRole implements Predicate +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final Rel role; + public Rel getRole() { return this.role; } + + // -------------------------------------------- // + // INSTANCE AND CONTRUCT + // -------------------------------------------- // + + public static PredicateRole get(Rel role) { return new PredicateRole(role); } + public PredicateRole(Rel role) + { + this.role = role; + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public boolean apply(MPlayer mplayer) + { + if (mplayer == null) return false; + return mplayer.getRole() == this.role; + } +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsList.java b/src/com/massivecraft/factions/cmd/CmdFactionsList.java index 7da8b729..87aa093b 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsList.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsList.java @@ -11,10 +11,14 @@ import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.Predicate; +import com.massivecraft.massivecore.PredicateAnd; +import com.massivecraft.massivecore.PredicateVisibleTo; import com.massivecraft.massivecore.command.Parameter; import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; import com.massivecraft.massivecore.pager.Pager; import com.massivecraft.massivecore.pager.Stringifier; +import com.massivecraft.massivecore.store.SenderColl; import com.massivecraft.massivecore.util.Txt; public class CmdFactionsList extends FactionsCommand @@ -45,6 +49,7 @@ public class CmdFactionsList extends FactionsCommand // Args int page = this.readArg(); final MPlayer msender = this.msender; + Predicate onlinePredicate = PredicateAnd.get(SenderColl.PREDICATE_ONLINE, PredicateVisibleTo.get(sender)); // NOTE: The faction list is quite slow and mostly thread safe. // We run it asynchronously to spare the primary server thread. @@ -62,7 +67,7 @@ public class CmdFactionsList extends FactionsCommand { return Txt.parse("%s %d/%d online, %d/%d/%d", faction.getName(msender), - faction.getMPlayersWhereOnline(true).size(), + faction.getMPlayersWhere(onlinePredicate).size(), faction.getMPlayers().size(), faction.getLandCount(), faction.getPowerRounded(), diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 9618d4cc..8f9cdd79 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -1,7 +1,17 @@ package com.massivecraft.factions.entity; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeSet; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -11,17 +21,21 @@ import com.massivecraft.factions.EconomyParticipator; import com.massivecraft.factions.FactionEqualsPredicate; import com.massivecraft.factions.Factions; import com.massivecraft.factions.Lang; +import com.massivecraft.factions.PredicateRole; import com.massivecraft.factions.Rel; import com.massivecraft.factions.RelationParticipator; -import com.massivecraft.factions.util.*; +import com.massivecraft.factions.util.MiscUtil; +import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.massivecore.CaseInsensitiveComparator; import com.massivecraft.massivecore.Named; +import com.massivecraft.massivecore.Predicate; import com.massivecraft.massivecore.collections.MassiveMapDef; import com.massivecraft.massivecore.collections.MassiveTreeSetDef; import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.money.Money; import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.store.Entity; +import com.massivecraft.massivecore.store.SenderColl; import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; @@ -1038,49 +1052,31 @@ public class Faction extends Entity implements EconomyParticipator, Nam return new ArrayList(this.mplayers); } - public List getMPlayersWhereOnline(boolean online) + public List getMPlayersWhere(Predicate predicate) { List ret = this.getMPlayers(); - Iterator iter = ret.iterator(); - while (iter.hasNext()) + for (Iterator it = ret.iterator(); it.hasNext();) { - MPlayer mplayer = iter.next(); - if (mplayer.isOnline() != online) - { - iter.remove(); - } - } - return ret; - } - - public List getMPlayersWhereRole(Rel role) - { - List ret = this.getMPlayers(); - Iterator iter = ret.iterator(); - while (iter.hasNext()) - { - MPlayer mplayer = iter.next(); - if (mplayer.getRole() != role) - { - iter.remove(); - } + if ( ! predicate.apply(it.next())) it.remove(); } return ret; } + public List getMPlayersWhereOnline(boolean online) + { + return this.getMPlayersWhere(online ? SenderColl.PREDICATE_ONLINE : SenderColl.PREDICATE_OFFLINE); + } + + public List getMPlayersWhereRole(Rel role) + { + return this.getMPlayersWhere(PredicateRole.get(role)); + } + public MPlayer getLeader() { - List ret = this.getMPlayers(); - Iterator iter = ret.iterator(); - while (iter.hasNext()) - { - MPlayer mplayer = iter.next(); - if (mplayer.getRole() == Rel.LEADER) - { - return mplayer; - } - } - return null; + List ret = this.getMPlayersWhereRole(Rel.LEADER); + if (ret.size() == 0) return null; + return ret.get(0); } public List getOnlineCommandSenders()