diff --git a/src/com/massivecraft/factions/FactionListComparator.java b/src/com/massivecraft/factions/FactionListComparator.java index 8935736d..096cbd13 100644 --- a/src/com/massivecraft/factions/FactionListComparator.java +++ b/src/com/massivecraft/factions/FactionListComparator.java @@ -1,19 +1,33 @@ package com.massivecraft.factions; +import java.lang.ref.WeakReference; import java.util.Comparator; +import org.bukkit.command.CommandSender; + import com.massivecraft.factions.entity.Faction; import com.massivecraft.massivecore.comparator.ComparatorComparable; +import com.massivecraft.massivecore.util.IdUtil; public class FactionListComparator implements Comparator { + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final WeakReference watcher; + public CommandSender getWatcher() { return this.watcher.get(); } + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - - private static FactionListComparator i = new FactionListComparator(); - public static FactionListComparator get() { return i; } - + + public static FactionListComparator get(Object watcherObject) { return new FactionListComparator(watcherObject); } + public FactionListComparator(Object watcherObject) + { + this.watcher = new WeakReference<>(IdUtil.getSender(watcherObject)); + } + // -------------------------------------------- // // OVERRIDE: COMPARATOR // -------------------------------------------- // @@ -36,7 +50,7 @@ public class FactionListComparator implements Comparator if (ret != 0) return ret; // Players Online - ret = f2.getMPlayersWhereOnline(true).size() - f1.getMPlayersWhereOnline(true).size(); + ret = f2.getMPlayersWhereOnlineTo(this.getWatcher()).size() - f1.getMPlayersWhereOnlineTo(this.getWatcher()).size(); if (ret != 0) return ret; // Players Total diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsList.java b/src/com/massivecraft/factions/cmd/CmdFactionsList.java index c5ed9c05..9885aab4 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsList.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsList.java @@ -3,6 +3,7 @@ package com.massivecraft.factions.cmd; import java.util.List; import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; import com.massivecraft.factions.FactionListComparator; import com.massivecraft.factions.Factions; @@ -15,10 +16,6 @@ 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.predicate.Predicate; -import com.massivecraft.massivecore.predicate.PredicateAnd; -import com.massivecraft.massivecore.predicate.PredicateVisibleTo; -import com.massivecraft.massivecore.store.SenderColl; import com.massivecraft.massivecore.util.Txt; public class CmdFactionsList extends FactionsCommand @@ -48,26 +45,26 @@ public class CmdFactionsList extends FactionsCommand { // Args int page = this.readArg(); + final CommandSender sender = this.sender; final MPlayer msender = this.msender; - final 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. // Pager Create - final Pager pager = new Pager(this, "Faction List", page, new Stringifier() { + final Pager pager = new Pager<>(this, "Faction List", page, new Stringifier() { @Override public String toString(Faction faction, int index) { if (faction.isNone()) { - return Txt.parse("Factionless %d online", FactionColl.get().getNone().getMPlayersWhereOnline(true).size()); + return Txt.parse("Factionless %d online", FactionColl.get().getNone().getMPlayersWhereOnlineTo(sender).size()); } else { return Txt.parse("%s %d/%d online, %d/%d/%d", faction.getName(msender), - faction.getMPlayersWhere(onlinePredicate).size(), + faction.getMPlayersWhereOnlineTo(sender).size(), faction.getMPlayers().size(), faction.getLandCount(), faction.getPowerRounded(), @@ -83,7 +80,7 @@ public class CmdFactionsList extends FactionsCommand public void run() { // Pager Items - final List factions = FactionColl.get().getAll(FactionListComparator.get()); + final List factions = FactionColl.get().getAll(FactionListComparator.get(sender)); pager.setItems(factions); // Pager Message diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 666725de..d7586e10 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -35,6 +35,8 @@ import com.massivecraft.massivecore.comparator.ComparatorCaseInsensitive; import com.massivecraft.massivecore.mixin.MixinMessage; import com.massivecraft.massivecore.money.Money; import com.massivecraft.massivecore.predicate.Predicate; +import com.massivecraft.massivecore.predicate.PredicateAnd; +import com.massivecraft.massivecore.predicate.PredicateVisibleTo; import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.store.Entity; import com.massivecraft.massivecore.store.SenderColl; @@ -1062,7 +1064,12 @@ public class Faction extends Entity implements EconomyParticipator, Nam public List getMPlayersWhereOnline(boolean online) { return this.getMPlayersWhere(online ? SenderColl.PREDICATE_ONLINE : SenderColl.PREDICATE_OFFLINE); - } + } + + public List getMPlayersWhereOnlineTo(Object senderObject) + { + return this.getMPlayersWhere(PredicateAnd.get(SenderColl.PREDICATE_ONLINE, PredicateVisibleTo.get(senderObject))); + } public List getMPlayersWhereRole(Rel role) {