diff --git a/src/com/massivecraft/factions/entity/MPlayer.java b/src/com/massivecraft/factions/entity/MPlayer.java index a12ffa0d..38a6448f 100644 --- a/src/com/massivecraft/factions/entity/MPlayer.java +++ b/src/com/massivecraft/factions/entity/MPlayer.java @@ -46,7 +46,7 @@ public class MPlayer extends SenderEntity implements EconomyParticipato } // -------------------------------------------- // - // OVERRIDE: ENTITY + // LOAD // -------------------------------------------- // @Override @@ -65,6 +65,10 @@ public class MPlayer extends SenderEntity implements EconomyParticipato return this; } + // -------------------------------------------- // + // IS DEFAULT + // -------------------------------------------- // + @Override public boolean isDefault() { @@ -81,30 +85,38 @@ public class MPlayer extends SenderEntity implements EconomyParticipato return true; } + // -------------------------------------------- // + // UPDATE FACTION INDEXES + // -------------------------------------------- // + + public void updateFactionIndexes(Faction before, Faction after) + { + // NoChange + if (MUtil.equals(before, after)) return; + + // Before + if (before != null) before.mplayers.remove(this); + + // After + if (after != null) after.mplayers.add(this); + } + @Override public void postAttach(String id) { - // If inited ... if (!Factions.get().isDatabaseInitialized()) return; - - // ... update the index. - Faction faction = this.getFaction(); - faction.mplayers.add(this); - - //Factions.get().log(Txt.parse("postAttach added %s aka %s to %s aka %s.", id, MixinDisplayName.get().getDisplayName(id), faction.getId(), faction.getName())); + Faction before = null; + Faction after = this.getFaction(); + this.updateFactionIndexes(before, after); } @Override public void preDetach(String id) { - // If inited ... if (!Factions.get().isDatabaseInitialized()) return; - - // ... update the index. - Faction faction = this.getFaction(); - faction.mplayers.remove(this); - - //Factions.get().log(Txt.parse("preDetach removed %s aka %s to %s aka %s.", id, MixinDisplayName.get().getDisplayName(id), faction.getId(), faction.getName())); + Faction before = this.getFaction(); + Faction after = null; + this.updateFactionIndexes(before, after); } // -------------------------------------------- // @@ -280,6 +292,7 @@ public class MPlayer extends SenderEntity implements EconomyParticipato if (oldFaction != null) oldFaction.mplayers.remove(this); if (faction != null) faction.mplayers.add(this); + /* String oldFactionIdDesc = "NULL"; String oldFactionNameDesc = "NULL"; if (oldFaction != null) @@ -294,8 +307,8 @@ public class MPlayer extends SenderEntity implements EconomyParticipato factionIdDesc = faction.getId(); factionNameDesc = faction.getName(); } - Factions.get().log(Txt.parse("setFactionId moved %s aka %s from %s aka %s to %s aka %s.", this.getId(), this.getDisplayName(IdUtil.getConsole()), oldFactionIdDesc, oldFactionNameDesc, factionIdDesc, factionNameDesc)); + */ // Mark as changed this.changed(); diff --git a/src/com/massivecraft/factions/entity/MPlayerColl.java b/src/com/massivecraft/factions/entity/MPlayerColl.java index 2c7affb3..292e09cd 100644 --- a/src/com/massivecraft/factions/entity/MPlayerColl.java +++ b/src/com/massivecraft/factions/entity/MPlayerColl.java @@ -1,6 +1,7 @@ package com.massivecraft.factions.entity; import java.util.Collection; +import java.util.Map.Entry; import org.bukkit.Bukkit; @@ -8,6 +9,7 @@ import com.massivecraft.factions.Factions; import com.massivecraft.massivecore.store.SenderColl; import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.Txt; +import com.massivecraft.massivecore.xlib.gson.JsonObject; public class MPlayerColl extends SenderColl { @@ -28,6 +30,55 @@ public class MPlayerColl extends SenderColl super.onTick(); } + // -------------------------------------------- // + // UPDATE FACTION INDEXES + // -------------------------------------------- // + + @Override + public synchronized MPlayer removeAtLocalFixed(String id) + { + if (!Factions.get().isDatabaseInitialized()) return super.removeAtLocalFixed(id); + + MPlayer mplayer = this.id2entity.get(id); + + if (mplayer != null) + { + Faction before = mplayer.getFaction(); + Faction after = null; + mplayer.updateFactionIndexes(before, after); + } + + return super.removeAtLocalFixed(id); + } + + @Override + public synchronized void loadFromRemoteFixed(String id, Entry remoteEntry) + { + if (!Factions.get().isDatabaseInitialized()) + { + super.loadFromRemoteFixed(id, remoteEntry); + return; + } + + MPlayer mplayer = null; + + // Before + Faction before = null; + if (mplayer == null) mplayer = this.id2entity.get(id); + if (mplayer != null) before = mplayer.getFaction(); + + // Super + super.loadFromRemoteFixed(id, remoteEntry); + + // After + Faction after = null; + if (mplayer == null) mplayer = this.id2entity.get(id); + if (mplayer != null) after = mplayer.getFaction(); + + // Perform + if (mplayer != null) mplayer.updateFactionIndexes(before, after); + } + // -------------------------------------------- // // EXTRAS // -------------------------------------------- //