Remove random custom special faction ids and create update process for it.

This commit is contained in:
Olof Larsson 2014-10-01 22:00:08 +02:00
parent cfc95f4895
commit 2f021ecc72
9 changed files with 164 additions and 16 deletions

View File

@ -13,7 +13,7 @@
</properties> </properties>
<build> <build>
<defaultGoal>clean install</defaultGoal> <defaultGoal>install</defaultGoal>
<finalName>Factions</finalName> <finalName>Factions</finalName>
<sourceDirectory>${basedir}/src/main/java/</sourceDirectory> <sourceDirectory>${basedir}/src/main/java/</sourceDirectory>
<resources> <resources>

View File

@ -58,6 +58,10 @@ public class Factions extends MassivePlugin
public final static String FACTION_MONEY_ACCOUNT_ID_PREFIX = "faction-"; 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 // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
@ -120,6 +124,7 @@ public class Factions extends MassivePlugin
MPlayerColl.get().init(); MPlayerColl.get().init();
FactionColl.get().init(); FactionColl.get().init();
BoardColl.get().init(); BoardColl.get().init();
UpdateUtil.updateSpecialIds();
FactionColl.get().reindexMPlayers(); FactionColl.get().reindexMPlayers();
this.databaseInitialized = true; this.databaseInitialized = true;

View File

@ -17,19 +17,21 @@ public class TerritoryAccess
// FIELDS: RAW // FIELDS: RAW
// -------------------------------------------- // // -------------------------------------------- //
// TODO: remake private final
// no default value, can't be null // no default value, can't be null
private final String hostFactionId; public String hostFactionId;
public String getHostFactionId() { return this.hostFactionId; } public String getHostFactionId() { return this.hostFactionId; }
// default is true // default is true
private final boolean hostFactionAllowed; private final boolean hostFactionAllowed;
public boolean isHostFactionAllowed() { return this.hostFactionAllowed; } public boolean isHostFactionAllowed() { return this.hostFactionAllowed; }
// TODO: remake private final
// default is empty // default is empty
private final Set<String> factionIds; public Set<String> factionIds;
public Set<String> getFactionIds() { return this.factionIds; } public Set<String> getFactionIds() { return this.factionIds; }
// TODO: remate private final // TODO: remake private final
// default is empty // default is empty
public Set<String> playerIds; public Set<String> playerIds;
public Set<String> getPlayerIds() { return this.playerIds; } public Set<String> getPlayerIds() { return this.playerIds; }

View File

@ -90,7 +90,7 @@ public class Board extends Entity<Board> implements BoardInterface
if (ps == null) return null; if (ps == null) return null;
ps = ps.getChunkCoords(true); ps = ps.getChunkCoords(true);
TerritoryAccess ret = this.map.get(ps); 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; return ret;
} }
@ -109,7 +109,7 @@ public class Board extends Entity<Board> implements BoardInterface
{ {
ps = ps.getChunkCoords(true); 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); this.map.remove(ps);
} }

View File

@ -60,6 +60,11 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
@Override @Override
public void preDetach(String id) 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); Money.set(this, null, 0);
// Clean the board // Clean the board
@ -130,7 +135,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public boolean isNone() public boolean isNone()
{ {
return this.getId().equals(MConf.get().factionIdNone); return this.getId().equals(Factions.ID_NONE);
} }
public boolean isNormal() public boolean isNormal()

View File

@ -85,7 +85,7 @@ public class FactionColl extends Coll<Faction>
public Faction getNone() public Faction getNone()
{ {
String id = MConf.get().factionIdNone; String id = Factions.ID_NONE;
Faction faction = this.get(id); Faction faction = this.get(id);
if (faction != null) return faction; if (faction != null) return faction;
@ -118,7 +118,7 @@ public class FactionColl extends Coll<Faction>
public Faction getSafezone() public Faction getSafezone()
{ {
String id = MConf.get().factionIdSafezone; String id = Factions.ID_SAFEZONE;
Faction faction = this.get(id); Faction faction = this.get(id);
if (faction != null) return faction; if (faction != null) return faction;
@ -151,7 +151,7 @@ public class FactionColl extends Coll<Faction>
public Faction getWarzone() public Faction getWarzone()
{ {
String id = MConf.get().factionIdWarzone; String id = Factions.ID_WARZONE;
Faction faction = this.get(id); Faction faction = this.get(id);
if (faction != null) return faction; if (faction != null) return faction;

View File

@ -6,7 +6,6 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
@ -88,16 +87,21 @@ public class MConf extends Entity<MConf>
// -------------------------------------------- // // -------------------------------------------- //
// SPECIAL FACTION IDS // 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 factionIdNone = null;
public String factionIdSafezone = UUID.randomUUID().toString(); public String factionIdSafezone = null;
public String factionIdWarzone = UUID.randomUUID().toString(); public String factionIdWarzone = null;
// -------------------------------------------- // // -------------------------------------------- //
// DEFAULTS // DEFAULTS
// -------------------------------------------- // // -------------------------------------------- //
public String defaultPlayerFactionId = this.factionIdNone; public String defaultPlayerFactionId = Factions.ID_NONE;
public Rel defaultPlayerRole = Rel.RECRUIT; public Rel defaultPlayerRole = Rel.RECRUIT;
public double defaultPlayerPower = 0.0; public double defaultPlayerPower = 0.0;

View File

@ -186,7 +186,7 @@ public class MPlayer extends SenderEntity<MPlayer> implements EconomyParticipato
public boolean hasFaction() 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 // This setter is so long because it search for default/null case and takes care of updating the faction member index

View File

@ -2,10 +2,17 @@ package com.massivecraft.factions.update;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import com.massivecraft.factions.Const; import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions; 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.factions.entity.MConf;
import com.massivecraft.massivecore.MassiveCore; import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.store.Coll; import com.massivecraft.massivecore.store.Coll;
@ -120,4 +127,129 @@ public class UpdateUtil
return ret; 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<String> before = entity.factionIds;
if (before == null) return false;
Set<String> after = new LinkedHashSet<String>();
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;
}
} }