Simplify instantiation of permission handlers

- Change permission handler to retrieve the services it needs, instead of getting them from the PermissionsManager
- Reduce visibility of some methods
- Add Reloadable interface
This commit is contained in:
ljacqu 2016-06-14 19:09:45 +02:00
parent ff2f8a63bb
commit 1377b10c24
5 changed files with 87 additions and 93 deletions

View File

@ -1,25 +1,22 @@
package fr.xephi.authme.permission;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.permission.handlers.BPermissionsHandler;
import fr.xephi.authme.permission.handlers.GroupManagerHandler;
import fr.xephi.authme.permission.handlers.PermissionHandler;
import fr.xephi.authme.permission.handlers.PermissionHandlerException;
import fr.xephi.authme.permission.handlers.PermissionsBukkitHandler;
import fr.xephi.authme.permission.handlers.PermissionsExHandler;
import fr.xephi.authme.permission.handlers.VaultHandler;
import fr.xephi.authme.permission.handlers.ZPermissionsHandler;
import fr.xephi.authme.util.StringUtils;
import net.milkbowl.vault.permission.Permission;
import org.anjocaido.groupmanager.GroupManager;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService;
import ru.tehkode.permissions.bukkit.PermissionsEx;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
@ -38,7 +35,7 @@ import java.util.List;
* @author Tim Visée, http://timvisee.com
* @version 0.3
*/
public class PermissionsManager {
public class PermissionsManager implements Reloadable {
private final Server server;
private final PluginManager pluginManager;
@ -74,90 +71,17 @@ public class PermissionsManager {
* Setup and hook into the permissions systems.
*/
@PostConstruct
public void setup() {
// Force-unhook from current hooked permissions systems
unhook();
private void setup() {
// Loop through all the available permissions system types
for (PermissionsSystemType type : PermissionsSystemType.values()) {
// Try to find and hook the current plugin if available, print an error if failed
try {
// Try to find the plugin for the current permissions system
Plugin plugin = pluginManager.getPlugin(type.getPluginName());
// Make sure a plugin with this name was found
if (plugin == null)
continue;
// Make sure the plugin is enabled before hooking
if (!plugin.isEnabled()) {
ConsoleLogger.info("Not hooking into " + type.getName() + " because it's disabled!");
continue;
}
// Use the proper method to hook this plugin
switch (type) {
case PERMISSIONS_EX:
// Get the permissions manager for PermissionsEx and make sure it isn't null
if (PermissionsEx.getPermissionManager() == null) {
ConsoleLogger.info("Failed to hook into " + type.getName() + "!");
continue;
}
handler = new PermissionsExHandler(PermissionsEx.getPermissionManager());
break;
case ESSENTIALS_GROUP_MANAGER:
// Set the plugin instance
handler = new GroupManagerHandler((GroupManager) plugin);
break;
case Z_PERMISSIONS:
// Set the zPermissions service and make sure it's valid
ZPermissionsService zPermissionsService = Bukkit.getServicesManager().load(ZPermissionsService.class);
if (zPermissionsService == null) {
ConsoleLogger.info("Failed to hook into " + type.getName() + "!");
continue;
}
handler = new ZPermissionsHandler(zPermissionsService);
break;
case VAULT:
// Get the permissions provider service
RegisteredServiceProvider<Permission> permissionProvider = this.server.getServicesManager().getRegistration(Permission.class);
if (permissionProvider == null) {
ConsoleLogger.info("Failed to hook into " + type.getName() + "!");
continue;
}
// Get the Vault provider and make sure it's valid
Permission vaultPerms = permissionProvider.getProvider();
if (vaultPerms == null) {
ConsoleLogger.info("Not using " + type.getName() + " because it's disabled!");
continue;
}
handler = new VaultHandler(vaultPerms);
break;
case B_PERMISSIONS:
handler = new BPermissionsHandler();
break;
case PERMISSIONS_BUKKIT:
handler = new PermissionsBukkitHandler();
break;
default:
}
// Show a success message
PermissionHandler handler = getPermissionHandler(type);
if (handler != null) {
// Show a success message and return
this.handler = handler;
ConsoleLogger.info("Hooked into " + type.getName() + "!");
// Return the used permissions system type
return;
}
} catch (Exception ex) {
// An error occurred, show a warning message
ConsoleLogger.logException("Error while hooking into " + type.getName(), ex);
@ -168,10 +92,42 @@ public class PermissionsManager {
ConsoleLogger.info("No supported permissions system found! Permissions are disabled!");
}
private PermissionHandler getPermissionHandler(PermissionsSystemType type) throws PermissionHandlerException {
// Try to find the plugin for the current permissions system
Plugin plugin = pluginManager.getPlugin(type.getPluginName());
if (plugin == null) {
return null;
}
// Make sure the plugin is enabled before hooking
if (!plugin.isEnabled()) {
ConsoleLogger.info("Not hooking into " + type.getName() + " because it's disabled!");
return null;
}
switch (type) {
case PERMISSIONS_EX:
return new PermissionsExHandler();
case ESSENTIALS_GROUP_MANAGER:
return new GroupManagerHandler((GroupManager) plugin);
case Z_PERMISSIONS:
return new ZPermissionsHandler();
case VAULT:
return new VaultHandler(server);
case B_PERMISSIONS:
return new BPermissionsHandler();
case PERMISSIONS_BUKKIT:
return new PermissionsBukkitHandler();
default:
throw new IllegalStateException("Unhandled permission type '" + type + "'");
}
}
/**
* Break the hook with all permission systems.
*/
public void unhook() {
private void unhook() {
// Reset the current used permissions system
this.handler = null;
@ -182,11 +138,12 @@ public class PermissionsManager {
/**
* Reload the permissions manager, and re-hook all permission plugins.
*/
@Override
public void reload() {
// Unhook all permission plugins
unhook();
// Set up the permissions manager again, return the result
// Set up the permissions manager again
setup();
}

View File

@ -0,0 +1,11 @@
package fr.xephi.authme.permission.handlers;
/**
* Exception during the instantiation of a {@link PermissionHandler}.
*/
public class PermissionHandlerException extends Exception {
public PermissionHandlerException(String message) {
super(message);
}
}

View File

@ -15,8 +15,11 @@ public class PermissionsExHandler implements PermissionHandler {
private PermissionManager permissionManager;
public PermissionsExHandler(PermissionManager permissionManager) {
this.permissionManager = permissionManager;
public PermissionsExHandler() throws PermissionHandlerException {
permissionManager = PermissionsEx.getPermissionManager();
if (permissionManager == null) {
throw new PermissionHandlerException("Could not get manager of PermissionsEx");
}
}
@Override

View File

@ -3,7 +3,9 @@ package fr.xephi.authme.permission.handlers;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsSystemType;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.Arrays;
import java.util.List;
@ -12,8 +14,24 @@ public class VaultHandler implements PermissionHandler {
private Permission vaultProvider;
public VaultHandler(Permission vaultProvider) {
this.vaultProvider = vaultProvider;
public VaultHandler(Server server) throws PermissionHandlerException {
this.vaultProvider = getVaultPermission(server);
}
private static Permission getVaultPermission(Server server) throws PermissionHandlerException {
// Get the permissions provider service
RegisteredServiceProvider<Permission> permissionProvider = server
.getServicesManager().getRegistration(Permission.class);
if (permissionProvider == null) {
throw new PermissionHandlerException("Could not load permissions provider service");
}
// Get the Vault provider and make sure it's valid
Permission vaultPerms = permissionProvider.getProvider();
if (vaultPerms == null) {
throw new PermissionHandlerException("Could not load Vault permissions provider");
}
return vaultPerms;
}
@Override

View File

@ -14,7 +14,12 @@ public class ZPermissionsHandler implements PermissionHandler {
private ZPermissionsService zPermissionsService;
public ZPermissionsHandler(ZPermissionsService zPermissionsService) {
public ZPermissionsHandler() throws PermissionHandlerException {
// Set the zPermissions service and make sure it's valid
ZPermissionsService zPermissionsService = Bukkit.getServicesManager().load(ZPermissionsService.class);
if (zPermissionsService == null) {
throw new PermissionHandlerException("Failed to get the ZPermissions service!");
}
this.zPermissionsService = zPermissionsService;
}