From 2c58aed1ba98b6b54d11ec1651ab10bb910a18e9 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 25 Mar 2017 00:47:01 +0100 Subject: [PATCH] Factions Cleanup - Database lazy cleaning --- src/com/massivecraft/factions/Factions.java | 42 +++--- .../factions/TerritoryAccess.java | 59 ++++---- .../cmd/CmdFactionsAccessFaction.java | 2 +- .../factions/cmd/CmdFactionsAccessPlayer.java | 2 +- .../factions/cmd/CmdFactionsClean.java | 142 ++++++++++++++++-- .../massivecraft/factions/entity/Board.java | 26 ---- .../factions/entity/BoardColl.java | 13 -- .../factions/entity/BoardInterface.java | 1 - .../massivecraft/factions/entity/Faction.java | 59 +++----- .../factions/entity/FactionColl.java | 20 --- .../massivecraft/factions/entity/MPlayer.java | 76 ++++++---- .../factions/entity/MPlayerColl.java | 27 ---- 12 files changed, 246 insertions(+), 223 deletions(-) diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 78061158..8e7bbeea 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -95,10 +95,6 @@ public class Factions extends MassivePlugin // FIELDS // -------------------------------------------- // - // Database Initialized - private boolean databaseInitialized; - public boolean isDatabaseInitialized() { return this.databaseInitialized; } - // Mixins @Deprecated public PowerMixin getPowerMixin() { return PowerMixin.get(); } @Deprecated public void setPowerMixin(PowerMixin powerMixin) { PowerMixin.get().setInstance(powerMixin); } @@ -117,28 +113,28 @@ public class Factions extends MassivePlugin // Register Faction accountId Extractor // TODO: Perhaps this should be placed in the econ integration somewhere? MUtil.registerExtractor(String.class, "accountId", ExtractorFactionAccountId.get()); - - // Initialize Database - // MConf should always be activated first for all plugins. It's simply a standard. The config should have no dependencies. - // MFlag and MPerm are both dependency free. - // Next we activate Faction, MPlayer and Board. The order is carefully chosen based on foreign keys and indexing direction. - // MPlayer --> Faction - // We actually only have an index that we maintain for the MPlayer --> Faction one. - // The Board could currently be activated in any order but the current placement is an educated guess. - // In the future we might want to find all chunks from the faction or something similar. - // We also have the /f access system where the player can be granted specific access, possibly supporting the idea of such a reverse index. - this.databaseInitialized = false; - MigratorMConf001EnumerationUtil.get().setActive(true); - MConfColl.get().setActive(true); - MFlagColl.get().setActive(true); - MPermColl.get().setActive(true); - FactionColl.get().setActive(true); - MPlayerColl.get().setActive(true); - BoardColl.get().setActive(true); - this.databaseInitialized = true; // Activate this.activate( + // Migrator + MigratorMConf001EnumerationUtil.class, + + // Coll + // MConf should always be activated first for all plugins. It's simply a standard. The config should have no dependencies. + // MFlag and MPerm are both dependency free. + // Next we activate Faction, MPlayer and Board. The order is carefully chosen based on foreign keys and indexing direction. + // MPlayer --> Faction + // We actually only have an index that we maintain for the MPlayer --> Faction one. + // The Board could currently be activated in any order but the current placement is an educated guess. + // In the future we might want to find all chunks from the faction or something similar. + // We also have the /f access system where the player can be granted specific access, possibly supporting the idea of such a reverse index. + MConfColl.class, + MFlagColl.class, + MPermColl.class, + FactionColl.class, + MPlayerColl.class, + BoardColl.class, + // Command CmdFactions.class, diff --git a/src/com/massivecraft/factions/TerritoryAccess.java b/src/com/massivecraft/factions/TerritoryAccess.java index 397c489d..f449b483 100644 --- a/src/com/massivecraft/factions/TerritoryAccess.java +++ b/src/com/massivecraft/factions/TerritoryAccess.java @@ -3,13 +3,11 @@ package com.massivecraft.factions; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.collections.MassiveSet; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.Set; -import java.util.TreeSet; public class TerritoryAccess { @@ -17,18 +15,16 @@ public class TerritoryAccess // FIELDS: RAW // -------------------------------------------- // - // TODO: Remake private final // no default value, can't be null - public String hostFactionId; + private final 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 - public Set factionIds; + private final Set factionIds; public Set getFactionIds() { return this.factionIds; } // default is empty @@ -53,7 +49,7 @@ public class TerritoryAccess return valueOf(hostFactionId, with, factionIds, playerIds); } - Set factionIds = new HashSet<>(this.getFactionIds()); + Set factionIds = new MassiveSet<>(this.getFactionIds()); if (with) { factionIds.add(factionId); @@ -68,7 +64,7 @@ public class TerritoryAccess public TerritoryAccess withPlayerId(String playerId, boolean with) { playerId = playerId.toLowerCase(); - Set playerIds = new HashSet<>(this.getPlayerIds()); + Set playerIds = new MassiveSet<>(this.getPlayerIds()); if (with) { playerIds.add(playerId); @@ -80,17 +76,6 @@ public class TerritoryAccess return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } - // The complex ones - public TerritoryAccess toggleFactionId(String factionId) - { - return this.withFactionId(factionId, !this.isFactionIdGranted(factionId)); - } - - public TerritoryAccess togglePlayerId(String playerId) - { - return this.withPlayerId(playerId, !this.isPlayerIdGranted(playerId)); - } - // -------------------------------------------- // // FIELDS: DIRECT // -------------------------------------------- // @@ -100,23 +85,33 @@ public class TerritoryAccess return FactionColl.get().get(this.getHostFactionId()); } - public LinkedHashSet getGrantedMPlayers() + public Set getGrantedMPlayers() { - LinkedHashSet ret = new LinkedHashSet<>(); + // Create + Set ret = new MassiveSet<>(); + + // Fill for (String playerId : this.getPlayerIds()) { ret.add(MPlayer.get(playerId)); } + + // Return return ret; } - public LinkedHashSet getGrantedFactions() + public Set getGrantedFactions() { - LinkedHashSet ret = new LinkedHashSet<>(); + // Create + Set ret = new MassiveSet<>(); + + // Fill for (String factionId : this.getFactionIds()) { ret.add(FactionColl.get().get(factionId)); } + + // Return return ret; } @@ -129,7 +124,7 @@ public class TerritoryAccess if (hostFactionId == null) throw new IllegalArgumentException("hostFactionId was null"); this.hostFactionId = hostFactionId; - Set factionIdsInner = new TreeSet<>(); + Set factionIdsInner = new MassiveSet<>(); if (factionIds != null) { factionIdsInner.addAll(factionIds); @@ -140,7 +135,7 @@ public class TerritoryAccess } this.factionIds = Collections.unmodifiableSet(factionIdsInner); - Set playerIdsInner = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + Set playerIdsInner = new MassiveSet<>(); if (playerIds != null) { for (String playerId : playerIds) @@ -171,20 +166,24 @@ public class TerritoryAccess // INSTANCE METHODS // -------------------------------------------- // - public boolean isFactionIdGranted(String factionId) + public boolean isFactionGranted(Faction faction) { + String factionId = faction.getId(); + if (this.getHostFactionId().equals(factionId)) { return this.isHostFactionAllowed(); } + return this.getFactionIds().contains(factionId); } // Note that the player can have access without being specifically granted. // The player could for example be a member of a granted faction. - public boolean isPlayerIdGranted(String playerId) + public boolean isMPlayerGranted(MPlayer mplayer) { - return this.getPlayerIds().contains(playerId); + String mplayerId = mplayer.getId(); + return this.getPlayerIds().contains(mplayerId); } // A "default" TerritoryAccess could be serialized as a simple string only. @@ -203,7 +202,7 @@ public class TerritoryAccess // null means standard access public Boolean hasTerritoryAccess(MPlayer mplayer) { - if (this.getPlayerIds().contains(mplayer.getId())) return true; + if (this.isMPlayerGranted(mplayer)) return true; String factionId = mplayer.getFaction().getId(); if (this.getFactionIds().contains(factionId)) return true; diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java index 90d5b13f..4bff373c 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java @@ -29,7 +29,7 @@ public class CmdFactionsAccessFaction extends CmdFactionsAccessAbstract { // Args Faction faction = this.readArg(); - boolean newValue = this.readArg(!ta.isFactionIdGranted(faction.getId())); + boolean newValue = this.readArg(!ta.isFactionGranted(faction)); // MPerm if (!MPerm.getPermAccess().has(msender, hostFaction, true)) return; diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java index f376cf02..b3c68bd1 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java @@ -29,7 +29,7 @@ public class CmdFactionsAccessPlayer extends CmdFactionsAccessAbstract { // Args MPlayer mplayer = this.readArg(); - boolean newValue = this.readArg(!ta.isPlayerIdGranted(mplayer.getId())); + boolean newValue = this.readArg(!ta.isMPlayerGranted(mplayer)); // MPerm if (!MPerm.getPermAccess().has(msender, hostFaction, true)) return; diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsClean.java b/src/com/massivecraft/factions/cmd/CmdFactionsClean.java index dda1fd45..11211517 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsClean.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsClean.java @@ -1,10 +1,21 @@ package com.massivecraft.factions.cmd; +import com.massivecraft.factions.Rel; +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.MPlayer; import com.massivecraft.factions.entity.MPlayerColl; import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.util.Txt; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map.Entry; + public class CmdFactionsClean extends FactionsCommand { // -------------------------------------------- // @@ -15,22 +26,133 @@ public class CmdFactionsClean extends FactionsCommand public void perform() throws MassiveException { Object message; - - // Apply - int chunks = BoardColl.get().clean(); - int players = MPlayerColl.get().clean(); + int count; // Title message = Txt.titleize("Factions Cleaner Results"); message(message); - // Chunks - message = Txt.parse("%d chunks were cleaned.", chunks); - message(message); + // Yada + cleanMessage(this.cleanPlayer(), "player"); + cleanMessage(this.cleanFactionInvites(), "faction invites"); + cleanMessage(this.cleanFactionRelationWhishes(), "faction relation whishes"); + cleanMessage(this.cleanBoardHost(), "chunk whole"); + cleanMessage(this.cleanBoardGrant(), "chunk access"); + } + + // -------------------------------------------- // + // CLEAN + // -------------------------------------------- // + + private void cleanMessage(int count, String name) + { + msg("%d %s", count, name); + } + + private int cleanPlayer() + { + int ret = 0; - // Players - message = Txt.parse("%d players were cleaned.", players); - message(message); + for (MPlayer mplayer : MPlayerColl.get().getAll()) + { + if (!mplayer.isFactionOrphan()) continue; + + mplayer.resetFactionData(); + ret += 1; + } + + return ret; + } + + private int cleanFactionInvites() + { + int ret = 0; + + for (Faction faction : FactionColl.get().getAll()) + { + Collection invitedPlayerIds = faction.getInvitedPlayerIds(); + if (invitedPlayerIds.isEmpty()) continue; + + ret += invitedPlayerIds.size(); + invitedPlayerIds.clear(); + faction.changed(); + } + + return ret; + } + + private int cleanFactionRelationWhishes() + { + int ret = 0; + + for (Faction faction : FactionColl.get().getAll()) + { + for (Iterator> iterator = faction.getRelationWishes().entrySet().iterator(); iterator.hasNext();) + { + Entry entry = iterator.next(); + String factionId = entry.getKey(); + if (FactionColl.get().containsId(factionId)) continue; + + iterator.remove(); + ret += 1; + faction.changed(); + } + } + + return ret; + } + + private int cleanBoardHost() + { + int ret = 0; + + for (Board board : BoardColl.get().getAll()) + { + for (Entry entry : board.getMap().entrySet()) + { + PS ps = entry.getKey(); + TerritoryAccess territoryAccess = entry.getValue(); + String factionId = territoryAccess.getHostFactionId(); + + if (FactionColl.get().containsId(factionId)) continue; + + board.removeAt(ps); + ret += 1; + } + } + + return ret; + } + + private int cleanBoardGrant() + { + int ret = 0; + + for (Board board : BoardColl.get().getAll()) + { + for (Entry entry : board.getMap().entrySet()) + { + PS ps = entry.getKey(); + TerritoryAccess territoryAccess = entry.getValue(); + boolean changed = false; + + for (String factionId : territoryAccess.getFactionIds()) + { + if (FactionColl.get().containsId(factionId)) continue; + + territoryAccess = territoryAccess.withFactionId(factionId, false); + ret += 1; + changed = true; + } + + if (changed) + { + board.setTerritoryAccessAt(ps, territoryAccess); + } + } + } + + return ret; } } diff --git a/src/com/massivecraft/factions/entity/Board.java b/src/com/massivecraft/factions/entity/Board.java index 479fff36..0720f628 100644 --- a/src/com/massivecraft/factions/entity/Board.java +++ b/src/com/massivecraft/factions/entity/Board.java @@ -157,32 +157,6 @@ public class Board extends Entity implements BoardInterface } } - // Removes orphaned foreign keys - @Override - public int clean() - { - int ret = 0; - - if (!FactionColl.get().isActive()) return ret; - - for (Entry entry : this.map.entrySet()) - { - TerritoryAccess territoryAccess = entry.getValue(); - String factionId = territoryAccess.getHostFactionId(); - - if (FactionColl.get().containsId(factionId)) continue; - - PS ps = entry.getKey(); - this.removeAt(ps); - - ret += 0; - - Factions.get().log("Board cleaner removed "+factionId+" from "+ps); - } - - return ret; - } - // CHUNKS @Override diff --git a/src/com/massivecraft/factions/entity/BoardColl.java b/src/com/massivecraft/factions/entity/BoardColl.java index 23c74db3..ad908f7b 100644 --- a/src/com/massivecraft/factions/entity/BoardColl.java +++ b/src/com/massivecraft/factions/entity/BoardColl.java @@ -117,19 +117,6 @@ public class BoardColl extends Coll implements BoardInterface } } - @Override - public int clean() - { - int ret = 0; - - for (Board board : this.getAll()) - { - ret += board.clean(); - } - - return ret; - } - // CHUNKS @Override diff --git a/src/com/massivecraft/factions/entity/BoardInterface.java b/src/com/massivecraft/factions/entity/BoardInterface.java index b31993f7..c23d93a3 100644 --- a/src/com/massivecraft/factions/entity/BoardInterface.java +++ b/src/com/massivecraft/factions/entity/BoardInterface.java @@ -21,7 +21,6 @@ public interface BoardInterface // REMOVE void removeAt(PS ps); void removeAll(Faction faction); - int clean(); // CHUNKS Set getChunks(Faction faction); diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index e44b1366..693518ac 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -10,9 +10,10 @@ import com.massivecraft.factions.predicate.PredicateMPlayerRole; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.massivecore.collections.MassiveList; +import com.massivecraft.massivecore.collections.MassiveMap; import com.massivecraft.massivecore.collections.MassiveMapDef; -import com.massivecraft.massivecore.collections.MassiveTreeSetDef; -import com.massivecraft.massivecore.comparator.ComparatorCaseInsensitive; +import com.massivecraft.massivecore.collections.MassiveSet; +import com.massivecraft.massivecore.collections.MassiveSetDef; import com.massivecraft.massivecore.mixin.MixinMessage; import com.massivecraft.massivecore.money.Money; import com.massivecraft.massivecore.predicate.Predicate; @@ -28,18 +29,14 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.TreeSet; public class Faction extends Entity implements FactionsParticipator { @@ -83,9 +80,7 @@ public class Faction extends Entity implements FactionsParticipator @Override public void preDetach(String id) { - // The database must be fully inited. - // We may move factions around during upgrades. - if (!Factions.get().isDatabaseInitialized()) return; + if (!this.isLive()) return; // NOTE: Existence check is required for compatibility with some plugins. // If they have money ... @@ -94,12 +89,6 @@ public class Faction extends Entity implements FactionsParticipator // ... remove it. Money.set(this, null, 0); } - - // Clean the board - BoardColl.get().clean(); - - // Clean the mplayers - MPlayerColl.get().clean(); } // -------------------------------------------- // @@ -146,7 +135,7 @@ public class Faction extends Entity implements FactionsParticipator // This is the ids of the invited players. // They are actually "senderIds" since you can invite "@console" to your faction. // Null means no one is invited - private MassiveTreeSetDef invitedPlayerIds = new MassiveTreeSetDef<>(ComparatorCaseInsensitive.get()); + private MassiveSetDef invitedPlayerIds = new MassiveSetDef<>(); // The keys in this map are factionIds. // Null means no special relation whishes. @@ -252,8 +241,7 @@ public class Faction extends Entity implements FactionsParticipator if (target != null) { target = target.trim(); - // This code should be kept for a while to clean out the previous default text that was actually stored in the database. - if (target.length() == 0 || target.equals("Default faction description :(")) + if (target.isEmpty()) { target = null; } @@ -293,7 +281,7 @@ public class Faction extends Entity implements FactionsParticipator if (target != null) { target = target.trim(); - if (target.length() == 0) + if (target.isEmpty()) { target = null; } @@ -463,7 +451,7 @@ public class Faction extends Entity implements FactionsParticipator // RAW - public TreeSet getInvitedPlayerIds() + public Set getInvitedPlayerIds() { return this.invitedPlayerIds; } @@ -471,14 +459,7 @@ public class Faction extends Entity implements FactionsParticipator public void setInvitedPlayerIds(Collection invitedPlayerIds) { // Clean input - MassiveTreeSetDef target = new MassiveTreeSetDef<>(ComparatorCaseInsensitive.get()); - if (invitedPlayerIds != null) - { - for (String invitedPlayerId : invitedPlayerIds) - { - target.add(invitedPlayerId.toLowerCase()); - } - } + MassiveSetDef target = new MassiveSetDef<>(invitedPlayerIds); // Detect Nochange if (MUtil.equals(this.invitedPlayerIds, target)) return; @@ -504,7 +485,7 @@ public class Faction extends Entity implements FactionsParticipator public boolean setInvited(String playerId, boolean invited) { - List invitedPlayerIds = new ArrayList<>(this.getInvitedPlayerIds()); + List invitedPlayerIds = new MassiveList<>(this.getInvitedPlayerIds()); boolean ret; if (invited) { @@ -516,7 +497,6 @@ public class Faction extends Entity implements FactionsParticipator } this.setInvitedPlayerIds(invitedPlayerIds); return ret; - } public void setInvited(MPlayer mplayer, boolean invited) @@ -526,13 +506,14 @@ public class Faction extends Entity implements FactionsParticipator public List getInvitedMPlayers() { - List mplayers = new ArrayList<>(); + List mplayers = new MassiveList<>(); for (String id : this.getInvitedPlayerIds()) { MPlayer mplayer = MPlayer.get(id); mplayers.add(mplayer); } + return mplayers; } @@ -604,7 +585,7 @@ public class Faction extends Entity implements FactionsParticipator public Map getFlags() { // We start with default values ... - Map ret = new LinkedHashMap<>(); + Map ret = new MassiveMap<>(); for (MFlag mflag : MFlag.getAll()) { ret.put(mflag, mflag.isStandard()); @@ -638,7 +619,7 @@ public class Faction extends Entity implements FactionsParticipator public void setFlags(Map flags) { - Map flagIds = new LinkedHashMap<>(); + Map flagIds = new MassiveMap<>(); for (Entry entry : flags.entrySet()) { flagIds.put(entry.getKey().getId(), entry.getValue()); @@ -730,10 +711,10 @@ public class Faction extends Entity implements FactionsParticipator public Map> getPerms() { // We start with default values ... - Map> ret = new LinkedHashMap<>(); + Map> ret = new MassiveMap<>(); for (MPerm mperm : MPerm.getAll()) { - ret.put(mperm, new LinkedHashSet<>(mperm.getStandard())); + ret.put(mperm, new MassiveSet<>(mperm.getStandard())); } // ... and if anything is explicitly set we use that info ... @@ -755,7 +736,7 @@ public class Faction extends Entity implements FactionsParticipator MPerm mperm = MPerm.get(id); if (mperm == null) continue; - ret.put(mperm, new LinkedHashSet<>(entry.getValue())); + ret.put(mperm, new MassiveSet<>(entry.getValue())); } return ret; @@ -763,7 +744,7 @@ public class Faction extends Entity implements FactionsParticipator public void setPerms(Map> perms) { - Map> permIds = new LinkedHashMap<>(); + Map> permIds = new MassiveMap<>(); for (Entry> entry : perms.entrySet()) { permIds.put(entry.getKey().getId(), entry.getValue()); @@ -1054,7 +1035,7 @@ public class Faction extends Entity implements FactionsParticipator public List getOnlineCommandSenders() { // Create Ret - List ret = new ArrayList<>(); + List ret = new MassiveList<>(); // Fill Ret for (CommandSender sender : IdUtil.getLocalSenders()) @@ -1074,7 +1055,7 @@ public class Faction extends Entity implements FactionsParticipator public List getOnlinePlayers() { // Create Ret - List ret = new ArrayList<>(); + List ret = new MassiveList<>(); // Fill Ret for (Player player : MUtil.getOnlinePlayers()) diff --git a/src/com/massivecraft/factions/entity/FactionColl.java b/src/com/massivecraft/factions/entity/FactionColl.java index 49a3c346..61a8019a 100644 --- a/src/com/massivecraft/factions/entity/FactionColl.java +++ b/src/com/massivecraft/factions/entity/FactionColl.java @@ -47,26 +47,6 @@ public class FactionColl extends Coll this.createSpecialFactions(); } - @Override - public Faction get(Object oid) - { - Faction ret = super.get(oid); - - // We should only trigger automatic clean if the whole database system is initialized. - // A cleaning can only be successful if all data is available. - // Example Reason: When creating the special factions for the first time "createSpecialFactions" a clean would be triggered otherwise. - if (ret == null && Factions.get().isDatabaseInitialized()) - { - String message = Txt.parse("Non existing factionId %s requested. Cleaning all boards and mplayers.", this.fixId(oid)); - Factions.get().log(message); - - BoardColl.get().clean(); - MPlayerColl.get().clean(); - } - - return ret; - } - // -------------------------------------------- // // SPECIAL FACTIONS // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/MPlayer.java b/src/com/massivecraft/factions/entity/MPlayer.java index 6da4b2fc..6e52fc4e 100644 --- a/src/com/massivecraft/factions/entity/MPlayer.java +++ b/src/com/massivecraft/factions/entity/MPlayer.java @@ -27,6 +27,7 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.lang.ref.WeakReference; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; @@ -166,13 +167,20 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // Null means default specified in MConf. private Boolean territoryInfoTitles = null; - // The id for the faction this player is currently autoclaiming for. + // The Faction this player is currently autoclaiming for. // Null means the player isn't auto claiming. // NOTE: This field will not be saved to the database ever. - private transient Faction autoClaimFaction = null; + private transient WeakReference autoClaimFaction = new WeakReference<>(null); - public Faction getAutoClaimFaction() { return this.autoClaimFaction; } - public void setAutoClaimFaction(Faction autoClaimFaction) { this.autoClaimFaction = autoClaimFaction; } + public Faction getAutoClaimFaction() + { + if (this.isFactionOrphan()) return null; + Faction ret = this.autoClaimFaction.get(); + if (ret == null) return null; + if (ret.detached()) return null; + return ret; + } + public void setAutoClaimFaction(Faction autoClaimFaction) { this.autoClaimFaction = new WeakReference<>(autoClaimFaction); } // Does the player have /f seechunk activated? // NOTE: This field will not be saved to the database ever. @@ -225,31 +233,43 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // -------------------------------------------- // // FIELD: factionId // -------------------------------------------- // - - @Deprecated - public String getDefaultFactionId() + + private Faction getFactionInternal() { - return MConf.get().defaultPlayerFactionId; + String effectiveFactionId = this.convertGet(this.factionId, MConf.get().defaultPlayerFactionId); + return Faction.get(effectiveFactionId); + } + + public boolean isFactionOrphan() + { + return this.getFactionInternal() == null; } - // This method never returns null + @Deprecated public String getFactionId() { - if (this.factionId == null) return MConf.get().defaultPlayerFactionId; - return this.factionId; + return this.getFaction().getId(); } // This method never returns null public Faction getFaction() { - Faction ret = Faction.get(this.getFactionId()); - if (ret == null) ret = Faction.get(MConf.get().defaultPlayerFactionId); + Faction ret; + + ret = this.getFactionInternal(); + + // Adopt orphans + if (ret == null) + { + ret = FactionColl.get().getNone(); + } + return ret; } - + public boolean hasFaction() { - return !this.getFactionId().equals(Factions.ID_NONE); + return !this.getFaction().isNone(); } // This setter is so long because it search for default/null case and takes @@ -284,14 +304,10 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // FIELD: role // -------------------------------------------- // - @Deprecated - public Rel getDefaultRole() - { - return MConf.get().defaultPlayerRole; - } - public Rel getRole() { + if (this.isFactionOrphan()) return Rel.RECRUIT; + if (this.role == null) return MConf.get().defaultPlayerRole; return this.role; } @@ -314,15 +330,20 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // -------------------------------------------- // // FIELD: title // -------------------------------------------- // - + // TODO: Improve upon the has and get stuff. + // TODO: Has should depend on get. Visualisation should be done elsewhere. + public boolean hasTitle() { - return this.title != null; + return !this.isFactionOrphan() && this.title != null; } public String getTitle() { + if (this.isFactionOrphan()) return NOTITLE; + if (this.hasTitle()) return this.title; + return NOTITLE; } @@ -339,15 +360,6 @@ public class MPlayer extends SenderEntity implements FactionsParticipat } } - // NOTE: That we parse the title here is considered part of the 1.8 --> - // 2.0 migration. - // This should be removed once the migration phase is considered to be - // over. - if (target != null) - { - target = Txt.parse(target); - } - // Detect Nochange if (MUtil.equals(this.title, target)) return; diff --git a/src/com/massivecraft/factions/entity/MPlayerColl.java b/src/com/massivecraft/factions/entity/MPlayerColl.java index 51d1dc6c..3c8c864a 100644 --- a/src/com/massivecraft/factions/entity/MPlayerColl.java +++ b/src/com/massivecraft/factions/entity/MPlayerColl.java @@ -2,8 +2,6 @@ package com.massivecraft.factions.entity; import com.massivecraft.factions.Factions; import com.massivecraft.massivecore.store.SenderColl; -import com.massivecraft.massivecore.util.IdUtil; -import com.massivecraft.massivecore.util.Txt; import org.bukkit.Bukkit; import java.util.Collection; @@ -31,31 +29,6 @@ public class MPlayerColl extends SenderColl // EXTRAS // -------------------------------------------- // - public int clean() - { - int ret = 0; - - if (!FactionColl.get().isActive()) return ret; - - // For each player ... - for (MPlayer mplayer : this.getAll()) - { - // ... who doesn't have a valid faction ... - String factionId = mplayer.getFactionId(); - if (FactionColl.get().containsId(factionId)) continue; - - // ... reset their faction data ... - mplayer.resetFactionData(); - ret += 1; - - // ... and log. - String message = Txt.parse("Reset data for %s . Unknown factionId %s", mplayer.getDisplayName(IdUtil.getConsole()), factionId); - Factions.get().log(message); - } - - return ret; - } - public void considerRemovePlayerMillis() { // If the config option is 0 or below that means the server owner want it disabled.