From d015f19181556e8aca6caefb539722dc3367077f Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Sun, 14 Apr 2019 23:18:10 +0200 Subject: [PATCH] Change the underlying mechanics of /f access --- src/com/massivecraft/factions/Factions.java | 5 +- .../factions/TerritoryAccess.java | 176 ++++++------------ .../adapter/TerritoryAccessAdapter.java | 33 ++-- .../cmd/CmdFactionsAccessAbstract.java | 3 +- .../cmd/CmdFactionsAccessFaction.java | 4 +- .../factions/cmd/CmdFactionsAccessPlayer.java | 4 +- .../factions/cmd/CmdFactionsClean.java | 7 +- .../factions/cmd/CmdFactionsPermShow.java | 24 +-- .../massivecraft/factions/entity/Board.java | 2 - .../massivecraft/factions/entity/Faction.java | 6 +- .../massivecraft/factions/entity/MPerm.java | 98 +++++----- ...MigratorTerritoryAccess001Restructure.java | 50 +++++ 12 files changed, 182 insertions(+), 230 deletions(-) create mode 100644 src/com/massivecraft/factions/entity/migrator/MigratorTerritoryAccess001Restructure.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 3ddf29e9..0be282b8 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -64,6 +64,7 @@ import com.massivecraft.factions.entity.migrator.MigratorMConf005Warps; import com.massivecraft.factions.entity.migrator.MigratorMPerm001Warps; import com.massivecraft.factions.entity.migrator.MigratorMPlayer001Ranks; import com.massivecraft.factions.entity.migrator.MigratorMPlayer002UsingAdminMode; +import com.massivecraft.factions.entity.migrator.MigratorTerritoryAccess001Restructure; import com.massivecraft.factions.event.EventFactionsChunkChangeType; import com.massivecraft.factions.integration.V18.IntegrationV18; import com.massivecraft.factions.integration.V19.IntegrationV19; @@ -131,6 +132,7 @@ public class Factions extends MassivePlugin MUtil.registerExtractor(String.class, "accountId", ExtractorFactionAccountId.get()); MigratorUtil.addJsonRepresentation(Board.class, Board.MAP_TYPE); + MigratorUtil.setTargetVersion(TerritoryAccess.class, 1); // Activate this.activateAuto(); @@ -154,7 +156,8 @@ public class Factions extends MassivePlugin MigratorMConf005Warps.class, MigratorMPerm001Warps.class, MigratorMPlayer001Ranks.class, - MigratorMPlayer002UsingAdminMode.class + MigratorMPlayer002UsingAdminMode.class, + MigratorTerritoryAccess001Restructure.class ); } diff --git a/src/com/massivecraft/factions/TerritoryAccess.java b/src/com/massivecraft/factions/TerritoryAccess.java index 6f49cfb6..6e4dbca1 100644 --- a/src/com/massivecraft/factions/TerritoryAccess.java +++ b/src/com/massivecraft/factions/TerritoryAccess.java @@ -1,7 +1,10 @@ package com.massivecraft.factions; import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPerm.MPermable; import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.massivecore.collections.MassiveSet; import java.util.Collection; @@ -21,60 +24,46 @@ public class TerritoryAccess // default is true private final boolean hostFactionAllowed; public boolean isHostFactionAllowed() { return this.hostFactionAllowed; } - + // default is empty - private final Set factionIds; - public Set getFactionIds() { return this.factionIds; } - - // default is empty - private final Set playerIds; - public Set getPlayerIds() { return this.playerIds; } - + private final Set grantedIds; + public Set getGrantedIds() { return this.grantedIds; } + + // -------------------------------------------- // + // FIELDS: VERSION + // -------------------------------------------- // + + public int version = 1; + // -------------------------------------------- // // FIELDS: DELTA // -------------------------------------------- // // The simple ones - public TerritoryAccess withHostFactionId(String hostFactionId) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } - public TerritoryAccess withHostFactionAllowed(Boolean hostFactionAllowed) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } - public TerritoryAccess withFactionIds(Collection factionIds) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } - public TerritoryAccess withPlayerIds(Collection playerIds) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); } + public TerritoryAccess withHostFactionId(String hostFactionId) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds); } + public TerritoryAccess withHostFactionAllowed(Boolean hostFactionAllowed) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds); } + public TerritoryAccess withGrantedIds(Collection factionIds) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds); } // The intermediate ones - public TerritoryAccess withFactionId(String factionId, boolean with) + public TerritoryAccess withGrantedId(String grantedId, boolean with) { - if (this.getHostFactionId().equals(factionId)) + if (this.getHostFactionId().equals(grantedId)) { - return valueOf(hostFactionId, with, factionIds, playerIds); + return valueOf(hostFactionId, with, grantedIds); } - Set factionIds = new MassiveSet<>(this.getFactionIds()); + Set grantedIds = new MassiveSet<>(this.getGrantedIds()); if (with) { - factionIds.add(factionId); + grantedIds.add(grantedId); } else { - factionIds.remove(factionId); + grantedIds.remove(grantedId); } - return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); + return valueOf(hostFactionId, hostFactionAllowed, grantedIds); } - - public TerritoryAccess withPlayerId(String playerId, boolean with) - { - playerId = playerId.toLowerCase(); - Set playerIds = new MassiveSet<>(this.getPlayerIds()); - if (with) - { - playerIds.add(playerId); - } - else - { - playerIds.remove(playerId); - } - return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); - } - + // -------------------------------------------- // // FIELDS: DIRECT // -------------------------------------------- // @@ -86,67 +75,36 @@ public class TerritoryAccess return Faction.get(this.getHostFactionId()); } - public Set getGrantedMPlayers() + public Set getGranteds() { - // Create - Set ret = new MassiveSet<>(); - - // Fill - for (String playerId : this.getPlayerIds()) - { - ret.add(MPlayer.get(playerId)); - } - - // Return - return ret; - } - - public Set getGrantedFactions() - { - // Create - Set ret = new MassiveSet<>(); - - // Fill - for (String factionId : this.getFactionIds()) - { - Faction faction = Faction.get(factionId); - if (faction == null) continue; - ret.add(faction); - } - - // Return - return ret; + return MPerm.idsToMPermables(this.getGrantedIds()); } // -------------------------------------------- // // PRIVATE CONSTRUCTOR // -------------------------------------------- // - - private TerritoryAccess(String hostFactionId, Boolean hostFactionAllowed, Collection factionIds, Collection playerIds) + + // Strictly for GSON only + private TerritoryAccess() { - if (hostFactionId == null) throw new IllegalArgumentException("hostFactionId was null"); + this.hostFactionId = null; + this.hostFactionAllowed = true; + this.grantedIds = null; + } + + private TerritoryAccess(String hostFactionId, Boolean hostFactionAllowed, Collection grantedIds) + { + if (hostFactionId == null) throw new NullPointerException("hostFactionId"); + if (grantedIds == null) throw new NullPointerException("grantedIds"); this.hostFactionId = hostFactionId; - Set factionIdsInner = new MassiveSet<>(); - if (factionIds != null) + Set grantedIdsInner = new MassiveSet<>(); + grantedIdsInner.addAll(grantedIds); + if (grantedIdsInner.remove(hostFactionId)) { - factionIdsInner.addAll(factionIds); - if (factionIdsInner.remove(hostFactionId)) - { - hostFactionAllowed = true; - } + hostFactionAllowed = true; } - this.factionIds = Collections.unmodifiableSet(factionIdsInner); - - Set playerIdsInner = new MassiveSet<>(); - if (playerIds != null) - { - for (String playerId : playerIds) - { - playerIdsInner.add(playerId.toLowerCase()); - } - } - this.playerIds = Collections.unmodifiableSet(playerIdsInner); + this.grantedIds = Collections.unmodifiableSet(grantedIdsInner); this.hostFactionAllowed = (hostFactionAllowed == null || hostFactionAllowed); } @@ -155,45 +113,35 @@ public class TerritoryAccess // FACTORY: VALUE OF // -------------------------------------------- // - public static TerritoryAccess valueOf(String hostFactionId, Boolean hostFactionAllowed, Collection factionIds, Collection playerIds) + public static TerritoryAccess valueOf(String hostFactionId, Boolean hostFactionAllowed, Collection grantedIds) { - return new TerritoryAccess(hostFactionId, hostFactionAllowed, factionIds, playerIds); + return new TerritoryAccess(hostFactionId, hostFactionAllowed, grantedIds); } public static TerritoryAccess valueOf(String hostFactionId) { - return valueOf(hostFactionId, null, null, null); + return valueOf(hostFactionId, null, Collections.emptySet()); } // -------------------------------------------- // // INSTANCE METHODS // -------------------------------------------- // - - public boolean isFactionGranted(Faction faction) + + public boolean isGranted(MPermable permable) { - String factionId = faction.getId(); - - if (this.getHostFactionId().equals(factionId)) - { - return this.isHostFactionAllowed(); - } - - return this.getFactionIds().contains(factionId); + return isGranted(permable.getId()); } - - // 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 isMPlayerGranted(MPlayer mplayer) + + public boolean isGranted(String permableId) { - String mplayerId = mplayer.getId(); - return this.getPlayerIds().contains(mplayerId); + return this.getGrantedIds().contains(permableId); } // A "default" TerritoryAccess could be serialized as a simple string only. // The host faction is still allowed (default) and no faction or player has been granted explicit access (default). public boolean isDefault() { - return this.isHostFactionAllowed() && this.getFactionIds().isEmpty() && this.getPlayerIds().isEmpty(); + return this.isHostFactionAllowed() && this.getGrantedIds().isEmpty(); } // -------------------------------------------- // @@ -202,20 +150,14 @@ public class TerritoryAccess public AccessStatus getTerritoryAccess(MPlayer mplayer) { - if (this.isMPlayerGranted(mplayer)) return AccessStatus.ELEVATED; + if (isGranted(mplayer.getId())) return AccessStatus.ELEVATED; + if (isGranted(mplayer.getFaction().getId())) return AccessStatus.ELEVATED; + if (isGranted(mplayer.getRank().getId())) return AccessStatus.ELEVATED; + if (isGranted(RelationUtil.getRelationOfThatToMe(mplayer, this.getHostFaction()).toString())) return AccessStatus.ELEVATED; - String factionId = mplayer.getFaction().getId(); - if (this.getFactionIds().contains(factionId)) return AccessStatus.ELEVATED; - - if (this.getHostFactionId().equals(factionId) && !this.isHostFactionAllowed()) return AccessStatus.DECREASED; + if (this.getHostFactionId().equals(mplayer.getFaction().getId()) && !this.isHostFactionAllowed()) return AccessStatus.DECREASED; return AccessStatus.STANDARD; } - - @Deprecated - public Boolean hasTerritoryAccess(MPlayer mplayer) - { - return this.getTerritoryAccess(mplayer).hasAccess(); - } - + } diff --git a/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java b/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java index 596eb098..afb8ff39 100644 --- a/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java +++ b/src/com/massivecraft/factions/adapter/TerritoryAccessAdapter.java @@ -11,6 +11,7 @@ import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.reflect.TypeToken; import com.massivecraft.factions.TerritoryAccess; +import com.massivecraft.massivecore.store.migrator.MigratorUtil; import java.lang.reflect.Type; import java.util.Set; @@ -23,8 +24,9 @@ public class TerritoryAccessAdapter implements JsonDeserializer public static final String HOST_FACTION_ID = "hostFactionId"; public static final String HOST_FACTION_ALLOWED = "hostFactionAllowed"; - public static final String FACTION_IDS = "factionIds"; - public static final String PLAYER_IDS = "playerIds"; + /*public static final String FACTION_IDS = "factionIds"; + public static final String PLAYER_IDS = "playerIds";*/ + public static final String GRANTED_IDS = "grantedIds"; public static final Type SET_OF_STRING_TYPE = new TypeToken>(){}.getType(); @@ -55,8 +57,7 @@ public class TerritoryAccessAdapter implements JsonDeserializer // Prepare variables String hostFactionId = null; Boolean hostFactionAllowed = null; - Set factionIds = null; - Set playerIds = null; + Set grantedIds = null; // Read variables (test old values first) JsonElement element = null; @@ -68,16 +69,11 @@ public class TerritoryAccessAdapter implements JsonDeserializer element = obj.get("open"); if (element == null) element = obj.get(HOST_FACTION_ALLOWED); if (element != null) hostFactionAllowed = element.getAsBoolean(); + + element = obj.get(GRANTED_IDS); + if (element != null) grantedIds = context.deserialize(element, SET_OF_STRING_TYPE); - element = obj.get("factions"); - if (element == null) element = obj.get(FACTION_IDS); - if (element != null) factionIds = context.deserialize(element, SET_OF_STRING_TYPE); - - element = obj.get("fplayers"); - if (element == null) element = obj.get(PLAYER_IDS); - if (element != null) playerIds = context.deserialize(element, SET_OF_STRING_TYPE); - - return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); + return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, grantedIds); } @Override @@ -101,16 +97,13 @@ public class TerritoryAccessAdapter implements JsonDeserializer obj.addProperty(HOST_FACTION_ALLOWED, src.isHostFactionAllowed()); } - if (!src.getFactionIds().isEmpty()) + if (!src.getGrantedIds().isEmpty()) { - obj.add(FACTION_IDS, context.serialize(src.getFactionIds(), SET_OF_STRING_TYPE)); - } - - if (!src.getPlayerIds().isEmpty()) - { - obj.add(PLAYER_IDS, context.serialize(src.getPlayerIds(), SET_OF_STRING_TYPE)); + obj.add(GRANTED_IDS, context.serialize(src.getGrantedIds(), SET_OF_STRING_TYPE)); } + obj.add(MigratorUtil.VERSION_FIELD_NAME, new JsonPrimitive(src.version)); + return obj; } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java index 3d21c36a..798b15b2 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java @@ -59,8 +59,7 @@ public abstract class CmdFactionsAccessAbstract extends FactionsCommand msg("Host Faction: %s", hostFaction.describeTo(msender, true)); msg("Host Faction Allowed: %s", ta.isHostFactionAllowed() ? Txt.parse("TRUE") : Txt.parse("FALSE")); - msg("Granted Players: %s", describeRelationParticipators(ta.getGrantedMPlayers(), msender)); - msg("Granted Factions: %s", describeRelationParticipators(ta.getGrantedFactions(), msender)); + msg("Granted to: %s", CmdFactionsPermShow.permablesToDisplayString(ta.getGranteds(), msender)); } public static String describeRelationParticipators(Collection relationParticipators, RelationParticipator observer) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java index 4bff373c..8a8103ce 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java @@ -29,13 +29,13 @@ public class CmdFactionsAccessFaction extends CmdFactionsAccessAbstract { // Args Faction faction = this.readArg(); - boolean newValue = this.readArg(!ta.isFactionGranted(faction)); + boolean newValue = this.readArg(!ta.isGranted(faction)); // MPerm if (!MPerm.getPermAccess().has(msender, hostFaction, true)) return; // Apply - ta = ta.withFactionId(faction.getId(), newValue); + ta = ta.withGrantedId(faction.getId(), newValue); BoardColl.get().setTerritoryAccessAt(chunk, ta); // Inform diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java index b3c68bd1..04698fbf 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java @@ -29,13 +29,13 @@ public class CmdFactionsAccessPlayer extends CmdFactionsAccessAbstract { // Args MPlayer mplayer = this.readArg(); - boolean newValue = this.readArg(!ta.isMPlayerGranted(mplayer)); + boolean newValue = this.readArg(!ta.isGranted(mplayer)); // MPerm if (!MPerm.getPermAccess().has(msender, hostFaction, true)) return; // Apply - ta = ta.withPlayerId(mplayer.getId(), newValue); + ta = ta.withGrantedId(mplayer.getId(), newValue); BoardColl.get().setTerritoryAccessAt(chunk, ta); // Inform diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsClean.java b/src/com/massivecraft/factions/cmd/CmdFactionsClean.java index 087ce8aa..480dbb0e 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsClean.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsClean.java @@ -7,6 +7,7 @@ import com.massivecraft.factions.entity.BoardColl; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.Invitation; +import com.massivecraft.factions.entity.MPerm; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.entity.MPlayerColl; import com.massivecraft.massivecore.MassiveException; @@ -144,11 +145,11 @@ public class CmdFactionsClean extends FactionsCommand TerritoryAccess territoryAccess = entry.getValue(); boolean changed = false; - for (String factionId : territoryAccess.getFactionIds()) + for (String grantedIds : territoryAccess.getGrantedIds()) { - if (FactionColl.get().containsId(factionId)) continue; + if (MPerm.idToMPermableOptional(grantedIds).isPresent()) continue; - territoryAccess = territoryAccess.withFactionId(factionId, false); + territoryAccess = territoryAccess.withGrantedId(grantedIds, false); ret += 1; changed = true; } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPermShow.java b/src/com/massivecraft/factions/cmd/CmdFactionsPermShow.java index 09ee4f50..0cc716ce 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPermShow.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPermShow.java @@ -1,15 +1,11 @@ package com.massivecraft.factions.cmd; -import com.massivecraft.factions.Rel; import com.massivecraft.factions.cmd.type.TypeFaction; import com.massivecraft.factions.cmd.type.TypeMPerm; import com.massivecraft.factions.entity.Faction; -import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.MPerm; import com.massivecraft.factions.entity.MPerm.MPermable; import com.massivecraft.factions.entity.MPlayer; -import com.massivecraft.factions.entity.MPlayerColl; -import com.massivecraft.factions.entity.Rank; import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.util.Txt; @@ -62,26 +58,10 @@ public class CmdFactionsPermShow extends FactionsCommand msg("In %s permission %s is granted to %s.", faction.describeTo(msender), mperm.getDesc(true, false), permableNames); } + @Deprecated public static MPerm.MPermable idToMPermable(String id) { - MPlayer mplayer = MPlayerColl.get().get(id, false); - if (mplayer != null) return mplayer; - - Faction faction = Faction.get(id); - if (faction != null) return faction; - - for (Faction f : FactionColl.get().getAll()) - { - Rank rank = f.getRank(id); - if (rank != null) return rank; - } - - if (Rel.ALLY.name().equalsIgnoreCase(id)) return Rel.ALLY; - if (Rel.TRUCE.name().equalsIgnoreCase(id)) return Rel.TRUCE; - if (Rel.NEUTRAL.name().equalsIgnoreCase(id)) return Rel.NEUTRAL; - if (Rel.ENEMY.name().equalsIgnoreCase(id)) return Rel.ENEMY; - - throw new RuntimeException(id); + return MPerm.idToMPermable(id); } public static String permablesToDisplayString(Collection permables, Object watcherObject) diff --git a/src/com/massivecraft/factions/entity/Board.java b/src/com/massivecraft/factions/entity/Board.java index 424ac616..8ac59350 100644 --- a/src/com/massivecraft/factions/entity/Board.java +++ b/src/com/massivecraft/factions/entity/Board.java @@ -52,8 +52,6 @@ public class Board extends Entity implements BoardInterface // FIELDS // -------------------------------------------- // - // TODO: Make TerritoryAccess immutable. - private ConcurrentSkipListMap map; public Map getMap() { return Collections.unmodifiableMap(this.map); } public Map getMapRaw() { return this.map; } diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 2032f7a2..349a2dc2 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -6,7 +6,6 @@ import com.massivecraft.factions.FactionsIndex; import com.massivecraft.factions.FactionsParticipator; import com.massivecraft.factions.Rel; import com.massivecraft.factions.RelationParticipator; -import com.massivecraft.factions.cmd.CmdFactionsPermShow; import com.massivecraft.factions.entity.MPerm.MPermable; import com.massivecraft.factions.predicate.PredicateCommandSenderFaction; import com.massivecraft.factions.predicate.PredicateMPlayerRank; @@ -867,9 +866,7 @@ public class Faction extends Entity implements FactionsParticipator, MP public Set getPermittedPermables(String permId) { - return getPermitted(permId).stream() - .map(CmdFactionsPermShow::idToMPermable) - .collect(Collectors.toSet()); + return MPerm.idsToMPermables(getPermitted(permId)); } public Set getPermittedPermables(MPerm mperm) @@ -882,6 +879,7 @@ public class Faction extends Entity implements FactionsParticipator, MP if (permableId == null) throw new NullPointerException("permableId"); if (permId == null) throw new NullPointerException("permId"); + // TODO: Isn't this section redundant and just a copy of that from getPermitted? Set permables = this.perms.get(permId); if (permables == null) { diff --git a/src/com/massivecraft/factions/entity/MPerm.java b/src/com/massivecraft/factions/entity/MPerm.java index db15a747..092b0714 100644 --- a/src/com/massivecraft/factions/entity/MPerm.java +++ b/src/com/massivecraft/factions/entity/MPerm.java @@ -16,15 +16,19 @@ import com.massivecraft.massivecore.comparator.ComparatorSmart; import com.massivecraft.massivecore.predicate.PredicateIsRegistered; import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.store.Entity; +import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Comparator; import java.util.List; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; public class MPerm extends Entity implements Prioritized, Registerable, Named { @@ -414,7 +418,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N } // -------------------------------------------- // - // UTIL: ASCII + // PERMABLES // -------------------------------------------- // public static List getPermables(Faction faction) @@ -430,60 +434,6 @@ public class MPerm extends Entity implements Prioritized, Registerable, N return list; } - - /*public static String getStateHeaders(Faction faction) - { - if (faction == null) throw new NullPointerException("faction"); - - String ret = ""; - for (MPermable permable : getPermables(faction)) - { - ret += permable.getColor().toString(); - ret += permable.getShortName().toUpperCase(); - ret += " "; - } - - return ret; - } - - public String getStateInfo(Faction faction, boolean withDesc) - { - if (faction == null) throw new NullPointerException("faction"); - - String ret = ""; - - for (MPermable permable : getPermables(faction)) - { - if (faction.isPermablePermitted(permable, this)) - { - ret += "YES"; - } - else - { - ret += "NOO"; - } - ret += " "; - } - - String color = ""; - if (!this.isVisible()) - { - color = ""; - } - else if (this.isEditable()) - { - color = ""; - } - - ret += color; - ret += this.getName(); - - ret = Txt.parse(ret); - - if (withDesc) ret += " " + this.getDesc(); - - return ret; - }*/ public interface MPermable extends Named, Identified { @@ -504,5 +454,43 @@ public class MPerm extends Entity implements Prioritized, Registerable, N String getDisplayName(Object senderObject); } + + public static Set idsToMPermables(Collection ids) + { + return ids.stream() + .map(MPerm::idToMPermable) + .collect(Collectors.toSet()); + } + + public static MPermable idToMPermable(String id) + { + return idToMPermableOptional(id).orElseThrow(() -> new RuntimeException(id)); + } + + public static Optional idToMPermableOptional(String id) + { + MPlayer mplayer = MPlayerColl.get().get(id, false); + if (mplayer != null) return Optional.of(mplayer); + + // Workaround for registered senders + // Players ussually have a power, which makes sure they are in the coll + if (IdUtil.getRegistryIdToSender().containsKey(id)) return Optional.of(MPlayerColl.get().get(id, true)); + + Faction faction = Faction.get(id); + if (faction != null) return Optional.of(faction); + + for (Faction f : FactionColl.get().getAll()) + { + Rank rank = f.getRank(id); + if (rank != null) return Optional.of(rank); + } + + if (Rel.ALLY.name().equalsIgnoreCase(id)) return Optional.of(Rel.ALLY); + if (Rel.TRUCE.name().equalsIgnoreCase(id)) return Optional.of(Rel.TRUCE); + if (Rel.NEUTRAL.name().equalsIgnoreCase(id)) return Optional.of(Rel.NEUTRAL); + if (Rel.ENEMY.name().equalsIgnoreCase(id)) return Optional.of(Rel.ENEMY); + + return Optional.empty(); + } } diff --git a/src/com/massivecraft/factions/entity/migrator/MigratorTerritoryAccess001Restructure.java b/src/com/massivecraft/factions/entity/migrator/MigratorTerritoryAccess001Restructure.java new file mode 100644 index 00000000..01f0836d --- /dev/null +++ b/src/com/massivecraft/factions/entity/migrator/MigratorTerritoryAccess001Restructure.java @@ -0,0 +1,50 @@ +package com.massivecraft.factions.entity.migrator; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.massivecraft.factions.TerritoryAccess; +import com.massivecraft.factions.adapter.TerritoryAccessAdapter; +import com.massivecraft.massivecore.store.migrator.MigratorRoot; + +public class MigratorTerritoryAccess001Restructure extends MigratorRoot +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static MigratorTerritoryAccess001Restructure i = new MigratorTerritoryAccess001Restructure(); + public static MigratorTerritoryAccess001Restructure get() { return i; } + private MigratorTerritoryAccess001Restructure() + { + super(TerritoryAccess.class); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void migrateInner(JsonObject entity) + { + JsonElement factionIds = entity.remove("factionIds"); + JsonElement playerIds = entity.remove("playerIds"); + + JsonArray grantedIds = new JsonArray(); + + if (factionIds != null && factionIds.isJsonArray()) + { + JsonArray factionIdsArr = factionIds.getAsJsonArray(); + grantedIds.addAll(factionIdsArr); + } + + if (playerIds != null && playerIds.isJsonArray()) + { + JsonArray playerIdsArr = playerIds.getAsJsonArray(); + grantedIds.addAll(playerIdsArr); + } + + if (grantedIds.size() > 0) entity.add(TerritoryAccessAdapter.GRANTED_IDS, grantedIds); + } + +}