Better way to remove faction from boards implemented. Also prevents orphaned foreign key errors.
This commit is contained in:
		@@ -6,6 +6,7 @@ import java.util.Map.Entry;
 | 
			
		||||
import org.bukkit.ChatColor;
 | 
			
		||||
import org.bukkit.World;
 | 
			
		||||
 | 
			
		||||
import com.bukkit.mcteam.factions.util.Log;
 | 
			
		||||
import com.bukkit.mcteam.factions.util.TextUtil;
 | 
			
		||||
import com.bukkit.mcteam.util.AsciiCompass;
 | 
			
		||||
 | 
			
		||||
@@ -53,9 +54,33 @@ public class Board {
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	//----------------------------------------------//
 | 
			
		||||
	// Purge faction
 | 
			
		||||
	// Clean boards
 | 
			
		||||
	//----------------------------------------------//
 | 
			
		||||
	
 | 
			
		||||
	// These functions search boards for orphaned foreign keys
 | 
			
		||||
	
 | 
			
		||||
	public void clean() {
 | 
			
		||||
		Iterator<Entry<Coord, Integer>> iter = coordFactionIds.entrySet().iterator();
 | 
			
		||||
		while (iter.hasNext()) {
 | 
			
		||||
			Entry<Coord, Integer> entry = iter.next();
 | 
			
		||||
			if ( ! EM.factionExists(entry.getValue())) {
 | 
			
		||||
				Log.debug("Cleaner removed coord with non existing factionId "+entry.getValue());
 | 
			
		||||
				iter.remove();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static void cleanAll() {
 | 
			
		||||
		for (Board board : getAll()) {
 | 
			
		||||
			Log.debug("Cleaning board: "+board.id);
 | 
			
		||||
			board.clean();
 | 
			
		||||
		}
 | 
			
		||||
	}	
 | 
			
		||||
	
 | 
			
		||||
	//----------------------------------------------//
 | 
			
		||||
	// Purge faction Currently skipped and we use clean instead as that will solve orphaned keys to :)
 | 
			
		||||
	//----------------------------------------------//
 | 
			
		||||
	/*
 | 
			
		||||
	public void purgeFaction(int factionId) {
 | 
			
		||||
		Iterator<Entry<Coord, Integer>> iter = coordFactionIds.entrySet().iterator();
 | 
			
		||||
		while (iter.hasNext()) {
 | 
			
		||||
@@ -76,7 +101,7 @@ public class Board {
 | 
			
		||||
	}
 | 
			
		||||
	public static void purgeFactionFromAllBoards(Faction faction) {
 | 
			
		||||
		purgeFactionFromAllBoards(faction.id);
 | 
			
		||||
	}
 | 
			
		||||
	}*/
 | 
			
		||||
	
 | 
			
		||||
	//----------------------------------------------//
 | 
			
		||||
	// Coord count
 | 
			
		||||
@@ -106,7 +131,6 @@ public class Board {
 | 
			
		||||
		return getFactionCoordCountAllBoards(faction.id);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
	//----------------------------------------------//
 | 
			
		||||
	// Map generation
 | 
			
		||||
	//----------------------------------------------//
 | 
			
		||||
 
 | 
			
		||||
@@ -41,9 +41,10 @@ public class EM {
 | 
			
		||||
		folderBase.mkdirs();
 | 
			
		||||
		configLoad();
 | 
			
		||||
		Log.threshold = Conf.logThreshold;
 | 
			
		||||
		boardLoadAll();
 | 
			
		||||
		followerLoadAll();
 | 
			
		||||
		factionLoadAll();
 | 
			
		||||
		followerLoadAll();
 | 
			
		||||
		boardLoadAll();
 | 
			
		||||
		Board.cleanAll();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	//----------------------------------------------//
 | 
			
		||||
@@ -333,9 +334,17 @@ public class EM {
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static Faction factionGet(Integer factionId) {
 | 
			
		||||
		if ( ! factions.containsKey(factionId)) {
 | 
			
		||||
			Log.warn("Non existing factionId "+factionId+" requested from EM! Issuing board cleaning!");
 | 
			
		||||
			Board.cleanAll();
 | 
			
		||||
		}
 | 
			
		||||
		return factions.get(factionId);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static boolean factionExists(Integer factionId) {
 | 
			
		||||
		return factions.containsKey(factionId);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static Collection<Faction> factionGetAll() {
 | 
			
		||||
		return factions.values();
 | 
			
		||||
	}
 | 
			
		||||
@@ -355,7 +364,8 @@ public class EM {
 | 
			
		||||
		// Follower might get orphaned foreign id's
 | 
			
		||||
		
 | 
			
		||||
		// purge from all boards
 | 
			
		||||
		Board.purgeFactionFromAllBoards(id);
 | 
			
		||||
		//Board.purgeFactionFromAllBoards(id);
 | 
			
		||||
		Board.cleanAll();
 | 
			
		||||
		
 | 
			
		||||
		// Remove the file
 | 
			
		||||
		File file = new File(folderFaction, id+ext);
 | 
			
		||||
 
 | 
			
		||||
@@ -133,8 +133,6 @@ public class FactionsPlayerListener extends PlayerListener{
 | 
			
		||||
		Follower me = Follower.get(event.getPlayer());
 | 
			
		||||
		Board board = Board.get(event.getPlayer().getWorld());
 | 
			
		||||
		
 | 
			
		||||
		Log.debug("Player "+me.getName()+" is in world: "+board.id);
 | 
			
		||||
		
 | 
			
		||||
		if (me.isMapAutoUpdating()) {
 | 
			
		||||
			me.sendMessage(board.getMap(me.getFaction(), Coord.from(me), me.getPlayer().getLocation().getYaw()), false);
 | 
			
		||||
		} else {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user