From 6c8f04f8293764e51adfb4194579d210c69af883 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 1 Jan 2018 21:11:50 +0000 Subject: [PATCH] Implement PermissionsBukkit migration --- bukkit/pom.xml | 7 + .../migration/MigrationPermissionsBukkit.java | 166 ++++++++++++++++++ .../impl/migration/MigrationMainCommand.java | 26 +-- 3 files changed, 188 insertions(+), 11 deletions(-) create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsBukkit.java diff --git a/bukkit/pom.xml b/bukkit/pom.xml index 5f1711575..73d81b9e7 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -160,5 +160,12 @@ 2.12 provided + + + com.platymuus + bukkit-permissions + 2.5 + provided + diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsBukkit.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsBukkit.java new file mode 100644 index 000000000..d4719b28f --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsBukkit.java @@ -0,0 +1,166 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.migration; + +import com.platymuus.bukkit.permissions.PermissionsPlugin; + +import me.lucko.luckperms.api.event.cause.CreationCause; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandPermission; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.abstraction.SubCommand; +import me.lucko.luckperms.common.commands.impl.migration.MigrationUtils; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.locale.CommandSpec; +import me.lucko.luckperms.common.locale.LocaleManager; +import me.lucko.luckperms.common.logging.ProgressLogger; +import me.lucko.luckperms.common.model.Group; +import me.lucko.luckperms.common.model.PermissionHolder; +import me.lucko.luckperms.common.model.User; +import me.lucko.luckperms.common.node.NodeFactory; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.utils.Predicates; +import me.lucko.luckperms.common.utils.SafeIterator; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; + +public class MigrationPermissionsBukkit extends SubCommand { + public MigrationPermissionsBukkit(LocaleManager locale) { + super(CommandSpec.MIGRATION_COMMAND.spec(locale), "permissionsbukkit", CommandPermission.MIGRATION, Predicates.alwaysFalse()); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) throws CommandException { + ProgressLogger log = new ProgressLogger("PermissionsBukkit"); + log.addListener(plugin.getConsoleSender()); + log.addListener(sender); + + log.log("Starting."); + + if (!Bukkit.getPluginManager().isPluginEnabled("PermissionsBukkit")) { + log.logErr("Plugin not loaded."); + return CommandResult.STATE_ERROR; + } + + PermissionsPlugin permissionsBukkit = (PermissionsPlugin) Bukkit.getPluginManager().getPlugin("PermissionsBukkit"); + FileConfiguration config = permissionsBukkit.getConfig(); + + // Migrate all groups + log.log("Starting group migration."); + AtomicInteger groupCount = new AtomicInteger(0); + + ConfigurationSection groupsSection = config.getConfigurationSection("groups"); + + SafeIterator.iterate(groupsSection.getKeys(false), key -> { + final String groupName = MigrationUtils.standardizeName(key); + plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join(); + Group lpGroup = plugin.getGroupManager().getIfLoaded(groupName); + + // migrate data + if (groupsSection.isConfigurationSection(key)) { + migrate(lpGroup, groupsSection.getConfigurationSection(key)); + } + + plugin.getStorage().saveGroup(lpGroup).join(); + log.logAllProgress("Migrated {} groups so far.", groupCount.incrementAndGet()); + }); + log.log("Migrated " + groupCount.get() + " groups"); + + // Migrate all users + log.log("Starting user migration."); + AtomicInteger userCount = new AtomicInteger(0); + + ConfigurationSection usersSection = config.getConfigurationSection("users"); + + SafeIterator.iterate(usersSection.getKeys(false), key -> { + UUID uuid = BukkitMigrationUtils.lookupUuid(log, key); + if (uuid == null) { + return; + } + + plugin.getStorage().loadUser(uuid, null).join(); + User lpUser = plugin.getUserManager().getIfLoaded(uuid); + + // migrate data + if (usersSection.isConfigurationSection(key)) { + migrate(lpUser, usersSection.getConfigurationSection(key)); + } + + plugin.getUserManager().cleanup(lpUser); + plugin.getStorage().saveUser(lpUser); + log.logProgress("Migrated {} users so far.", userCount.incrementAndGet()); + }); + + log.log("Migrated " + userCount.get() + " users."); + log.log("Success! Migration complete."); + return CommandResult.SUCCESS; + } + + private static void migrate(PermissionHolder holder, ConfigurationSection data) { + // migrate permissions + if (data.isConfigurationSection("permissions")) { + ConfigurationSection permsSection = data.getConfigurationSection("permissions"); + for (String perm : permsSection.getKeys(false)) { + boolean value = permsSection.getBoolean(perm); + holder.setPermission(MigrationUtils.parseNode(perm, value).build()); + } + } + + if (data.isConfigurationSection("worlds")) { + ConfigurationSection worldSection = data.getConfigurationSection("worlds"); + for (String world : worldSection.getKeys(false)) { + if (worldSection.isConfigurationSection(world)) { + ConfigurationSection permsSection = worldSection.getConfigurationSection(world); + for (String perm : permsSection.getKeys(false)) { + boolean value = permsSection.getBoolean(perm); + holder.setPermission(MigrationUtils.parseNode(perm, value).setWorld(world).build()); + } + } + } + } + + // migrate parents + if (data.isList("groups")) { + List groups = data.getStringList("groups"); + for (String group : groups) { + holder.setPermission(NodeFactory.buildGroupNode(MigrationUtils.standardizeName(group)).build()); + } + } + if (data.isList("inheritance")) { + List groups = data.getStringList("inheritance"); + for (String group : groups) { + holder.setPermission(NodeFactory.buildGroupNode(MigrationUtils.standardizeName(group)).build()); + } + } + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/migration/MigrationMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/migration/MigrationMainCommand.java index 353efa9a2..4ac0985f6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/migration/MigrationMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/migration/MigrationMainCommand.java @@ -25,7 +25,7 @@ package me.lucko.luckperms.common.commands.impl.migration; -import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableBiMap; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandPermission; @@ -47,16 +47,20 @@ import java.util.Optional; import java.util.concurrent.locks.ReentrantLock; public class MigrationMainCommand extends MainCommand { - private static final Map PLUGINS = ImmutableMap.builder() - .put("org.anjocaido.groupmanager.GroupManager", "me.lucko.luckperms.bukkit.migration.MigrationGroupManager") - .put("ru.tehkode.permissions.bukkit.PermissionsEx", "me.lucko.luckperms.bukkit.migration.MigrationPermissionsEx") - .put("com.github.gustav9797.PowerfulPermsAPI.PowerfulPermsPlugin", "me.lucko.luckperms.bukkit.migration.MigrationPowerfulPerms") - .put("org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService", "me.lucko.luckperms.bukkit.migration.MigrationZPermissions") - .put("net.alpenblock.bungeeperms.BungeePerms", "me.lucko.luckperms.bungee.migration.MigrationBungeePerms") - .put("de.bananaco.bpermissions.api.WorldManager", "me.lucko.luckperms.bukkit.migration.MigrationBPermissions") - .put("ninja.leaping.permissionsex.sponge.PermissionsExPlugin", "me.lucko.luckperms.sponge.migration.MigrationPermissionsEx") - .put("io.github.djxy.permissionmanager.sponge.SpongePlugin", "me.lucko.luckperms.sponge.migration.MigrationPermissionManager") - .build(); + private static final Map PLUGINS = ImmutableBiMap.builder() + // bukkit + .put("me.lucko.luckperms.bukkit.migration.MigrationGroupManager", "org.anjocaido.groupmanager.GroupManager") + .put("me.lucko.luckperms.bukkit.migration.MigrationPermissionsEx", "ru.tehkode.permissions.bukkit.PermissionsEx") + .put("me.lucko.luckperms.bukkit.migration.MigrationPowerfulPerms", "com.github.gustav9797.PowerfulPermsAPI.PowerfulPermsPlugin") + .put("me.lucko.luckperms.bukkit.migration.MigrationZPermissions", "org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService") + .put("me.lucko.luckperms.bukkit.migration.MigrationBPermissions", "de.bananaco.bpermissions.api.WorldManager") + .put("me.lucko.luckperms.bukkit.migration.MigrationPermissionsBukkit", "com.platymuus.bukkit.permissions.PermissionsPlugin") + // bungee + .put("me.lucko.luckperms.bungee.migration.MigrationBungeePerms", "net.alpenblock.bungeeperms.BungeePerms") + // sponge + .put("me.lucko.luckperms.sponge.migration.MigrationPermissionsEx", "ninja.leaping.permissionsex.sponge.PermissionsExPlugin") + .put("me.lucko.luckperms.sponge.migration.MigrationPermissionManager", "io.github.djxy.permissionmanager.sponge.SpongePlugin") + .build().inverse(); private final ReentrantLock lock = new ReentrantLock(); private List> commands = null;