From f2b24003bf351c049943e384c60360fb95bc00c9 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 12 Apr 2013 09:51:14 +0200 Subject: [PATCH] Do use the faction id constants --- src/com/massivecraft/factions/Board.java | 4 +- .../massivecraft/factions/BoardOldDepr.java | 369 ------------------ src/com/massivecraft/factions/ConfServer.java | 2 +- src/com/massivecraft/factions/FPlayer.java | 7 +- src/com/massivecraft/factions/Faction.java | 2 +- .../adapters/TerritoryAccessAdapter.java | 3 +- 6 files changed, 10 insertions(+), 377 deletions(-) delete mode 100644 src/com/massivecraft/factions/BoardOldDepr.java diff --git a/src/com/massivecraft/factions/Board.java b/src/com/massivecraft/factions/Board.java index 24917b69..e9b10caa 100644 --- a/src/com/massivecraft/factions/Board.java +++ b/src/com/massivecraft/factions/Board.java @@ -86,7 +86,7 @@ public class Board extends Entity implements BoardInterface if (ps == null) return null; ps = ps.getChunkCoords(true); TerritoryAccess ret = this.map.get(ps); - if (ret == null) ret = new TerritoryAccess("0"); + if (ret == null) ret = new TerritoryAccess(Const.FACTIONID_NONE); return ret; } @@ -104,7 +104,7 @@ public class Board extends Entity implements BoardInterface { ps = ps.getChunkCoords(true); - if (territoryAccess == null || (territoryAccess.getHostFactionId().equals("0") && territoryAccess.isDefault())) + if (territoryAccess == null || (territoryAccess.getHostFactionId().equals(Const.FACTIONID_NONE) && territoryAccess.isDefault())) { // TODO: Listen to an event instead! // And this is probably the place where the event should be triggered! diff --git a/src/com/massivecraft/factions/BoardOldDepr.java b/src/com/massivecraft/factions/BoardOldDepr.java deleted file mode 100644 index c41110ba..00000000 --- a/src/com/massivecraft/factions/BoardOldDepr.java +++ /dev/null @@ -1,369 +0,0 @@ -package com.massivecraft.factions; - -import java.io.*; -import java.lang.reflect.Type; -import java.util.ArrayList; -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; -import org.bukkit.Location; -import org.bukkit.block.Block; - -import com.massivecraft.mcore.util.DiscUtil; -import com.massivecraft.mcore.util.Txt; -import com.massivecraft.mcore.xlib.gson.reflect.TypeToken; -import com.massivecraft.factions.integration.LWCFeatures; -import com.massivecraft.factions.iface.RelationParticipator; -import com.massivecraft.factions.util.AsciiCompass; - - -public class BoardOldDepr -{ - private static transient File file = new File(Factions.get().getDataFolder(), "board.json"); - private static transient HashMap flocationIds = new HashMap(); - - // -------------------------------------------- // - // GET AND SET - // -------------------------------------------- // - public static String getIdAt(FLocation flocation) - { - if ( ! flocationIds.containsKey(flocation)) return "0"; - - return flocationIds.get(flocation).getHostFactionId(); - } - - public static TerritoryAccess getTerritoryAccessAt(FLocation flocation) - { - if ( ! flocationIds.containsKey(flocation)) - { - return new TerritoryAccess("0"); - } - return flocationIds.get(flocation); - } - - public static Faction getFactionAt(FLocation flocation) - { - return FactionColl.i.get(getIdAt(flocation)); - } - public static Faction getFactionAt(Location location) - { - return getFactionAt(new FLocation(location)); - } - public static Faction getFactionAt(Block block) - { - return getFactionAt(new FLocation(block)); - } - - public static void setIdAt(String id, FLocation flocation) - { - if (id == "0") - removeAt(flocation); - - flocationIds.put(flocation, new TerritoryAccess(id)); - } - - public static void setFactionAt(Faction faction, FLocation flocation) - { - setIdAt(faction.getId(), flocation); - } - - public static void removeAt(FLocation flocation) - { - if(ConfServer.onUnclaimResetLwcLocks && LWCFeatures.getEnabled()) - LWCFeatures.clearAllChests(flocation); - - flocationIds.remove(flocation); - } - - public static void unclaimAll(String factionId) - { - Iterator> iter = flocationIds.entrySet().iterator(); - while (iter.hasNext()) - { - Entry entry = iter.next(); - if (entry.getValue().getHostFactionId().equals(factionId)) - { - if(ConfServer.onUnclaimResetLwcLocks && LWCFeatures.getEnabled()) - LWCFeatures.clearAllChests(entry.getKey()); - - 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) - { - Faction faction = getFactionAt(flocation); - FLocation a = flocation.getRelative(1, 0); - FLocation b = flocation.getRelative(-1, 0); - FLocation c = flocation.getRelative(0, 1); - 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) - { - FLocation a = flocation.getRelative(1, 0); - FLocation b = flocation.getRelative(-1, 0); - FLocation c = flocation.getRelative(0, 1); - FLocation d = flocation.getRelative(0, -1); - return faction == getFactionAt(a) || faction == getFactionAt(b) || faction == getFactionAt(c) || faction == getFactionAt(d); - } - - - // -------------------------------------------- // - // CLEANER. REMOVE ORPHANED FOREIGN KEYS - // -------------------------------------------- // - - public static void clean() - { - Iterator> iter = flocationIds.entrySet().iterator(); - while (iter.hasNext()) { - Entry entry = iter.next(); - if ( ! FactionColl.i.exists(entry.getValue().getHostFactionId())) - { - if(ConfServer.onUnclaimResetLwcLocks && LWCFeatures.getEnabled()) - LWCFeatures.clearAllChests(entry.getKey()); - - Factions.get().log("Board cleaner removed "+entry.getValue().getHostFactionId()+" from "+entry.getKey()); - iter.remove(); - } - } - } - - // -------------------------------------------- // - // COORD COUNT - // -------------------------------------------- // - - public static int getFactionCoordCount(String factionId) - { - int ret = 0; - for (TerritoryAccess thatFactionId : flocationIds.values()) - { - if(thatFactionId.getHostFactionId().equals(factionId)) - { - ret += 1; - } - } - return ret; - } - - public static int getFactionCoordCount(Faction faction) - { - return getFactionCoordCount(faction.getId()); - } - - public static int getFactionCoordCountInWorld(Faction faction, String worldName) - { - String factionId = faction.getId(); - int ret = 0; - Iterator> iter = flocationIds.entrySet().iterator(); - while (iter.hasNext()) { - Entry entry = iter.next(); - if (entry.getValue().getHostFactionId().equals(factionId) && entry.getKey().getWorldName().equals(worldName)) - { - ret += 1; - } - } - return ret; - } - - // -------------------------------------------- // - // 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 getMap(RelationParticipator observer, FLocation flocation, double inDegrees) - { - ArrayList ret = new ArrayList(); - Faction factionLoc = getFactionAt(flocation); - ret.add(Txt.titleize("("+flocation.getCoordString()+") "+factionLoc.getTag(observer))); - - int halfWidth = Const.MAP_WIDTH / 2; - int halfHeight = Const.MAP_HEIGHT / 2; - FLocation topLeft = flocation.getRelative(-halfWidth, -halfHeight); - int width = halfWidth * 2 + 1; - int height = halfHeight * 2 + 1; - - //Make room for the list of tags - height--; - - - Map fList = new HashMap(); - int chrIdx = 0; - - // For each row - for (int dz = 0; dz < height; dz++) - { - // Draw and add that row - String row = ""; - for (int dx = 0; dx < width; dx++) - { - if(dx == halfWidth && dz == halfHeight) - { - row += ChatColor.AQUA+"+"; - continue; - } - - FLocation flocationHere = topLeft.getRelative(dx, dz); - Faction factionHere = getFactionAt(flocationHere); - if (factionHere.isNone()) - { - row += ChatColor.GRAY+"-"; - } - else - { - if (!fList.containsKey(factionHere)) - fList.put(factionHere, Const.MAP_KEY_CHARS[chrIdx++]); - char fchar = fList.get(factionHere); - row += factionHere.getColorTo(observer) + "" + fchar; - } - } - ret.add(row); - } - - // Get the compass - ArrayList asciiCompass = AsciiCompass.getAsciiCompass(inDegrees, ChatColor.RED, Txt.parse("")); - - // 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)); - - String fRow = ""; - for(Faction keyfaction : fList.keySet()) - { - fRow += ""+keyfaction.getColorTo(observer) + fList.get(keyfaction) + ": " + keyfaction.getTag() + " "; - } - fRow = fRow.trim(); - ret.add(fRow); - - return ret; - } - - - // -------------------------------------------- // - // Persistance - // -------------------------------------------- // - - public static Map> dumpAsSaveFormat() - { - Map> worldCoordIds = new HashMap>(); - - String worldName, coords; - TerritoryAccess data; - - for (Entry entry : flocationIds.entrySet()) - { - worldName = entry.getKey().getWorldName(); - coords = entry.getKey().getCoordString(); - data = entry.getValue(); - if ( ! worldCoordIds.containsKey(worldName)) - { - worldCoordIds.put(worldName, new TreeMap()); - } - - worldCoordIds.get(worldName).put(coords, data); - } - - return worldCoordIds; - } - - public static void loadFromSaveFormat(Map> worldCoordIds) - { - flocationIds.clear(); - - String worldName; - String[] coords; - int x, z; - TerritoryAccess data; - - for (Entry> entry : worldCoordIds.entrySet()) - { - worldName = entry.getKey(); - for (Entry entry2 : entry.getValue().entrySet()) - { - coords = entry2.getKey().trim().split("[,\\s]+"); - x = Integer.parseInt(coords[0]); - z = Integer.parseInt(coords[1]); - data = entry2.getValue(); - flocationIds.put(new FLocation(worldName, x, z), data); - } - } - } - - public static boolean save() - { - //Factions.log("Saving board to disk"); - - try - { - DiscUtil.write(file, Factions.get().gson.toJson(dumpAsSaveFormat())); - } - catch (Exception e) - { - e.printStackTrace(); - Factions.get().log("Failed to save the board to disk."); - return false; - } - - return true; - } - - public static boolean load() - { - Factions.get().log("Loading board from disk"); - - if ( ! file.exists()) - { - Factions.get().log("No board to load from disk. Creating new file."); - save(); - return true; - } - - try - { - Type type = new TypeToken>>(){}.getType(); - Map> worldCoordIds = Factions.get().gson.fromJson(DiscUtil.read(file), type); - loadFromSaveFormat(worldCoordIds); - } - catch (Exception e) - { - e.printStackTrace(); - Factions.get().log("Failed to load the board from disk."); - return false; - } - - return true; - } -} - - - - - - - - - - - - - - - - - - - diff --git a/src/com/massivecraft/factions/ConfServer.java b/src/com/massivecraft/factions/ConfServer.java index c8338a4a..710e6949 100644 --- a/src/com/massivecraft/factions/ConfServer.java +++ b/src/com/massivecraft/factions/ConfServer.java @@ -68,7 +68,7 @@ public class ConfServer extends SimpleConfig public static int factionMemberLimit = 0; // what faction ID to start new players in when they first join the server; default is 0, "no faction" - public static String newPlayerStartingFactionID = "0"; + public static String newPlayerStartingFactionID = Const.FACTIONID_NONE; //public static boolean showMapFactionKey = true; //public static boolean showNeutralFactionsOnMap = true; diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 1afaf057..84d50c40 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -68,7 +68,7 @@ public class FPlayer extends SenderEntity implements EconomyParticipato private String factionId; public Faction getFaction() { if(this.factionId == null) {return null;} return FactionColl.get().get(this.factionId); } public String getFactionId() { return this.factionId; } - public boolean hasFaction() { return this.factionId != null && ! factionId.equals("0"); } + public boolean hasFaction() { return this.factionId != null && ! factionId.equals(Const.FACTIONID_NONE); } public void setFaction(Faction faction) { Faction oldFaction = this.getFaction(); @@ -149,7 +149,7 @@ public class FPlayer extends SenderEntity implements EconomyParticipato this.loginPvpDisabled = (ConfServer.noPVPDamageToOthersForXSecondsAfterLogin > 0) ? true : false; this.powerBoost = 0.0; - if ( ! ConfServer.newPlayerStartingFactionID.equals("0") && FactionColl.get().containsId(ConfServer.newPlayerStartingFactionID)) + if ( ! ConfServer.newPlayerStartingFactionID.equals(Const.FACTIONID_NONE) && FactionColl.get().containsId(ConfServer.newPlayerStartingFactionID)) { this.factionId = ConfServer.newPlayerStartingFactionID; } @@ -166,7 +166,8 @@ public class FPlayer extends SenderEntity implements EconomyParticipato } } - this.factionId = "0"; // The default neutral faction + // TODO: Should we not rather use ConfServer.newPlayerStartingFactionID here? + this.factionId = Const.FACTIONID_NONE; // The default neutral faction this.role = Rel.MEMBER; this.title = ""; diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index ae127b84..b87bac9b 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -320,7 +320,7 @@ public class Faction extends Entity implements EconomyParticipa public boolean isNone() { - return this.getId().equals("0"); + return this.getId().equals(Const.FACTIONID_NONE); } // ------------------------------- diff --git a/src/com/massivecraft/factions/adapters/TerritoryAccessAdapter.java b/src/com/massivecraft/factions/adapters/TerritoryAccessAdapter.java index 4c6c74ac..b0745917 100644 --- a/src/com/massivecraft/factions/adapters/TerritoryAccessAdapter.java +++ b/src/com/massivecraft/factions/adapters/TerritoryAccessAdapter.java @@ -14,6 +14,7 @@ import com.massivecraft.mcore.xlib.gson.JsonPrimitive; import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; import com.massivecraft.mcore.xlib.gson.JsonSerializer; +import com.massivecraft.factions.Const; import com.massivecraft.factions.Factions; import com.massivecraft.factions.TerritoryAccess; @@ -97,7 +98,7 @@ public class TerritoryAccessAdapter implements JsonDeserializer if (src.isDefault()) { // if Wilderness (faction "0") and default access values, no need to store it - if (src.getHostFactionId().equals("0")) + if (src.getHostFactionId().equals(Const.FACTIONID_NONE)) return null; return new JsonPrimitive(src.getHostFactionId());