From 249770d2cde965827544e536f19d397ff44ced66 Mon Sep 17 00:00:00 2001 From: Trent Hensler Date: Sat, 3 Feb 2018 12:33:28 -0800 Subject: [PATCH] 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. --- .../com/massivecraft/factions/Faction.java | 7 ++- .../factions/cmd/FPromoteCommand.java | 2 +- .../factions/struct/Relation.java | 3 +- .../massivecraft/factions/struct/Role.java | 3 +- .../factions/zcore/fperms/Permissable.java | 4 ++ .../factions/zcore/persist/MemoryFaction.java | 46 ++++++++++++++++--- 6 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/massivecraft/factions/zcore/fperms/Permissable.java diff --git a/src/main/java/com/massivecraft/factions/Faction.java b/src/main/java/com/massivecraft/factions/Faction.java index 0c9aa59d..993b98dc 100644 --- a/src/main/java/com/massivecraft/factions/Faction.java +++ b/src/main/java/com/massivecraft/factions/Faction.java @@ -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(); diff --git a/src/main/java/com/massivecraft/factions/cmd/FPromoteCommand.java b/src/main/java/com/massivecraft/factions/cmd/FPromoteCommand.java index c0c06358..3b9dc337 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FPromoteCommand.java +++ b/src/main/java/com/massivecraft/factions/cmd/FPromoteCommand.java @@ -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) { diff --git a/src/main/java/com/massivecraft/factions/struct/Relation.java b/src/main/java/com/massivecraft/factions/struct/Relation.java index 57c822af..2332bb58 100644 --- a/src/main/java/com/massivecraft/factions/struct/Relation.java +++ b/src/main/java/com/massivecraft/factions/struct/Relation.java @@ -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()), diff --git a/src/main/java/com/massivecraft/factions/struct/Role.java b/src/main/java/com/massivecraft/factions/struct/Role.java index ee97bb38..dfe0a7b8 100644 --- a/src/main/java/com/massivecraft/factions/struct/Role.java +++ b/src/main/java/com/massivecraft/factions/struct/Role.java @@ -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), diff --git a/src/main/java/com/massivecraft/factions/zcore/fperms/Permissable.java b/src/main/java/com/massivecraft/factions/zcore/fperms/Permissable.java new file mode 100644 index 00000000..44e2ce93 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/zcore/fperms/Permissable.java @@ -0,0 +1,4 @@ +package com.massivecraft.factions.zcore.fperms; + +public interface Permissable { +} diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java index 1a6ae601..a20518d2 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java @@ -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> permissions = new HashMap<>(); + protected Map> permissions = new HashMap<>(); public HashMap> 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 accessMap = permissions.get(relation); + public Access getAccess(Permissable permissable, Action action) { + Map 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 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 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 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 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() {