From 350ef9b5e6eff53ed562ae081555be94e2ec87c9 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 29 Jan 2017 18:16:17 +0100 Subject: [PATCH] PermissionHandlers: add default methods for trivial duplications - Add easy default methods on PermissionHandler interface (override whenever there's a better way!) - Change getGroups() signature to return a Collection instead of a List --- .../authme/permission/PermissionsManager.java | 12 ++++----- .../handlers/BPermissionsHandler.java | 19 +++++--------- .../handlers/PermissionHandler.java | 17 +++++++++--- .../handlers/PermissionsBukkitHandler.java | 26 ++++--------------- .../handlers/PermissionsExHandler.java | 17 +++++------- .../permission/handlers/VaultHandler.java | 6 +++++ .../handlers/ZPermissionsHandler.java | 18 ++++++------- 7 files changed, 51 insertions(+), 64 deletions(-) diff --git a/src/main/java/fr/xephi/authme/permission/PermissionsManager.java b/src/main/java/fr/xephi/authme/permission/PermissionsManager.java index 6af00070a..497c99ab3 100644 --- a/src/main/java/fr/xephi/authme/permission/PermissionsManager.java +++ b/src/main/java/fr/xephi/authme/permission/PermissionsManager.java @@ -19,8 +19,8 @@ import org.bukkit.plugin.PluginManager; import javax.annotation.PostConstruct; import javax.inject.Inject; -import java.util.ArrayList; -import java.util.List; +import java.util.Collection; +import java.util.Collections; /** *

@@ -255,12 +255,12 @@ public class PermissionsManager implements Reloadable { * * @param player The player. * - * @return Permission groups, or an empty list if this feature is not supported. + * @return Permission groups, or an empty collection if this feature is not supported. */ - public List getGroups(Player player) { + public Collection getGroups(Player player) { // If no permissions system is used, return an empty list if (!isEnabled()) - return new ArrayList<>(); + return Collections.emptyList(); return handler.getGroups(player); } @@ -389,7 +389,7 @@ public class PermissionsManager implements Reloadable { return false; // Get a list of current groups - List groupNames = getGroups(player); + Collection groupNames = getGroups(player); // Remove each group return removeGroups(player, groupNames.toArray(new String[groupNames.size()])); diff --git a/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java index 9f52214b9..849ecd657 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/BPermissionsHandler.java @@ -9,6 +9,12 @@ import org.bukkit.entity.Player; import java.util.Arrays; import java.util.List; +/** + * Handler for bPermissions. + * + * @see bPermissions Bukkit page + * @see bPermissions on Github + */ public class BPermissionsHandler implements PermissionHandler { @Override @@ -49,19 +55,6 @@ public class BPermissionsHandler implements PermissionHandler { return Arrays.asList(ApiLayer.getGroups(player.getWorld().getName(), CalculableType.USER, player.getName())); } - @Override - public String getPrimaryGroup(Player player) { - // Get the groups of the player - List groups = getGroups(player); - - // Make sure there is any group available, or return null - if (groups.isEmpty()) - return null; - - // Return the first group - return groups.get(0); - } - @Override public PermissionsSystemType getPermissionSystem() { return PermissionsSystemType.B_PERMISSIONS; diff --git a/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java index 71c7a2879..36f6497ce 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/PermissionHandler.java @@ -2,9 +2,10 @@ package fr.xephi.authme.permission.handlers; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsSystemType; +import fr.xephi.authme.util.Utils; import org.bukkit.entity.Player; -import java.util.List; +import java.util.Collection; public interface PermissionHandler { @@ -48,7 +49,9 @@ public interface PermissionHandler { * @return True if the player is in the specified group, false otherwise. * False is also returned if groups aren't supported by the used permissions system. */ - boolean isInGroup(Player player, String group); + default boolean isInGroup(Player player, String group) { + return getGroups(player).contains(group); + } /** * Remove the permission group of a player, if supported. @@ -80,7 +83,7 @@ public interface PermissionHandler { * * @return Permission groups, or an empty list if this feature is not supported. */ - List getGroups(Player player); + Collection getGroups(Player player); /** * Get the primary group of a player, if available. @@ -89,7 +92,13 @@ public interface PermissionHandler { * * @return The name of the primary permission group. Or null. */ - String getPrimaryGroup(Player player); + default String getPrimaryGroup(Player player) { + Collection groups = getGroups(player); + if (Utils.isCollectionEmpty(groups)) { + return null; + } + return groups.iterator().next(); + } /** * Get the permission system that is being used. diff --git a/src/main/java/fr/xephi/authme/permission/handlers/PermissionsBukkitHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsBukkitHandler.java index c215b8d17..b7773e7a2 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/PermissionsBukkitHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsBukkitHandler.java @@ -12,6 +12,11 @@ import org.bukkit.plugin.PluginManager; import java.util.ArrayList; import java.util.List; +/** + * Handler for PermissionsBukkit. + * + * @see PermissionsBukkit Bukkit page + */ public class PermissionsBukkitHandler implements PermissionHandler { private PermissionsPlugin permissionsBukkitInstance; @@ -39,13 +44,6 @@ public class PermissionsBukkitHandler implements PermissionHandler { return false; } - @Override - public boolean isInGroup(Player player, String group) { - List groupNames = getGroups(player); - - return groupNames.contains(group); - } - @Override public boolean removeFromGroup(Player player, String group) { return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player removegroup " + player.getName() + " " + group); @@ -65,20 +63,6 @@ public class PermissionsBukkitHandler implements PermissionHandler { return groups; } - @Override - public String getPrimaryGroup(Player player) { - // Get the groups of the player - List groups = getGroups(player); - - // Make sure there is any group available, or return null - if (groups.isEmpty()) { - return null; - } - - // Return the first group - return groups.get(0); - } - @Override public PermissionsSystemType getPermissionSystem() { return PermissionsSystemType.PERMISSIONS_BUKKIT; diff --git a/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java index b11d00003..9b4550d84 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/PermissionsExHandler.java @@ -10,6 +10,12 @@ import ru.tehkode.permissions.bukkit.PermissionsEx; import java.util.ArrayList; import java.util.List; +/** + * Handler for PermissionsEx. + * + * @see PermissionsEx Bukkit page + * @see PermissionsEx on Github + */ public class PermissionsExHandler implements PermissionHandler { private PermissionManager permissionManager; @@ -72,17 +78,6 @@ public class PermissionsExHandler implements PermissionHandler { return user.getParentIdentifiers(null); } - @Override - public String getPrimaryGroup(Player player) { - PermissionUser user = permissionManager.getUser(player); - - List groups = user.getParentIdentifiers(null); - if (groups.isEmpty()) - return null; - - return groups.get(0); - } - @Override public PermissionsSystemType getPermissionSystem() { return PermissionsSystemType.PERMISSIONS_EX; diff --git a/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java index 8955569ec..79badc53e 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/VaultHandler.java @@ -10,6 +10,12 @@ import org.bukkit.plugin.RegisteredServiceProvider; import java.util.Arrays; import java.util.List; +/** + * Handler for permissions via Vault. + * + * @see Vault Bukkit page + * @see Vault on Github + */ public class VaultHandler implements PermissionHandler { private Permission vaultProvider; diff --git a/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java b/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java index 498ba4a91..1de19f1d7 100644 --- a/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java +++ b/src/main/java/fr/xephi/authme/permission/handlers/ZPermissionsHandler.java @@ -6,10 +6,15 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService; -import java.util.ArrayList; -import java.util.List; +import java.util.Collection; import java.util.Map; +/** + * Handler for zPermissions. + * + * @see zPermissions Bukkit page + * @see zPermissions on Github + */ public class ZPermissionsHandler implements PermissionHandler { private ZPermissionsService zPermissionsService; @@ -42,11 +47,6 @@ public class ZPermissionsHandler implements PermissionHandler { return false; } - @Override - public boolean isInGroup(Player player, String group) { - return getGroups(player).contains(group); - } - @Override public boolean removeFromGroup(Player player, String group) { return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "permissions player " + player.getName() + " removegroup " + group); @@ -58,9 +58,9 @@ public class ZPermissionsHandler implements PermissionHandler { } @Override - public List getGroups(Player player) { + public Collection getGroups(Player player) { // TODO Gnat008 20160631: Use UUID not name? - return new ArrayList(zPermissionsService.getPlayerGroups(player.getName())); + return zPermissionsService.getPlayerGroups(player.getName()); } @Override