Search in sources :

Example 1 with WorldManager

use of de.bananaco.bpermissions.api.WorldManager in project LuckPerms by lucko.

the class MigrationBPermissions method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List<String> args, String label) {
    ProgressLogger log = new ProgressLogger("bPermissions");
    log.addListener(plugin.getConsoleSender());
    log.addListener(sender);
    log.log("Starting.");
    WorldManager worldManager = WorldManager.getInstance();
    if (worldManager == null) {
        log.logError("Plugin not loaded.");
        return CommandResult.STATE_ERROR;
    }
    log.log("Forcing the plugin to load all data. This could take a while.");
    for (World world : worldManager.getAllWorlds()) {
        log.log("Loading users in world " + world.getName());
        YamlConfiguration yamlWorldUsers = null;
        try {
            yamlWorldUsers = (YamlConfiguration) UCONFIG_FIELD.get(world);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        if (yamlWorldUsers == null) {
            continue;
        }
        ConfigurationSection configSection = yamlWorldUsers.getConfigurationSection("users");
        if (configSection == null) {
            continue;
        }
        Set<String> users = configSection.getKeys(false);
        if (users == null) {
            log.logError("Couldn't get a list of users.");
            return CommandResult.FAILURE;
        }
        AtomicInteger userLoadCount = new AtomicInteger(0);
        users.forEach(s -> {
            world.loadOne(s, CalculableType.USER);
            log.logProgress("Forcefully loaded {} users so far.", userLoadCount.incrementAndGet());
        });
    }
    log.log("Forcefully loaded all users.");
    // Migrate one world at a time.
    log.log("Starting world migration.");
    Iterators.iterate(worldManager.getAllWorlds(), world -> {
        log.log("Migrating world: " + world.getName());
        // Migrate all groups
        log.log("Starting group migration in world " + world.getName() + ".");
        AtomicInteger groupCount = new AtomicInteger(0);
        Iterators.iterate(world.getAll(CalculableType.GROUP), group -> {
            String groupName = MigrationUtils.standardizeName(group.getName());
            if (group.getName().equalsIgnoreCase(world.getDefaultGroup())) {
                groupName = NodeFactory.DEFAULT_GROUP_NAME;
            }
            // Make a LuckPerms group for the one being migrated.
            Group lpGroup = plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join();
            MigrationUtils.setGroupWeight(lpGroup, group.getPriority());
            migrateHolder(world, group, lpGroup);
            plugin.getStorage().saveGroup(lpGroup);
            log.logAllProgress("Migrated {} groups so far.", groupCount.incrementAndGet());
        });
        log.log("Migrated " + groupCount.get() + " groups in world " + world.getName() + ".");
        // Migrate all users
        log.log("Starting user migration in world " + world.getName() + ".");
        AtomicInteger userCount = new AtomicInteger(0);
        Iterators.iterate(world.getAll(CalculableType.USER), user -> {
            // There is no mention of UUIDs in the API. I assume that name = uuid. idk?
            UUID uuid = BukkitMigrationUtils.lookupUuid(log, user.getName());
            if (uuid == null) {
                return;
            }
            // Make a LuckPerms user for the one being migrated.
            User lpUser = plugin.getStorage().loadUser(uuid, null).join();
            migrateHolder(world, user, lpUser);
            plugin.getStorage().saveUser(lpUser);
            plugin.getUserManager().cleanup(lpUser);
            log.logProgress("Migrated {} users so far.", userCount.incrementAndGet());
        });
        log.log("Migrated " + userCount.get() + " users in world " + world.getName() + ".");
    });
    log.log("Success! Migration complete.");
    return CommandResult.SUCCESS;
}
Also used : Group(me.lucko.luckperms.common.model.Group) User(me.lucko.luckperms.common.model.User) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WorldManager(de.bananaco.bpermissions.api.WorldManager) ProgressLogger(me.lucko.luckperms.common.logging.ProgressLogger) World(de.bananaco.bpermissions.api.World) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) UUID(java.util.UUID) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

World (de.bananaco.bpermissions.api.World)1 WorldManager (de.bananaco.bpermissions.api.WorldManager)1 UUID (java.util.UUID)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ProgressLogger (me.lucko.luckperms.common.logging.ProgressLogger)1 Group (me.lucko.luckperms.common.model.Group)1 User (me.lucko.luckperms.common.model.User)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)1