Use LuckPerms 4 API for older LuckPerms, and 5 for newer ones

This commit is contained in:
Mike Primm 2019-12-15 12:53:50 -06:00
parent 70ae3fbc37
commit b966903331
4 changed files with 135 additions and 26 deletions

View File

@ -4,6 +4,7 @@ description = 'dynmap'
dependencies { dependencies {
compile group: 'org.bukkit', name: 'bukkit', version:'1.7.10-R0.1-SNAPSHOT' compile group: 'org.bukkit', name: 'bukkit', version:'1.7.10-R0.1-SNAPSHOT'
compile 'com.nijikokun.bukkit:Permissions:3.1.6' compile 'com.nijikokun.bukkit:Permissions:3.1.6'
compile 'me.lucko.luckperms:luckperms-api:4.3'
compile 'net.luckperms:api:5.0' compile 'net.luckperms:api:5.0'
compile project(":dynmap-api") compile project(":dynmap-api")
compile project(path: ":DynmapCore", configuration: "shadow") compile project(path: ":DynmapCore", configuration: "shadow")

View File

@ -91,6 +91,7 @@ import org.dynmap.bukkit.permissions.GroupManagerPermissions;
import org.dynmap.bukkit.permissions.PermissionProvider; import org.dynmap.bukkit.permissions.PermissionProvider;
import org.dynmap.bukkit.permissions.bPermPermissions; import org.dynmap.bukkit.permissions.bPermPermissions;
import org.dynmap.bukkit.permissions.LuckPermsPermissions; import org.dynmap.bukkit.permissions.LuckPermsPermissions;
import org.dynmap.bukkit.permissions.LuckPerms5Permissions;
import org.dynmap.common.BiomeMap; import org.dynmap.common.BiomeMap;
import org.dynmap.common.DynmapCommandSender; import org.dynmap.common.DynmapCommandSender;
import org.dynmap.common.DynmapPlayer; import org.dynmap.common.DynmapPlayer;
@ -863,6 +864,8 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI {
permissions = GroupManagerPermissions.create(getServer(), "dynmap"); permissions = GroupManagerPermissions.create(getServer(), "dynmap");
if (permissions == null) if (permissions == null)
permissions = LuckPermsPermissions.create(getServer(), "dynmap"); permissions = LuckPermsPermissions.create(getServer(), "dynmap");
if (permissions == null)
permissions = LuckPerms5Permissions.create(getServer(), "dynmap");
if (permissions == null) if (permissions == null)
permissions = BukkitPermissions.create("dynmap", perdefs); permissions = BukkitPermissions.create("dynmap", perdefs);
if (permissions == null) if (permissions == null)

View File

@ -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<String> hasOfflinePermissions(String player, Set<String> perms) {
Set<String> 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> T joinFuture(Future<T> future) {
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -6,12 +6,9 @@ import java.util.UUID;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import net.luckperms.api.LuckPerms; import me.lucko.luckperms.api.LuckPermsApi;
import net.luckperms.api.model.user.User; import me.lucko.luckperms.api.User;
import net.luckperms.api.model.user.UserManager; import me.lucko.luckperms.api.caching.PermissionData;
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.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -21,19 +18,24 @@ import org.dynmap.Log;
public class LuckPermsPermissions implements PermissionProvider { public class LuckPermsPermissions implements PermissionProvider {
String name; String name;
LuckPerms luckPerms; LuckPermsApi luckPerms;
public static LuckPermsPermissions create(Server server, String name) { 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")) if (!server.getPluginManager().isPluginEnabled("LuckPerms"))
return null; return null;
LuckPerms luckPerms = server.getServicesManager().load(LuckPerms.class); LuckPermsApi luckPerms = server.getServicesManager().load(LuckPermsApi.class);
if (luckPerms == null) if (luckPerms == null)
return 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); return new LuckPermsPermissions(name, luckPerms);
} }
public LuckPermsPermissions(String name, LuckPerms luckPerms) { public LuckPermsPermissions(String name, LuckPermsApi luckPerms) {
this.name = name; this.name = name;
this.luckPerms = luckPerms; this.luckPerms = luckPerms;
} }
@ -46,10 +48,10 @@ public class LuckPermsPermissions implements PermissionProvider {
@Override @Override
public Set<String> hasOfflinePermissions(String player, Set<String> perms) { public Set<String> hasOfflinePermissions(String player, Set<String> perms) {
Set<String> result = new HashSet<>(); Set<String> result = new HashSet<>();
CachedPermissionData user = getUser(player); PermissionData user = getUser(player);
if (user != null) { if (user != null) {
for (String p : perms) { for (String p : perms) {
if (user.checkPermission(name + "." + p).asBoolean()) if (user.getPermissionValue(name + "." + p).asBoolean())
result.add(p); result.add(p);
} }
} }
@ -58,39 +60,34 @@ public class LuckPermsPermissions implements PermissionProvider {
@Override @Override
public boolean hasOfflinePermission(String player, String perm) { public boolean hasOfflinePermission(String player, String perm) {
CachedPermissionData user = getUser(player); PermissionData user = getUser(player);
if (user == null) if (user == null)
return false; 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); OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username);
UUID uuid; UUID uuid;
if (offlinePlayer != null && offlinePlayer.getUniqueId() != null) if (offlinePlayer != null && offlinePlayer.getUniqueId() != null)
uuid = offlinePlayer.getUniqueId(); uuid = offlinePlayer.getUniqueId();
else else
uuid = joinFuture(luckPerms.getUserManager().lookupUniqueId(username)); uuid = joinFuture(luckPerms.getStorage().getUUID(username));
if (uuid == null) if (uuid == null)
return null; return null;
User user = luckPerms.getUserManager().getUser(uuid); User user = luckPerms.getUser(uuid);
if (user == null) { if (user == null) {
joinFuture(luckPerms.getUserManager().loadUser(uuid)); joinFuture(luckPerms.getStorage().loadUser(uuid));
user = luckPerms.getUserManager().getUser(uuid); user = luckPerms.getUser(uuid);
} }
if (user == null) if (user == null)
return null; return null;
CachedDataManager data = user.getCachedData(); return user.getCachedData().getPermissionData(luckPerms.getContextManager().getStaticContexts());
return luckPerms
.getContextManager()
.getQueryOptions(user)
.map(queryOptions -> data.getPermissionData(queryOptions))
.orElse(null);
} }
private static <T> T joinFuture(Future<T> future) { private static <T> T joinFuture(Future<T> future) {