From b19dbb1b8fbe8bbad70693951e37a754e06d7fd3 Mon Sep 17 00:00:00 2001 From: vemacs Date: Tue, 2 Jun 2015 13:08:53 -0600 Subject: [PATCH] Make permissions-plugin specific implementations extend Vault, fix build permission checks --- ...Handler.java => AbstractVaultHandler.java} | 14 +- .../essentials/perm/BPermissions2Handler.java | 42 +----- .../essentials/perm/GenericVaultHandler.java | 4 + .../essentials/perm/GroupManagerHandler.java | 64 +------- .../perm/PermissionsBukkitHandler.java | 66 --------- .../essentials/perm/PermissionsExHandler.java | 76 +--------- .../essentials/perm/PermissionsHandler.java | 122 +++++++--------- .../essentials/perm/PrivilegesHandler.java | 43 ------ .../essentials/perm/SimplyPermsHandler.java | 40 +---- .../essentials/perm/ZPermissionsHandler.java | 138 ------------------ 10 files changed, 65 insertions(+), 544 deletions(-) rename Essentials/src/com/earth2me/essentials/perm/{VaultHandler.java => AbstractVaultHandler.java} (82%) create mode 100644 Essentials/src/com/earth2me/essentials/perm/GenericVaultHandler.java delete mode 100644 Essentials/src/com/earth2me/essentials/perm/PermissionsBukkitHandler.java delete mode 100644 Essentials/src/com/earth2me/essentials/perm/ZPermissionsHandler.java diff --git a/Essentials/src/com/earth2me/essentials/perm/VaultHandler.java b/Essentials/src/com/earth2me/essentials/perm/AbstractVaultHandler.java similarity index 82% rename from Essentials/src/com/earth2me/essentials/perm/VaultHandler.java rename to Essentials/src/com/earth2me/essentials/perm/AbstractVaultHandler.java index bb9c1d479..5c5aa966c 100644 --- a/Essentials/src/com/earth2me/essentials/perm/VaultHandler.java +++ b/Essentials/src/com/earth2me/essentials/perm/AbstractVaultHandler.java @@ -1,24 +1,18 @@ package com.earth2me.essentials.perm; -import com.earth2me.essentials.Essentials; import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.permission.Permission; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import java.util.Arrays; import java.util.List; -public class VaultHandler extends SuperpermsHandler { - - private Essentials plugin; +public abstract class AbstractVaultHandler extends SuperpermsHandler { private static Permission perms = null; private static Chat chat = null; - public VaultHandler(Essentials plugin) { - this.plugin = plugin; - } - public boolean setupProviders() { try { Class.forName("net.milkbowl.vault.permission.Permission"); @@ -27,9 +21,9 @@ public class VaultHandler extends SuperpermsHandler { return false; } - RegisteredServiceProvider permsProvider = plugin.getServer().getServicesManager().getRegistration(Permission.class); + RegisteredServiceProvider permsProvider = Bukkit.getServer().getServicesManager().getRegistration(Permission.class); perms = permsProvider.getProvider(); - RegisteredServiceProvider chatProvider = plugin.getServer().getServicesManager().getRegistration(Chat.class); + RegisteredServiceProvider chatProvider = Bukkit.getServer().getServicesManager().getRegistration(Chat.class); chat = chatProvider.getProvider(); return perms != null && chat != null; } diff --git a/Essentials/src/com/earth2me/essentials/perm/BPermissions2Handler.java b/Essentials/src/com/earth2me/essentials/perm/BPermissions2Handler.java index 067909078..4b2162af1 100644 --- a/Essentials/src/com/earth2me/essentials/perm/BPermissions2Handler.java +++ b/Essentials/src/com/earth2me/essentials/perm/BPermissions2Handler.java @@ -1,51 +1,11 @@ package com.earth2me.essentials.perm; -import de.bananaco.bpermissions.api.ApiLayer; -import de.bananaco.bpermissions.api.util.CalculableType; import org.bukkit.entity.Player; -import java.util.Arrays; -import java.util.List; - - -public class BPermissions2Handler extends SuperpermsHandler { - public BPermissions2Handler() { - - } - - @Override - public String getGroup(final Player base) { - final List groups = getGroups(base); - if (groups == null || groups.isEmpty()) { - return null; - } - return groups.get(0); - } - - @Override - public List getGroups(final Player base) { - final String[] groups = ApiLayer.getGroups(base.getWorld().getName(), CalculableType.USER, base.getName()); - return Arrays.asList(groups); - } - - @Override - public boolean inGroup(final Player base, final String group) { - return ApiLayer.hasGroupRecursive(base.getWorld().getName(), CalculableType.USER, base.getName(), group); - } +public class BPermissions2Handler extends AbstractVaultHandler { @Override public boolean canBuild(final Player base, final String group) { return hasPermission(base, "bPermissions.build"); } - - @Override - public String getPrefix(final Player base) { - return ApiLayer.getValue(base.getWorld().getName(), CalculableType.USER, base.getName(), "prefix"); - } - - @Override - public String getSuffix(final Player base) { - return ApiLayer.getValue(base.getWorld().getName(), CalculableType.USER, base.getName(), "suffix"); - } - } diff --git a/Essentials/src/com/earth2me/essentials/perm/GenericVaultHandler.java b/Essentials/src/com/earth2me/essentials/perm/GenericVaultHandler.java new file mode 100644 index 000000000..2a41489b3 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/perm/GenericVaultHandler.java @@ -0,0 +1,4 @@ +package com.earth2me.essentials.perm; + +public class GenericVaultHandler extends AbstractVaultHandler { +} diff --git a/Essentials/src/com/earth2me/essentials/perm/GroupManagerHandler.java b/Essentials/src/com/earth2me/essentials/perm/GroupManagerHandler.java index 84894c79f..472eef037 100644 --- a/Essentials/src/com/earth2me/essentials/perm/GroupManagerHandler.java +++ b/Essentials/src/com/earth2me/essentials/perm/GroupManagerHandler.java @@ -6,78 +6,18 @@ import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -import java.util.Arrays; -import java.util.List; - -public class GroupManagerHandler implements IPermissionsHandler { +public class GroupManagerHandler extends AbstractVaultHandler { private final transient GroupManager groupManager; public GroupManagerHandler(final Plugin permissionsPlugin) { groupManager = ((GroupManager) permissionsPlugin); } - @Override - public String getGroup(final Player base) { - final AnjoPermissionsHandler handler = getHandler(base); - if (handler == null) { - return null; - } - return handler.getGroup(base.getName()); - } - - @Override - public List getGroups(final Player base) { - final AnjoPermissionsHandler handler = getHandler(base); - if (handler == null) { - return null; - } - return Arrays.asList(handler.getGroups(base.getName())); - } - @Override public boolean canBuild(final Player base, final String group) { final AnjoPermissionsHandler handler = getHandler(base); - if (handler == null) { - return false; - } - return handler.canUserBuild(base.getName()); - } - - @Override - public boolean inGroup(final Player base, final String group) { - AnjoPermissionsHandler handler = getHandler(base); - if (handler == null) { - return false; - } - return handler.inGroup(base.getName(), group); - } - - @Override - public boolean hasPermission(final Player base, final String node) { - AnjoPermissionsHandler handler = getHandler(base); - if (handler == null) { - return false; - } - return handler.has(base, node); - } - - @Override - public String getPrefix(final Player base) { - AnjoPermissionsHandler handler = getHandler(base); - if (handler == null) { - return null; - } - return handler.getUserPrefix(base.getName()); - } - - @Override - public String getSuffix(final Player base) { - AnjoPermissionsHandler handler = getHandler(base); - if (handler == null) { - return null; - } - return handler.getUserSuffix(base.getName()); + return handler != null && handler.canUserBuild(base.getName()); } private AnjoPermissionsHandler getHandler(final Player base) { diff --git a/Essentials/src/com/earth2me/essentials/perm/PermissionsBukkitHandler.java b/Essentials/src/com/earth2me/essentials/perm/PermissionsBukkitHandler.java deleted file mode 100644 index ae29b85a8..000000000 --- a/Essentials/src/com/earth2me/essentials/perm/PermissionsBukkitHandler.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.earth2me.essentials.perm; - -import com.platymuus.bukkit.permissions.Group; -import com.platymuus.bukkit.permissions.PermissionInfo; -import com.platymuus.bukkit.permissions.PermissionsPlugin; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - - -public class PermissionsBukkitHandler extends SuperpermsHandler { - private final transient PermissionsPlugin plugin; - - public PermissionsBukkitHandler(final Plugin plugin) { - this.plugin = (PermissionsPlugin) plugin; - } - - @Override - public String getGroup(final Player base) { - final List groups = getPBGroups(base); - if (groups == null || groups.isEmpty()) { - return null; - } - return groups.get(0).getName(); - } - - @Override - public List getGroups(final Player base) { - final List groups = getPBGroups(base); - if (groups.size() == 1) { - return Collections.singletonList(groups.get(0).getName()); - } - final List groupNames = new ArrayList(groups.size()); - for (Group group : groups) { - groupNames.add(group.getName()); - } - return groupNames; - } - - private List getPBGroups(final Player base) { - final PermissionInfo info = plugin.getPlayerInfo(base.getName()); - if (info == null) { - return Collections.emptyList(); - } - final List groups = info.getGroups(); - if (groups == null || groups.isEmpty()) { - return Collections.emptyList(); - } - return groups; - } - - @Override - public boolean inGroup(final Player base, final String group) { - final List groups = getPBGroups(base); - for (Group group1 : groups) { - if (group1.getName().equalsIgnoreCase(group)) { - return true; - } - } - return false; - } - -} diff --git a/Essentials/src/com/earth2me/essentials/perm/PermissionsExHandler.java b/Essentials/src/com/earth2me/essentials/perm/PermissionsExHandler.java index 0173bb387..6ceb621ab 100644 --- a/Essentials/src/com/earth2me/essentials/perm/PermissionsExHandler.java +++ b/Essentials/src/com/earth2me/essentials/perm/PermissionsExHandler.java @@ -5,88 +5,16 @@ import ru.tehkode.permissions.PermissionManager; import ru.tehkode.permissions.PermissionUser; import ru.tehkode.permissions.bukkit.PermissionsEx; -import java.util.Arrays; -import java.util.List; - - -public class PermissionsExHandler extends SuperpermsHandler { +public class PermissionsExHandler extends AbstractVaultHandler { private final transient PermissionManager manager; public PermissionsExHandler() { manager = PermissionsEx.getPermissionManager(); } - @Override - public String getGroup(final Player base) { - final PermissionUser user = manager.getUser(base.getName()); - if (user == null) { - return null; - } - - String[] groupsNames = user.getGroupsNames(); - if (groupsNames == null || groupsNames.length == 0) { - return null; - } - - return groupsNames[0]; - } - - @Override - public List getGroups(final Player base) { - final PermissionUser user = manager.getUser(base.getName()); - if (user == null) { - return null; - } - - String[] groupsNames = user.getGroupsNames(); - if (groupsNames == null) { - return null; - } - - return Arrays.asList(groupsNames); - } - @Override public boolean canBuild(final Player base, final String group) { final PermissionUser user = manager.getUser(base.getName()); - if (user == null) { - return false; - } - - return user.getOptionBoolean("build", base.getWorld().getName(), false); - } - - @Override - public boolean inGroup(final Player base, final String group) { - final PermissionUser user = manager.getUser(base.getName()); - if (user == null) { - return false; - } - - return user.inGroup(group); - } - - @Override - public boolean hasPermission(final Player base, final String node) { - return super.hasPermission(base, node); - } - - @Override - public String getPrefix(final Player base) { - final PermissionUser user = manager.getUser(base.getName()); - if (user == null) { - return null; - } - return user.getPrefix(base.getWorld().getName()); - } - - @Override - public String getSuffix(final Player base) { - final PermissionUser user = manager.getUser(base.getName()); - if (user == null) { - return null; - } - - return user.getSuffix(base.getWorld().getName()); + return user != null && user.getOptionBoolean("build", base.getWorld().getName(), false); } } diff --git a/Essentials/src/com/earth2me/essentials/perm/PermissionsHandler.java b/Essentials/src/com/earth2me/essentials/perm/PermissionsHandler.java index 724bdc5f0..cad220806 100644 --- a/Essentials/src/com/earth2me/essentials/perm/PermissionsHandler.java +++ b/Essentials/src/com/earth2me/essentials/perm/PermissionsHandler.java @@ -15,7 +15,6 @@ public class PermissionsHandler implements IPermissionsHandler { private transient IPermissionsHandler handler = new NullPermissionsHandler(); private transient String defaultGroup = "default"; private final transient Essentials ess; - private static final Logger LOGGER = Logger.getLogger("Essentials"); private transient boolean useSuperperms = false; public PermissionsHandler(final Essentials plugin) { @@ -96,91 +95,72 @@ public class PermissionsHandler implements IPermissionsHandler { public void checkPermissions() { final PluginManager pluginManager = ess.getServer().getPluginManager(); - final Plugin vaultAPI = pluginManager.getPlugin("Vault"); if (vaultAPI != null && vaultAPI.isEnabled()) { - if (!(handler instanceof VaultHandler)) { - VaultHandler vault = new VaultHandler(ess); + final Plugin permExPlugin = pluginManager.getPlugin("PermissionsEx"); + if (permExPlugin != null && permExPlugin.isEnabled()) { + if (!(handler instanceof PermissionsExHandler)) { + ess.getLogger().info("Using PermissionsEX based permissions with Vault."); + handler = new PermissionsExHandler(); + } + return; + } + + final Plugin GMplugin = pluginManager.getPlugin("GroupManager"); + if (GMplugin != null && GMplugin.isEnabled()) { + if (!(handler instanceof GroupManagerHandler)) { + ess.getLogger().info("Using GroupManager based permissions with Vault."); + handler = new GroupManagerHandler(GMplugin); + } + return; + } + + final Plugin simplyPermsPlugin = pluginManager.getPlugin("SimplyPerms"); + if (simplyPermsPlugin != null && simplyPermsPlugin.isEnabled()) { + if (!(handler instanceof SimplyPermsHandler)) { + ess.getLogger().info("Using SimplyPerms based permissions with Vault."); + handler = new SimplyPermsHandler(); + } + return; + } + + final Plugin privPlugin = pluginManager.getPlugin("Privileges"); + if (privPlugin != null && privPlugin.isEnabled()) { + if (!(handler instanceof PrivilegesHandler)) { + ess.getLogger().info("Using Privileges based permissions with Vault."); + handler = new PrivilegesHandler(); + } + return; + } + + final Plugin bPermPlugin = pluginManager.getPlugin("bPermissions"); + if (bPermPlugin != null && bPermPlugin.isEnabled()) { + if (!(handler instanceof BPermissions2Handler)) { + ess.getLogger().info("Using bPermissions based permissions with Vault."); + handler = new BPermissions2Handler(); + } + return; + } + + if (!(handler instanceof GenericVaultHandler)) { + AbstractVaultHandler vault = new GenericVaultHandler(); if (vault.setupProviders()) { - LOGGER.log(Level.INFO, "Essentials: Using Vault based permissions."); + ess.getLogger().info("Using generic Vault based permissions."); handler = vault; } } return; } - final Plugin permExPlugin = pluginManager.getPlugin("PermissionsEx"); - if (permExPlugin != null && permExPlugin.isEnabled()) { - if (!(handler instanceof PermissionsExHandler)) { - LOGGER.log(Level.INFO, "Essentials: Using PermissionsEx based permissions."); - handler = new PermissionsExHandler(); - } - return; - } - - final Plugin GMplugin = pluginManager.getPlugin("GroupManager"); - if (GMplugin != null && GMplugin.isEnabled()) { - if (!(handler instanceof GroupManagerHandler)) { - LOGGER.log(Level.INFO, "Essentials: Using GroupManager based permissions."); - handler = new GroupManagerHandler(GMplugin); - } - return; - } - - final Plugin permBukkitPlugin = pluginManager.getPlugin("PermissionsBukkit"); - if (permBukkitPlugin != null && permBukkitPlugin.isEnabled()) { - if (!(handler instanceof PermissionsBukkitHandler)) { - LOGGER.log(Level.INFO, "Essentials: Using PermissionsBukkit based permissions."); - handler = new PermissionsBukkitHandler(permBukkitPlugin); - } - return; - } - - final Plugin simplyPermsPlugin = pluginManager.getPlugin("SimplyPerms"); - if (simplyPermsPlugin != null && simplyPermsPlugin.isEnabled()) { - if (!(handler instanceof SimplyPermsHandler)) { - LOGGER.log(Level.INFO, "Essentials: Using SimplyPerms based permissions."); - handler = new SimplyPermsHandler(simplyPermsPlugin); - } - return; - } - - final Plugin privPlugin = pluginManager.getPlugin("Privileges"); - if (privPlugin != null && privPlugin.isEnabled()) { - if (!(handler instanceof PrivilegesHandler)) { - LOGGER.log(Level.INFO, "Essentials: Using Privileges based permissions."); - handler = new PrivilegesHandler(privPlugin); - } - return; - } - - final Plugin bPermPlugin = pluginManager.getPlugin("bPermissions"); - if (bPermPlugin != null && bPermPlugin.isEnabled()) { - if (!(handler instanceof BPermissions2Handler)) { - LOGGER.log(Level.INFO, "Essentials: Using bPermissions2 based permissions."); - handler = new BPermissions2Handler(); - } - return; - } - - final Plugin zPermsPlugin = pluginManager.getPlugin("zPermissions"); - if (zPermsPlugin != null && zPermsPlugin.isEnabled()) { - if (!(handler instanceof ZPermissionsHandler)) { - LOGGER.log(Level.INFO, "Essentials: Using zPermissions based permissions."); - handler = new ZPermissionsHandler(ess); - } - return; - } - if (useSuperperms) { if (!(handler instanceof SuperpermsHandler)) { - LOGGER.log(Level.INFO, "Essentials: Using superperms based permissions."); + ess.getLogger().info("Using superperms based permissions."); handler = new SuperpermsHandler(); } } else { if (!(handler instanceof ConfigPermissionsHandler)) { - LOGGER.log(Level.INFO, "Essentials: Using config file enhanced permissions."); - LOGGER.log(Level.INFO, "Permissions listed in as player-commands will be given to all users."); + ess.getLogger().info("Essentials: Using config file enhanced permissions."); + ess.getLogger().info("Permissions listed in as player-commands will be given to all users."); handler = new ConfigPermissionsHandler(ess); } } diff --git a/Essentials/src/com/earth2me/essentials/perm/PrivilegesHandler.java b/Essentials/src/com/earth2me/essentials/perm/PrivilegesHandler.java index 43adbbb2d..a2b01dc76 100644 --- a/Essentials/src/com/earth2me/essentials/perm/PrivilegesHandler.java +++ b/Essentials/src/com/earth2me/essentials/perm/PrivilegesHandler.java @@ -1,53 +1,10 @@ package com.earth2me.essentials.perm; -import net.krinsoft.privileges.Privileges; -import net.krinsoft.privileges.groups.Group; -import net.krinsoft.privileges.groups.GroupManager; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import java.util.ArrayList; -import java.util.List; public class PrivilegesHandler extends SuperpermsHandler { - private final transient Privileges plugin; - private final GroupManager manager; - - public PrivilegesHandler(final Plugin plugin) { - this.plugin = (Privileges) plugin; - this.manager = this.plugin.getGroupManager(); - } - - @Override - public String getGroup(final Player base) { - Group group = manager.getGroup(base); - if (group == null) { - return null; - } - return group.getName(); - } - - @Override - public List getGroups(final Player base) { - Group group = manager.getGroup(base); - if (group == null) { - return new ArrayList(); - } - return group.getGroupTree(); - } - - @Override - public boolean inGroup(final Player base, final String group) { - Group pGroup = manager.getGroup(base); - if (pGroup == null) { - return false; - } - return pGroup.isMemberOf(group); - } - @Override public boolean canBuild(Player base, String group) { return hasPermission(base, "privileges.build"); } - } diff --git a/Essentials/src/com/earth2me/essentials/perm/SimplyPermsHandler.java b/Essentials/src/com/earth2me/essentials/perm/SimplyPermsHandler.java index f8c935482..703d8650c 100644 --- a/Essentials/src/com/earth2me/essentials/perm/SimplyPermsHandler.java +++ b/Essentials/src/com/earth2me/essentials/perm/SimplyPermsHandler.java @@ -1,48 +1,10 @@ package com.earth2me.essentials.perm; -import net.crystalyx.bukkit.simplyperms.SimplyAPI; -import net.crystalyx.bukkit.simplyperms.SimplyPlugin; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import java.util.List; - -public class SimplyPermsHandler extends SuperpermsHandler { - - private final transient SimplyAPI api; - - public SimplyPermsHandler(final Plugin plugin) { - this.api = ((SimplyPlugin) plugin).getAPI(); - } - - @Override - public String getGroup(final Player base) { - final List groups = api.getPlayerGroups(base.getName()); - if (groups == null || groups.isEmpty()) { - return null; - } - return groups.get(0); - } - - @Override - public List getGroups(final Player base) { - return api.getPlayerGroups(base.getName()); - } - - @Override - public boolean inGroup(final Player base, final String group) { - final List groups = api.getPlayerGroups(base.getName()); - for (String group1 : groups) { - if (group1.equalsIgnoreCase(group)) { - return true; - } - } - return false; - } +public class SimplyPermsHandler extends AbstractVaultHandler { @Override public boolean canBuild(Player base, String group) { return hasPermission(base, "permissions.allow.build"); } - } diff --git a/Essentials/src/com/earth2me/essentials/perm/ZPermissionsHandler.java b/Essentials/src/com/earth2me/essentials/perm/ZPermissionsHandler.java deleted file mode 100644 index 2c356f76e..000000000 --- a/Essentials/src/com/earth2me/essentials/perm/ZPermissionsHandler.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.earth2me.essentials.perm; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.server.ServiceRegisterEvent; -import org.bukkit.plugin.Plugin; -import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.UUID; - - -public class ZPermissionsHandler extends SuperpermsHandler implements Listener { - private ZPermissionsService service = null; - private boolean hasGetPlayerPrimaryGroup = false; // This is a post-1.0 addition - - public ZPermissionsHandler(Plugin plugin) { - acquireZPermissionsService(); - if (!isReady()) { - // Shouldn't get to this point, since caller checks if zPerms - // is enabled. But for the sake of correctness... - Bukkit.getPluginManager().registerEvents(this, plugin); - } - } - - @EventHandler - public void onServiceRegister(ServiceRegisterEvent event) { - if (ZPermissionsService.class.equals(event.getProvider().getService())) { - acquireZPermissionsService(); - } - } - - @Override - public String getGroup(Player base) { - if (!isReady()) { - return super.getGroup(base); - } else { - return getPrimaryGroup(base.getUniqueId()); - } - } - - @Override - public List getGroups(Player base) { - if (!isReady()) { - return super.getGroups(base); - } else { - return new ArrayList(service.getPlayerGroups(base.getUniqueId())); - } - } - - @Override - public boolean inGroup(Player base, String group) { - if (!isReady()) { - return super.inGroup(base, group); - } else { - Set groups = service.getPlayerGroups(base.getUniqueId()); - for (String test : groups) { - if (test.equalsIgnoreCase(group)) { - return true; - } - } - return false; - } - } - - @Override - public String getPrefix(Player base) { - if (!isReady()) { - return super.getPrefix(base); - } else { - return getPrefixSuffix(base, "prefix"); - } - } - - @Override - public String getSuffix(Player base) { - if (!isReady()) { - return super.getSuffix(base); - } else { - return getPrefixSuffix(base, "suffix"); - } - } - - private String getPrefixSuffix(Player base, String metadataName) { - String playerPrefixSuffix; - try { - playerPrefixSuffix = service.getPlayerMetadata(base.getUniqueId(), metadataName, String.class); - } catch (IllegalStateException e) { - // User error. They set prefix to a non-string. - playerPrefixSuffix = null; - } - if (playerPrefixSuffix == null) { - // Try prefix/suffix of their "primary group" - try { - return service.getGroupMetadata(getPrimaryGroup(base.getUniqueId()), metadataName, String.class); - } catch (IllegalStateException e) { - // User error, again - return null; - } - } else { - return playerPrefixSuffix; - } - } - - private void acquireZPermissionsService() { - service = Bukkit.getServicesManager().load(ZPermissionsService.class); - if (isReady()) { - // getPlayerPrimaryGroup(String) was added in an unreleased version - // Check if it exists. - try { - service.getClass().getMethod("getPlayerPrimaryGroup", String.class); - hasGetPlayerPrimaryGroup = true; - } catch (NoSuchMethodException e) { - hasGetPlayerPrimaryGroup = false; - } catch (SecurityException e) { - hasGetPlayerPrimaryGroup = false; - } - } - } - - private boolean isReady() { - return service != null; - } - - private String getPrimaryGroup(UUID playerId) { - if (hasGetPlayerPrimaryGroup) { - return service.getPlayerPrimaryGroup(playerId); - } else { - // Fall back to using highest-weight assigned group - List groups = service.getPlayerAssignedGroups(playerId); - return groups.get(0); - } - } -}