From 47ba56de9ef28c4986790376cfdb81adeb1c6f5e Mon Sep 17 00:00:00 2001 From: Brettflan Date: Fri, 13 Jan 2012 03:46:31 -0600 Subject: [PATCH] Optimization: Factions (as objects) now maintain a list of FPlayers in the faction for faster lookup INTEGRATION NOTE: getFPlayers() and getFPlayersWhereOnline(boolean online) now return Set instead of ArrayList, so other plugins which hook into those methods will probably need to be updated. --- src/com/massivecraft/factions/FPlayer.java | 11 +++-- src/com/massivecraft/factions/Faction.java | 55 ++++++++++++++------- src/com/massivecraft/factions/Factions.java | 8 ++- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index e855303b..04ae13a9 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -1,6 +1,5 @@ package com.massivecraft.factions; -import java.util.ArrayList; import java.util.HashSet; import java.util.Set; @@ -48,6 +47,9 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator public boolean hasFaction() { return ! factionId.equals("0"); } public void setFaction(Faction faction) { + Faction oldFaction = this.getFaction(); + if (oldFaction != null) oldFaction.removeFPlayer(this); + faction.addFPlayer(this); this.factionId = faction.getId(); SpoutFeatures.updateAppearances(this.getPlayer()); } @@ -130,6 +132,10 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator public final void resetFactionData(boolean doSpotUpdate) { + Faction currentFaction = this.getFaction(); + if (currentFaction != null) + currentFaction.removeFPlayer(this); + this.factionId = "0"; // The default neutral faction this.chatMode = ChatMode.PUBLIC; this.role = Rel.MEMBER; @@ -491,8 +497,7 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator } // Am I the last one in the faction? - ArrayList fplayers = myFaction.getFPlayers(); - if (fplayers.size() == 1 && fplayers.get(0) == this) + if (myFaction.getFPlayers().size() == 1) { // Transfer all money if (Econ.shouldBeUsed()) diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index 3d0143dd..72e590c5 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -22,7 +22,11 @@ public class Faction extends Entity implements EconomyParticipator { // FIELD: relationWish private Map relationWish; - + + // FIELD: fplayers + // speedy lookup of players in faction + private Set fplayers = new HashSet(); + // FIELD: invites // Where string is a lowercase player name private Set invites; @@ -279,7 +283,7 @@ public class Faction extends Entity implements EconomyParticipator } double ret = 0; - for (FPlayer fplayer : this.getFPlayers()) + for (FPlayer fplayer : fplayers) { ret += fplayer.getPower(); } @@ -298,7 +302,7 @@ public class Faction extends Entity implements EconomyParticipator } double ret = 0; - for (FPlayer fplayer : this.getFPlayers()) + for (FPlayer fplayer : fplayers) { ret += fplayer.getPowerMax(); } @@ -336,31 +340,48 @@ public class Faction extends Entity implements EconomyParticipator // ------------------------------- // FPlayers // ------------------------------- - - public ArrayList getFPlayers() + + // maintain the reference list of FPlayers in this faction + public void refreshFPlayers() { - ArrayList ret = new ArrayList(); - //if (this.isPlayerFreeType()) return ret; + fplayers.clear(); + if (this.isNone()) return; for (FPlayer fplayer : FPlayers.i.get()) { if (fplayer.getFaction() == this) { - ret.add(fplayer); + fplayers.add(fplayer); } } + } + public boolean addFPlayer(FPlayer fplayer) + { + if (this.isNone()) return false; + return fplayers.add(fplayer); + } + public boolean removeFPlayer(FPlayer fplayer) + { + if (this.isNone()) return false; + + return fplayers.remove(fplayer); + } + + public Set getFPlayers() + { + // return a shallow copy of the FPlayer list, to prevent tampering and concurrency issues + Set ret = new HashSet(fplayers); return ret; } - public ArrayList getFPlayersWhereOnline(boolean online) + public Set getFPlayersWhereOnline(boolean online) { - ArrayList ret = new ArrayList(); - //if (this.isPlayerFreeType()) return ret; + Set ret = new HashSet(); - for (FPlayer fplayer : FPlayers.i.get()) + for (FPlayer fplayer : fplayers) { - if (fplayer.getFaction() == this && fplayer.isOnline() == online) + if (fplayer.isOnline() == online) { ret.add(fplayer); } @@ -373,9 +394,9 @@ public class Faction extends Entity implements EconomyParticipator { //if ( ! this.isNormal()) return null; - for (FPlayer fplayer : FPlayers.i.get()) + for (FPlayer fplayer : fplayers) { - if (fplayer.getFaction() == this && fplayer.getRole() == Rel.LEADER) + if (fplayer.getRole() == Rel.LEADER) { return fplayer; } @@ -388,9 +409,9 @@ public class Faction extends Entity implements EconomyParticipator ArrayList ret = new ArrayList(); //if ( ! this.isNormal()) return ret; - for (FPlayer fplayer : FPlayers.i.get()) + for (FPlayer fplayer : fplayers) { - if (fplayer.getFaction() == this && fplayer.getRole() == role) + if (fplayer.getRole() == role) { ret.add(fplayer); } diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 39362274..9191f42e 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -92,7 +92,13 @@ public class Factions extends EntityCollection setFlagsForSafeZone(safeZone); if (warZone != null && ! warZone.getFlag(FFlag.PERMANENT)) setFlagsForWarZone(warZone); - + + // populate all faction player lists + for (Faction faction : i.get()) + { + faction.refreshFPlayers(); + } + return true; }