From 5dc551c05df27540325a4b470f74be3536ed60c3 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 21 Aug 2016 23:31:38 +0100 Subject: [PATCH] Complete the migration system --- bukkit/src/main/resources/plugin.yml | 2 +- bungee/src/main/resources/plugin.yml | 1 + .../luckperms/commands/CommandManager.java | 3 +- .../migration/MigrationMainCommand.java | 22 ++- .../subcommands/MigrationGroupManager.java | 155 +++++++++++++++++- .../subcommands/MigrationPermissionsEx.java | 37 ++++- .../subcommands/MigrationPowerfulPerms.java | 4 +- .../subcommands/MigrationZPermissions.java | 15 +- 8 files changed, 213 insertions(+), 26 deletions(-) diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index d2c90490..df9c012e 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] +softdepend: [Vault, PermissionsEx, GroupManager, PowerfulPerms, zPermissions] # For migration description: A permissions plugin commands: luckperms: diff --git a/bungee/src/main/resources/plugin.yml b/bungee/src/main/resources/plugin.yml index a9873e49..aa5c3b6f 100644 --- a/bungee/src/main/resources/plugin.yml +++ b/bungee/src/main/resources/plugin.yml @@ -2,4 +2,5 @@ name: LuckPerms author: Luck version: ${release.version}.${git.closest.tag.commit.count} main: me.lucko.luckperms.LPBungeePlugin +softdepend: [VPowerfulPerms] # For migration description: A permissions plugin \ No newline at end of file diff --git a/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java b/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java index 58ca429c..d85f39c6 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java +++ b/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java @@ -32,6 +32,7 @@ import me.lucko.luckperms.commands.group.DeleteGroup; import me.lucko.luckperms.commands.group.GroupMainCommand; import me.lucko.luckperms.commands.group.ListGroups; import me.lucko.luckperms.commands.log.LogMainCommand; +import me.lucko.luckperms.commands.migration.MigrationMainCommand; import me.lucko.luckperms.commands.misc.DebugCommand; import me.lucko.luckperms.commands.misc.ImportCommand; import me.lucko.luckperms.commands.misc.InfoCommand; @@ -63,7 +64,7 @@ public class CommandManager { .add(new InfoCommand()) .add(new DebugCommand()) .add(new ImportCommand()) - // .add(new MigrationMainCommand()) TODO + .add(new MigrationMainCommand()) .add(new CreateGroup()) .add(new DeleteGroup()) .add(new ListGroups()) 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 ef071c13..181c3d69 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 @@ -27,7 +27,10 @@ import me.lucko.luckperms.commands.CommandResult; import me.lucko.luckperms.commands.MainCommand; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; +import me.lucko.luckperms.commands.migration.subcommands.MigrationGroupManager; +import me.lucko.luckperms.commands.migration.subcommands.MigrationPermissionsEx; import me.lucko.luckperms.commands.migration.subcommands.MigrationPowerfulPerms; +import me.lucko.luckperms.commands.migration.subcommands.MigrationZPermissions; import me.lucko.luckperms.constants.Constants; import me.lucko.luckperms.constants.Message; @@ -42,9 +45,24 @@ public class MigrationMainCommand extends MainCommand { super("Migration", "/%s migration", 1, null); try { + Class.forName("org.anjocaido.groupmanager.GroupManager"); + subCommands.add(new MigrationGroupManager()); + } catch (Throwable ignored) {} + + try { + Class.forName("ru.tehkode.permissions.bukkit.PermissionsEx"); + subCommands.add(new MigrationPermissionsEx()); + } catch (Throwable ignored) {} + + try { + Class.forName("com.github.cheesesoftware.PowerfulPermsAPI.PowerfulPermsPlugin"); subCommands.add(new MigrationPowerfulPerms()); - } catch (Throwable ignored) { - } + } catch (Throwable ignored) {} + + try { + Class.forName("org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService"); + subCommands.add(new MigrationZPermissions()); + } catch (Throwable ignored) {} } diff --git a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationGroupManager.java b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationGroupManager.java index 9bcc8928..f1d764aa 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationGroupManager.java +++ b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationGroupManager.java @@ -29,14 +29,25 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Permission; +import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; +import org.anjocaido.groupmanager.GlobalGroups; import org.anjocaido.groupmanager.GroupManager; +import org.anjocaido.groupmanager.data.Group; +import org.anjocaido.groupmanager.data.User; +import org.anjocaido.groupmanager.dataholder.WorldDataHolder; +import org.anjocaido.groupmanager.dataholder.worlds.WorldsHolder; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; public class MigrationGroupManager extends SubCommand { public MigrationGroupManager() { super("groupmanager", "Migration from GroupManager", - "/%s migration groupmanager [world names]", Permission.MIGRATION, Predicate.alwaysFalse()); + "/%s migration groupmanager [world names]", Permission.MIGRATION, Predicate.is(0)); } @Override @@ -47,13 +58,145 @@ public class MigrationGroupManager extends SubCommand { return CommandResult.STATE_ERROR; } + final List worlds = args.stream() + .map(String::toLowerCase) + .collect(Collectors.toList()); + GroupManager gm = (GroupManager) plugin.getPlugin("GroupManager"); - // Migrate all users. - log.info("GroupManager Migration: Starting user migration."); + // Migrate Global Groups + log.info("GroupManager Migration: Starting Global Group migration."); - // gm.getWorldsHolder().getWorldData(). - // TODO - return null; + GlobalGroups gg; + try { + gg = (GlobalGroups) GroupManager.class.getMethod("getGlobalGroups").invoke(gm); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + return CommandResult.FAILURE; + } + + for (Group g : gg.getGroupList()) { + plugin.getDatastore().createAndLoadGroup(g.getName().toLowerCase()); + me.lucko.luckperms.groups.Group group = plugin.getGroupManager().get(g.getName().toLowerCase()); + + for (String node : g.getPermissionList()) { + boolean value = true; + if (node.startsWith("!")) { + node = node.substring(1); + value = false; + } + + try { + group.setPermission(node, value); + } catch (ObjectAlreadyHasException ignored) {} + } + + for (String s : g.getInherits()) { + try { + group.setPermission("group." + s.toLowerCase(), true); + } catch (ObjectAlreadyHasException ignored) {} + } + + } + + Map> users = new HashMap<>(); + Map> groups = new HashMap<>(); + + WorldsHolder wh; + try { + wh = (WorldsHolder) GroupManager.class.getMethod("getWorldsHolder").invoke(gm); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + return CommandResult.FAILURE; + } + + // Collect data for all users and groups. + log.info("GroupManager Migration: Starting user and group migration."); + for (String world : worlds) { + world = world.toLowerCase(); + + WorldDataHolder wdh; + + try { + wdh = (WorldDataHolder) WorldsHolder.class.getMethod("getWorldData", String.class).invoke(wh, world); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + return CommandResult.FAILURE; + } + + for (Group g : wdh.getGroupList()) { + groups.putIfAbsent(g.getName().toLowerCase(), new HashMap<>()); + + for (String node : g.getPermissionList()) { + boolean value = true; + if (node.startsWith("!")) { + node = node.substring(1); + value = false; + } + + groups.get(g.getName().toLowerCase()).put("global-" + world + "/" + node, value); + } + + for (String s : g.getInherits()) { + groups.get(g.getName().toLowerCase()).put("global-" + world + "/group." + s.toLowerCase(), true); + } + } + + for (User user : wdh.getUserList()) { + UUID uuid; + try { + uuid = UUID.fromString(user.getUUID()); + } catch (IllegalArgumentException e){ + continue; + } + + users.putIfAbsent(uuid, new HashMap<>()); + + for (String node : user.getPermissionList()) { + boolean value = true; + if (node.startsWith("!")) { + node = node.substring(1); + value = false; + } + + users.get(uuid).put("global-" + world + "/" + node, value); + } + + users.get(uuid).put("global-" + world + "/group." + user.getGroupName().toLowerCase(), true); + } + + } + + log.info("GroupManager Migration: All existing GroupManager data has been processed. Now beginning the import process."); + + for (Map.Entry> e : users.entrySet()) { + plugin.getDatastore().loadOrCreateUser(e.getKey(), "null"); + me.lucko.luckperms.users.User user = plugin.getUserManager().get(e.getKey()); + + for (Map.Entry n : e.getValue().entrySet()) { + try { + user.setPermission(n.getKey(), n.getValue()); + } catch (ObjectAlreadyHasException ignored) {} + } + + plugin.getDatastore().saveUser(user); + plugin.getUserManager().cleanup(user); + } + + for (Map.Entry> e : groups.entrySet()) { + plugin.getDatastore().createAndLoadGroup(e.getKey()); + me.lucko.luckperms.groups.Group group = plugin.getGroupManager().get(e.getKey()); + + for (Map.Entry n : e.getValue().entrySet()) { + try { + group.setPermission(n.getKey(), n.getValue()); + } catch (ObjectAlreadyHasException ignored) {} + } + + plugin.getDatastore().saveGroup(group); + } + + log.info("GroupManager Migration: Success! Completed without any errors."); + return CommandResult.SUCCESS; } } 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 fa5cb44a..1f373670 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 @@ -37,8 +37,10 @@ import ru.tehkode.permissions.PermissionManager; import ru.tehkode.permissions.PermissionUser; import ru.tehkode.permissions.bukkit.PermissionsEx; +import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; public class MigrationPermissionsEx extends SubCommand { public MigrationPermissionsEx() { @@ -55,8 +57,24 @@ public class MigrationPermissionsEx extends SubCommand { return CommandResult.STATE_ERROR; } + if (plugin.getType() != LuckPermsPlugin.Type.BUKKIT) { + // Sponge uses a completely different version of PEX. + log.severe("PEX import is not supported on this platform."); + return CommandResult.STATE_ERROR; + } + + final List worlds = args.stream() + .map(String::toLowerCase) + .collect(Collectors.toList()); + PermissionsEx pex = (PermissionsEx) plugin.getPlugin("PermissionsEx"); - PermissionManager manager = null; // TODO + PermissionManager manager; // The compiler complains if you call the method directly, as Bukkit is not in this module. + try { + manager = (PermissionManager) PermissionsEx.class.getMethod("getPermissionsManager").invoke(pex); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + return CommandResult.FAILURE; + } // Migrate all users log.info("PermissionsEx Migration: Starting user migration."); @@ -81,8 +99,8 @@ public class MigrationPermissionsEx extends SubCommand { // Probably won't happen. I have no API docs on getOwnPermissions#null though. } - if (args != null && !args.isEmpty()) { - for (String world : args) { + if (worlds != null && !worlds.isEmpty()) { + for (String world : worlds) { for (String node : user.getOwnPermissions(world)) { boolean value = true; if (node.startsWith("!")) { @@ -103,8 +121,8 @@ public class MigrationPermissionsEx extends SubCommand { } catch (ObjectAlreadyHasException ignored) {} } - if (args != null && !args.isEmpty()) { - for (String world : args) { + if (worlds != null && !worlds.isEmpty()) { + for (String world : worlds) { for (String s : user.getGroupNames(world)) { try { lpUser.setPermission("group." + s.toLowerCase(), true, "global", world); @@ -140,8 +158,8 @@ public class MigrationPermissionsEx extends SubCommand { // Probably won't happen. I have no API docs on getOwnPermissions#null though. } - if (args != null && !args.isEmpty()) { - for (String world : args) { + if (worlds != null && !worlds.isEmpty()) { + for (String world : worlds) { for (String node : group.getOwnPermissions(world)) { boolean value = true; if (node.startsWith("!")) { @@ -162,8 +180,8 @@ public class MigrationPermissionsEx extends SubCommand { } catch (ObjectAlreadyHasException ignored) {} } - if (args != null && !args.isEmpty()) { - for (String world : args) { + if (worlds != null && !worlds.isEmpty()) { + for (String world : worlds) { for (PermissionGroup g : group.getParents(world)) { try { lpGroup.setPermission("group." + g.getName().toLowerCase(), true, "global", world); @@ -173,6 +191,7 @@ public class MigrationPermissionsEx extends SubCommand { } } + log.info("PermissionsEx Migration: Success! Completed without any errors."); return CommandResult.SUCCESS; } } diff --git a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationPowerfulPerms.java b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationPowerfulPerms.java index e8db26c2..5ec3257d 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationPowerfulPerms.java +++ b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationPowerfulPerms.java @@ -56,7 +56,7 @@ public class MigrationPowerfulPerms extends SubCommand { */ private static Method getPlayerGroupsMethod = null; private static Method getGroupMethod = null; - private static boolean legacy = true; + private static boolean legacy = false; static { try { @@ -320,7 +320,7 @@ public class MigrationPowerfulPerms extends SubCommand { } // We done. - log.info("PowerfulPerms Migration: Success! Migration completed without any errors."); + log.info("PowerfulPerms Migration: Success! Completed without any errors."); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationZPermissions.java b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationZPermissions.java index 9a8e00ae..02548d02 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationZPermissions.java +++ b/common/src/main/java/me/lucko/luckperms/commands/migration/subcommands/MigrationZPermissions.java @@ -38,6 +38,7 @@ import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.stream.Collectors; /** * <3 <3 zPermissions <3 <3 @@ -63,6 +64,10 @@ public class MigrationZPermissions extends SubCommand { return CommandResult.STATE_ERROR; } + final List worlds = args.stream() + .map(String::toLowerCase) + .collect(Collectors.toList()); + // Migrate all users. log.info("zPermissions Migration: Starting user migration."); for (UUID u : service.getAllPlayersUUID()) { @@ -75,8 +80,8 @@ public class MigrationZPermissions extends SubCommand { } catch (ObjectAlreadyHasException ignored) {} } - if (args != null && !args.isEmpty()) { - for (String world : args) { + if (worlds != null && !worlds.isEmpty()) { + for (String world : worlds) { for (Map.Entry e : service.getPlayerPermissions(world, null, u).entrySet()) { try { user.setPermission(e.getKey(), e.getValue(), "global", world); @@ -119,8 +124,8 @@ public class MigrationZPermissions extends SubCommand { } catch (ObjectAlreadyHasException ignored) {} } - if (args != null && !args.isEmpty()) { - for (String world : args) { + if (worlds != null && !worlds.isEmpty()) { + for (String world : worlds) { for (Map.Entry e : service.getGroupPermissions(world, null, g).entrySet()) { try { group.setPermission(e.getKey(), e.getValue(), "global", world); @@ -132,7 +137,7 @@ public class MigrationZPermissions extends SubCommand { plugin.getDatastore().saveGroup(group); } - log.info("zPermissions Migration: Complete!"); + log.info("zPermissions Migration: Success! Completed without any errors."); return CommandResult.SUCCESS; } }