From 9d4aaeae6c5cd60c3ad60b933fe5420d648b82a5 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 23 Mar 2011 12:45:21 +0100 Subject: [PATCH] reworked the board --- src/com/bukkit/mcteam/factions/Board.java | 86 +++++++++++-------- src/com/bukkit/mcteam/factions/FLocation.java | 5 ++ .../listeners/FactionsPlayerListener.java | 3 +- src/com/bukkit/mcteam/gson/Gson.java | 1 + 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/src/com/bukkit/mcteam/factions/Board.java b/src/com/bukkit/mcteam/factions/Board.java index addd9b4b..12deffd8 100644 --- a/src/com/bukkit/mcteam/factions/Board.java +++ b/src/com/bukkit/mcteam/factions/Board.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; +import java.util.TreeMap; import org.bukkit.ChatColor; @@ -16,25 +17,19 @@ import com.bukkit.mcteam.gson.reflect.TypeToken; import com.bukkit.mcteam.util.AsciiCompass; import com.bukkit.mcteam.util.DiscUtil; -// TODO rework to use single layer hash map and convert from and to the formay while saving and loading!! - public class Board { - protected static transient File file = new File(Factions.instance.getDataFolder(), "board.json"); - private static Map> worldCoordIds = new HashMap>(); + private static transient File file = new File(Factions.instance.getDataFolder(), "board.json"); + private static transient HashMap flocationIds = new HashMap(); //----------------------------------------------// // Get and Set //----------------------------------------------// public static int getIdAt(FLocation flocation) { - if ( ! worldCoordIds.containsKey(flocation.getWorldName())) { + if ( ! flocationIds.containsKey(flocation)) { return 0; } - if ( ! worldCoordIds.get(flocation.getWorldName()).containsKey(flocation.getCoordString()) ) { - return 0; - } - - return worldCoordIds.get(flocation.getWorldName()).get(flocation.getCoordString()); + return flocationIds.get(flocation); } public static Faction getFactionAt(FLocation flocation) { @@ -46,11 +41,7 @@ public class Board { removeAt(flocation); } - if ( ! worldCoordIds.containsKey(flocation.getWorldName())) { - worldCoordIds.put(flocation.getWorldName(), new HashMap()); - } - - worldCoordIds.get(flocation.getWorldName()).put(flocation.getCoordString(), id); + flocationIds.put(flocation, id); } public static void setFactionAt(Faction faction, FLocation flocation) { @@ -58,10 +49,7 @@ public class Board { } public static void removeAt(FLocation flocation) { - if ( ! worldCoordIds.containsKey(flocation.getWorldName())) { - return; - } - worldCoordIds.get(flocation.getWorldName()).remove(flocation.getCoordString()); + flocationIds.remove(flocation); } // Is this coord NOT completely surrounded by coords claimed by the same faction? @@ -81,14 +69,12 @@ public class Board { //----------------------------------------------// public static void clean() { - for (String worldName : worldCoordIds.keySet()) { - Iterator> iter = worldCoordIds.get(worldName).entrySet().iterator(); - while (iter.hasNext()) { - Entry entry = iter.next(); - if ( ! Faction.exists(entry.getValue())) { - Factions.log("Board cleaner removed non existing faction id "+entry.getValue()+" from "+worldName+" "+entry.getKey()); - iter.remove(); - } + Iterator> iter = flocationIds.entrySet().iterator(); + while (iter.hasNext()) { + Entry entry = iter.next(); + if ( ! Faction.exists(entry.getValue())) { + Factions.log("Board cleaner removed "+entry.getValue()+" from "+entry.getKey()); + iter.remove(); } } } @@ -99,11 +85,9 @@ public class Board { public static int getFactionCoordCount(int factionId) { int ret = 0; - for (Map coordIds : worldCoordIds.values()) { - for (int thatFactionId : coordIds.values()) { - if(thatFactionId == factionId) { - ret += 1; - } + for (int thatFactionId : flocationIds.values()) { + if(thatFactionId == factionId) { + ret += 1; } } return ret; @@ -168,16 +152,49 @@ public class Board { // Persistance // -------------------------------------------- // + public static Map> dumpAsSaveFormat() { + Map> worldCoordIds = new HashMap>(); + + for (Entry entry : flocationIds.entrySet()) { + String worldName = entry.getKey().getWorldName(); + String coords = entry.getKey().getCoordString(); + Integer id = entry.getValue(); + if ( ! worldCoordIds.containsKey(worldName)) { + worldCoordIds.put(worldName, new TreeMap()); + } + + worldCoordIds.get(worldName).put(coords, id); + } + + return worldCoordIds; + } + + public static void loadFromSaveFormat(Map> worldCoordIds) { + flocationIds.clear(); + + for (Entry> entry : worldCoordIds.entrySet()) { + String worldName = entry.getKey(); + for (Entry entry2 : entry.getValue().entrySet()) { + String[] coords = entry2.getKey().trim().split("[,\\s]+"); + int x = Integer.parseInt(coords[0]); + int z = Integer.parseInt(coords[1]); + int factionId = entry2.getValue(); + flocationIds.put(new FLocation(worldName, x, z), factionId); + } + } + } + public static boolean save() { //Factions.log("Saving board to disk"); try { - DiscUtil.write(file, Factions.gson.toJson(worldCoordIds)); + DiscUtil.write(file, Factions.gson.toJson(dumpAsSaveFormat())); } catch (IOException e) { Factions.log("Failed to save the board to disk."); e.printStackTrace(); return false; } + return true; } @@ -192,7 +209,8 @@ public class Board { try { Type type = new TypeToken>>(){}.getType(); - worldCoordIds = Factions.gson.fromJson(DiscUtil.read(file), type); + Map> worldCoordIds = Factions.gson.fromJson(DiscUtil.read(file), type); + loadFromSaveFormat(worldCoordIds); } catch (IOException e) { Factions.log("Failed to load the board from disk."); e.printStackTrace(); diff --git a/src/com/bukkit/mcteam/factions/FLocation.java b/src/com/bukkit/mcteam/factions/FLocation.java index d046ffd4..7a5e4f94 100644 --- a/src/com/bukkit/mcteam/factions/FLocation.java +++ b/src/com/bukkit/mcteam/factions/FLocation.java @@ -73,6 +73,11 @@ public class FLocation { public String getCoordString() { return ""+x+","+z; } + + @Override + public String toString() { + return "["+this.getWorldName()+","+this.getCoordString()+"]"; + } //----------------------------------------------// // Misc diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java index b639aa88..3c8eff5a 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java @@ -150,8 +150,7 @@ public class FactionsPlayerListener extends PlayerListener{ } } - - //currently checking placement/use of: redstone, sign, flint&steel, beds (not currently detected by Bukkit), buckets (empty, water, lava), repeater (not currently detected by Bukkit) + public boolean playerCanUseItemHere(Player player, Block block, Material material) { if ( ! Conf.territoryDenyUseageMaterials.contains(material)) { diff --git a/src/com/bukkit/mcteam/gson/Gson.java b/src/com/bukkit/mcteam/gson/Gson.java index 847140c0..c4609c02 100644 --- a/src/com/bukkit/mcteam/gson/Gson.java +++ b/src/com/bukkit/mcteam/gson/Gson.java @@ -20,6 +20,7 @@ import com.bukkit.mcteam.gson.stream.JsonReader; import com.bukkit.mcteam.gson.stream.JsonToken; import com.bukkit.mcteam.gson.stream.JsonWriter; import com.bukkit.mcteam.gson.stream.MalformedJsonException; +import com.bukkit.mcteam.gson.JsonSerializationContextDefault; import java.io.IOException; import java.io.Reader; import java.io.StringReader;