From 09bc3df8417a9ba55752714f96da992eb5f1725d Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 13 Feb 2011 15:18:23 +0100 Subject: [PATCH] Better way to remove faction from boards implemented. Also prevents orphaned foreign key errors. --- .../mcteam/factions/entities/Board.java | 30 +++++++++++++++++-- .../bukkit/mcteam/factions/entities/EM.java | 16 ++++++++-- .../listeners/FactionsPlayerListener.java | 2 -- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/com/bukkit/mcteam/factions/entities/Board.java b/src/com/bukkit/mcteam/factions/entities/Board.java index 2548d91c..708d7150 100644 --- a/src/com/bukkit/mcteam/factions/entities/Board.java +++ b/src/com/bukkit/mcteam/factions/entities/Board.java @@ -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> iter = coordFactionIds.entrySet().iterator(); + while (iter.hasNext()) { + Entry 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> 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 //----------------------------------------------// diff --git a/src/com/bukkit/mcteam/factions/entities/EM.java b/src/com/bukkit/mcteam/factions/entities/EM.java index 2844df12..e643ff75 100644 --- a/src/com/bukkit/mcteam/factions/entities/EM.java +++ b/src/com/bukkit/mcteam/factions/entities/EM.java @@ -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 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); diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java index 9fe429ed..902b3dab 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java @@ -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 {