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
This commit is contained in:
ljacqu 2017-01-29 18:16:17 +01:00
parent 95945ffd22
commit 350ef9b5e6
7 changed files with 51 additions and 64 deletions

View File

@ -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;
/**
* <p>
@ -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<String> getGroups(Player player) {
public Collection<String> 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<String> groupNames = getGroups(player);
Collection<String> groupNames = getGroups(player);
// Remove each group
return removeGroups(player, groupNames.toArray(new String[groupNames.size()]));

View File

@ -9,6 +9,12 @@ import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.List;
/**
* Handler for bPermissions.
*
* @see <a href="https://dev.bukkit.org/projects/bpermissions">bPermissions Bukkit page</a>
* @see <a href="https://github.com/rymate1234/bPermissions/">bPermissions on Github</a>
*/
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<String> 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;

View File

@ -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<String> getGroups(Player player);
Collection<String> 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<String> groups = getGroups(player);
if (Utils.isCollectionEmpty(groups)) {
return null;
}
return groups.iterator().next();
}
/**
* Get the permission system that is being used.

View File

@ -12,6 +12,11 @@ import org.bukkit.plugin.PluginManager;
import java.util.ArrayList;
import java.util.List;
/**
* Handler for PermissionsBukkit.
*
* @see <a href="https://dev.bukkit.org/projects/permbukkit">PermissionsBukkit Bukkit page</a>
*/
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<String> 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<String> 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;

View File

@ -10,6 +10,12 @@ import ru.tehkode.permissions.bukkit.PermissionsEx;
import java.util.ArrayList;
import java.util.List;
/**
* Handler for PermissionsEx.
*
* @see <a href="https://dev.bukkit.org/projects/permissionsex">PermissionsEx Bukkit page</a>
* @see <a href="https://github.com/PEXPlugins/PermissionsEx">PermissionsEx on Github</a>
*/
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<String> groups = user.getParentIdentifiers(null);
if (groups.isEmpty())
return null;
return groups.get(0);
}
@Override
public PermissionsSystemType getPermissionSystem() {
return PermissionsSystemType.PERMISSIONS_EX;

View File

@ -10,6 +10,12 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.Arrays;
import java.util.List;
/**
* Handler for permissions via Vault.
*
* @see <a href="https://dev.bukkit.org/projects/vault">Vault Bukkit page</a>
* @see <a href="https://github.com/milkbowl/Vault">Vault on Github</a>
*/
public class VaultHandler implements PermissionHandler {
private Permission vaultProvider;

View File

@ -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 <a href="https://dev.bukkit.org/projects/zpermissions">zPermissions Bukkit page</a>
* @see <a href="https://github.com/ZerothAngel/zPermissions">zPermissions on Github</a>
*/
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<String> getGroups(Player player) {
public Collection<String> getGroups(Player player) {
// TODO Gnat008 20160631: Use UUID not name?
return new ArrayList<String>(zPermissionsService.getPlayerGroups(player.getName()));
return zPermissionsService.getPlayerGroups(player.getName());
}
@Override