From b96690333121ec1519d09faee133c1bf113596eb Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Sun, 15 Dec 2019 12:53:50 -0600 Subject: [PATCH] Use LuckPerms 4 API for older LuckPerms, and 5 for newer ones --- spigot/build.gradle | 1 + .../java/org/dynmap/bukkit/DynmapPlugin.java | 3 + .../permissions/LuckPerms5Permissions.java | 108 ++++++++++++++++++ .../permissions/LuckPermsPermissions.java | 49 ++++---- 4 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPerms5Permissions.java diff --git a/spigot/build.gradle b/spigot/build.gradle index 53b0eb15..aaebdcab 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 'net.luckperms:api:5.0' compile project(":dynmap-api") compile project(path: ":DynmapCore", configuration: "shadow") diff --git a/spigot/src/main/java/org/dynmap/bukkit/DynmapPlugin.java b/spigot/src/main/java/org/dynmap/bukkit/DynmapPlugin.java index d10a7e6a..a17ff15d 100644 --- a/spigot/src/main/java/org/dynmap/bukkit/DynmapPlugin.java +++ b/spigot/src/main/java/org/dynmap/bukkit/DynmapPlugin.java @@ -91,6 +91,7 @@ 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.bukkit.permissions.LuckPerms5Permissions; import org.dynmap.common.BiomeMap; import org.dynmap.common.DynmapCommandSender; import org.dynmap.common.DynmapPlayer; @@ -863,6 +864,8 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { permissions = GroupManagerPermissions.create(getServer(), "dynmap"); if (permissions == null) permissions = LuckPermsPermissions.create(getServer(), "dynmap"); + if (permissions == null) + permissions = LuckPerms5Permissions.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/LuckPerms5Permissions.java b/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPerms5Permissions.java new file mode 100644 index 00000000..2d5d4169 --- /dev/null +++ b/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPerms5Permissions.java @@ -0,0 +1,108 @@ +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 net.luckperms.api.LuckPerms; +import net.luckperms.api.model.user.User; +import net.luckperms.api.model.user.UserManager; +import net.luckperms.api.cacheddata.CachedPermissionData; +import net.luckperms.api.cacheddata.CachedDataManager; +import net.luckperms.api.query.QueryOptions; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; +import org.bukkit.command.CommandSender; +import org.dynmap.Log; + +public class LuckPerms5Permissions implements PermissionProvider { + String name; + LuckPerms luckPerms; + + public static LuckPerms5Permissions create(Server server, String name) { + try { + Class.forName("net.luckperms.api.LuckPerms"); /* See if class exists */ + } catch (ClassNotFoundException cnfx) { + return null; + } + if (!server.getPluginManager().isPluginEnabled("LuckPerms")) + return null; + LuckPerms luckPerms = server.getServicesManager().load(LuckPerms.class); + if (luckPerms == null) + return null; + Log.info("Using LuckPerms " + luckPerms.getPluginMetadata().getVersion() + " for access control"); + return new LuckPerms5Permissions(name, luckPerms); + } + + public LuckPerms5Permissions(String name, LuckPerms 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<>(); + CachedPermissionData user = getUser(player); + if (user != null) { + for (String p : perms) { + if (user.checkPermission(name + "." + p).asBoolean()) + result.add(p); + } + } + return result; + } + + @Override + public boolean hasOfflinePermission(String player, String perm) { + CachedPermissionData user = getUser(player); + if (user == null) + return false; + return user.checkPermission(name + "." + perm).asBoolean(); + } + + private CachedPermissionData getUser(String username) { + OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username); + UUID uuid; + + if (offlinePlayer != null && offlinePlayer.getUniqueId() != null) + uuid = offlinePlayer.getUniqueId(); + else + uuid = joinFuture(luckPerms.getUserManager().lookupUniqueId(username)); + + if (uuid == null) + return null; + + User user = luckPerms.getUserManager().getUser(uuid); + if (user == null) { + joinFuture(luckPerms.getUserManager().loadUser(uuid)); + user = luckPerms.getUserManager().getUser(uuid); + } + + if (user == null) + return null; + + CachedDataManager data = user.getCachedData(); + return luckPerms + .getContextManager() + .getQueryOptions(user) + .map(queryOptions -> data.getPermissionData(queryOptions)) + .orElse(null); + } + + private static T joinFuture(Future future) { + try { + return future.get(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } +} diff --git a/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPermsPermissions.java b/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPermsPermissions.java index f0848a7e..7dc92f60 100644 --- a/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPermsPermissions.java +++ b/spigot/src/main/java/org/dynmap/bukkit/permissions/LuckPermsPermissions.java @@ -6,12 +6,9 @@ import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import net.luckperms.api.LuckPerms; -import net.luckperms.api.model.user.User; -import net.luckperms.api.model.user.UserManager; -import net.luckperms.api.cacheddata.CachedPermissionData; -import net.luckperms.api.cacheddata.CachedDataManager; -import net.luckperms.api.query.QueryOptions; +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; @@ -21,19 +18,24 @@ import org.dynmap.Log; public class LuckPermsPermissions implements PermissionProvider { String name; - LuckPerms luckPerms; + LuckPermsApi luckPerms; public static LuckPermsPermissions create(Server server, String name) { + try { + Class.forName("me.lucko.luckperms.api.LuckPermsApi"); /* See if class exists */ + } catch (ClassNotFoundException cnfx) { + return null; + } if (!server.getPluginManager().isPluginEnabled("LuckPerms")) return null; - LuckPerms luckPerms = server.getServicesManager().load(LuckPerms.class); + LuckPermsApi luckPerms = server.getServicesManager().load(LuckPermsApi.class); if (luckPerms == null) return null; - Log.info("Using LuckPerms " + luckPerms.getPluginMetadata().getVersion() + " for access control"); + Log.info("Using LuckPerms " + luckPerms.getPlatformInfo().getVersion() + " for access control"); return new LuckPermsPermissions(name, luckPerms); } - public LuckPermsPermissions(String name, LuckPerms luckPerms) { + public LuckPermsPermissions(String name, LuckPermsApi luckPerms) { this.name = name; this.luckPerms = luckPerms; } @@ -46,10 +48,10 @@ public class LuckPermsPermissions implements PermissionProvider { @Override public Set hasOfflinePermissions(String player, Set perms) { Set result = new HashSet<>(); - CachedPermissionData user = getUser(player); + PermissionData user = getUser(player); if (user != null) { for (String p : perms) { - if (user.checkPermission(name + "." + p).asBoolean()) + if (user.getPermissionValue(name + "." + p).asBoolean()) result.add(p); } } @@ -58,39 +60,34 @@ public class LuckPermsPermissions implements PermissionProvider { @Override public boolean hasOfflinePermission(String player, String perm) { - CachedPermissionData user = getUser(player); + PermissionData user = getUser(player); if (user == null) return false; - return user.checkPermission(name + "." + perm).asBoolean(); + return user.getPermissionValue(name + "." + perm).asBoolean(); } - private CachedPermissionData getUser(String username) { + 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.getUserManager().lookupUniqueId(username)); + uuid = joinFuture(luckPerms.getStorage().getUUID(username)); if (uuid == null) return null; - User user = luckPerms.getUserManager().getUser(uuid); + User user = luckPerms.getUser(uuid); if (user == null) { - joinFuture(luckPerms.getUserManager().loadUser(uuid)); - user = luckPerms.getUserManager().getUser(uuid); + joinFuture(luckPerms.getStorage().loadUser(uuid)); + user = luckPerms.getUser(uuid); } if (user == null) return null; - CachedDataManager data = user.getCachedData(); - return luckPerms - .getContextManager() - .getQueryOptions(user) - .map(queryOptions -> data.getPermissionData(queryOptions)) - .orElse(null); + return user.getCachedData().getPermissionData(luckPerms.getContextManager().getStaticContexts()); } private static T joinFuture(Future future) { @@ -100,4 +97,4 @@ public class LuckPermsPermissions implements PermissionProvider { throw new RuntimeException(e); } } -} +} \ No newline at end of file