Check player visibility in CmdFactionsList

This commit is contained in:
BuildTools 2016-01-25 09:49:43 +01:00 committed by Olof Larsson
parent 2b7a505a7c
commit bc02fe5949
3 changed files with 73 additions and 37 deletions

View File

@ -0,0 +1,35 @@
package com.massivecraft.factions;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.massivecore.Predicate;
public class PredicateRole implements Predicate<MPlayer>
{
// -------------------------------------------- //
// 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;
}
}

View File

@ -11,10 +11,14 @@ import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.massivecore.MassiveException; 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.Parameter;
import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; import com.massivecraft.massivecore.command.requirement.RequirementHasPerm;
import com.massivecraft.massivecore.pager.Pager; import com.massivecraft.massivecore.pager.Pager;
import com.massivecraft.massivecore.pager.Stringifier; import com.massivecraft.massivecore.pager.Stringifier;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsList extends FactionsCommand public class CmdFactionsList extends FactionsCommand
@ -45,6 +49,7 @@ public class CmdFactionsList extends FactionsCommand
// Args // Args
int page = this.readArg(); int page = this.readArg();
final MPlayer msender = this.msender; final MPlayer msender = this.msender;
Predicate<MPlayer> onlinePredicate = PredicateAnd.get(SenderColl.PREDICATE_ONLINE, PredicateVisibleTo.get(sender));
// NOTE: The faction list is quite slow and mostly thread safe. // NOTE: The faction list is quite slow and mostly thread safe.
// We run it asynchronously to spare the primary server thread. // We run it asynchronously to spare the primary server thread.
@ -62,7 +67,7 @@ public class CmdFactionsList extends FactionsCommand
{ {
return Txt.parse("%s<i> %d/%d online, %d/%d/%d", return Txt.parse("%s<i> %d/%d online, %d/%d/%d",
faction.getName(msender), faction.getName(msender),
faction.getMPlayersWhereOnline(true).size(), faction.getMPlayersWhere(onlinePredicate).size(),
faction.getMPlayers().size(), faction.getMPlayers().size(),
faction.getLandCount(), faction.getLandCount(),
faction.getPowerRounded(), faction.getPowerRounded(),

View File

@ -1,7 +1,17 @@
package com.massivecraft.factions.entity; 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.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -11,17 +21,21 @@ import com.massivecraft.factions.EconomyParticipator;
import com.massivecraft.factions.FactionEqualsPredicate; import com.massivecraft.factions.FactionEqualsPredicate;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Lang; import com.massivecraft.factions.Lang;
import com.massivecraft.factions.PredicateRole;
import com.massivecraft.factions.Rel; import com.massivecraft.factions.Rel;
import com.massivecraft.factions.RelationParticipator; 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.CaseInsensitiveComparator;
import com.massivecraft.massivecore.Named; import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.Predicate;
import com.massivecraft.massivecore.collections.MassiveMapDef; import com.massivecraft.massivecore.collections.MassiveMapDef;
import com.massivecraft.massivecore.collections.MassiveTreeSetDef; import com.massivecraft.massivecore.collections.MassiveTreeSetDef;
import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.money.Money; import com.massivecraft.massivecore.money.Money;
import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.store.Entity; import com.massivecraft.massivecore.store.Entity;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
@ -1038,49 +1052,31 @@ public class Faction extends Entity<Faction> implements EconomyParticipator, Nam
return new ArrayList<MPlayer>(this.mplayers); return new ArrayList<MPlayer>(this.mplayers);
} }
public List<MPlayer> getMPlayersWhereOnline(boolean online) public List<MPlayer> getMPlayersWhere(Predicate<? super MPlayer> predicate)
{ {
List<MPlayer> ret = this.getMPlayers(); List<MPlayer> ret = this.getMPlayers();
Iterator<MPlayer> iter = ret.iterator(); for (Iterator<MPlayer> it = ret.iterator(); it.hasNext();)
while (iter.hasNext())
{ {
MPlayer mplayer = iter.next(); if ( ! predicate.apply(it.next())) it.remove();
if (mplayer.isOnline() != online)
{
iter.remove();
}
} }
return ret; return ret;
} }
public List<MPlayer> getMPlayersWhereOnline(boolean online)
{
return this.getMPlayersWhere(online ? SenderColl.PREDICATE_ONLINE : SenderColl.PREDICATE_OFFLINE);
}
public List<MPlayer> getMPlayersWhereRole(Rel role) public List<MPlayer> getMPlayersWhereRole(Rel role)
{ {
List<MPlayer> ret = this.getMPlayers(); return this.getMPlayersWhere(PredicateRole.get(role));
Iterator<MPlayer> iter = ret.iterator();
while (iter.hasNext())
{
MPlayer mplayer = iter.next();
if (mplayer.getRole() != role)
{
iter.remove();
}
}
return ret;
} }
public MPlayer getLeader() public MPlayer getLeader()
{ {
List<MPlayer> ret = this.getMPlayers(); List<MPlayer> ret = this.getMPlayersWhereRole(Rel.LEADER);
Iterator<MPlayer> iter = ret.iterator(); if (ret.size() == 0) return null;
while (iter.hasNext()) return ret.get(0);
{
MPlayer mplayer = iter.next();
if (mplayer.getRole() == Rel.LEADER)
{
return mplayer;
}
}
return null;
} }
public List<CommandSender> getOnlineCommandSenders() public List<CommandSender> getOnlineCommandSenders()