Applied most PermissionsManager patches from original source

This commit is contained in:
Tim Vis'ee 2016-03-11 11:33:37 +01:00
parent 558b3fd997
commit ae9cd626a5
2 changed files with 150 additions and 148 deletions

View File

@ -24,7 +24,6 @@ import de.bananaco.bpermissions.api.CalculableType;
import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.CollectionUtils;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import ru.tehkode.permissions.PermissionManager;
import ru.tehkode.permissions.PermissionUser; import ru.tehkode.permissions.PermissionUser;
import ru.tehkode.permissions.bukkit.PermissionsEx; import ru.tehkode.permissions.bukkit.PermissionsEx;
@ -38,7 +37,7 @@ import ru.tehkode.permissions.bukkit.PermissionsEx;
* Written by Tim Visée. * Written by Tim Visée.
* </p> * </p>
* @author Tim Visée, http://timvisee.com * @author Tim Visée, http://timvisee.com
* @version 0.2.1 * @version 0.3
*/ */
public class PermissionsManager implements PermissionsService { public class PermissionsManager implements PermissionsService {
@ -60,8 +59,9 @@ public class PermissionsManager implements PermissionsService {
private Logger log; private Logger log;
/** /**
* Type of permissions system that is currently used. * Type of permissions system that is currently used.
* Null if no permissions system is hooked and/or used.
*/ */
private PermissionsSystemType permsType = PermissionsSystemType.NONE; private PermissionsSystemType permsType = null;
/** /**
* Essentials group manager instance. * Essentials group manager instance.
*/ */
@ -90,7 +90,7 @@ public class PermissionsManager implements PermissionsService {
* @return False if there isn't any permissions system used. * @return False if there isn't any permissions system used.
*/ */
public boolean isEnabled() { public boolean isEnabled() {
return !permsType.equals(PermissionsSystemType.NONE); return permsType != null;
} }
/** /**
@ -108,110 +108,96 @@ public class PermissionsManager implements PermissionsService {
* @return The detected permissions system. * @return The detected permissions system.
*/ */
public PermissionsSystemType setup() { public PermissionsSystemType setup() {
// Force-unhook from current hooked permissions systems
unhook();
// Define the plugin manager // Define the plugin manager
final PluginManager pm = this.server.getPluginManager(); final PluginManager pluginManager = this.server.getPluginManager();
// Reset used permissions system type // Reset used permissions system type flag
permsType = PermissionsSystemType.NONE; permsType = null;
// PermissionsEx, check if it's available // 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 {
Plugin pex = pm.getPlugin("PermissionsEx"); // Try to find the plugin for the current permissions system
if (pex != null) { Plugin plugin = pluginManager.getPlugin(type.getPluginName());
PermissionManager pexPerms = PermissionsEx.getPermissionManager();
if (pexPerms != null) {
permsType = PermissionsSystemType.PERMISSIONS_EX;
System.out.println("[" + plugin.getName() + "] Hooked into PermissionsEx!"); // Make sure a plugin with this name was found
return permsType; if(plugin == null)
} continue;
}
} catch (Exception ex) { // Make sure the plugin is enabled before hooking
// An error occurred, show a warning message if(!plugin.isEnabled()) {
System.out.println("[" + plugin.getName() + "] Error while hooking into PermissionsEx!"); this.log.info("Not hooking into " + type.getName() + " because it's disabled!");
continue;
} }
// PermissionsBukkit, check if it's available // Use the proper method to hook this plugin
try { switch(type) {
Plugin bukkitPerms = pm.getPlugin("PermissionsBukkit"); case PERMISSIONS_EX:
if (bukkitPerms != null) { // Get the permissions manager for PermissionsEx and make sure it isn't null
permsType = PermissionsSystemType.PERMISSIONS_BUKKIT; if(PermissionsEx.getPermissionManager() == null) {
System.out.println("[" + plugin.getName() + "] Hooked into PermissionsBukkit!"); this.log.info("Failed to hook into " + type.getName() + "!");
return permsType; continue;
}
} catch (Exception ex) {
// An error occurred, show a warning message
System.out.println("[" + plugin.getName() + "] Error while hooking into PermissionsBukkit!");
} }
// bPermissions, check if it's available break;
try {
Plugin bPerms = pm.getPlugin("bPermissions");
if (bPerms != null) {
permsType = PermissionsSystemType.B_PERMISSIONS;
System.out.println("[" + plugin.getName() + "] Hooked into bPermissions!");
return permsType;
}
} catch (Exception ex) {
// An error occurred, show a warning message
System.out.println("[" + plugin.getName() + "] Error while hooking into bPermissions!");
}
// Essentials Group Manager, check if it's available case ESSENTIALS_GROUP_MANAGER:
try { // Set the plugin instance
final Plugin groupManagerPlugin = pm.getPlugin("GroupManager"); groupManagerPerms = (GroupManager) plugin;
if (groupManagerPlugin != null && groupManagerPlugin.isEnabled()) { break;
permsType = PermissionsSystemType.ESSENTIALS_GROUP_MANAGER;
groupManagerPerms = (GroupManager) groupManagerPlugin;
System.out.println("[" + plugin.getName() + "] Hooked into Essentials Group Manager!");
return permsType;
}
} catch (Exception ex) {
// An error occurred, show a warning message
System.out.println("[" + plugin.getName() + "] Error while hooking into Essentials Group Manager!");
}
// zPermissions, check if it's available case Z_PERMISSIONS:
try { // Set the zPermissions service and make sure it's valid
Plugin zPerms = pm.getPlugin("zPermissions");
if (zPerms != null) {
zPermissionsService = Bukkit.getServicesManager().load(ZPermissionsService.class); zPermissionsService = Bukkit.getServicesManager().load(ZPermissionsService.class);
if (zPermissionsService != null) { if(zPermissionsService == null) {
permsType = PermissionsSystemType.Z_PERMISSIONS; this.log.info("Failed to hook into " + type.getName() + "!");
System.out.println("[" + plugin.getName() + "] Hooked into zPermissions!"); continue;
return permsType;
}
}
} catch (Exception ex) {
// An error occurred, show a warning message
System.out.println("[" + plugin.getName() + "] Error while hooking into zPermissions!");
} }
// Vault, check if it's available break;
try {
final Plugin vaultPlugin = pm.getPlugin("Vault"); case VAULT:
if (vaultPlugin != null && vaultPlugin.isEnabled()) { // Get the permissions provider service
RegisteredServiceProvider<Permission> permissionProvider = this.server.getServicesManager().getRegistration(Permission.class); RegisteredServiceProvider<Permission> permissionProvider = this.server.getServicesManager().getRegistration(Permission.class);
if (permissionProvider != null) { if (permissionProvider == null) {
vaultPerms = permissionProvider.getProvider(); this.log.info("Failed to hook into " + type.getName() + "!");
if (vaultPerms.isEnabled()) { continue;
permsType = PermissionsSystemType.VAULT;
System.out.println("[" + plugin.getName() + "] Hooked into Vault Permissions!");
return permsType;
} else {
System.out.println("[" + plugin.getName() + "] Not using Vault Permissions, Vault Permissions is disabled!");
}
}
}
} catch (Exception ex) {
// An error occurred, show a warning message
System.out.println("[" + plugin.getName() + "] Error while hooking into Vault Permissions!");
} }
// No recognized permissions system found // Get the Vault provider and make sure it's valid
permsType = PermissionsSystemType.NONE; vaultPerms = permissionProvider.getProvider();
System.out.println("[" + plugin.getName() + "] No supported permissions system found! Permissions disabled!"); if(vaultPerms == null) {
return PermissionsSystemType.NONE; this.log.info("Not using " + type.getName() + " because it's disabled!");
continue;
}
break;
default:
}
// Set the hooked permissions system type
this.permsType = type;
// Show a success message
this.log.info("Hooked into " + type.getName() + "!");
// Return the used permissions system type
return type;
} catch (Exception ex) {
// An error occurred, show a warning message
this.log.info("Error while hooking into " + type.getName() + "!");
}
}
// No recognized permissions system found, show a message and return
this.log.info("No supported permissions system found! Permissions are disabled!");
return null;
} }
/** /**
@ -219,7 +205,7 @@ public class PermissionsManager implements PermissionsService {
*/ */
public void unhook() { public void unhook() {
// Reset the current used permissions system // Reset the current used permissions system
this.permsType = PermissionsSystemType.NONE; this.permsType = null;
// Print a status message to the console // Print a status message to the console
this.log.info("Unhooked from Permissions!"); this.log.info("Unhooked from Permissions!");
@ -383,13 +369,9 @@ public class PermissionsManager implements PermissionsService {
// Vault // Vault
return vaultPerms.has(player, permsNode); return vaultPerms.has(player, permsNode);
case NONE: default:
// Not hooked into any permissions system, return default // Not hooked into any permissions system, return default
return def; return def;
default:
// Something went wrong, return false to prevent problems
return false;
} }
} }
@ -416,12 +398,8 @@ public class PermissionsManager implements PermissionsService {
// Vault // Vault
return vaultPerms.hasGroupSupport(); return vaultPerms.hasGroupSupport();
case NONE:
// Not hooked into any permissions system, return false
return false;
default: default:
// Something went wrong, return false to prevent problems // Not hooked into any permissions system, return false
return false; return false;
} }
} }
@ -469,12 +447,8 @@ public class PermissionsManager implements PermissionsService {
// Vault // Vault
return Arrays.asList(vaultPerms.getPlayerGroups(player)); return Arrays.asList(vaultPerms.getPlayerGroups(player));
case NONE:
// Not hooked into any permissions system, return an empty list
return new ArrayList<>();
default: default:
// Something went wrong, return an empty list to prevent problems // Not hooked into any permissions system, return an empty list
return new ArrayList<>(); return new ArrayList<>();
} }
} }
@ -521,12 +495,8 @@ public class PermissionsManager implements PermissionsService {
// Vault // Vault
return vaultPerms.getPrimaryGroup(player); return vaultPerms.getPrimaryGroup(player);
case NONE:
// Not hooked into any permissions system, return null
return null;
default: default:
// Something went wrong, return null to prevent problems // Not hooked into any permissions system, return null
return null; return null;
} }
} }
@ -575,12 +545,8 @@ public class PermissionsManager implements PermissionsService {
// Vault // Vault
return vaultPerms.playerInGroup(player, groupName); return vaultPerms.playerInGroup(player, groupName);
case NONE:
// Not hooked into any permissions system, return an empty list
return false;
default: default:
// Something went wrong, return an empty list to prevent problems // Not hooked into any permissions system, return an empty list
return false; return false;
} }
} }
@ -632,12 +598,8 @@ public class PermissionsManager implements PermissionsService {
vaultPerms.playerAddGroup(player, groupName); vaultPerms.playerAddGroup(player, groupName);
return true; return true;
case NONE:
// Not hooked into any permissions system, return false
return false;
default: default:
// Something went wrong, return false // Not hooked into any permissions system, return false
return false; return false;
} }
} }
@ -713,12 +675,8 @@ public class PermissionsManager implements PermissionsService {
vaultPerms.playerRemoveGroup(player, groupName); vaultPerms.playerRemoveGroup(player, groupName);
return true; return true;
case NONE:
// Not hooked into any permissions system, return false
return false;
default: default:
// Something went wrong, return false // Not hooked into any permissions system, return false
return false; return false;
} }
} }
@ -802,12 +760,8 @@ public class PermissionsManager implements PermissionsService {
vaultPerms.playerAddGroup(player, groupName); vaultPerms.playerAddGroup(player, groupName);
return true; return true;
case NONE:
// Not hooked into any permissions system, return false
return false;
default: default:
// Something went wrong, return false // Not hooked into any permissions system, return false
return false; return false;
} }
} }
@ -867,6 +821,4 @@ public class PermissionsManager implements PermissionsService {
// Remove each group // Remove each group
return removeGroups(player, groupNames); return removeGroups(player, groupNames);
} }
} }

View File

@ -5,32 +5,82 @@ package fr.xephi.authme.permission;
*/ */
public enum PermissionsSystemType { public enum PermissionsSystemType {
NONE("None"), /**
* Permissions Ex.
*/
PERMISSIONS_EX("PermissionsEx", "PermissionsEx"),
PERMISSIONS_EX("PermissionsEx"), /**
* Permissions Bukkit.
*/
PERMISSIONS_BUKKIT("Permissions Bukkit", "PermissionsBukkit"),
PERMISSIONS_BUKKIT("Permissions Bukkit"), /**
* bPermissions.
*/
B_PERMISSIONS("bPermissions", "bPermissions"),
B_PERMISSIONS("bPermissions"), /**
* Essentials Group Manager.
*/
ESSENTIALS_GROUP_MANAGER("Essentials Group Manager", "GroupManager"),
ESSENTIALS_GROUP_MANAGER("Essentials Group Manager"), /**
* zPermissions.
*/
Z_PERMISSIONS("zPermissions", "zPermissions"),
Z_PERMISSIONS("zPermissions"), /**
* Vault.
*/
VAULT("Vault", "Vault");
VAULT("Vault"); /**
* The display name of the permissions system.
*/
public String name;
public final String name; /**
* The name of the permissions system plugin.
*/
public String pluginName;
/** /**
* Constructor for PermissionsSystemType. * Constructor for PermissionsSystemType.
* *
* @param name The name the permissions manager goes by * @param name Display name of the permissions system.
* @param pluginName Name of the plugin.
*/ */
PermissionsSystemType(String name) { PermissionsSystemType(String name, String pluginName) {
this.name = name; this.name = name;
this.pluginName = pluginName;
} }
/**
* Get the display name of the permissions system.
*
* @return Display name.
*/
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Return the plugin name.
*
* @return Plugin name.
*/
public String getPluginName() {
return this.pluginName;
}
/**
* Cast the permissions system type to a string.
*
* @return The display name of the permissions system.
*/
@Override
public String toString() {
return getName();
}
} }