Pass 1 at reworking the Territory Access system.

This commit is contained in:
Olof Larsson 2013-04-29 10:23:39 +02:00
parent 03684c78f1
commit 66b4eb4b92
13 changed files with 478 additions and 332 deletions

View File

@ -10,9 +10,10 @@ permissions:
# -------------------------------------------- # # -------------------------------------------- #
# THE REAL NODES # THE REAL NODES
# -------------------------------------------- # # -------------------------------------------- #
factions.access: {description: grant territory access, with the proper fperm, default: false} factions.access: {description: manage access, with the proper fperm, default: false}
factions.access.any: {description: grant territory access for another faction, default: false} factions.access.faction: {description: grant faction access, with the proper fperm, default: false}
factions.access.view: {description: view territory access, default: false} factions.access.player: {description: grant player access, with the proper fperm, default: false}
factions.access.view: {description: view access, default: false}
factions.admin: {description: enable adminmode, default: false} factions.admin: {description: enable adminmode, default: false}
factions.autoclaim: {description: autoclaim when walking, default: false} factions.autoclaim: {description: autoclaim when walking, default: false}
factions.claim: {description: claim land where you stand, default: false} factions.claim: {description: claim land where you stand, default: false}
@ -35,6 +36,7 @@ permissions:
factions.leave: {description: leave your faction, default: false} factions.leave: {description: leave your faction, default: false}
factions.list: {description: list all factions, default: false} factions.list: {description: list all factions, default: false}
factions.map: {description: show territory map, default: false} factions.map: {description: show territory map, default: false}
factions.money: {description: manage faction money, default: false}
factions.money.balance: {description: show faction money, default: false} factions.money.balance: {description: show faction money, default: false}
factions.money.balance.any: {description: show another factions money, default: false} factions.money.balance.any: {description: show another factions money, default: false}
factions.money.deposit: {description: deposit to faction, default: false} factions.money.deposit: {description: deposit to faction, default: false}
@ -64,7 +66,8 @@ permissions:
factions.*: factions.*:
children: children:
factions.access: true factions.access: true
factions.access.any: true factions.access.faction: true
factions.access.player: true
factions.access.view: true factions.access.view: true
factions.admin: true factions.admin: true
factions.autoclaim: true factions.autoclaim: true
@ -88,7 +91,14 @@ permissions:
factions.leave: true factions.leave: true
factions.list: true factions.list: true
factions.map: true factions.map: true
factions.money.*: true factions.money: true
factions.money.balance: true
factions.money.balance.any: true
factions.money.deposit: true
factions.money.f2f: true
factions.money.f2p: true
factions.money.p2f: true
factions.money.withdraw: true
factions.officer: true factions.officer: true
factions.officer.any: true factions.officer.any: true
factions.open: true factions.open: true
@ -105,15 +115,6 @@ permissions:
factions.unclaim: true factions.unclaim: true
factions.unclaimall: true factions.unclaimall: true
factions.version: true factions.version: true
factions.money.*:
children:
factions.money.balance: true
factions.money.balance.any: true
factions.money.deposit: true
factions.money.f2f: true
factions.money.f2p: true
factions.money.p2f: true
factions.money.withdraw: true
# -------------------------------------------- # # -------------------------------------------- #
# KITS # KITS
# -------------------------------------------- # # -------------------------------------------- #
@ -144,12 +145,14 @@ permissions:
factions.kit.rank0: factions.kit.rank0:
default: false default: false
children: children:
factions.create: true
factions.access: true factions.access: true
factions.access.faction: true
factions.access.player: true
factions.access.view: true factions.access.view: true
factions.autoclaim: true factions.autoclaim: true
factions.claim: true factions.claim: true
factions.claim.radius: true factions.claim.radius: true
factions.create: true
factions.demote: true factions.demote: true
factions.description: true factions.description: true
factions.disband: true factions.disband: true
@ -163,7 +166,14 @@ permissions:
factions.leave: true factions.leave: true
factions.list: true factions.list: true
factions.map: true factions.map: true
factions.money.*: true factions.money: true
factions.money.balance: true
factions.money.balance.any: true
factions.money.deposit: true
factions.money.f2f: true
factions.money.f2p: true
factions.money.p2f: true
factions.money.withdraw: true
factions.officer: true factions.officer: true
factions.open: true factions.open: true
factions.perm: true factions.perm: true

View File

@ -11,8 +11,9 @@ public enum Perm
// -------------------------------------------- // // -------------------------------------------- //
ACCESS("access"), ACCESS("access"),
ACCESS_ANY("access.any"),
ACCESS_VIEW("access.view"), ACCESS_VIEW("access.view"),
ACCESS_PLAYER("access.player"),
ACCESS_FACTION("access.faction"),
ADMIN("admin"), ADMIN("admin"),
AUTOCLAIM("autoclaim"), AUTOCLAIM("autoclaim"),
CLAIM("claim"), CLAIM("claim"),
@ -35,6 +36,7 @@ public enum Perm
LEAVE("leave"), LEAVE("leave"),
LIST("list"), LIST("list"),
MAP("map"), MAP("map"),
MONEY("money"),
MONEY_BALANCE("money.balance"), MONEY_BALANCE("money.balance"),
MONEY_BALANCE_ANY("money.balance.any"), MONEY_BALANCE_ANY("money.balance.any"),
MONEY_DEPOSIT("money.deposit"), MONEY_DEPOSIT("money.deposit"),

View File

@ -1,14 +1,21 @@
package com.massivecraft.factions; package com.massivecraft.factions;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.UPlayer; import com.massivecraft.factions.entity.UPlayer;
import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColls; import com.massivecraft.factions.entity.FactionColls;
import com.massivecraft.factions.entity.UPlayerColl;
import com.massivecraft.factions.entity.UPlayerColls;
public class TerritoryAccess public class TerritoryAccess
{ {
@ -16,167 +23,236 @@ public class TerritoryAccess
// FIELDS: RAW // FIELDS: RAW
// -------------------------------------------- // // -------------------------------------------- //
private String hostFactionId; // no default value, can't be null
private final String hostFactionId;
public String getHostFactionId() { return this.hostFactionId; } public String getHostFactionId() { return this.hostFactionId; }
public void setHostFactionId(String hostFactionId) { this.hostFactionId = hostFactionId; }
private boolean hostFactionAllowed = true; // default is true
private final boolean hostFactionAllowed;
public boolean isHostFactionAllowed() { return this.hostFactionAllowed; } public boolean isHostFactionAllowed() { return this.hostFactionAllowed; }
public void setHostFactionAllowed(boolean hostFactionAllowed) { this.hostFactionAllowed = hostFactionAllowed; }
private Set<String> factionIds = new LinkedHashSet<String>(); // default is empty
private final Set<String> factionIds;
public Set<String> getFactionIds() { return this.factionIds; } public Set<String> getFactionIds() { return this.factionIds; }
private Set<String> fplayerIds = new LinkedHashSet<String>(); // default is empty
public Set<String> getFPlayerIds() { return this.fplayerIds; } private final Set<String> playerIds;
public Set<String> getPlayerIds() { return this.playerIds; }
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // FIELDS: DELTA
// -------------------------------------------- // // -------------------------------------------- //
public TerritoryAccess(String hostFactionId) // 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<String> factionIds) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); }
public TerritoryAccess withPlayerIds(Collection<String> playerIds) { return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds); }
// The intermediate ones
public TerritoryAccess withFactionId(String factionId, boolean with)
{ {
if (this.getHostFactionId().equals(factionId))
{
return valueOf(hostFactionId, with, factionIds, playerIds);
}
Set<String> factionIds = new HashSet<String>(this.getFactionIds());
if (with)
{
factionIds.add(factionId);
}
else
{
factionIds.remove(factionId);
}
return valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds);
}
public TerritoryAccess withPlayerId(String playerId, boolean with)
{
playerId = playerId.toLowerCase();
Set<String> playerIds = new HashSet<String>(this.getPlayerIds());
if (with)
{
playerIds.add(playerId);
}
else
{
playerIds.remove(playerId);
}
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: UNIVERSED
// -------------------------------------------- //
public Faction getHostFaction(Object universe)
{
return FactionColls.get().get(universe).get(this.getHostFactionId());
}
public LinkedHashSet<UPlayer> getGrantedUPlayers(Object universe)
{
LinkedHashSet<UPlayer> ret = new LinkedHashSet<UPlayer>();
UPlayerColl coll = UPlayerColls.get().get(universe);
for (String playerId : this.getPlayerIds())
{
ret.add(coll.get(playerId));
}
return ret;
}
public LinkedHashSet<Faction> getGrantedFactions(Object universe)
{
LinkedHashSet<Faction> ret = new LinkedHashSet<Faction>();
FactionColl coll = FactionColls.get().get(universe);
for (String factionId : this.getFactionIds())
{
ret.add(coll.get(factionId));
}
return ret;
}
// -------------------------------------------- //
// PRIVATE CONSTRUCTOR
// -------------------------------------------- //
private TerritoryAccess(String hostFactionId, Boolean hostFactionAllowed, Collection<String> factionIds, Collection<String> playerIds)
{
if (hostFactionId == null) throw new IllegalArgumentException("hostFactionId was null");
this.hostFactionId = hostFactionId; this.hostFactionId = hostFactionId;
}
public TerritoryAccess() Set<String> factionIdsInner = new TreeSet<String>();
if (factionIds != null)
{ {
factionIdsInner.addAll(factionIds);
if (factionIdsInner.remove(hostFactionId))
{
hostFactionAllowed = true;
}
}
this.factionIds = Collections.unmodifiableSet(factionIdsInner);
Set<String> playerIdsInner = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
if (playerIds != null)
{
for (String playerId : playerIds)
{
playerIdsInner.add(playerId.toLowerCase());
}
}
this.playerIds = Collections.unmodifiableSet(playerIdsInner);
this.hostFactionAllowed = (hostFactionAllowed == null || hostFactionAllowed);
} }
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS: UTILS // FACTORY: VALUE OF
// -------------------------------------------- // // -------------------------------------------- //
public void addFaction(String factionId) { this.getFactionIds().add(factionId); } public static TerritoryAccess valueOf(String hostFactionId, Boolean hostFactionAllowed, Collection<String> factionIds, Collection<String> playerIds)
public void addFaction(Faction faction) { this.addFaction(faction.getId()); }
public void removeFaction(String factionId) { this.getFactionIds().remove(factionId); }
public void removeFaction(Faction faction) { this.removeFaction(faction.getId()); }
// return true if faction was added, false if it was removed
public boolean toggleFaction(String factionId)
{ {
// if the host faction, special handling return new TerritoryAccess(hostFactionId, hostFactionAllowed, factionIds, playerIds);
if (this.doesHostFactionMatch(factionId))
{
this.hostFactionAllowed ^= true;
return this.hostFactionAllowed;
} }
if (this.getFactionIds().contains(factionId)) public static TerritoryAccess valueOf(String hostFactionId)
{ {
this.removeFaction(factionId); return valueOf(hostFactionId, null, null, null);
return false;
}
this.addFaction(factionId);
return true;
}
public boolean toggleFaction(Faction faction)
{
return this.toggleFaction(faction.getId());
}
public void addFPlayer(String fplayerID) { this.getFPlayerIds().add(fplayerID); }
public void addFPlayer(UPlayer fplayer) { this.addFPlayer(fplayer.getId()); }
public void removeFPlayer(String fplayerID) { this.getFPlayerIds().remove(fplayerID); }
public void removeFPlayer(UPlayer fplayer) { this.removeFPlayer(fplayer.getId()); }
public boolean toggleFPlayer(String fplayerID)
{
if (this.getFPlayerIds().contains(fplayerID))
{
this.removeFPlayer(fplayerID);
return false;
}
this.addFPlayer(fplayerID);
return true;
}
public boolean toggleFPlayer(UPlayer fplayer)
{
return this.toggleFPlayer(fplayer.getId());
}
public boolean doesHostFactionMatch(Object testSubject)
{
if (testSubject instanceof String)
return hostFactionId.equals((String)testSubject);
else if (testSubject instanceof CommandSender)
return hostFactionId.equals(UPlayer.get(testSubject).getFactionId());
else if (testSubject instanceof UPlayer)
return hostFactionId.equals(((UPlayer)testSubject).getFactionId());
else if (testSubject instanceof Faction)
return hostFactionId.equals(((Faction)testSubject).getId());
return false;
} }
// -------------------------------------------- // // -------------------------------------------- //
// UTILS // INSTANCE METHODS
// -------------------------------------------- // // -------------------------------------------- //
// considered "default" if host faction is still allowed and nobody has been granted access public boolean isFactionIdGranted(String factionId)
{
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)
{
return this.getPlayerIds().contains(playerId);
}
// 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() public boolean isDefault()
{ {
return this.hostFactionAllowed && this.factionIds.isEmpty() && this.fplayerIds.isEmpty(); return this.isHostFactionAllowed() && this.getFactionIds().isEmpty() && this.getPlayerIds().isEmpty();
} }
public void setDefault(String factionId) // TODO: This looks like an extractor in my eyes.
// TODO: Perhaps create a factionId extractor?
public boolean doesHostFactionMatch(Object testSubject)
{ {
this.hostFactionId = factionId; String factionId = null;
this.hostFactionAllowed = true; if (testSubject instanceof String)
this.factionIds.clear(); {
this.fplayerIds.clear(); factionId = (String)testSubject;
}
else if (testSubject instanceof CommandSender)
{
factionId = UPlayer.get(testSubject).getFactionId();
}
else if (testSubject instanceof UPlayer)
{
factionId = ((UPlayer)testSubject).getFactionId();
}
else if (testSubject instanceof Faction)
{
factionId = ((Faction)testSubject).getId();
}
return this.getHostFactionId().equals(factionId);
} }
public String factionList(Faction universeExtractable) // -------------------------------------------- //
{ // DERPINGTON CHECKS
StringBuilder list = new StringBuilder(); // -------------------------------------------- //
for (String factionID : factionIds)
{
if (list.length() > 0)
list.append(", ");
list.append(FactionColls.get().get(universeExtractable).get(factionID).getName());
}
return list.toString();
}
public String fplayerList()
{
StringBuilder list = new StringBuilder();
for (String fplayerID : fplayerIds)
{
if (list.length() > 0)
list.append(", ");
list.append(fplayerID);
}
return list.toString();
}
// these return false if not granted explicit access, or true if granted explicit access (in FPlayer or Faction lists) // these return false if not granted explicit access, or true if granted explicit access (in FPlayer or Faction lists)
// they do not take into account hostFactionAllowed, which will need to be checked separately (as to not override FPerms which are denied for faction members and such) // they do not take into account hostFactionAllowed, which will need to be checked separately (as to not override FPerms which are denied for faction members and such)
public boolean subjectHasAccess(Object testSubject) public boolean subjectHasAccess(Object testSubject)
{ {
if (testSubject instanceof Player) if (testSubject instanceof Player)
{
return fPlayerHasAccess(UPlayer.get(testSubject)); return fPlayerHasAccess(UPlayer.get(testSubject));
}
else if (testSubject instanceof UPlayer) else if (testSubject instanceof UPlayer)
{
return fPlayerHasAccess((UPlayer)testSubject); return fPlayerHasAccess((UPlayer)testSubject);
}
else if (testSubject instanceof Faction) else if (testSubject instanceof Faction)
{
return factionHasAccess((Faction)testSubject); return factionHasAccess((Faction)testSubject);
}
return false; return false;
} }
public boolean fPlayerHasAccess(UPlayer fplayer) public boolean fPlayerHasAccess(UPlayer fplayer)
{ {
if (factionHasAccess(fplayer.getFactionId())) return true; return this.isPlayerIdGranted(fplayer.getId()) || this.isFactionIdGranted(fplayer.getFaction().getId());
return fplayerIds.contains(fplayer.getId());
} }
public boolean factionHasAccess(Faction faction) public boolean factionHasAccess(Faction faction)
{ {
return factionHasAccess(faction.getId()); return this.isFactionIdGranted(faction.getId());
}
public boolean factionHasAccess(String factionId)
{
return factionIds.contains(factionId);
} }
// this should normally only be checked after running subjectHasAccess() or fPlayerHasAccess() above to see if they have access explicitly granted // this should normally only be checked after running subjectHasAccess() or fPlayerHasAccess() above to see if they have access explicitly granted
@ -186,24 +262,4 @@ public class TerritoryAccess
return ( ! this.isHostFactionAllowed() && this.doesHostFactionMatch(testSubject) && ! FPerm.ACCESS.has(testSubject, hostFaction, false)); return ( ! this.isHostFactionAllowed() && this.doesHostFactionMatch(testSubject) && ! FPerm.ACCESS.has(testSubject, hostFaction, false));
} }
//----------------------------------------------//
// COMPARISON
//----------------------------------------------//
@Override
public int hashCode()
{
return this.hostFactionId.hashCode();
}
@Override
public boolean equals(Object obj)
{
if (obj == this) return true;
if (!(obj instanceof TerritoryAccess)) return false;
TerritoryAccess that = (TerritoryAccess) obj;
return this.hostFactionId.equals(that.hostFactionId) && this.hostFactionAllowed == that.hostFactionAllowed && this.factionIds == that.factionIds && this.fplayerIds == that.fplayerIds;
}
} }

View File

@ -1,10 +1,8 @@
package com.massivecraft.factions.adapter; package com.massivecraft.factions.adapter;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Iterator; import java.util.Set;
import java.util.logging.Level;
import com.massivecraft.mcore.xlib.gson.JsonArray;
import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext; import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.mcore.xlib.gson.JsonDeserializer; import com.massivecraft.mcore.xlib.gson.JsonDeserializer;
import com.massivecraft.mcore.xlib.gson.JsonElement; import com.massivecraft.mcore.xlib.gson.JsonElement;
@ -13,20 +11,22 @@ import com.massivecraft.mcore.xlib.gson.JsonParseException;
import com.massivecraft.mcore.xlib.gson.JsonPrimitive; import com.massivecraft.mcore.xlib.gson.JsonPrimitive;
import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; import com.massivecraft.mcore.xlib.gson.JsonSerializationContext;
import com.massivecraft.mcore.xlib.gson.JsonSerializer; import com.massivecraft.mcore.xlib.gson.JsonSerializer;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.TerritoryAccess; import com.massivecraft.factions.TerritoryAccess;
public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>, JsonSerializer<TerritoryAccess> public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>, JsonSerializer<TerritoryAccess>
{ {
//----------------------------------------------// // -------------------------------------------- //
// CONSTANTS // CONSTANTS
//----------------------------------------------// // -------------------------------------------- //
public static final String ID = "ID"; public static final String HOST_FACTION_ID = "hostFactionId";
public static final String OPEN = "open"; public static final String HOST_FACTION_ALLOWED = "hostFactionAllowed";
public static final String FACTIONS = "factions"; public static final String FACTION_IDS = "factionIds";
public static final String FPLAYERS = "fplayers"; public static final String PLAYER_IDS = "playerIds";
public static final Type SET_OF_STRING_TYPE = new TypeToken<Set<String>>(){}.getType();
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
@ -42,95 +42,76 @@ public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>
@Override @Override
public TerritoryAccess deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException public TerritoryAccess deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{ {
try // isDefault <=> simple hostFactionId string
{
// if stored as simple string, it's just the faction ID and default values are to be used
if (json.isJsonPrimitive()) if (json.isJsonPrimitive())
{ {
String factionID = json.getAsString(); String hostFactionId = json.getAsString();
return new TerritoryAccess(factionID); return TerritoryAccess.valueOf(hostFactionId);
} }
// otherwise, it's stored as an object and all data should be present // Otherwise object
JsonObject obj = json.getAsJsonObject(); JsonObject obj = json.getAsJsonObject();
if (obj == null) return null;
String factionID = obj.get(ID).getAsString(); // Prepare variables
boolean hostAllowed = obj.get(OPEN).getAsBoolean(); String hostFactionId = null;
JsonArray factions = obj.getAsJsonArray(FACTIONS); Boolean hostFactionAllowed = null;
JsonArray fplayers = obj.getAsJsonArray(FPLAYERS); Set<String> factionIds = null;
Set<String> playerIds = null;
TerritoryAccess access = new TerritoryAccess(factionID); // Read variables (test old values first)
access.setHostFactionAllowed(hostAllowed); JsonElement element = null;
Iterator<JsonElement> iter = factions.iterator(); element = obj.get("ID");
while (iter.hasNext()) if (element == null) element = obj.get(HOST_FACTION_ID);
{ hostFactionId = element.getAsString();
access.addFaction(iter.next().getAsString());
}
iter = fplayers.iterator(); element = obj.get("open");
while (iter.hasNext()) if (element == null) element = obj.get(HOST_FACTION_ALLOWED);
{ if (element != null) hostFactionAllowed = element.getAsBoolean();
access.addFPlayer(iter.next().getAsString());
}
return access; 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");
catch (Exception ex) if (element == null) element = obj.get(PLAYER_IDS);
{ if (element != null) playerIds = context.deserialize(element, SET_OF_STRING_TYPE);
ex.printStackTrace();
Factions.get().log(Level.WARNING, "Error encountered while deserializing TerritoryAccess data."); return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, factionIds, playerIds);
return null;
}
} }
@Override @Override
public JsonElement serialize(TerritoryAccess src, Type typeOfSrc, JsonSerializationContext context) public JsonElement serialize(TerritoryAccess src, Type typeOfSrc, JsonSerializationContext context)
{
try
{ {
if (src == null) return null; if (src == null) return null;
// if default values, store as simple string // isDefault <=> simple hostFactionId string
if (src.isDefault()) if (src.isDefault())
{ {
return new JsonPrimitive(src.getHostFactionId()); return new JsonPrimitive(src.getHostFactionId());
} }
// otherwise, store all data // Otherwise object
JsonObject obj = new JsonObject(); JsonObject obj = new JsonObject();
JsonArray factions = new JsonArray(); obj.addProperty(HOST_FACTION_ID, src.getHostFactionId());
JsonArray fplayers = new JsonArray();
Iterator<String> iter = src.getFactionIds().iterator(); if (!src.isHostFactionAllowed())
while (iter.hasNext())
{ {
factions.add(new JsonPrimitive(iter.next())); obj.addProperty(HOST_FACTION_ALLOWED, src.isHostFactionAllowed());
} }
iter = src.getFPlayerIds().iterator(); if (!src.getFactionIds().isEmpty())
while (iter.hasNext())
{ {
fplayers.add(new JsonPrimitive(iter.next())); obj.add(FACTION_IDS, context.serialize(src.getFactionIds(), SET_OF_STRING_TYPE));
} }
obj.addProperty(ID, src.getHostFactionId()); if (!src.getPlayerIds().isEmpty())
obj.addProperty(OPEN, src.isHostFactionAllowed()); {
obj.add(FACTIONS, factions); obj.add(PLAYER_IDS, context.serialize(src.getPlayerIds(), SET_OF_STRING_TYPE));
obj.add(FPLAYERS, fplayers); }
return obj; return obj;
}
catch (Exception ex)
{
ex.printStackTrace();
Factions.get().log(Level.WARNING, "Error encountered while serializing TerritoryAccess data.");
return null;
}
} }
} }

View File

@ -1,111 +1,36 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.Perm; import com.massivecraft.factions.Perm;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.factions.cmd.arg.ARUPlayer;
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.cmd.req.ReqFactionsEnabled; import com.massivecraft.factions.cmd.req.ReqFactionsEnabled;
import com.massivecraft.factions.entity.BoardColls; import com.massivecraft.mcore.cmd.HelpCommand;
import com.massivecraft.factions.entity.UPlayer; import com.massivecraft.mcore.cmd.req.ReqHasPerm;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.mcore.cmd.req.ReqIsPlayer; import com.massivecraft.mcore.cmd.req.ReqIsPlayer;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.util.Txt;
public class CmdFactionsAccess extends FCommand public class CmdFactionsAccess extends FCommand
{ {
public CmdFactionsAccessView cmdFactionsAccessView = new CmdFactionsAccessView();
public CmdFactionsAccessPlayer cmdFactionsAccessPlayer = new CmdFactionsAccessPlayer();
public CmdFactionsAccessFaction cmdFactionsAccessFaction = new CmdFactionsAccessFaction();
public CmdFactionsAccess() public CmdFactionsAccess()
{ {
this.addAliases("access"); this.addAliases("access");
this.addOptionalArg("view|p|player|f|faction", "view");
this.addOptionalArg("name", "you");
this.setDesc("view or grant access for the claimed territory you are in");
// TODO: Missing permission node here!?
this.addRequirements(ReqFactionsEnabled.get()); this.addRequirements(ReqFactionsEnabled.get());
this.addRequirements(ReqIsPlayer.get()); this.addRequirements(ReqIsPlayer.get());
this.addRequirements(ReqHasPerm.get(Perm.ACCESS.node));
this.addSubCommand(this.cmdFactionsAccessView);
this.addSubCommand(this.cmdFactionsAccessPlayer);
this.addSubCommand(this.cmdFactionsAccessFaction);
} }
@Override @Override
public void perform() public void perform()
{ {
String type = this.arg(0); this.getCommandChain().add(this);
type = (type == null) ? "" : type.toLowerCase(); HelpCommand.getInstance().execute(this.sender, this.args, this.commandChain);
PS chunk = PS.valueOf(me).getChunk(true);
TerritoryAccess territory = BoardColls.get().getTerritoryAccessAt(chunk);
Faction locFaction = BoardColls.get().getFactionAt(chunk);
boolean accessAny = Perm.ACCESS_ANY.has(sender, false);
if (type.isEmpty() || type.equals("view"))
{
if ( ! accessAny && ! Perm.ACCESS_VIEW.has(sender, true)) return;
if ( ! accessAny && ! territory.doesHostFactionMatch(usender))
{
msg("<b>This territory isn't controlled by your faction, so you can't view the access list.");
return;
}
showAccessList(territory, locFaction);
return;
} }
if ( ! accessAny && ! Perm.ACCESS.has(sender, true)) return;
if ( ! accessAny && ! FPerm.ACCESS.has(usender, locFaction, true)) return;
boolean doPlayer = true;
if (type.equals("f") || type.equals("faction"))
{
doPlayer = false;
}
else if (!type.equals("p") && !type.equals("player"))
{
msg("<b>You must specify \"p\" or \"player\" to indicate a player or \"f\" or \"faction\" to indicate a faction.");
msg("<b>ex. /f access p SomePlayer -or- /f access f SomeFaction");
msg("<b>Alternately, you can use the command with nothing (or \"view\") specified to simply view the access list.");
return;
}
String target = "";
boolean added;
if (doPlayer)
{
UPlayer targetPlayer = this.arg(1, ARUPlayer.getStartAny(usender), usender);
if (targetPlayer == null) return;
added = territory.toggleFPlayer(targetPlayer);
target = "Player \""+targetPlayer.getName()+"\"";
}
else
{
Faction targetFaction = this.arg(1, ARFaction.get(usenderFaction), usenderFaction);
if (targetFaction == null) return;
added = territory.toggleFaction(targetFaction);
target = "Faction \""+targetFaction.getName()+"\"";
}
msg("<i>%s has been %s<i> the access list for this territory.", target, Txt.parse(added ? "<lime>added to" : "<rose>removed from"));
showAccessList(territory, locFaction);
}
private void showAccessList(TerritoryAccess territory, Faction locFaction)
{
msg("<i>Host faction %s <i>has %s<i> in this territory.", locFaction.getName(), Txt.parse(territory.isHostFactionAllowed() ? "<lime>normal access" : "<rose>restricted access"));
String players = territory.fplayerList();
String factions = territory.factionList(locFaction);
if (factions.isEmpty())
msg("No factions have been explicitly granted access.");
else
msg("Factions with explicit access: " + factions);
if (players.isEmpty())
msg("No players have been explicitly granted access.");
else
msg("Players with explicit access: " + players);
}
} }

View File

@ -0,0 +1,61 @@
package com.massivecraft.factions.cmd;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.factions.cmd.req.ReqFactionsEnabled;
import com.massivecraft.factions.entity.BoardColls;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.mcore.cmd.req.ReqIsPlayer;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.ps.PSFormatHumanSpace;
import com.massivecraft.mcore.util.Txt;
public abstract class CmdFactionsAccessAbstract extends FCommand
{
public PS chunk;
public TerritoryAccess ta;
public Faction hostFaction;
public CmdFactionsAccessAbstract()
{
this.addRequirements(ReqFactionsEnabled.get());
this.addRequirements(ReqIsPlayer.get());
}
@Override
public void perform()
{
chunk = PS.valueOf(me).getChunk(true);
ta = BoardColls.get().getTerritoryAccessAt(chunk);
hostFaction = ta.getHostFaction(usender);
this.innerPerform();
}
public abstract void innerPerform();
public void sendAccessInfo()
{
sendMessage(Txt.titleize("Access at " + chunk.toString(PSFormatHumanSpace.get())));
msg("<k>Host Faction: %s", hostFaction.describeTo(usender, true));
msg("<k>Host Faction Allowed: %s", ta.isHostFactionAllowed() ? Txt.parse("<lime>TRUE") : Txt.parse("<rose>FALSE"));
msg("<k>Granted Players: %s", describeRelationParticipators(ta.getGrantedUPlayers(usender), usender));
msg("<k>Granted Factions: %s", describeRelationParticipators(ta.getGrantedFactions(usender), usender));
}
public static String describeRelationParticipators(Collection<? extends RelationParticipator> relationParticipators, RelationParticipator observer)
{
if (relationParticipators.size() == 0) return Txt.parse("<silver><em>none");
List<String> descriptions = new ArrayList<String>();
for (RelationParticipator relationParticipator : relationParticipators)
{
descriptions.add(relationParticipator.describeTo(observer));
}
return Txt.implodeCommaAnd(descriptions, Txt.parse("<i>, "), Txt.parse(" <i>and "));
}
}

View File

@ -0,0 +1,43 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.entity.BoardColls;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.mcore.cmd.arg.ARBoolean;
import com.massivecraft.mcore.cmd.req.ReqHasPerm;
public class CmdFactionsAccessFaction extends CmdFactionsAccessAbstract
{
public CmdFactionsAccessFaction()
{
this.addAliases("f", "faction");
this.addRequiredArg("faction");
this.addOptionalArg("yes/no", "toggle");
this.addRequirements(ReqHasPerm.get(Perm.ACCESS_FACTION.node));
}
@Override
public void innerPerform()
{
// Args
Faction faction = this.arg(0, ARFaction.get(usender));
if (faction == null) return;
Boolean newValue = this.arg(1, ARBoolean.get(), !ta.isFactionIdGranted(faction.getId()));
if (newValue == null) return;
// FPerm
if (FPerm.ACCESS.has(usender, hostFaction, true)) return;
// Apply
ta = ta.withFactionId(faction.getId(), newValue);
BoardColls.get().setTerritoryAccessAt(chunk, ta);
// Inform
this.sendAccessInfo();
}
}

View File

@ -0,0 +1,43 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.cmd.arg.ARUPlayer;
import com.massivecraft.factions.entity.BoardColls;
import com.massivecraft.factions.entity.UPlayer;
import com.massivecraft.mcore.cmd.arg.ARBoolean;
import com.massivecraft.mcore.cmd.req.ReqHasPerm;
public class CmdFactionsAccessPlayer extends CmdFactionsAccessAbstract
{
public CmdFactionsAccessPlayer()
{
this.addAliases("p", "player");
this.addRequiredArg("player");
this.addOptionalArg("yes/no", "toggle");
this.addRequirements(ReqHasPerm.get(Perm.ACCESS_PLAYER.node));
}
@Override
public void innerPerform()
{
// Args
UPlayer uplayer = this.arg(0, ARUPlayer.getStartAny(usender));
if (uplayer == null) return;
Boolean newValue = this.arg(1, ARBoolean.get(), !ta.isPlayerIdGranted(uplayer.getId()));
if (newValue == null) return;
// FPerm
if (FPerm.ACCESS.has(usender, hostFaction, true)) return;
// Apply
ta = ta.withPlayerId(uplayer.getId(), newValue);
BoardColls.get().setTerritoryAccessAt(chunk, ta);
// Inform
this.sendAccessInfo();
}
}

View File

@ -0,0 +1,21 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.mcore.cmd.req.ReqHasPerm;
public class CmdFactionsAccessView extends CmdFactionsAccessAbstract
{
public CmdFactionsAccessView()
{
this.addAliases("v", "view");
this.addRequirements(ReqHasPerm.get(Perm.ACCESS_VIEW.node));
}
@Override
public void innerPerform()
{
this.sendAccessInfo();
}
}

View File

@ -1,7 +1,10 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.cmd.req.ReqBankCommandsEnabled; import com.massivecraft.factions.cmd.req.ReqBankCommandsEnabled;
import com.massivecraft.factions.cmd.req.ReqFactionsEnabled;
import com.massivecraft.mcore.cmd.HelpCommand; import com.massivecraft.mcore.cmd.HelpCommand;
import com.massivecraft.mcore.cmd.req.ReqHasPerm;
public class CmdFactionsMoney extends FCommand public class CmdFactionsMoney extends FCommand
{ {
@ -16,10 +19,9 @@ public class CmdFactionsMoney extends FCommand
{ {
this.addAliases("money"); this.addAliases("money");
this.setDesc("faction money commands"); this.addRequirements(ReqFactionsEnabled.get());
this.setHelp("The faction money commands.");
this.addRequirements(ReqBankCommandsEnabled.get()); this.addRequirements(ReqBankCommandsEnabled.get());
this.addRequirements(ReqHasPerm.get(Perm.MONEY.node));
this.addSubCommand(this.cmdMoneyBalance); this.addSubCommand(this.cmdMoneyBalance);
this.addSubCommand(this.cmdMoneyDeposit); this.addSubCommand(this.cmdMoneyDeposit);

View File

@ -2,6 +2,7 @@ package com.massivecraft.factions.cmd;
import com.massivecraft.factions.cmd.arg.ARFaction; import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.cmd.req.ReqBankCommandsEnabled; import com.massivecraft.factions.cmd.req.ReqBankCommandsEnabled;
import com.massivecraft.factions.cmd.req.ReqFactionsEnabled;
import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.Perm; import com.massivecraft.factions.Perm;
@ -15,6 +16,7 @@ public class CmdFactionsMoneyBalance extends FCommand
this.addOptionalArg("faction", "you"); this.addOptionalArg("faction", "you");
this.addRequirements(ReqFactionsEnabled.get());
this.addRequirements(ReqHasPerm.get(Perm.MONEY_BALANCE.node)); this.addRequirements(ReqHasPerm.get(Perm.MONEY_BALANCE.node));
this.addRequirements(ReqBankCommandsEnabled.get()); this.addRequirements(ReqBankCommandsEnabled.get());
} }

View File

@ -17,7 +17,7 @@ public class ARFaction extends ArgReaderAbstract<Faction>
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public static ARFaction get(Object o) { return new ARFaction(FactionColls.get().get(o)); } public static ARFaction get(Object universe) { return new ARFaction(FactionColls.get().get(universe)); }
private ARFaction(FactionColl coll) private ARFaction(FactionColl coll)
{ {
this.coll = coll; this.coll = coll;

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 = new TerritoryAccess(UConf.get(this).factionIdNone); if (ret == null) ret = TerritoryAccess.valueOf(UConf.get(this).factionIdNone);
return ret; return ret;
} }
@ -98,8 +98,8 @@ public class Board extends Entity<Board> implements BoardInterface
public Faction getFactionAt(PS ps) public Faction getFactionAt(PS ps)
{ {
if (ps == null) return null; if (ps == null) return null;
String factionId = this.getTerritoryAccessAt(ps).getHostFactionId(); TerritoryAccess ta = this.getTerritoryAccessAt(ps);
return FactionColls.get().get(this).get(factionId); return ta.getHostFaction(ps);
} }
// SET // SET
@ -127,7 +127,7 @@ public class Board extends Entity<Board> implements BoardInterface
TerritoryAccess territoryAccess = null; TerritoryAccess territoryAccess = null;
if (faction != null) if (faction != null)
{ {
territoryAccess = new TerritoryAccess(faction.getId()); territoryAccess = TerritoryAccess.valueOf(faction.getId());
} }
this.setTerritoryAccessAt(ps, territoryAccess); this.setTerritoryAccessAt(ps, territoryAccess);
} }