reworked the board

This commit is contained in:
Olof Larsson 2011-03-23 12:45:21 +01:00
parent bee15556c9
commit 9d4aaeae6c
4 changed files with 59 additions and 36 deletions

View File

@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.TreeMap;
import org.bukkit.ChatColor; 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.AsciiCompass;
import com.bukkit.mcteam.util.DiscUtil; 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 { public class Board {
protected static transient File file = new File(Factions.instance.getDataFolder(), "board.json"); private static transient File file = new File(Factions.instance.getDataFolder(), "board.json");
private static Map<String,Map<String,Integer>> worldCoordIds = new HashMap<String,Map<String,Integer>>(); private static transient HashMap<FLocation, Integer> flocationIds = new HashMap<FLocation, Integer>();
//----------------------------------------------// //----------------------------------------------//
// Get and Set // Get and Set
//----------------------------------------------// //----------------------------------------------//
public static int getIdAt(FLocation flocation) { public static int getIdAt(FLocation flocation) {
if ( ! worldCoordIds.containsKey(flocation.getWorldName())) { if ( ! flocationIds.containsKey(flocation)) {
return 0; return 0;
} }
if ( ! worldCoordIds.get(flocation.getWorldName()).containsKey(flocation.getCoordString()) ) { return flocationIds.get(flocation);
return 0;
}
return worldCoordIds.get(flocation.getWorldName()).get(flocation.getCoordString());
} }
public static Faction getFactionAt(FLocation flocation) { public static Faction getFactionAt(FLocation flocation) {
@ -46,11 +41,7 @@ public class Board {
removeAt(flocation); removeAt(flocation);
} }
if ( ! worldCoordIds.containsKey(flocation.getWorldName())) { flocationIds.put(flocation, id);
worldCoordIds.put(flocation.getWorldName(), new HashMap<String,Integer>());
}
worldCoordIds.get(flocation.getWorldName()).put(flocation.getCoordString(), id);
} }
public static void setFactionAt(Faction faction, FLocation flocation) { public static void setFactionAt(Faction faction, FLocation flocation) {
@ -58,10 +49,7 @@ public class Board {
} }
public static void removeAt(FLocation flocation) { public static void removeAt(FLocation flocation) {
if ( ! worldCoordIds.containsKey(flocation.getWorldName())) { flocationIds.remove(flocation);
return;
}
worldCoordIds.get(flocation.getWorldName()).remove(flocation.getCoordString());
} }
// Is this coord NOT completely surrounded by coords claimed by the same faction? // Is this coord NOT completely surrounded by coords claimed by the same faction?
@ -81,14 +69,12 @@ public class Board {
//----------------------------------------------// //----------------------------------------------//
public static void clean() { public static void clean() {
for (String worldName : worldCoordIds.keySet()) { Iterator<Entry<FLocation, Integer>> iter = flocationIds.entrySet().iterator();
Iterator<Entry<String, Integer>> iter = worldCoordIds.get(worldName).entrySet().iterator(); while (iter.hasNext()) {
while (iter.hasNext()) { Entry<FLocation, Integer> entry = iter.next();
Entry<String, Integer> entry = iter.next(); if ( ! Faction.exists(entry.getValue())) {
if ( ! Faction.exists(entry.getValue())) { Factions.log("Board cleaner removed "+entry.getValue()+" from "+entry.getKey());
Factions.log("Board cleaner removed non existing faction id "+entry.getValue()+" from "+worldName+" "+entry.getKey()); iter.remove();
iter.remove();
}
} }
} }
} }
@ -99,11 +85,9 @@ public class Board {
public static int getFactionCoordCount(int factionId) { public static int getFactionCoordCount(int factionId) {
int ret = 0; int ret = 0;
for (Map<String, Integer> coordIds : worldCoordIds.values()) { for (int thatFactionId : flocationIds.values()) {
for (int thatFactionId : coordIds.values()) { if(thatFactionId == factionId) {
if(thatFactionId == factionId) { ret += 1;
ret += 1;
}
} }
} }
return ret; return ret;
@ -168,16 +152,49 @@ public class Board {
// Persistance // Persistance
// -------------------------------------------- // // -------------------------------------------- //
public static Map<String,Map<String,Integer>> dumpAsSaveFormat() {
Map<String,Map<String,Integer>> worldCoordIds = new HashMap<String,Map<String,Integer>>();
for (Entry<FLocation, Integer> 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<String,Integer>());
}
worldCoordIds.get(worldName).put(coords, id);
}
return worldCoordIds;
}
public static void loadFromSaveFormat(Map<String,Map<String,Integer>> worldCoordIds) {
flocationIds.clear();
for (Entry<String,Map<String,Integer>> entry : worldCoordIds.entrySet()) {
String worldName = entry.getKey();
for (Entry<String,Integer> 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() { public static boolean save() {
//Factions.log("Saving board to disk"); //Factions.log("Saving board to disk");
try { try {
DiscUtil.write(file, Factions.gson.toJson(worldCoordIds)); DiscUtil.write(file, Factions.gson.toJson(dumpAsSaveFormat()));
} catch (IOException e) { } catch (IOException e) {
Factions.log("Failed to save the board to disk."); Factions.log("Failed to save the board to disk.");
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
return true; return true;
} }
@ -192,7 +209,8 @@ public class Board {
try { try {
Type type = new TypeToken<Map<String,Map<String,Integer>>>(){}.getType(); Type type = new TypeToken<Map<String,Map<String,Integer>>>(){}.getType();
worldCoordIds = Factions.gson.fromJson(DiscUtil.read(file), type); Map<String,Map<String,Integer>> worldCoordIds = Factions.gson.fromJson(DiscUtil.read(file), type);
loadFromSaveFormat(worldCoordIds);
} catch (IOException e) { } catch (IOException e) {
Factions.log("Failed to load the board from disk."); Factions.log("Failed to load the board from disk.");
e.printStackTrace(); e.printStackTrace();

View File

@ -74,6 +74,11 @@ public class FLocation {
return ""+x+","+z; return ""+x+","+z;
} }
@Override
public String toString() {
return "["+this.getWorldName()+","+this.getCoordString()+"]";
}
//----------------------------------------------// //----------------------------------------------//
// Misc // Misc
//----------------------------------------------// //----------------------------------------------//

View File

@ -151,7 +151,6 @@ 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) { public boolean playerCanUseItemHere(Player player, Block block, Material material) {
if ( ! Conf.territoryDenyUseageMaterials.contains(material)) { if ( ! Conf.territoryDenyUseageMaterials.contains(material)) {

View File

@ -20,6 +20,7 @@ import com.bukkit.mcteam.gson.stream.JsonReader;
import com.bukkit.mcteam.gson.stream.JsonToken; import com.bukkit.mcteam.gson.stream.JsonToken;
import com.bukkit.mcteam.gson.stream.JsonWriter; import com.bukkit.mcteam.gson.stream.JsonWriter;
import com.bukkit.mcteam.gson.stream.MalformedJsonException; import com.bukkit.mcteam.gson.stream.MalformedJsonException;
import com.bukkit.mcteam.gson.JsonSerializationContextDefault;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;