Saber-Factions/src/main/java/com/massivecraft/factions/Board.java

306 lines
11 KiB
Java
Raw Normal View History

2011-07-18 22:06:02 +02:00
package com.massivecraft.factions;
2011-02-06 13:36:11 +01:00
import com.massivecraft.factions.struct.Relation;
2011-07-18 22:06:02 +02:00
import com.massivecraft.factions.util.AsciiCompass;
2011-10-08 23:22:02 +02:00
import com.massivecraft.factions.zcore.util.DiscUtil;
2014-04-04 20:55:21 +02:00
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.libs.com.google.gson.reflect.TypeToken;
import java.io.File;
import java.lang.reflect.Type;
import java.util.*;
import java.util.Map.Entry;
public class Board {
private static transient File file = new File(P.p.getDataFolder(), "board.json");
private static transient HashMap<FLocation, String> flocationIds = new HashMap<FLocation, String>();
//----------------------------------------------//
// Get and Set
//----------------------------------------------//
public static String getIdAt(FLocation flocation) {
if (!flocationIds.containsKey(flocation)) {
return "0";
}
return flocationIds.get(flocation);
}
public static Faction getFactionAt(FLocation flocation) {
return Factions.i.get(getIdAt(flocation));
}
public static void setIdAt(String id, FLocation flocation) {
clearOwnershipAt(flocation);
2014-04-15 19:42:09 +02:00
if (id.equals("0")) {
2014-04-04 20:55:21 +02:00
removeAt(flocation);
}
flocationIds.put(flocation, id);
}
public static void setFactionAt(Faction faction, FLocation flocation) {
setIdAt(faction.getId(), flocation);
}
public static void removeAt(FLocation flocation) {
2014-07-01 22:10:18 +02:00
clearOwnershipAt(flocation);
flocationIds.remove(flocation);
2014-04-04 20:55:21 +02:00
}
// not to be confused with claims, ownership referring to further member-specific ownership of a claim
public static void clearOwnershipAt(FLocation flocation) {
2014-07-01 22:10:18 +02:00
Faction faction = getFactionAt(flocation);
if (faction != null && faction.isNormal()) {
2014-04-04 20:55:21 +02:00
faction.clearClaimOwnership(flocation);
}
}
public static void unclaimAll(String factionId) {
2014-07-01 22:10:18 +02:00
Faction faction = Factions.i.get(factionId);
if (faction != null && faction.isNormal()) {
2014-04-04 20:55:21 +02:00
faction.clearAllClaimOwnership();
}
2014-07-01 22:10:18 +02:00
Iterator<Entry<FLocation, String>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext()) {
Entry<FLocation, String> entry = iter.next();
if (entry.getValue().equals(factionId)) {
2014-04-04 20:55:21 +02:00
iter.remove();
}
}
}
// 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) {
2014-07-01 22:10:18 +02:00
Faction faction = getFactionAt(flocation);
FLocation a = flocation.getRelative(1, 0);
FLocation b = flocation.getRelative(-1, 0);
FLocation c = flocation.getRelative(0, 1);
2014-04-04 20:55:21 +02:00
FLocation d = flocation.getRelative(0, -1);
return faction != getFactionAt(a) || faction != getFactionAt(b) || faction != getFactionAt(c) || faction != getFactionAt(d);
}
// Is this coord connected to any coord claimed by the specified faction?
public static boolean isConnectedLocation(FLocation flocation, Faction faction) {
2014-07-01 22:10:18 +02:00
FLocation a = flocation.getRelative(1, 0);
FLocation b = flocation.getRelative(-1, 0);
FLocation c = flocation.getRelative(0, 1);
FLocation d = flocation.getRelative(0, -1);
2014-04-04 20:55:21 +02:00
return faction == getFactionAt(a) || faction == getFactionAt(b) || faction == getFactionAt(c) || faction == getFactionAt(d);
}
//----------------------------------------------//
// Cleaner. Remove orphaned foreign keys
//----------------------------------------------//
public static void clean() {
2014-07-01 22:10:18 +02:00
Iterator<Entry<FLocation, String>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext()) {
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();
2014-04-04 20:55:21 +02:00
}
}
}
//----------------------------------------------//
// Coord count
//----------------------------------------------//
public static int getFactionCoordCount(String factionId) {
2014-07-01 22:10:18 +02:00
int ret = 0;
for (String thatFactionId : flocationIds.values()) {
2014-04-04 20:55:21 +02:00
if (thatFactionId.equals(factionId)) {
ret += 1;
}
2014-07-01 22:10:18 +02:00
}
return ret;
2014-04-04 20:55:21 +02:00
}
public static int getFactionCoordCount(Faction faction) {
return getFactionCoordCount(faction.getId());
}
public static int getFactionCoordCountInWorld(Faction faction, String worldName) {
2014-07-01 22:10:18 +02:00
String factionId = faction.getId();
int ret = 0;
Iterator<Entry<FLocation, String>> iter = flocationIds.entrySet().iterator();
while (iter.hasNext()) {
2014-04-04 20:55:21 +02:00
Entry<FLocation, String> entry = iter.next();
if (entry.getValue().equals(factionId) && entry.getKey().getWorldName().equals(worldName)) {
ret += 1;
}
2014-07-01 22:10:18 +02:00
}
return ret;
2014-04-04 20:55:21 +02:00
}
//----------------------------------------------//
// Map generation
//----------------------------------------------//
/**
* The map is relative to a coord and a faction 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) {
2014-07-01 22:10:18 +02:00
ArrayList<String> ret = new ArrayList<String>();
Faction factionLoc = getFactionAt(flocation);
2014-04-04 20:55:21 +02:00
ret.add(P.p.txt.titleize("(" + flocation.getCoordString() + ") " + factionLoc.getTag(faction)));
2014-07-01 22:10:18 +02:00
int halfWidth = Conf.mapWidth / 2;
int halfHeight = Conf.mapHeight / 2;
FLocation topLeft = flocation.getRelative(-halfWidth, -halfHeight);
int width = halfWidth * 2 + 1;
2014-04-04 20:55:21 +02:00
int height = halfHeight * 2 + 1;
if (Conf.showMapFactionKey) {
height--;
}
2014-07-01 22:10:18 +02:00
Map<String, Character> fList = new HashMap<String, Character>();
int chrIdx = 0;
2014-04-04 20:55:21 +02:00
// For each row
for (int dz = 0; dz < height; dz++) {
// Draw and add that row
2014-07-01 22:10:18 +02:00
String row = "";
for (int dx = 0; dx < width; dx++) {
2014-04-04 20:55:21 +02:00
if (dx == halfWidth && dz == halfHeight) {
row += ChatColor.AQUA + "+";
2014-07-01 21:52:40 +02:00
} else {
2014-04-04 20:55:21 +02:00
FLocation flocationHere = topLeft.getRelative(dx, dz);
Faction factionHere = getFactionAt(flocationHere);
2014-07-01 22:10:18 +02:00
Relation relation = faction.getRelationTo(factionHere);
if (factionHere.isNone()) {
2014-04-04 20:55:21 +02:00
row += ChatColor.GRAY + "-";
2014-07-01 21:52:40 +02:00
} else if (factionHere.isSafeZone()) {
2014-04-04 20:55:21 +02:00
row += Conf.colorPeaceful + "+";
2014-07-01 21:52:40 +02:00
} else if (factionHere.isWarZone()) {
2014-04-04 20:55:21 +02:00
row += ChatColor.DARK_RED + "+";
2014-07-01 21:52:40 +02:00
} else if (factionHere == faction ||
factionHere == factionLoc ||
relation.isAtLeast(Relation.ALLY) ||
(Conf.showNeutralFactionsOnMap && relation.equals(Relation.NEUTRAL)) ||
(Conf.showEnemyFactionsOnMap && relation.equals(Relation.ENEMY))) {
2014-07-01 21:49:42 +02:00
if (!fList.containsKey(factionHere.getTag())) {
2014-04-04 20:55:21 +02:00
fList.put(factionHere.getTag(), Conf.mapKeyChrs[chrIdx++]);
2014-07-01 22:10:18 +02:00
}
char tag = fList.get(factionHere.getTag());
row += factionHere.getColorTo(faction) + "" + tag;
2014-07-01 21:52:40 +02:00
} else {
2014-04-04 20:55:21 +02:00
row += ChatColor.GRAY + "-";
}
}
2014-07-01 22:10:18 +02:00
}
ret.add(row);
2014-04-04 20:55:21 +02:00
}
// Get the compass
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));
ret.set(2, asciiCompass.get(1) + ret.get(2).substring(3 * 3));
ret.set(3, asciiCompass.get(2) + ret.get(3).substring(3 * 3));
// Add the faction key
if (Conf.showMapFactionKey) {
2014-07-01 22:10:18 +02:00
String fRow = "";
for (String key : fList.keySet()) {
2014-04-04 20:55:21 +02:00
fRow += String.format("%s%s: %s ", ChatColor.GRAY, fList.get(key), key);
2014-07-01 22:10:18 +02:00
}
ret.add(fRow);
2014-04-04 20:55:21 +02:00
}
return ret;
}
// -------------------------------------------- //
// Persistance
// -------------------------------------------- //
public static Map<String, Map<String, String>> dumpAsSaveFormat() {
2014-04-15 21:19:00 +02:00
Map<String, Map<String, String>> worldCoordIds = new HashMap<String, Map<String, String>>();
2014-04-04 20:55:21 +02:00
2014-07-01 22:10:18 +02:00
String worldName, coords;
String id;
2014-04-04 20:55:21 +02:00
for (Entry<FLocation, String> entry : flocationIds.entrySet()) {
2014-07-01 22:10:18 +02:00
worldName = entry.getKey().getWorldName();
coords = entry.getKey().getCoordString();
id = entry.getValue();
2014-04-04 20:55:21 +02:00
if (!worldCoordIds.containsKey(worldName)) {
worldCoordIds.put(worldName, new TreeMap<String, String>());
}
worldCoordIds.get(worldName).put(coords, id);
}
return worldCoordIds;
}
public static void loadFromSaveFormat(Map<String, Map<String, String>> worldCoordIds) {
flocationIds.clear();
2014-07-01 22:10:18 +02:00
String worldName;
String[] coords;
int x, z;
String factionId;
2014-04-04 20:55:21 +02:00
for (Entry<String, Map<String, String>> entry : worldCoordIds.entrySet()) {
2014-07-01 22:10:18 +02:00
worldName = entry.getKey();
for (Entry<String, String> entry2 : entry.getValue().entrySet()) {
coords = entry2.getKey().trim().split("[,\\s]+");
x = Integer.parseInt(coords[0]);
z = Integer.parseInt(coords[1]);
factionId = entry2.getValue();
2014-04-04 20:55:21 +02:00
flocationIds.put(new FLocation(worldName, x, z), factionId);
}
}
}
public static boolean save() {
//Factions.log("Saving board to disk");
try {
DiscUtil.write(file, P.p.gson.toJson(dumpAsSaveFormat()));
} catch (Exception e) {
2014-07-01 22:10:18 +02:00
e.printStackTrace();
P.p.log("Failed to save the board to disk.");
return false;
2014-04-04 20:55:21 +02:00
}
return true;
}
public static boolean load() {
P.p.log("Loading board from disk");
if (!file.exists()) {
2014-07-01 22:10:18 +02:00
P.p.log("No board to load from disk. Creating new file.");
save();
return true;
2014-04-04 20:55:21 +02:00
}
2011-02-06 13:36:11 +01:00
2014-04-04 20:55:21 +02:00
try {
2014-07-01 22:10:18 +02:00
Type type = new TypeToken<Map<String, Map<String, String>>>() {
}.getType();
2014-04-04 20:55:21 +02:00
Map<String, Map<String, String>> worldCoordIds = P.p.gson.fromJson(DiscUtil.read(file), type);
loadFromSaveFormat(worldCoordIds);
} catch (Exception e) {
2014-07-01 22:10:18 +02:00
e.printStackTrace();
P.p.log("Failed to load the board from disk.");
return false;
2014-04-04 20:55:21 +02:00
}
2011-02-06 13:36:11 +01:00
2014-04-04 20:55:21 +02:00
return true;
}
2011-02-06 13:36:11 +01:00
}