From 10574748ffd15e704d7f7c8c8044e81c5a91c542 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 25 Jul 2016 20:44:01 +0100 Subject: [PATCH] Add inheritance methods to Group --- .../java/me/lucko/luckperms/api/Group.java | 153 +++++++++++ .../implementation/internal/GroupLink.java | 109 ++++++++ .../java/me/lucko/luckperms/groups/Group.java | 238 ++++++++++++++++++ 3 files changed, 500 insertions(+) diff --git a/api/src/main/java/me/lucko/luckperms/api/Group.java b/api/src/main/java/me/lucko/luckperms/api/Group.java index fdc98aeca..4bf595cda 100644 --- a/api/src/main/java/me/lucko/luckperms/api/Group.java +++ b/api/src/main/java/me/lucko/luckperms/api/Group.java @@ -1,5 +1,10 @@ package me.lucko.luckperms.api; +import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; +import me.lucko.luckperms.exceptions.ObjectLacksException; + +import java.util.List; + /** * Wrapper interface for internal Group instances * The implementations of this interface limit access to the Group and add parameter checks to further prevent @@ -8,11 +13,159 @@ package me.lucko.luckperms.api; @SuppressWarnings("unused") public interface Group extends PermissionObject { + /** + * @return the name of the group + */ String getName(); + /** + * check to see if a group inherits a group + * @param group The group to check membership of + * @return true if the user is a member of the group + */ + boolean inheritsGroup(Group group); + + /** + * check to see if the group inherits a group on a specific server + * @param group The group to check membership of + * @param server The server to check on + * @return true if the group inherits the group + */ + boolean inheritsGroup(Group group, String server); + + /** + * check to see if the group inherits a group on a specific server + * @param group The group to check membership of + * @param server The server to check on + * @param world The world to check on + * @return true if the group inherits the group + */ + boolean inheritsGroup(Group group, String server, String world); + + /** + * Make this group inherit another group + * @param group the group to be inherited + * @throws ObjectAlreadyHasException if the group already inherits the group + */ + void setInheritGroup(Group group) throws ObjectAlreadyHasException; + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param server The server to add the group on + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + void setInheritGroup(Group group, String server) throws ObjectAlreadyHasException; + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param server The server to add the group on + * @param world The world to add the group on + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + void setInheritGroup(Group group, String server, String world) throws ObjectAlreadyHasException; + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param expireAt when the group should expire + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + void setInheritGroup(Group group, long expireAt) throws ObjectAlreadyHasException; + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param server The server to add the group on + * @param expireAt when the group should expire + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + void setInheritGroup(Group group, String server, long expireAt) throws ObjectAlreadyHasException; + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param server The server to add the group on + * @param world The world to add the group on + * @param expireAt when the group should expire + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + void setInheritGroup(Group group, String server, String world, long expireAt) throws ObjectAlreadyHasException; + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @throws ObjectLacksException if the group does not already inherit the group + */ + void unsetInheritGroup(Group group) throws ObjectLacksException; + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param temporary if the group being removed is temporary + * @throws ObjectLacksException if the group does not already inherit the group + */ + void unsetInheritGroup(Group group, boolean temporary) throws ObjectLacksException; + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param server The server to remove the group on + * @throws ObjectLacksException if the group does not already inherit the group + */ + void unsetInheritGroup(Group group, String server) throws ObjectLacksException; + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param server The server to remove the group on + * @param world The world to remove the group on + * @throws ObjectLacksException if the group does not already inherit the group + */ + void unsetInheritGroup(Group group, String server, String world) throws ObjectLacksException; + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param server The server to remove the group on + * @param temporary if the group being removed is temporary + * @throws ObjectLacksException if the group does not already inherit the group + */ + void unsetInheritGroup(Group group, String server, boolean temporary) throws ObjectLacksException; + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param server The server to remove the group on + * @param world The world to remove the group on + * @param temporary if the group being removed is temporary + * @throws ObjectLacksException if the group does not already inherit the group + */ + void unsetInheritGroup(Group group, String server, String world, boolean temporary) throws ObjectLacksException; + /** * Clear all of the groups permission nodes */ void clearNodes(); + /** + * Get a {@link List} of all of the groups the group inherits, on all servers + * @return a {@link List} of group names + */ + List getGroupNames(); + + /** + * Get a {@link List} of the groups the group inherits on a specific server + * @param server the server to check + * @return a {@link List} of group names + */ + List getLocalGroups(String server, String world); + + /** + * Get a {@link List} of the groups the group inherits on a specific server + * @param server the server to check + * @return a {@link List} of group names + */ + List getLocalGroups(String server); } diff --git a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/GroupLink.java b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/GroupLink.java index b94806ba1..593053ae1 100644 --- a/common/src/main/java/me/lucko/luckperms/api/implementation/internal/GroupLink.java +++ b/common/src/main/java/me/lucko/luckperms/api/implementation/internal/GroupLink.java @@ -4,6 +4,10 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NonNull; import me.lucko.luckperms.api.Group; +import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; +import me.lucko.luckperms.exceptions.ObjectLacksException; + +import java.util.List; /** * Provides a link between {@link Group} and {@link me.lucko.luckperms.groups.Group} @@ -24,8 +28,113 @@ public class GroupLink extends PermissionObjectLink implements Group { return master.getName(); } + @Override + public boolean inheritsGroup(@NonNull Group group) { + Utils.checkGroup(group); + return master.inheritsGroup(((GroupLink) group).getMaster()); + } + + @Override + public boolean inheritsGroup(@NonNull Group group, @NonNull String server) { + Utils.checkGroup(group); + return master.inheritsGroup(((GroupLink) group).getMaster(), server); + } + + @Override + public boolean inheritsGroup(@NonNull Group group, @NonNull String server, @NonNull String world) { + Utils.checkGroup(group); + return master.inheritsGroup(((GroupLink) group).getMaster(), server, world); + } + + @Override + public void setInheritGroup(@NonNull Group group) throws ObjectAlreadyHasException { + Utils.checkGroup(group); + master.setInheritGroup(((GroupLink) group).getMaster()); + } + + @Override + public void setInheritGroup(@NonNull Group group, @NonNull String server) throws ObjectAlreadyHasException { + Utils.checkGroup(group); + master.setInheritGroup(((GroupLink) group).getMaster(), checkServer(server)); + } + + @Override + public void setInheritGroup(@NonNull Group group, @NonNull String server, @NonNull String world) throws ObjectAlreadyHasException { + Utils.checkGroup(group); + master.setInheritGroup(((GroupLink) group).getMaster(), checkServer(server), world); + } + + @Override + public void setInheritGroup(@NonNull Group group, @NonNull long expireAt) throws ObjectAlreadyHasException { + Utils.checkGroup(group); + master.setInheritGroup(((GroupLink) group).getMaster(), checkTime(expireAt)); + } + + @Override + public void setInheritGroup(@NonNull Group group, @NonNull String server, @NonNull long expireAt) throws ObjectAlreadyHasException { + Utils.checkGroup(group); + master.setInheritGroup(((GroupLink) group).getMaster(), checkServer(server), checkTime(expireAt)); + } + + @Override + public void setInheritGroup(@NonNull Group group, @NonNull String server, @NonNull String world, @NonNull long expireAt) throws ObjectAlreadyHasException { + Utils.checkGroup(group); + master.setInheritGroup(((GroupLink) group).getMaster(), checkServer(server), world, checkTime(expireAt)); + } + + @Override + public void unsetInheritGroup(@NonNull Group group) throws ObjectLacksException { + Utils.checkGroup(group); + master.unsetInheritGroup(((GroupLink) group).getMaster()); + } + + @Override + public void unsetInheritGroup(@NonNull Group group, @NonNull boolean temporary) throws ObjectLacksException { + Utils.checkGroup(group); + master.unsetInheritGroup(((GroupLink) group).getMaster(), temporary); + } + + @Override + public void unsetInheritGroup(@NonNull Group group, @NonNull String server) throws ObjectLacksException { + Utils.checkGroup(group); + master.unsetInheritGroup(((GroupLink) group).getMaster(), checkServer(server)); + } + + @Override + public void unsetInheritGroup(@NonNull Group group, @NonNull String server, @NonNull String world) throws ObjectLacksException { + Utils.checkGroup(group); + master.unsetInheritGroup(((GroupLink) group).getMaster(), checkServer(server), world); + } + + @Override + public void unsetInheritGroup(@NonNull Group group, @NonNull String server, @NonNull boolean temporary) throws ObjectLacksException { + Utils.checkGroup(group); + master.unsetInheritGroup(((GroupLink) group).getMaster(), checkServer(server), temporary); + } + + @Override + public void unsetInheritGroup(@NonNull Group group, @NonNull String server, @NonNull String world, @NonNull boolean temporary) throws ObjectLacksException { + Utils.checkGroup(group); + master.unsetInheritGroup(((GroupLink) group).getMaster(), checkServer(server), world, temporary); + } + @Override public void clearNodes() { master.clearNodes(); } + + @Override + public List getGroupNames() { + return master.getGroupNames(); + } + + @Override + public List getLocalGroups(@NonNull String server, @NonNull String world) { + return master.getLocalGroups(checkServer(server), world); + } + + @Override + public List getLocalGroups(@NonNull String server) { + return master.getLocalGroups(checkServer(server)); + } } diff --git a/common/src/main/java/me/lucko/luckperms/groups/Group.java b/common/src/main/java/me/lucko/luckperms/groups/Group.java index 6846a8363..dbdbdfcdc 100644 --- a/common/src/main/java/me/lucko/luckperms/groups/Group.java +++ b/common/src/main/java/me/lucko/luckperms/groups/Group.java @@ -4,8 +4,15 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; +import me.lucko.luckperms.exceptions.ObjectLacksException; +import me.lucko.luckperms.utils.Patterns; import me.lucko.luckperms.utils.PermissionObject; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + @ToString(of = {"name"}) @EqualsAndHashCode(of = {"name"}, callSuper = false) public class Group extends PermissionObject { @@ -21,10 +28,241 @@ public class Group extends PermissionObject { this.name = name; } + /** + * check to see if a group inherits a group + * @param group The group to check membership of + * @return true if the user is a member of the group + */ + public boolean inheritsGroup(Group group) { + return inheritsGroup(group, "global"); + } + + /** + * check to see if the group inherits a group on a specific server + * @param group The group to check membership of + * @param server The server to check on + * @return true if the group inherits the group + */ + public boolean inheritsGroup(Group group, String server) { + return hasPermission("group." + group.getName(), true, server); + } + + /** + * check to see if the group inherits a group on a specific server + * @param group The group to check membership of + * @param server The server to check on + * @param world The world to check on + * @return true if the group inherits the group + */ + public boolean inheritsGroup(Group group, String server, String world) { + return hasPermission("group." + group.getName(), true, server, world); + } + + /** + * Make this group inherit another group + * @param group the group to be inherited + * @throws ObjectAlreadyHasException if the group already inherits the group + */ + public void setInheritGroup(Group group) throws ObjectAlreadyHasException { + setInheritGroup(group, "global"); + } + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param server The server to add the group on + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + public void setInheritGroup(Group group, String server) throws ObjectAlreadyHasException { + if (server == null) { + server = "global"; + } + + setPermission("group." + group.getName(), true, server); + } + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param server The server to add the group on + * @param world The world to add the group on + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + public void setInheritGroup(Group group, String server, String world) throws ObjectAlreadyHasException { + if (server == null) { + server = "global"; + } + + setPermission("group." + group.getName(), true, server, world); + } + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param expireAt when the group should expire + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + public void setInheritGroup(Group group, long expireAt) throws ObjectAlreadyHasException { + setPermission("group." + group.getName(), true, expireAt); + } + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param server The server to add the group on + * @param expireAt when the group should expire + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + public void setInheritGroup(Group group, String server, long expireAt) throws ObjectAlreadyHasException { + if (server == null) { + server = "global"; + } + + setPermission("group." + group.getName(), true, server, expireAt); + } + + /** + * Make this group inherit another group on a specific server + * @param group the group to be inherited + * @param server The server to add the group on + * @param world The world to add the group on + * @param expireAt when the group should expire + * @throws ObjectAlreadyHasException if the group already inherits the group on that server + */ + public void setInheritGroup(Group group, String server, String world, long expireAt) throws ObjectAlreadyHasException { + if (server == null) { + server = "global"; + } + + setPermission("group." + group.getName(), true, server, world, expireAt); + } + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @throws ObjectLacksException if the group does not already inherit the group + */ + public void unsetInheritGroup(Group group) throws ObjectLacksException { + unsetInheritGroup(group, "global"); + } + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param temporary if the group being removed is temporary + * @throws ObjectLacksException if the group does not already inherit the group + */ + public void unsetInheritGroup(Group group, boolean temporary) throws ObjectLacksException { + unsetInheritGroup(group, "global", temporary); + } + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param server The server to remove the group on + * @throws ObjectLacksException if the group does not already inherit the group + */ + public void unsetInheritGroup(Group group, String server) throws ObjectLacksException { + if (server == null) { + server = "global"; + } + + unsetPermission("group." + group.getName(), server); + } + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param server The server to remove the group on + * @param world The world to remove the group on + * @throws ObjectLacksException if the group does not already inherit the group + */ + public void unsetInheritGroup(Group group, String server, String world) throws ObjectLacksException { + if (server == null) { + server = "global"; + } + + unsetPermission("group." + group.getName(), server, world); + } + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param server The server to remove the group on + * @param temporary if the group being removed is temporary + * @throws ObjectLacksException if the group does not already inherit the group + */ + public void unsetInheritGroup(Group group, String server, boolean temporary) throws ObjectLacksException { + if (server == null) { + server = "global"; + } + + unsetPermission("group." + group.getName(), server, temporary); + } + + /** + * Remove a previously set inheritance + * @param group the group to uninherit + * @param server The server to remove the group on + * @param world The world to remove the group on + * @param temporary if the group being removed is temporary + * @throws ObjectLacksException if the group does not already inherit the group + */ + public void unsetInheritGroup(Group group, String server, String world, boolean temporary) throws ObjectLacksException { + if (server == null) { + server = "global"; + } + + unsetPermission("group." + group.getName(), server, world, temporary); + } + /** * Clear all of the groups permission nodes */ public void clearNodes() { getNodes().clear(); } + + /** + * Get a {@link List} of all of the groups the group inherits, on all servers + * @return a {@link List} of group names + */ + public List getGroupNames() { + return getGroups(null, null, true); + } + + /** + * Get a {@link List} of the groups the group inherits on a specific server + * @param server the server to check + * @return a {@link List} of group names + */ + public List getLocalGroups(String server, String world) { + return getGroups(server, world, false); + } + + /** + * Get a {@link List} of the groups the group inherits on a specific server + * @param server the server to check + * @return a {@link List} of group names + */ + public List getLocalGroups(String server) { + return getLocalGroups(server, null); + } + + /** + * Get a {@link List} of the groups the group inherits on a specific server with the option to include global + * groups or all groups + * @param server Which server to check on + * @param world Which world to check on + * @param includeGlobal Whether to include global groups + * @return a {@link List} of group names + */ + private List getGroups(String server, String world, boolean includeGlobal) { + // Call super #getPermissions method, and just sort through those + Map perms = getPermissions(server, world, null, includeGlobal); + return perms.keySet().stream() + .filter(s -> Patterns.GROUP_MATCH.matcher(s).matches()) + .map(s -> Patterns.DOT_SPLIT.split(s, 2)[1]) + .collect(Collectors.toList()); + } }