Converting the command structure

This commit is contained in:
Olof Larsson
2011-10-08 23:22:02 +02:00
parent 0ce9cce9d3
commit 227d54dc5f
58 changed files with 582 additions and 538 deletions

View File

@ -5,81 +5,88 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.bukkit.ChatColor;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.util.AsciiCompass;
import com.massivecraft.factions.util.DiscUtil;
import com.massivecraft.factions.util.TextUtil;
import com.massivecraft.factions.zcore.util.DiscUtil;
public class Board
{
private static transient File file = new File(P.p.getDataFolder(), "board.json");
private static transient HashMap<FLocation, Integer> flocationIds = new HashMap<FLocation, Integer>();
private static transient HashMap<FLocation, String> flocationIds = new HashMap<FLocation, String>();
//----------------------------------------------//
// Get and Set
//----------------------------------------------//
public static int getIdAt(FLocation flocation) {
if ( ! flocationIds.containsKey(flocation)) {
return 0;
public static String getIdAt(FLocation flocation)
{
if ( ! flocationIds.containsKey(flocation))
{
return "0";
}
return flocationIds.get(flocation);
}
public static Faction getFactionAt(FLocation flocation) {
return Faction.get(getIdAt(flocation));
public static Faction getFactionAt(FLocation flocation)
{
return Factions.i.get(getIdAt(flocation));
}
public static void setIdAt(int id, FLocation flocation) {
public static void setIdAt(String id, FLocation flocation)
{
clearOwnershipAt(flocation);
if (id == 0) {
if (id == "0")
{
removeAt(flocation);
}
flocationIds.put(flocation, id);
}
public static void setFactionAt(Faction faction, FLocation flocation) {
public static void setFactionAt(Faction faction, FLocation flocation)
{
setIdAt(faction.getId(), flocation);
}
public static void removeAt(FLocation flocation) {
public static void removeAt(FLocation flocation)
{
clearOwnershipAt(flocation);
flocationIds.remove(flocation);
}
// not to be confused with claims, ownership referring to further member-specific ownership of a claim
public static void clearOwnershipAt(FLocation flocation) {
public static void clearOwnershipAt(FLocation flocation)
{
Faction faction = getFactionAt(flocation);
if (faction != null && faction.isNormal()) {
if (faction != null && faction.isNormal())
{
faction.clearClaimOwnership(flocation);
}
}
public static void unclaimAll(int factionId) {
Faction faction = Faction.get(factionId);
if (faction != null && faction.isNormal()) {
public static void unclaimAll(String factionId)
{
Faction faction = Factions.i.get(factionId);
if (faction != null && faction.isNormal())
{
faction.clearAllClaimOwnership();
}
Iterator<Entry<FLocation, Integer>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext()) {
Entry<FLocation, Integer> entry = iter.next();
if (entry.getValue().equals(factionId)) {
Iterator<Entry<FLocation, String>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext())
{
Entry<FLocation, String> entry = iter.next();
if (entry.getValue().equals(factionId))
{
iter.remove();
}
}
@ -87,7 +94,8 @@ public class Board
// Is this coord NOT completely surrounded by coords claimed by the same faction?
// Simpler: Is there any nearby coord with a faction other than the faction here?
public static boolean isBorderLocation(FLocation flocation) {
public static boolean isBorderLocation(FLocation flocation)
{
Faction faction = getFactionAt(flocation);
FLocation a = flocation.getRelative(1, 0);
FLocation b = flocation.getRelative(-1, 0);
@ -97,7 +105,8 @@ public class Board
}
// Is this coord connected to any coord claimed by the specified faction?
public static boolean isConnectedLocation(FLocation flocation, Faction faction) {
public static boolean isConnectedLocation(FLocation flocation, Faction faction)
{
FLocation a = flocation.getRelative(1, 0);
FLocation b = flocation.getRelative(-1, 0);
FLocation c = flocation.getRelative(0, 1);
@ -110,12 +119,14 @@ public class Board
// Cleaner. Remove orphaned foreign keys
//----------------------------------------------//
public static void clean() {
Iterator<Entry<FLocation, Integer>> iter = flocationIds.entrySet().iterator();
public static void clean()
{
Iterator<Entry<FLocation, String>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext()) {
Entry<FLocation, Integer> entry = iter.next();
if ( ! Faction.exists(entry.getValue())) {
P.log("Board cleaner removed "+entry.getValue()+" from "+entry.getKey());
Entry<FLocation, String> entry = iter.next();
if ( ! Factions.i.exists(entry.getValue()))
{
P.p.log("Board cleaner removed "+entry.getValue()+" from "+entry.getKey());
iter.remove();
}
}
@ -125,27 +136,33 @@ public class Board
// Coord count
//----------------------------------------------//
public static int getFactionCoordCount(int factionId) {
public static int getFactionCoordCount(String factionId)
{
int ret = 0;
for (int thatFactionId : flocationIds.values()) {
if(thatFactionId == factionId) {
for (String thatFactionId : flocationIds.values())
{
if(thatFactionId.equals(factionId))
{
ret += 1;
}
}
return ret;
}
public static int getFactionCoordCount(Faction faction) {
public static int getFactionCoordCount(Faction faction)
{
return getFactionCoordCount(faction.getId());
}
public static int getFactionCoordCountInWorld(Faction faction, String worldName) {
int factionId = faction.getId();
public static int getFactionCoordCountInWorld(Faction faction, String worldName)
{
String factionId = faction.getId();
int ret = 0;
Iterator<Entry<FLocation, Integer>> iter = flocationIds.entrySet().iterator();
Iterator<Entry<FLocation, String>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext()) {
Entry<FLocation, Integer> entry = iter.next();
if (entry.getValue() == factionId && entry.getKey().getWorldName().equals(worldName)) {
Entry<FLocation, String> entry = iter.next();
if (entry.getValue().equals(factionId) && entry.getKey().getWorldName().equals(worldName))
{
ret += 1;
}
}
@ -161,10 +178,11 @@ public class Board
* north is in the direction of decreasing x
* east is in the direction of decreasing z
*/
public static ArrayList<String> getMap(Faction faction, FLocation flocation, double inDegrees) {
public static ArrayList<String> getMap(Faction faction, FLocation flocation, double inDegrees)
{
ArrayList<String> ret = new ArrayList<String>();
Faction factionLoc = getFactionAt(flocation);
ret.add(TextUtil.titleize("("+flocation.getCoordString()+") "+factionLoc.getTag(faction)));
ret.add(P.p.txt.titleize("("+flocation.getCoordString()+") "+factionLoc.getTag(faction)));
int halfWidth = Conf.mapWidth / 2;
int halfHeight = Conf.mapHeight / 2;
@ -172,7 +190,8 @@ public class Board
int width = halfWidth * 2 + 1;
int height = halfHeight * 2 + 1;
if (Conf.showMapFactionKey) {
if (Conf.showMapFactionKey)
{
height--;
}
@ -216,7 +235,7 @@ public class Board
}
// Get the compass
ArrayList<String> asciiCompass = AsciiCompass.getAsciiCompass(inDegrees, ChatColor.RED, Conf.colorChrome);
ArrayList<String> asciiCompass = AsciiCompass.getAsciiCompass(inDegrees, ChatColor.RED, P.p.txt.parse("<a>"));
// Add the compass
ret.set(1, asciiCompass.get(0)+ret.get(1).substring(3*3));
@ -240,18 +259,18 @@ public class Board
// Persistance
// -------------------------------------------- //
public static Map<String,Map<String,Integer>> dumpAsSaveFormat() {
Map<String,Map<String,Integer>> worldCoordIds = new HashMap<String,Map<String,Integer>>();
public static Map<String,Map<String,String>> dumpAsSaveFormat() {
Map<String,Map<String,String>> worldCoordIds = new HashMap<String,Map<String,String>>();
String worldName, coords;
Integer id;
String id;
for (Entry<FLocation, Integer> entry : flocationIds.entrySet()) {
for (Entry<FLocation, String> entry : flocationIds.entrySet()) {
worldName = entry.getKey().getWorldName();
coords = entry.getKey().getCoordString();
id = entry.getValue();
if ( ! worldCoordIds.containsKey(worldName)) {
worldCoordIds.put(worldName, new TreeMap<String,Integer>());
worldCoordIds.put(worldName, new TreeMap<String,String>());
}
worldCoordIds.get(worldName).put(coords, id);
@ -260,16 +279,20 @@ public class Board
return worldCoordIds;
}
public static void loadFromSaveFormat(Map<String,Map<String,Integer>> worldCoordIds) {
public static void loadFromSaveFormat(Map<String,Map<String,String>> worldCoordIds)
{
flocationIds.clear();
String worldName;
String[] coords;
int x, z, factionId;
int x, z;
String factionId;
for (Entry<String,Map<String,Integer>> entry : worldCoordIds.entrySet()) {
for (Entry<String,Map<String,String>> entry : worldCoordIds.entrySet())
{
worldName = entry.getKey();
for (Entry<String,Integer> entry2 : entry.getValue().entrySet()) {
for (Entry<String,String> entry2 : entry.getValue().entrySet())
{
coords = entry2.getKey().trim().split("[,\\s]+");
x = Integer.parseInt(coords[0]);
z = Integer.parseInt(coords[1]);
@ -279,37 +302,45 @@ public class Board
}
}
public static boolean save() {
public static boolean save()
{
//Factions.log("Saving board to disk");
try {
try
{
DiscUtil.write(file, P.p.gson.toJson(dumpAsSaveFormat()));
} catch (Exception e) {
}
catch (Exception e)
{
e.printStackTrace();
P.log("Failed to save the board to disk.");
P.p.log("Failed to save the board to disk.");
return false;
}
return true;
}
public static boolean load() {
P.log("Loading board from disk");
public static boolean load()
{
P.p.log("Loading board from disk");
if ( ! file.exists()) {
if ( ! loadOld())
P.log("No board to load from disk. Creating new file.");
if ( ! file.exists())
{
P.p.log("No board to load from disk. Creating new file.");
save();
return true;
}
try {
Type type = new TypeToken<Map<String,Map<String,Integer>>>(){}.getType();
Map<String,Map<String,Integer>> worldCoordIds = P.p.gson.fromJson(DiscUtil.read(file), type);
try
{
Type type = new TypeToken<Map<String,Map<String,String>>>(){}.getType();
Map<String,Map<String,String>> worldCoordIds = P.p.gson.fromJson(DiscUtil.read(file), type);
loadFromSaveFormat(worldCoordIds);
} catch (Exception e) {
}
catch (Exception e)
{
e.printStackTrace();
P.log("Failed to load the board from disk.");
P.p.log("Failed to load the board from disk.");
return false;
}