Better way to remove faction from boards implemented. Also prevents orphaned foreign key errors.

This commit is contained in:
Olof Larsson 2011-02-13 15:18:23 +01:00
parent 19e0628f27
commit 09bc3df841
3 changed files with 40 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import java.util.Map.Entry;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.World; import org.bukkit.World;
import com.bukkit.mcteam.factions.util.Log;
import com.bukkit.mcteam.factions.util.TextUtil; import com.bukkit.mcteam.factions.util.TextUtil;
import com.bukkit.mcteam.util.AsciiCompass; 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) { public void purgeFaction(int factionId) {
Iterator<Entry<Coord, Integer>> iter = coordFactionIds.entrySet().iterator(); Iterator<Entry<Coord, Integer>> iter = coordFactionIds.entrySet().iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
@ -76,7 +101,7 @@ public class Board {
} }
public static void purgeFactionFromAllBoards(Faction faction) { public static void purgeFactionFromAllBoards(Faction faction) {
purgeFactionFromAllBoards(faction.id); purgeFactionFromAllBoards(faction.id);
} }*/
//----------------------------------------------// //----------------------------------------------//
// Coord count // Coord count
@ -106,7 +131,6 @@ public class Board {
return getFactionCoordCountAllBoards(faction.id); return getFactionCoordCountAllBoards(faction.id);
} }
//----------------------------------------------// //----------------------------------------------//
// Map generation // Map generation
//----------------------------------------------// //----------------------------------------------//

View File

@ -41,9 +41,10 @@ public class EM {
folderBase.mkdirs(); folderBase.mkdirs();
configLoad(); configLoad();
Log.threshold = Conf.logThreshold; Log.threshold = Conf.logThreshold;
boardLoadAll();
followerLoadAll();
factionLoadAll(); factionLoadAll();
followerLoadAll();
boardLoadAll();
Board.cleanAll();
} }
//----------------------------------------------// //----------------------------------------------//
@ -333,9 +334,17 @@ public class EM {
} }
public static Faction factionGet(Integer factionId) { 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); return factions.get(factionId);
} }
public static boolean factionExists(Integer factionId) {
return factions.containsKey(factionId);
}
public static Collection<Faction> factionGetAll() { public static Collection<Faction> factionGetAll() {
return factions.values(); return factions.values();
} }
@ -355,7 +364,8 @@ public class EM {
// Follower might get orphaned foreign id's // Follower might get orphaned foreign id's
// purge from all boards // purge from all boards
Board.purgeFactionFromAllBoards(id); //Board.purgeFactionFromAllBoards(id);
Board.cleanAll();
// Remove the file // Remove the file
File file = new File(folderFaction, id+ext); File file = new File(folderFaction, id+ext);

View File

@ -133,8 +133,6 @@ public class FactionsPlayerListener extends PlayerListener{
Follower me = Follower.get(event.getPlayer()); Follower me = Follower.get(event.getPlayer());
Board board = Board.get(event.getPlayer().getWorld()); Board board = Board.get(event.getPlayer().getWorld());
Log.debug("Player "+me.getName()+" is in world: "+board.id);
if (me.isMapAutoUpdating()) { if (me.isMapAutoUpdating()) {
me.sendMessage(board.getMap(me.getFaction(), Coord.from(me), me.getPlayer().getLocation().getYaw()), false); me.sendMessage(board.getMap(me.getFaction(), Coord.from(me), me.getPlayer().getLocation().getYaw()), false);
} else { } else {