From 0810d3fa2aaf07b5b5cb9f89341913c1a595c637 Mon Sep 17 00:00:00 2001 From: ZerothAngel Date: Thu, 20 Jun 2013 21:13:22 -0700 Subject: Add group/prefix/suffix support for zPermissions. --- Essentials/nbproject/project.properties | 4 +- .../essentials/perm/PermissionsHandler.java | 11 ++ .../essentials/perm/ZPermissionsHandler.java | 194 +++++++++++++++++++++ lib/zPermissions.jar | Bin 0 -> 327241 bytes 4 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 Essentials/src/com/earth2me/essentials/perm/ZPermissionsHandler.java create mode 100644 lib/zPermissions.jar diff --git a/Essentials/nbproject/project.properties b/Essentials/nbproject/project.properties index f749b8553..409eb7956 100644 --- a/Essentials/nbproject/project.properties +++ b/Essentials/nbproject/project.properties @@ -79,6 +79,7 @@ file.reference.PermissionsEx.jar=../lib/PermissionsEx.jar file.reference.Privileges.jar=..\\lib\\Privileges.jar file.reference.Vault.jar=../lib/Vault.jar file.reference.SimplyPerms.jar=../lib/SimplyPerms.jar +file.reference.zPermissions.jar=../lib/zPermissions.jar includes=** jar.archive.disabled=${jnlp.enabled} jar.compress=true @@ -99,7 +100,8 @@ javac.classpath=\ ${file.reference.Vault.jar}:\ ${file.reference.Privileges.jar}:\ ${file.reference.bpermissions2.jar}:\ - ${file.reference.SimplyPerms.jar} + ${file.reference.SimplyPerms.jar}:\ + ${file.reference.zPermissions.jar} # Space-separated list of extra javac options javac.compilerargs=-Xlint:unchecked javac.deprecation=false diff --git a/Essentials/src/com/earth2me/essentials/perm/PermissionsHandler.java b/Essentials/src/com/earth2me/essentials/perm/PermissionsHandler.java index 044fbb913..03f24bd85 100644 --- a/Essentials/src/com/earth2me/essentials/perm/PermissionsHandler.java +++ b/Essentials/src/com/earth2me/essentials/perm/PermissionsHandler.java @@ -166,6 +166,17 @@ public class PermissionsHandler implements IPermissionsHandler 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(plugin); + } + return; + } + if (useSuperperms) { if (!(handler instanceof SuperpermsHandler)) diff --git a/Essentials/src/com/earth2me/essentials/perm/ZPermissionsHandler.java b/Essentials/src/com/earth2me/essentials/perm/ZPermissionsHandler.java new file mode 100644 index 000000000..fda404b4b --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/perm/ZPermissionsHandler.java @@ -0,0 +1,194 @@ +package com.earth2me.essentials.perm; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +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; + +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.getName()); + } + } + + @Override + public List getGroups(Player base) + { + if (!isReady()) + { + return super.getGroups(base); + } + else + { + return new ArrayList(service.getPlayerGroups(base.getName())); + } + } + + @Override + public boolean inGroup(Player base, String group) + { + if (!isReady()) + { + return super.inGroup(base, group); + } + else + { + Set groups = service.getPlayerGroups(base.getName()); + 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"); + } + } + + @Override + public boolean canBuild(Player base, String group) + { + return hasPermission(base, "essentials.build"); + } + + private String getPrefixSuffix(Player base, String metadataName) + { + String playerPrefixSuffix; + try + { + playerPrefixSuffix = service.getPlayerMetadata(base.getName(), 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.getName()), 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(String playerName) + { + if (hasGetPlayerPrimaryGroup) + { + return service.getPlayerPrimaryGroup(playerName); + } + else + { + // Fall back to using highest-weight assigned group + List groups = service.getPlayerAssignedGroups(playerName); + return groups.get(0); + } + } + +} diff --git a/lib/zPermissions.jar b/lib/zPermissions.jar new file mode 100644 index 000000000..77b8ed40c Binary files /dev/null and b/lib/zPermissions.jar differ -- cgit v1.2.3