From 307569254f7ccbc7a2fc07871e5237de27dfbc6c Mon Sep 17 00:00:00 2001 From: Johny Muffin Date: Wed, 3 Jan 2024 15:13:49 +1000 Subject: [PATCH] Add optimized JPerms Support --- .../migration/JPermsPermissionsResolver.java | 83 +++++++++++++++++++ .../migration/PermissionsResolverManager.java | 11 +++ 2 files changed, 94 insertions(+) create mode 100644 src/main/java/com/sk89q/bukkit/migration/JPermsPermissionsResolver.java diff --git a/src/main/java/com/sk89q/bukkit/migration/JPermsPermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/JPermsPermissionsResolver.java new file mode 100644 index 0000000000..0f87a9a4c1 --- /dev/null +++ b/src/main/java/com/sk89q/bukkit/migration/JPermsPermissionsResolver.java @@ -0,0 +1,83 @@ +package com.sk89q.bukkit.migration; + +import com.johnymuffin.jperms.beta.JohnyPerms; +import com.johnymuffin.jperms.beta.JohnyPermsAPI; +import com.johnymuffin.jperms.core.models.PermissionsGroup; +import com.projectposeidon.api.PoseidonUUID; +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.entity.Player; + +import java.util.UUID; + +public class JPermsPermissionsResolver implements PermissionsResolver { + private Server server; + private JohnyPermsAPI api; + + public JPermsPermissionsResolver(final Server server) throws MissingPluginException, PluginAccessException { + this.server = server; + if (Bukkit.getServer().getPluginManager().getPlugin("JPerms") == null) { + throw new MissingPluginException(); + } + try { + this.api = JohnyPerms.getJPermsAPI(); + } catch (Exception e) { + throw new PluginAccessException(); + } + + } + + public void load() { + + } + + private UUID getPlayerUUID(final String name) { + for(Player player : Bukkit.getOnlinePlayers()) { + if(player.getName().equalsIgnoreCase(name)) { + return player.getUniqueId(); + } + } + return PoseidonUUID.getPlayerGracefulUUID(name); + } + + public boolean hasPermission(final String name, final String permission) { + UUID playerUUID = getPlayerUUID(name); + + return api.getUser(playerUUID).hasPermissionSomehow(permission, true) || api.getUser(playerUUID).getGroup().hasPermission(permission, true); + } + + @Override + public boolean hasPermission(String worldName, String name, String permission) { + //JPerms 1.0.0 does not support world permissions + return hasPermission(name, permission); + } + + public boolean inGroup(final String name, final String group) { + String[] groups = getGroups(name); + for(String g : groups) { + if(g.equalsIgnoreCase(group)) { + return true; + } + } + return false; + } + + public String[] getGroups(final String name) { + UUID playerUUID = getPlayerUUID(name); + PermissionsGroup group = api.getUser(playerUUID).getGroup(); + String[] groups = new String[group.getInheritanceGroups().length + 1]; + groups[0] = group.getName(); + for(int i = 0; i < group.getInheritanceGroups().length; i++) { + groups[i + 1] = group.getInheritanceGroups()[i].getName(); + } + return groups; + } + + public static class PluginAccessException extends Exception { + private static final long serialVersionUID = 7044832912491608706L; + } + + public static class MissingPluginException extends Exception { + private static final long serialVersionUID = 7044832912491608706L; + } +} diff --git a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java index 4c318dacaa..7c322d4f94 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java +++ b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java @@ -78,6 +78,7 @@ public PermissionsResolverManager(Plugin plugin, String name, Logger logger) { } public void findResolver() { if (tryPluginPermissionsResolver()) return; + if (tryJPermsPermissions()) return; if (tryNijiPermissions()) return; if (tryPermissionsEx()) return; if (tryDinnerPerms()) return; @@ -96,6 +97,16 @@ private boolean tryNijiPermissions() { return false; } } + + private boolean tryJPermsPermissions() { + try { + perms = new JPermsPermissionsResolver(server); + logger.info(name + ": Permissions plugin detected! Using Permissions plugin for permissions."); + return true; + } catch (Throwable e) { + return false; + } + } private boolean tryPermissionsEx() { try {