From 7970e2a1c673a93f4844df1a43093b4e4223c8f6 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 3 Sep 2016 18:15:52 +0100 Subject: [PATCH] Add bPermissions migration --- .../me/lucko/luckperms/LPBukkitPlugin.java | 10 + bukkit/src/main/resources/plugin.yml | 2 +- .../me/lucko/luckperms/LPBungeePlugin.java | 5 + common/pom.xml | 6 + .../me/lucko/luckperms/LuckPermsPlugin.java | 7 + .../migration/MigrationMainCommand.java | 5 + .../subcommands/MigrationBPermissions.java | 231 ++++++++++++++++++ .../subcommands/MigrationPermissionsEx.java | 42 +--- .../java/me/lucko/luckperms/groups/Group.java | 2 - .../storage/methods/JSONDatastore.java | 7 - .../java/me/lucko/luckperms/users/User.java | 3 - .../me/lucko/luckperms/LPSpongePlugin.java | 15 +- .../me/lucko/luckperms/users/SpongeUser.java | 2 - 13 files changed, 278 insertions(+), 59 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationBPermissions.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java index 8349470ff..b057cebd7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java @@ -217,6 +217,16 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { return getServer().getServicesManager().load(clazz); } + @SuppressWarnings("deprecation") + @Override + public UUID getUUID(String playerName) { + try { + return getServer().getOfflinePlayer(playerName).getUniqueId(); + } catch (Exception e) { + return null; + } + } + @Override public boolean isPluginLoaded(String name) { return getServer().getPluginManager().isPluginEnabled(name); diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index a161f3526..6a8f094e2 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -2,7 +2,7 @@ name: LuckPerms author: Luck version: ${release.version}.${git.closest.tag.commit.count} main: me.lucko.luckperms.LPBukkitPlugin -softdepend: [Vault, PermissionsEx, GroupManager, PowerfulPerms, zPermissions] # For migration +softdepend: [Vault, PermissionsEx, GroupManager, PowerfulPerms, zPermissions, bPermissions] # For migration description: A permissions plugin commands: luckperms: diff --git a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java index cab8110eb..63d92c735 100644 --- a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java @@ -185,6 +185,11 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { return null; } + @Override + public UUID getUUID(String playerName) { + return null; // Not needed on Bungee + } + @Override public boolean isPluginLoaded(String name) { return getProxy().getPluginManager().getPlugins().stream() diff --git a/common/pom.xml b/common/pom.xml index 0f2275382..ef6b59192 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -132,6 +132,12 @@ 3.0-dev-52 provided + + de.bananaco + bpermissions-api + 2.12 + provided + diff --git a/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java index 9fd0afab5..f50880dca 100644 --- a/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java @@ -136,6 +136,13 @@ public interface LuckPermsPlugin { */ Object getService(Class clazz); + /** + * Used as a backup for migration + * @param playerName the players name + * @return a uuid if found, or null if not + */ + UUID getUUID(String playerName); + /** * Checks if a plugin is loaded on the platform * @param name the name of the plugin diff --git a/common/src/main/java/me/lucko/luckperms/commands/migration/MigrationMainCommand.java b/common/src/main/java/me/lucko/luckperms/commands/migration/MigrationMainCommand.java index 9dab30d4e..825c84575 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/migration/MigrationMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/migration/MigrationMainCommand.java @@ -66,6 +66,11 @@ public class MigrationMainCommand extends MainCommand { subCommands.add(new MigrationBungeePerms()); } catch (Throwable ignored) {} + try { + Class.forName("de.bananaco.bpermissions.api.WorldManager"); + subCommands.add(new MigrationBPermissions()); + } catch (Throwable ignored) {} + } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationBPermissions.java b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationBPermissions.java new file mode 100644 index 000000000..6b48d443b --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationBPermissions.java @@ -0,0 +1,231 @@ +package me.lucko.luckperms.commands.migration.subcommands; + +import de.bananaco.bpermissions.api.*; +import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.api.Logger; +import me.lucko.luckperms.commands.CommandResult; +import me.lucko.luckperms.commands.Predicate; +import me.lucko.luckperms.commands.Sender; +import me.lucko.luckperms.commands.SubCommand; +import me.lucko.luckperms.constants.Constants; +import me.lucko.luckperms.core.PermissionHolder; +import me.lucko.luckperms.data.LogEntry; +import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; +import me.lucko.luckperms.utils.ArgumentChecker; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.*; + +import static me.lucko.luckperms.constants.Permission.MIGRATION; + +public class MigrationBPermissions extends SubCommand { + private static Field uConfigField; + private static Method getConfigurationSectionMethod = null; + private static Method getKeysMethod = null; + + static { + try { + uConfigField = Class.forName("de.bananaco.bpermissions.imp.YamlWorld").getDeclaredField("uconfig"); + uConfigField.setAccessible(true); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + private static Set getUsers(World world) { + try { + Object yamlWorldUsers = uConfigField.get(world); + if (getConfigurationSectionMethod == null) { + getConfigurationSectionMethod = yamlWorldUsers.getClass().getMethod("getConfigurationSection", String.class); + getConfigurationSectionMethod.setAccessible(true); + } + + Object configSection = getConfigurationSectionMethod.invoke(yamlWorldUsers, "users"); + if (configSection == null) { + return Collections.emptySet(); + } + + if (getKeysMethod == null) { + getKeysMethod = configSection.getClass().getMethod("getKeys", boolean.class); + getKeysMethod.setAccessible(true); + } + + return (Set) getKeysMethod.invoke(configSection, false); + } catch (Throwable t) { + t.printStackTrace(); + return null; + } + } + + public MigrationBPermissions() { + super("bpermissions", "Migration from bPermissions", "/%s migration bpermissions", MIGRATION, Predicate.alwaysFalse()); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) { + final Logger log = plugin.getLog(); + + WorldManager worldManager = WorldManager.getInstance(); + if (worldManager == null) { + log.severe("bPermissions Migration: Error -> bPermissions is not loaded."); + return CommandResult.STATE_ERROR; + } + + log.info("bPermissions Migration: Forcing the plugin to load all data. This could take a while."); + for (World world : worldManager.getAllWorlds()) { + Set users = getUsers(world); + if (users == null) { + log.severe("bPermissions Migration: Couldn't get a list of users."); + return CommandResult.FAILURE; + } + users.forEach(s -> world.loadOne(s, CalculableType.USER)); + } + + // Migrate one world at a time. + log.info("bPermissions Migration: Starting world migration."); + for (World world : worldManager.getAllWorlds()) { + log.info("bPermissions Migration: Migrating world: " + world.getName()); + + // Migrate all groups + log.info("bPermissions Migration: Starting group migration in world " + world.getName() + "."); + int groupCount = 0; + for (Calculable group : world.getAll(CalculableType.GROUP)) { + groupCount++; + String groupName = group.getName().toLowerCase(); + if (group.getName().equalsIgnoreCase(world.getDefaultGroup())) { + groupName = "default"; + } + + // Make a LuckPerms group for the one being migrated. + plugin.getDatastore().createAndLoadGroup(groupName); + me.lucko.luckperms.groups.Group lpGroup = plugin.getGroupManager().get(groupName); + try { + LogEntry.build() + .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) + .acted(lpGroup).action("create") + .build().submit(plugin); + } catch (Exception ex) { + ex.printStackTrace(); + } + + migrateHolder(plugin, world, group, lpGroup); + plugin.getDatastore().saveGroup(lpGroup); + } + log.info("bPermissions Migration: Migrated " + groupCount + " groups in world " + world.getName() + "."); + + // Migrate all users + log.info("bPermissions Migration: Starting user migration in world " + world.getName() + "."); + int userCount = 0; + for (Calculable user : world.getAll(CalculableType.USER)) { + userCount++; + + // There is no mention of UUIDs in the API. I assume that name = uuid. idk? + UUID uuid; + try { + uuid = UUID.fromString(user.getName()); + } catch (IllegalArgumentException e) { + uuid = plugin.getUUID(user.getName()); + } + + if (uuid == null) { + log.info("bPermissions Migration: Unable to migrate user " + user.getName() + ". Unable to get UUID."); + continue; + } + + // Make a LuckPerms user for the one being migrated. + plugin.getDatastore().loadUser(uuid, "null"); + me.lucko.luckperms.users.User lpUser = plugin.getUserManager().get(uuid); + + migrateHolder(plugin, world, user, lpUser); + + plugin.getDatastore().saveUser(lpUser); + plugin.getUserManager().cleanup(lpUser); + } + + log.info("bPermissions Migration: Migrated " + userCount + " users in world " + world.getName() + "."); + } + + log.info("bPermissions Migration: Success! Completed without any errors."); + return CommandResult.SUCCESS; + } + + private static void migrateHolder(LuckPermsPlugin plugin, World world, Calculable c, PermissionHolder holder) { + // Migrate the groups permissions in this world + for (Permission p : c.getPermissions()) { + try { + holder.setPermission(p.name(), p.isTrue(), "global", world.getName()); + LogEntry.build() + .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) + .acted(holder).action("set " + p.name() + " " + p.isTrue() + " global " + world.getName()) + .build().submit(plugin); + } catch (Exception ex) { + if (!(ex instanceof ObjectAlreadyHasException)) { + ex.printStackTrace(); + } + } + + // Include any child permissions + for (Map.Entry child : p.getChildren().entrySet()) { + try { + holder.setPermission(child.getKey(), child.getValue(), "global", world.getName()); + LogEntry.build() + .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) + .acted(holder).action("set " + child.getKey() + " " + child.getValue() + " global " + world.getName()) + .build().submit(plugin); + } catch (Exception ex) { + if (!(ex instanceof ObjectAlreadyHasException)) { + ex.printStackTrace(); + } + } + } + } + + // Migrate any inherited groups + for (Group parent : c.getGroups()) { + try { + holder.setPermission("group." + parent.getName(), true, "global", world.getName()); + LogEntry.build() + .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) + .acted(holder).action("setinherit " + parent.getName() + " global " + world.getName()) + .build().submit(plugin); + } catch (Exception ex) { + if (!(ex instanceof ObjectAlreadyHasException)) { + ex.printStackTrace(); + } + } + } + + // Migrate existing meta + for (Map.Entry meta : c.getMeta().entrySet()) { + if (meta.getKey().equalsIgnoreCase("prefix") || meta.getKey().equalsIgnoreCase("suffix")) { + String chatMeta = ArgumentChecker.escapeCharacters(meta.getValue()); + try { + holder.setPermission(meta.getKey().toLowerCase() + "." + c.getPriority() + "." + chatMeta, true); + LogEntry.build() + .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) + .acted(holder).action("set " + meta.getKey().toLowerCase() + "." + c.getPriority() + "." + chatMeta + " true") + .build().submit(plugin); + } catch (Exception ex) { + if (!(ex instanceof ObjectAlreadyHasException)) { + ex.printStackTrace(); + } + } + continue; + } + + try { + holder.setPermission("meta." + meta.getKey() + "." + meta.getValue(), true, "global", world.getName()); + LogEntry.build() + .actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName()) + .acted(holder).action("set meta." + meta.getKey() + "." + meta.getValue() + " true global " + world.getName()) + .build().submit(plugin); + } catch (Exception ex) { + if (!(ex instanceof ObjectAlreadyHasException)) { + ex.printStackTrace(); + } + } + } + } +} diff --git a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationPermissionsEx.java b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationPermissionsEx.java index c1cda2231..78ecd6bf9 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationPermissionsEx.java +++ b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationPermissionsEx.java @@ -43,41 +43,11 @@ import ru.tehkode.permissions.bukkit.PermissionsEx; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; public class MigrationPermissionsEx extends SubCommand { - - private static Class bukkitPlayer = null; - private static Method getUniqueIdMethod = null; - private static Method getPlayerMethod = null; - - static { - try { - bukkitPlayer = Class.forName("org.bukkit.entity.Player"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - - if (bukkitPlayer != null) { - try { - getUniqueIdMethod = bukkitPlayer.getMethod("getUniqueId"); - getUniqueIdMethod.setAccessible(true); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - } - - try { - getPlayerMethod = PermissionUser.class.getMethod("getPlayer"); - getPlayerMethod.setAccessible(true); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - } - public MigrationPermissionsEx() { super("permissionsex", "Migration from PermissionsEx", "/%s migration permissionsex [world names]", Permission.MIGRATION, Predicate.alwaysFalse()); @@ -273,19 +243,9 @@ public class MigrationPermissionsEx extends SubCommand { try { u = UUID.fromString(user.getIdentifier()); } catch (IllegalArgumentException e) { - u = ni.nameToUUID(user.getIdentifier()); - if (u == null) { - if (bukkitPlayer != null) { - try { - Object playerObj = getPlayerMethod.invoke(user); - u = (UUID) getUniqueIdMethod.invoke(playerObj); - - } catch (Throwable t) { - t.printStackTrace(); - } - } + u = plugin.getUUID(user.getIdentifier()); } } diff --git a/common/src/main/java/me/lucko/luckperms/groups/Group.java b/common/src/main/java/me/lucko/luckperms/groups/Group.java index 347a1e9ec..b466257da 100644 --- a/common/src/main/java/me/lucko/luckperms/groups/Group.java +++ b/common/src/main/java/me/lucko/luckperms/groups/Group.java @@ -30,14 +30,12 @@ import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.event.events.GroupAddEvent; import me.lucko.luckperms.api.implementation.internal.GroupLink; import me.lucko.luckperms.api.implementation.internal.PermissionHolderLink; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.core.PermissionHolder; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksException; import me.lucko.luckperms.utils.Identifiable; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; @ToString(of = {"name"}) diff --git a/common/src/main/java/me/lucko/luckperms/storage/methods/JSONDatastore.java b/common/src/main/java/me/lucko/luckperms/storage/methods/JSONDatastore.java index b84ae58f7..36cf28e90 100644 --- a/common/src/main/java/me/lucko/luckperms/storage/methods/JSONDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/storage/methods/JSONDatastore.java @@ -26,20 +26,13 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import lombok.Cleanup; import me.lucko.luckperms.LuckPermsPlugin; -import me.lucko.luckperms.api.LogEntry; -import me.lucko.luckperms.constants.Constants; -import me.lucko.luckperms.data.Log; import me.lucko.luckperms.groups.Group; -import me.lucko.luckperms.storage.Datastore; import me.lucko.luckperms.tracks.Track; import me.lucko.luckperms.users.User; import me.lucko.luckperms.utils.Node; import java.io.*; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.*; -import java.util.logging.Formatter; import java.util.stream.Collectors; import static me.lucko.luckperms.core.PermissionHolder.exportToLegacy; diff --git a/common/src/main/java/me/lucko/luckperms/users/User.java b/common/src/main/java/me/lucko/luckperms/users/User.java index 1e8a0825d..cf1416c7f 100644 --- a/common/src/main/java/me/lucko/luckperms/users/User.java +++ b/common/src/main/java/me/lucko/luckperms/users/User.java @@ -31,16 +31,13 @@ import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.event.events.GroupAddEvent; import me.lucko.luckperms.api.implementation.internal.GroupLink; import me.lucko.luckperms.api.implementation.internal.PermissionHolderLink; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.core.PermissionHolder; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksException; import me.lucko.luckperms.groups.Group; import me.lucko.luckperms.utils.Identifiable; -import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; diff --git a/sponge/src/main/java/me/lucko/luckperms/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/LPSpongePlugin.java index bb7ff3241..430ee0094 100644 --- a/sponge/src/main/java/me/lucko/luckperms/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/LPSpongePlugin.java @@ -244,6 +244,15 @@ public class LPSpongePlugin implements LuckPermsPlugin { return Sponge.getServiceManager().provideUnchecked(clazz); } + @Override + public UUID getUUID(String playerName) { + try { + return game.getServer().getPlayer(playerName).get().getUniqueId(); + } catch (Exception e) { + return null; + } + } + @Override public boolean isPluginLoaded(String name) { return game.getPluginManager().isLoaded(name); @@ -251,17 +260,17 @@ public class LPSpongePlugin implements LuckPermsPlugin { @Override public void runUpdateTask() { - scheduler.createTaskBuilder().async().execute(new UpdateTask(this)).submit(LPSpongePlugin.this); + scheduler.createTaskBuilder().async().execute(new UpdateTask(this)).submit(this); } @Override public void doAsync(Runnable r) { - scheduler.createTaskBuilder().async().execute(r).submit(LPSpongePlugin.this); + scheduler.createTaskBuilder().async().execute(r).submit(this); } @Override public void doSync(Runnable r) { - scheduler.createTaskBuilder().execute(r).submit(LPSpongePlugin.this); + scheduler.createTaskBuilder().execute(r).submit(this); } private void registerPermission(PermissionService p, String node) { diff --git a/sponge/src/main/java/me/lucko/luckperms/users/SpongeUser.java b/sponge/src/main/java/me/lucko/luckperms/users/SpongeUser.java index f816ceb8d..de4592a8a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/users/SpongeUser.java +++ b/sponge/src/main/java/me/lucko/luckperms/users/SpongeUser.java @@ -23,8 +23,6 @@ package me.lucko.luckperms.users; import me.lucko.luckperms.LPSpongePlugin; -import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent; -import me.lucko.luckperms.api.implementation.internal.UserLink; import java.util.UUID;