Add Permissable interface for f perm compatibility.

Allows Roles and Relations to be permissable, so we can set f perms for both of those in the same map.
This commit is contained in:
Trent Hensler 2018-02-03 12:33:28 -08:00
parent 9e8205b5e2
commit 249770d2cd
6 changed files with 54 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.LazyLocation;
import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action;
import com.massivecraft.factions.zcore.fperms.Permissable;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -138,9 +139,11 @@ public interface Faction extends EconomyParticipator {
public int getDeaths();
public Access getAccess(FPlayer fPlayer, Action perm);
public Access getAccess(Permissable permissable, Action action);
public void setPermission(Relation relation, Action action, Access access);
public Access getAccess(FPlayer player, Action action);
public void setPermission(Permissable permissable, Action action, Access access);
public void resetPerms();

View File

@ -38,7 +38,7 @@ public class FPromoteCommand extends FCommand {
return;
}
Access access = myFaction.getAccess(fme, Action.PROMOTE);
Access access = myFaction.getAccess(fme.getRole(), Action.PROMOTE);
// Well this is messy.
if (access == null || access == Access.UNDEFINED) {

View File

@ -1,11 +1,12 @@
package com.massivecraft.factions.struct;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.zcore.fperms.Permissable;
import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.ChatColor;
public enum Relation {
public enum Relation implements Permissable {
MEMBER(4, TL.RELATION_MEMBER_SINGULAR.toString()),
ALLY(3, TL.RELATION_ALLY_SINGULAR.toString()),
TRUCE(2, TL.RELATION_TRUCE_SINGULAR.toString()),

View File

@ -1,9 +1,10 @@
package com.massivecraft.factions.struct;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.zcore.fperms.Permissable;
import com.massivecraft.factions.zcore.util.TL;
public enum Role {
public enum Role implements Permissable {
ADMIN(3, TL.ROLE_ADMIN),
MODERATOR(2, TL.ROLE_MODERATOR),
NORMAL(1, TL.ROLE_NORMAL),

View File

@ -0,0 +1,4 @@
package com.massivecraft.factions.zcore.fperms;
public interface Permissable {
}

View File

@ -12,6 +12,7 @@ import com.massivecraft.factions.util.MiscUtil;
import com.massivecraft.factions.util.RelationUtil;
import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action;
import com.massivecraft.factions.zcore.fperms.Permissable;
import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -48,7 +49,7 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
private long lastDeath;
protected int maxVaults;
protected Role defaultRole;
protected Map<Relation, Map<Action, Access>> permissions = new HashMap<>();
protected Map<Permissable, Map<Action, Access>> permissions = new HashMap<>();
public HashMap<String, List<String>> getAnnouncements() {
return this.announcements;
@ -324,9 +325,8 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
// -------------------------------------------- //
public Access getAccess(FPlayer fPlayer, Action action) {
Relation relation = fPlayer.getRelationTo(this);
Map<Action, Access> accessMap = permissions.get(relation);
public Access getAccess(Permissable permissable, Action action) {
Map<Action, Access> accessMap = permissions.get(permissable);
if (accessMap != null && accessMap.containsKey(action)) {
return accessMap.get(action);
}
@ -334,8 +334,32 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
return null;
}
public void setPermission(Relation relation, Action action, Access access) {
Map<Action, Access> accessMap = permissions.get(relation);
/**
* Get the Access of a player. Will use player's Role if they are a faction member. Otherwise, uses their Relation.
*
* @param player
* @param action
* @return
*/
public Access getAccess(FPlayer player, Action action) {
Permissable perm;
if (player.getFaction() == this) {
perm = player.getRole();
} else {
perm = player.getFaction().getRelationTo(this);
}
Map<Action, Access> accessMap = permissions.get(perm);
if (accessMap != null && accessMap.containsKey(action)) {
return accessMap.get(action);
}
return null;
}
public void setPermission(Permissable permissable, Action action, Access access) {
Map<Action, Access> accessMap = permissions.get(permissable);
if (accessMap == null) {
accessMap = new HashMap<>();
}
@ -343,9 +367,12 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
accessMap.put(action, access);
}
public void resetPerms() {
P.p.log(Level.WARNING, "Resetting permissions for Faction: " + tag);
permissions.clear();
// First populate a map with undefined as the permission for each action.
Map<Action, Access> freshMap = new HashMap<>();
for (Action action : Action.values()) {
@ -356,6 +383,13 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
for (Relation relation : Relation.values()) {
permissions.put(relation, freshMap);
}
// And each role.
for (Role role : Role.values()) {
if (role != Role.ADMIN) {
permissions.put(role, freshMap);
}
}
}
public Role getDefaultRole() {