Remove random custom special faction ids and create update process for it.
This commit is contained in:
		@@ -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;
 | 
			
		||||
		
 | 
			
		||||
 
 | 
			
		||||
@@ -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<String> factionIds;
 | 
			
		||||
	public Set<String> factionIds;
 | 
			
		||||
	public Set<String> getFactionIds() { return this.factionIds; }
 | 
			
		||||
	
 | 
			
		||||
	// TODO: remate private final
 | 
			
		||||
	// TODO: remake private final
 | 
			
		||||
	// default is empty
 | 
			
		||||
	public Set<String> playerIds;
 | 
			
		||||
	public Set<String> getPlayerIds() { return this.playerIds; }
 | 
			
		||||
 
 | 
			
		||||
@@ -90,7 +90,7 @@ public class Board extends Entity<Board> 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<Board> 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);
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -60,6 +60,11 @@ public class Faction extends Entity<Faction> 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<Faction> implements EconomyParticipator
 | 
			
		||||
	
 | 
			
		||||
	public boolean isNone()
 | 
			
		||||
	{
 | 
			
		||||
		return this.getId().equals(MConf.get().factionIdNone);
 | 
			
		||||
		return this.getId().equals(Factions.ID_NONE);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public boolean isNormal()
 | 
			
		||||
 
 | 
			
		||||
@@ -85,7 +85,7 @@ public class FactionColl extends Coll<Faction>
 | 
			
		||||
	
 | 
			
		||||
	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<Faction>
 | 
			
		||||
	
 | 
			
		||||
	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<Faction>
 | 
			
		||||
	
 | 
			
		||||
	public Faction getWarzone()
 | 
			
		||||
	{
 | 
			
		||||
		String id = MConf.get().factionIdWarzone;
 | 
			
		||||
		String id = Factions.ID_WARZONE;
 | 
			
		||||
		Faction faction = this.get(id);
 | 
			
		||||
		if (faction != null) return faction;
 | 
			
		||||
		
 | 
			
		||||
 
 | 
			
		||||
@@ -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<MConf>
 | 
			
		||||
	// -------------------------------------------- //
 | 
			
		||||
	// 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;
 | 
			
		||||
	
 | 
			
		||||
 
 | 
			
		||||
@@ -186,7 +186,7 @@ public class MPlayer extends SenderEntity<MPlayer> 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 
 | 
			
		||||
 
 | 
			
		||||
@@ -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<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;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user