From 2f021ecc72526c3965dfe31a81af0afc77b0f72f Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 1 Oct 2014 22:00:08 +0200 Subject: [PATCH] Remove random custom special faction ids and create update process for it. --- pom.xml | 2 +- .../com/massivecraft/factions/Factions.java | 5 + .../factions/TerritoryAccess.java | 8 +- .../massivecraft/factions/entity/Board.java | 4 +- .../massivecraft/factions/entity/Faction.java | 7 +- .../factions/entity/FactionColl.java | 6 +- .../massivecraft/factions/entity/MConf.java | 14 +- .../massivecraft/factions/entity/MPlayer.java | 2 +- .../factions/update/UpdateUtil.java | 132 ++++++++++++++++++ 9 files changed, 164 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 86d7c5ed..dbb5969e 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ - clean install + install Factions ${basedir}/src/main/java/ diff --git a/src/main/java/com/massivecraft/factions/Factions.java b/src/main/java/com/massivecraft/factions/Factions.java index 55344e5c..fd43b801 100644 --- a/src/main/java/com/massivecraft/factions/Factions.java +++ b/src/main/java/com/massivecraft/factions/Factions.java @@ -58,6 +58,10 @@ public class Factions extends MassivePlugin public final static String FACTION_MONEY_ACCOUNT_ID_PREFIX = "faction-"; + public final static String ID_NONE = "none"; + public final static String ID_SAFEZONE = "safezone"; + public final static String ID_WARZONE = "warzone"; + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // @@ -120,6 +124,7 @@ public class Factions extends MassivePlugin MPlayerColl.get().init(); FactionColl.get().init(); BoardColl.get().init(); + UpdateUtil.updateSpecialIds(); FactionColl.get().reindexMPlayers(); this.databaseInitialized = true; diff --git a/src/main/java/com/massivecraft/factions/TerritoryAccess.java b/src/main/java/com/massivecraft/factions/TerritoryAccess.java index 7b3f3139..54596826 100644 --- a/src/main/java/com/massivecraft/factions/TerritoryAccess.java +++ b/src/main/java/com/massivecraft/factions/TerritoryAccess.java @@ -17,19 +17,21 @@ public class TerritoryAccess // FIELDS: RAW // -------------------------------------------- // + // TODO: remake private final // no default value, can't be null - private final String hostFactionId; + public String hostFactionId; public String getHostFactionId() { return this.hostFactionId; } // default is true private final boolean hostFactionAllowed; public boolean isHostFactionAllowed() { return this.hostFactionAllowed; } + // TODO: remake private final // default is empty - private final Set factionIds; + public Set factionIds; public Set getFactionIds() { return this.factionIds; } - // TODO: remate private final + // TODO: remake private final // default is empty public Set playerIds; public Set getPlayerIds() { return this.playerIds; } diff --git a/src/main/java/com/massivecraft/factions/entity/Board.java b/src/main/java/com/massivecraft/factions/entity/Board.java index 2e839bc0..9ef0868a 100644 --- a/src/main/java/com/massivecraft/factions/entity/Board.java +++ b/src/main/java/com/massivecraft/factions/entity/Board.java @@ -90,7 +90,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 = TerritoryAccess.valueOf(MConf.get().factionIdNone); + if (ret == null) ret = TerritoryAccess.valueOf(Factions.ID_NONE); return ret; } @@ -109,7 +109,7 @@ public class Board extends Entity implements BoardInterface { ps = ps.getChunkCoords(true); - if (territoryAccess == null || (territoryAccess.getHostFactionId().equals(MConf.get().factionIdNone) && territoryAccess.isDefault())) + if (territoryAccess == null || (territoryAccess.getHostFactionId().equals(Factions.ID_NONE) && territoryAccess.isDefault())) { this.map.remove(ps); } diff --git a/src/main/java/com/massivecraft/factions/entity/Faction.java b/src/main/java/com/massivecraft/factions/entity/Faction.java index 57b69cbc..204aa310 100644 --- a/src/main/java/com/massivecraft/factions/entity/Faction.java +++ b/src/main/java/com/massivecraft/factions/entity/Faction.java @@ -60,6 +60,11 @@ public class Faction extends Entity implements EconomyParticipator @Override public void preDetach(String id) { + // The database must be fully inited. + // We may move factions around during upgrades. + if (!Factions.get().isDatabaseInitialized()) return; + + // Zero balance Money.set(this, null, 0); // Clean the board @@ -130,7 +135,7 @@ public class Faction extends Entity implements EconomyParticipator public boolean isNone() { - return this.getId().equals(MConf.get().factionIdNone); + return this.getId().equals(Factions.ID_NONE); } public boolean isNormal() diff --git a/src/main/java/com/massivecraft/factions/entity/FactionColl.java b/src/main/java/com/massivecraft/factions/entity/FactionColl.java index c2e89eda..9ac463d6 100644 --- a/src/main/java/com/massivecraft/factions/entity/FactionColl.java +++ b/src/main/java/com/massivecraft/factions/entity/FactionColl.java @@ -85,7 +85,7 @@ public class FactionColl extends Coll public Faction getNone() { - String id = MConf.get().factionIdNone; + String id = Factions.ID_NONE; Faction faction = this.get(id); if (faction != null) return faction; @@ -118,7 +118,7 @@ public class FactionColl extends Coll public Faction getSafezone() { - String id = MConf.get().factionIdSafezone; + String id = Factions.ID_SAFEZONE; Faction faction = this.get(id); if (faction != null) return faction; @@ -151,7 +151,7 @@ public class FactionColl extends Coll public Faction getWarzone() { - String id = MConf.get().factionIdWarzone; + String id = Factions.ID_WARZONE; Faction faction = this.get(id); if (faction != null) return faction; diff --git a/src/main/java/com/massivecraft/factions/entity/MConf.java b/src/main/java/com/massivecraft/factions/entity/MConf.java index 16090b0b..cb180c53 100644 --- a/src/main/java/com/massivecraft/factions/entity/MConf.java +++ b/src/main/java/com/massivecraft/factions/entity/MConf.java @@ -6,7 +6,6 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.UUID; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -88,16 +87,21 @@ public class MConf extends Entity // -------------------------------------------- // // SPECIAL FACTION IDS // -------------------------------------------- // + // These are a deprecated remnant from the universe system. + // We needed these to understand the difference between wilderness in different universes. + // Now that we are back to one universe only, we can have static names like simply "none", "safezone" and "warzone". + // Previously we set them to UUID.randomUUID().toString() but now we set them to null. + // If the value is set we use it to update map entries and then set it to null really quick. - public String factionIdNone = UUID.randomUUID().toString(); - public String factionIdSafezone = UUID.randomUUID().toString(); - public String factionIdWarzone = UUID.randomUUID().toString(); + public String factionIdNone = null; + public String factionIdSafezone = null; + public String factionIdWarzone = null; // -------------------------------------------- // // DEFAULTS // -------------------------------------------- // - public String defaultPlayerFactionId = this.factionIdNone; + public String defaultPlayerFactionId = Factions.ID_NONE; public Rel defaultPlayerRole = Rel.RECRUIT; public double defaultPlayerPower = 0.0; diff --git a/src/main/java/com/massivecraft/factions/entity/MPlayer.java b/src/main/java/com/massivecraft/factions/entity/MPlayer.java index cfbabb08..1e082dd1 100644 --- a/src/main/java/com/massivecraft/factions/entity/MPlayer.java +++ b/src/main/java/com/massivecraft/factions/entity/MPlayer.java @@ -186,7 +186,7 @@ public class MPlayer extends SenderEntity implements EconomyParticipato public boolean hasFaction() { - return !this.getFactionId().equals(MConf.get().factionIdNone); + return !this.getFactionId().equals(Factions.ID_NONE); } // This setter is so long because it search for default/null case and takes care of updating the faction member index diff --git a/src/main/java/com/massivecraft/factions/update/UpdateUtil.java b/src/main/java/com/massivecraft/factions/update/UpdateUtil.java index d45ed04c..f51281cb 100644 --- a/src/main/java/com/massivecraft/factions/update/UpdateUtil.java +++ b/src/main/java/com/massivecraft/factions/update/UpdateUtil.java @@ -2,10 +2,17 @@ package com.massivecraft.factions.update; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import com.massivecraft.factions.Const; import com.massivecraft.factions.Factions; +import com.massivecraft.factions.TerritoryAccess; +import com.massivecraft.factions.entity.Board; +import com.massivecraft.factions.entity.BoardColl; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.MConf; import com.massivecraft.massivecore.MassiveCore; import com.massivecraft.massivecore.store.Coll; @@ -120,4 +127,129 @@ public class UpdateUtil return ret; } + // -------------------------------------------- // + // UPDATE SPECIAL IDS + // -------------------------------------------- // + + public static void updateSpecialIds() + { + if (MConf.get().factionIdNone != null) + { + updateSpecialId(MConf.get().factionIdNone, Factions.ID_NONE); + MConf.get().factionIdNone = null; + } + + if (MConf.get().factionIdSafezone != null) + { + updateSpecialId(MConf.get().factionIdSafezone, Factions.ID_SAFEZONE); + MConf.get().factionIdSafezone = null; + } + + if (MConf.get().factionIdWarzone != null) + { + updateSpecialId(MConf.get().factionIdWarzone, Factions.ID_WARZONE); + MConf.get().factionIdWarzone = null; + } + + MConf.get().sync(); + } + + public static void updateSpecialId(String from, String to) + { + // Get the coll. + FactionColl coll = FactionColl.get(); + + // A faction may already be occupying the to-id. + // We must remove it to make space for renaming. + // This faction is simply an auto-created faction with no references yet. + coll.detachId(to); + coll.syncId(to); + + // Get the faction and detach it + Faction faction = coll.detachId(from); + coll.syncId(from); + + // Attach it + coll.attach(faction, to); + coll.syncId(to); + + // Update that config special config option. + if (MConf.get().defaultPlayerFactionId.equals(from)) + { + MConf.get().defaultPlayerFactionId = to; + MConf.get().sync(); + } + + // Update all board entries. + updateBoards(from, to); + } + + public static void updateBoards(String from, String to) + { + for (Board board : BoardColl.get().getAll()) + { + updateBoard(board, from, to); + } + } + + public static void updateBoard(Board board, String from, String to) + { + boolean changed = false; + for (TerritoryAccess ta : board.getMap().values()) + { + changed |= updateTerritoryAccess(ta, from, to); + } + if (changed) + { + board.changed(); + board.sync(); + } + } + + public static boolean updateTerritoryAccess(TerritoryAccess entity, String from, String to) + { + boolean changed = false; + changed |= updateTerritoryHostFactionId(entity, from, to); + changed |= updateTerritoryAccessFactionIds(entity, from, to); + return changed; + } + + public static boolean updateTerritoryHostFactionId(TerritoryAccess entity, String from, String to) + { + String before = entity.hostFactionId; + if (before == null) return false; + if (!before.equals(from)) return false; + + entity.hostFactionId = to; + return true; + } + + public static boolean updateTerritoryAccessFactionIds(TerritoryAccess entity, String from, String to) + { + // Before and After + Set before = entity.factionIds; + if (before == null) return false; + Set after = new LinkedHashSet(); + for (String id : before) + { + if (id == null) continue; + if (id.equals(from)) + { + after.add(to); + } + else + { + after.add(from); + } + } + + // NoChange + if (MUtil.equals(before, after)) return false; + + // Apply + entity.factionIds = after; + //entity.sync(); + return true; + } + }