Create a (hopefully) proper index solution.
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
package com.massivecraft.factions.entity;
 | 
			
		||||
 | 
			
		||||
import com.massivecraft.factions.Factions;
 | 
			
		||||
import com.massivecraft.factions.FactionsIndex;
 | 
			
		||||
import com.massivecraft.factions.FactionsParticipator;
 | 
			
		||||
import com.massivecraft.factions.Rel;
 | 
			
		||||
import com.massivecraft.factions.RelationParticipator;
 | 
			
		||||
@@ -10,7 +11,6 @@ import com.massivecraft.factions.util.MiscUtil;
 | 
			
		||||
import com.massivecraft.factions.util.RelationUtil;
 | 
			
		||||
import com.massivecraft.massivecore.collections.MassiveList;
 | 
			
		||||
import com.massivecraft.massivecore.collections.MassiveMapDef;
 | 
			
		||||
import com.massivecraft.massivecore.collections.MassiveSet;
 | 
			
		||||
import com.massivecraft.massivecore.collections.MassiveTreeSetDef;
 | 
			
		||||
import com.massivecraft.massivecore.comparator.ComparatorCaseInsensitive;
 | 
			
		||||
import com.massivecraft.massivecore.mixin.MixinMessage;
 | 
			
		||||
@@ -1014,44 +1014,9 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
 | 
			
		||||
	// FOREIGN KEY: MPLAYER
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
	
 | 
			
		||||
	protected transient Set<MPlayer> mplayers = new MassiveSet<>();
 | 
			
		||||
	
 | 
			
		||||
	public void reindexMPlayers()
 | 
			
		||||
	{
 | 
			
		||||
		this.mplayers.clear();
 | 
			
		||||
		
 | 
			
		||||
		String factionId = this.getId();
 | 
			
		||||
		if (factionId == null) return;
 | 
			
		||||
		
 | 
			
		||||
		for (MPlayer mplayer : MPlayerColl.get().getAll())
 | 
			
		||||
		{
 | 
			
		||||
			if (!MUtil.equals(factionId, mplayer.getFactionId())) continue;
 | 
			
		||||
			this.mplayers.add(mplayer);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	// TODO: Even though this check method removeds the invalid entries it's not a true solution.
 | 
			
		||||
	// TODO: Find the bug causing non-attached MPlayers to be present in the index.
 | 
			
		||||
	private void checkMPlayerIndex()
 | 
			
		||||
	{
 | 
			
		||||
		Iterator<MPlayer> iter = this.mplayers.iterator();
 | 
			
		||||
		while (iter.hasNext())
 | 
			
		||||
		{
 | 
			
		||||
			MPlayer mplayer = iter.next();
 | 
			
		||||
			if (!mplayer.attached())
 | 
			
		||||
			{
 | 
			
		||||
				String msg = Txt.parse("<rose>WARN: <i>Faction <h>%s <i>aka <h>%s <i>had unattached mplayer in index:", this.getName(), this.getId());
 | 
			
		||||
				Factions.get().log(msg);
 | 
			
		||||
				Factions.get().log(Factions.get().getGson().toJson(mplayer));
 | 
			
		||||
				iter.remove();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public List<MPlayer> getMPlayers()
 | 
			
		||||
	{
 | 
			
		||||
		this.checkMPlayerIndex();
 | 
			
		||||
		return new ArrayList<>(this.mplayers);
 | 
			
		||||
		return new MassiveList<>(FactionsIndex.get().getMPlayers(this));
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public List<MPlayer> getMPlayersWhere(Predicate<? super MPlayer> predicate)
 | 
			
		||||
 
 | 
			
		||||
@@ -67,18 +67,6 @@ public class FactionColl extends Coll<Faction>
 | 
			
		||||
		return ret;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
	// INDEX
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
	
 | 
			
		||||
	public void reindexMPlayers()
 | 
			
		||||
	{
 | 
			
		||||
		for (Faction faction : this.getAll())
 | 
			
		||||
		{
 | 
			
		||||
			faction.reindexMPlayers();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
	// SPECIAL FACTIONS
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package com.massivecraft.factions.entity;
 | 
			
		||||
 | 
			
		||||
import com.massivecraft.factions.Factions;
 | 
			
		||||
import com.massivecraft.factions.FactionsIndex;
 | 
			
		||||
import com.massivecraft.factions.FactionsParticipator;
 | 
			
		||||
import com.massivecraft.factions.Perm;
 | 
			
		||||
import com.massivecraft.factions.Rel;
 | 
			
		||||
@@ -93,42 +94,16 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
 | 
			
		||||
	// UPDATE FACTION INDEXES
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
 | 
			
		||||
	public void updateFactionIndexes(String beforeId, String afterId)
 | 
			
		||||
	{
 | 
			
		||||
		// Really?
 | 
			
		||||
		if (!Factions.get().isDatabaseInitialized()) return;
 | 
			
		||||
		if (!this.attached()) return;
 | 
			
		||||
 | 
			
		||||
		// Fix IDs
 | 
			
		||||
		if (beforeId == null) beforeId = MConf.get().defaultPlayerFactionId;
 | 
			
		||||
		if (afterId == null) afterId = MConf.get().defaultPlayerFactionId;
 | 
			
		||||
 | 
			
		||||
		// NoChange
 | 
			
		||||
		if (MUtil.equals(beforeId, afterId)) return;
 | 
			
		||||
 | 
			
		||||
		// Resolve
 | 
			
		||||
		Faction before = FactionColl.get().get(beforeId, false);
 | 
			
		||||
		Faction after = FactionColl.get().get(afterId, false);
 | 
			
		||||
 | 
			
		||||
		// Apply
 | 
			
		||||
		if (before != null) before.mplayers.remove(this);
 | 
			
		||||
		if (after != null) after.mplayers.add(this);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void postAttach(String id)
 | 
			
		||||
	{
 | 
			
		||||
		String beforeId = null;
 | 
			
		||||
		String afterId = this.getFactionId();
 | 
			
		||||
		this.updateFactionIndexes(beforeId, afterId);
 | 
			
		||||
		FactionsIndex.get().update(this);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void preDetach(String id)
 | 
			
		||||
	{
 | 
			
		||||
		String before = this.getFactionId();
 | 
			
		||||
		String after = null;
 | 
			
		||||
		this.updateFactionIndexes(before, after);
 | 
			
		||||
		FactionsIndex.get().update(this);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
@@ -293,18 +268,8 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
 | 
			
		||||
		// Apply
 | 
			
		||||
		this.factionId = afterId;
 | 
			
		||||
 | 
			
		||||
		// Must be attached and initialized
 | 
			
		||||
		if (!this.attached()) return;
 | 
			
		||||
		if (!Factions.get().isDatabaseInitialized()) return;
 | 
			
		||||
 | 
			
		||||
		if (beforeId == null) beforeId = MConf.get().defaultPlayerFactionId;
 | 
			
		||||
 | 
			
		||||
		// Update index
 | 
			
		||||
		Faction before = Faction.get(beforeId);
 | 
			
		||||
		Faction after = this.getFaction();
 | 
			
		||||
 | 
			
		||||
		if (before != null) before.mplayers.remove(this);
 | 
			
		||||
		if (after != null) after.mplayers.add(this);
 | 
			
		||||
		// Index
 | 
			
		||||
		FactionsIndex.get().update(this);
 | 
			
		||||
 | 
			
		||||
		// Mark as changed
 | 
			
		||||
		this.changed();
 | 
			
		||||
 
 | 
			
		||||
@@ -4,11 +4,9 @@ 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;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
 | 
			
		||||
public class MPlayerColl extends SenderColl<MPlayer>
 | 
			
		||||
{
 | 
			
		||||
@@ -29,55 +27,6 @@ public class MPlayerColl extends SenderColl<MPlayer>
 | 
			
		||||
		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)
 | 
			
		||||
		{
 | 
			
		||||
			String beforeId = mplayer.getFactionId();
 | 
			
		||||
			String afterId = null;
 | 
			
		||||
			mplayer.updateFactionIndexes(beforeId, afterId);
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		return super.removeAtLocalFixed(id);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public synchronized void loadFromRemoteFixed(String id, Entry<JsonObject, Long> remoteEntry)
 | 
			
		||||
	{
 | 
			
		||||
		if (!Factions.get().isDatabaseInitialized())
 | 
			
		||||
		{
 | 
			
		||||
			super.loadFromRemoteFixed(id, remoteEntry);
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		MPlayer mplayer = null;
 | 
			
		||||
		
 | 
			
		||||
		// Before
 | 
			
		||||
		String beforeId = null;
 | 
			
		||||
		if (mplayer == null) mplayer = this.id2entity.get(id);
 | 
			
		||||
		if (mplayer != null) beforeId = mplayer.getFactionId();
 | 
			
		||||
		
 | 
			
		||||
		// Super
 | 
			
		||||
		super.loadFromRemoteFixed(id, remoteEntry);
 | 
			
		||||
		
 | 
			
		||||
		// After
 | 
			
		||||
		String afterId = null;
 | 
			
		||||
		if (mplayer == null) mplayer = this.id2entity.get(id);
 | 
			
		||||
		if (mplayer != null) afterId = mplayer.getFactionId();
 | 
			
		||||
		
 | 
			
		||||
		// Perform
 | 
			
		||||
		if (mplayer != null) mplayer.updateFactionIndexes(beforeId, afterId);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
	// EXTRAS
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user