2022-08-05 00:34:54 +02:00
|
|
|
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;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class TerritoryAccess {
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// FIELDS: RAW
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
// no default value, can't be null
|
|
|
|
private final String hostFactionId;
|
|
|
|
|
|
|
|
public String getHostFactionId() {
|
|
|
|
return this.hostFactionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default is true
|
|
|
|
private final boolean hostFactionAllowed;
|
|
|
|
|
|
|
|
public boolean isHostFactionAllowed() {
|
|
|
|
return this.hostFactionAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default is empty
|
|
|
|
private final Set<String> grantedIds;
|
|
|
|
|
|
|
|
public Set<String> getGrantedIds() {
|
|
|
|
return this.grantedIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default is null
|
|
|
|
private final String chunkName;
|
|
|
|
|
|
|
|
public String getChunkName() {
|
|
|
|
return this.chunkName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// FIELDS: VERSION
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public int version = 1;
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// FIELDS: DELTA
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
// The simple ones
|
|
|
|
public TerritoryAccess withHostFactionId(String hostFactionId) {
|
|
|
|
return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TerritoryAccess withHostFactionAllowed(Boolean hostFactionAllowed) {
|
|
|
|
return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TerritoryAccess withGrantedIds(Collection<String> grantedIds) {
|
|
|
|
return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TerritoryAccess withChunkName(String chunkName) {
|
|
|
|
return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The intermediate ones
|
|
|
|
public TerritoryAccess withGranted(MPermable mpermable, boolean with) {
|
|
|
|
return withGrantedId(mpermable.getId(), with);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TerritoryAccess withGrantedId(String grantedId, boolean with) {
|
|
|
|
if (this.getHostFactionId().equals(grantedId)) {
|
|
|
|
return valueOf(hostFactionId, with, grantedIds, chunkName);
|
|
|
|
}
|
|
|
|
|
|
|
|
Set<String> grantedIds = new MassiveSet<>(this.getGrantedIds());
|
|
|
|
if (with) {
|
|
|
|
grantedIds.add(grantedId);
|
|
|
|
} else {
|
|
|
|
grantedIds.remove(grantedId);
|
|
|
|
}
|
|
|
|
return valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// FIELDS: DIRECT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
// This method intentionally returns null if the Faction no longer exists.
|
|
|
|
// In Board we don't even return this TerritoryAccess if that is the case.
|
|
|
|
public Faction getHostFaction() {
|
|
|
|
return Faction.get(this.getHostFactionId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<MPermable> getGranteds() {
|
|
|
|
return MPerm.idsToMPermables(this.getGrantedIds());
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// PRIVATE CONSTRUCTOR
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
// Strictly for GSON only
|
|
|
|
private TerritoryAccess() {
|
|
|
|
this.hostFactionId = null;
|
|
|
|
this.hostFactionAllowed = true;
|
|
|
|
this.grantedIds = null;
|
|
|
|
this.chunkName = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private TerritoryAccess(String hostFactionId, Boolean hostFactionAllowed, Collection<String> grantedIds, String chunkName) {
|
2023-04-01 14:52:14 +02:00
|
|
|
if (hostFactionId == null) {
|
|
|
|
throw new NullPointerException("hostFactionId");
|
|
|
|
}
|
|
|
|
if (grantedIds == null) {
|
|
|
|
throw new NullPointerException("grantedIds");
|
|
|
|
}
|
2022-08-05 00:34:54 +02:00
|
|
|
this.hostFactionId = hostFactionId;
|
|
|
|
|
|
|
|
Set<String> grantedIdsInner = new MassiveSet<>();
|
|
|
|
grantedIdsInner.addAll(grantedIds);
|
|
|
|
if (grantedIdsInner.remove(hostFactionId)) {
|
|
|
|
hostFactionAllowed = true;
|
|
|
|
}
|
|
|
|
this.grantedIds = Collections.unmodifiableSet(grantedIdsInner);
|
|
|
|
|
|
|
|
this.hostFactionAllowed = (hostFactionAllowed == null || hostFactionAllowed);
|
|
|
|
|
|
|
|
this.chunkName = chunkName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// FACTORY: VALUE OF
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public static TerritoryAccess valueOf(String hostFactionId, Boolean hostFactionAllowed, Collection<String> grantedIds, String chunkName) {
|
2023-04-01 14:52:14 +02:00
|
|
|
if (hostFactionId == null) {
|
|
|
|
throw new NullPointerException("hostFactionId");
|
|
|
|
}
|
|
|
|
if (grantedIds == null) {
|
|
|
|
throw new NullPointerException("grantedIds");
|
|
|
|
}
|
2022-08-05 00:34:54 +02:00
|
|
|
return new TerritoryAccess(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TerritoryAccess valueOf(String hostFactionId) {
|
2023-04-01 14:52:14 +02:00
|
|
|
if (hostFactionId == null) {
|
|
|
|
throw new NullPointerException("hostFactionId");
|
|
|
|
}
|
2022-08-05 00:34:54 +02:00
|
|
|
return valueOf(hostFactionId, null, Collections.emptySet(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// INSTANCE METHODS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public boolean isGranted(MPermable permable) {
|
|
|
|
return isGranted(permable.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isGranted(String permableId) {
|
2023-04-01 14:52:14 +02:00
|
|
|
if (permableId.equals(this.hostFactionId)) {
|
|
|
|
return this.isHostFactionAllowed();
|
|
|
|
}
|
2022-08-05 00:34:54 +02:00
|
|
|
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.getGrantedIds().isEmpty() && this.getChunkName() == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// ACCESS STATUS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public AccessStatus getTerritoryAccess(MPlayer mplayer) {
|
2023-04-01 14:52:14 +02:00
|
|
|
if (isGranted(mplayer.getId())) {
|
|
|
|
return AccessStatus.ELEVATED;
|
|
|
|
}
|
|
|
|
if (isGranted(mplayer.getRank().getId())) {
|
|
|
|
return AccessStatus.ELEVATED;
|
|
|
|
}
|
2022-08-05 00:34:54 +02:00
|
|
|
|
|
|
|
if (this.getHostFactionId().equals(mplayer.getFaction().getId())) {
|
2023-04-01 14:52:14 +02:00
|
|
|
if (this.isHostFactionAllowed()) {
|
|
|
|
return AccessStatus.STANDARD;
|
|
|
|
} else {
|
|
|
|
return AccessStatus.DECREASED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isGranted(mplayer.getFaction().getId())) {
|
|
|
|
return AccessStatus.ELEVATED;
|
|
|
|
}
|
|
|
|
if (isGranted(RelationUtil.getRelationOfThatToMe(mplayer, this.getHostFaction()).toString())) {
|
|
|
|
return AccessStatus.ELEVATED;
|
2022-08-05 00:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return AccessStatus.STANDARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|