From 87638c6bf53e5e7861387011a7912e6f09d6bdeb Mon Sep 17 00:00:00 2001 From: Azarias Boutin Date: Wed, 20 Mar 2019 06:54:52 +0100 Subject: [PATCH] Added LuckPerms to the permissions for spigot --- spigot/build.gradle | 1 + .../java/org/dynmap/bukkit/DynmapPlugin.java | 3 + .../permissions/LuckPermsPermissions.java | 95 +++++++++++++++++++ spigot/src/main/resources/plugin.yml | 2 +- 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPermsPermissions.java diff --git a/spigot/build.gradle b/spigot/build.gradle index a5ddcc77..ba60c7b0 100644 --- a/spigot/build.gradle +++ b/spigot/build.gradle @@ -4,6 +4,7 @@ description = 'dynmap' dependencies { compile group: 'org.bukkit', name: 'bukkit', version:'1.7.10-R0.1-SNAPSHOT' compile 'com.nijikokun.bukkit:Permissions:3.1.6' + compile 'me.lucko.luckperms:luckperms-api:4.3' compile project(":dynmap-api") compile project(path: ":DynmapCore", configuration: "shadow") compile group: 'ru.tehkode', name: 'PermissionsEx', version:'1.19.1' diff --git a/spigot/src/main/java/org/dynmap/bukkit/DynmapPlugin.java b/spigot/src/main/java/org/dynmap/bukkit/DynmapPlugin.java index b9a81624..5f854975 100644 --- a/spigot/src/main/java/org/dynmap/bukkit/DynmapPlugin.java +++ b/spigot/src/main/java/org/dynmap/bukkit/DynmapPlugin.java @@ -89,6 +89,7 @@ import org.dynmap.bukkit.permissions.PermBukkitPermissions; import org.dynmap.bukkit.permissions.GroupManagerPermissions; import org.dynmap.bukkit.permissions.PermissionProvider; import org.dynmap.bukkit.permissions.bPermPermissions; +import org.dynmap.bukkit.permissions.LuckPermsPermissions; import org.dynmap.common.BiomeMap; import org.dynmap.common.DynmapCommandSender; import org.dynmap.common.DynmapPlayer; @@ -827,6 +828,8 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { permissions = NijikokunPermissions.create(getServer(), "dynmap"); if (permissions == null) permissions = GroupManagerPermissions.create(getServer(), "dynmap"); + if (permissions == null) + permissions = LuckPermsPermissions.create(getServer(), "dynmap"); if (permissions == null) permissions = BukkitPermissions.create("dynmap", perdefs); if (permissions == null) diff --git a/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPermsPermissions.java b/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPermsPermissions.java new file mode 100644 index 00000000..7ab582b2 --- /dev/null +++ b/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPermsPermissions.java @@ -0,0 +1,95 @@ +package org.dynmap.bukkit.permissions; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import me.lucko.luckperms.api.LuckPermsApi; +import me.lucko.luckperms.api.User; +import me.lucko.luckperms.api.caching.PermissionData; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; +import org.bukkit.command.CommandSender; +import org.dynmap.Log; + +public class LuckPermsPermissions implements PermissionProvider { + String name; + LuckPermsApi luckPerms; + + public static LuckPermsPermissions create(Server server, String name) { + if (!server.getPluginManager().isPluginEnabled("LuckPerms")) + return null; + LuckPermsApi luckPerms = server.getServicesManager().load(LuckPermsApi.class); + if (luckPerms == null) + return null; + Log.info("Using LuckPerms " + luckPerms.getPlatformInfo().getVersion() + " for access control"); + return new LuckPermsPermissions(name, luckPerms); + } + + public LuckPermsPermissions(String name, LuckPermsApi luckPerms) { + this.name = name; + this.luckPerms = luckPerms; + } + + @Override + public boolean has(CommandSender sender, String permission) { + return sender.hasPermission(name + "." + permission); + } + + @Override + public Set hasOfflinePermissions(String player, Set perms) { + Set result = new HashSet<>(); + PermissionData user = getUser(player); + if (user != null) { + for (String p : perms) { + if (user.getPermissionValue(name + "." + p).asBoolean()) + result.add(p); + } + } + return result; + } + + @Override + public boolean hasOfflinePermission(String player, String perm) { + PermissionData user = getUser(player); + if (user == null) + return false; + return user.getPermissionValue(name + "." + perm).asBoolean(); + } + + private PermissionData getUser(String username) { + OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username); + UUID uuid; + + if (offlinePlayer != null && offlinePlayer.getUniqueId() != null) + uuid = offlinePlayer.getUniqueId(); + else + uuid = joinFuture(luckPerms.getStorage().getUUID(username)); + + if (uuid == null) + return null; + + User user = luckPerms.getUser(uuid); + if (user == null) { + joinFuture(luckPerms.getStorage().loadUser(uuid)); + user = luckPerms.getUser(uuid); + } + + if (user == null) + return null; + + return user.getCachedData().getPermissionData(luckPerms.getContextManager().getStaticContexts()); + } + + private static T joinFuture(Future future) { + try { + return future.get(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/spigot/src/main/resources/plugin.yml b/spigot/src/main/resources/plugin.yml index f4002908..2e722eb7 100644 --- a/spigot/src/main/resources/plugin.yml +++ b/spigot/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ main: org.dynmap.bukkit.DynmapPlugin version: "${version}-${buildnumber}" authors: [mikeprimm] website: "https://forums.dynmap.us" -softdepend: [ Permissions, PermissionEx, bPermissions, PermissionsBukkit, GroupManager ] +softdepend: [ Permissions, PermissionEx, bPermissions, PermissionsBukkit, GroupManager, LuckPerm ] commands: dynmap: description: Controls Dynmap.