Search in sources :

Example 6 with ProgressLogger

use of me.lucko.luckperms.common.logging.ProgressLogger in project LuckPerms by lucko.

the class MigrationZPermissions method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List<String> args, String label) {
    ProgressLogger log = new ProgressLogger("zPermissions");
    log.addListener(plugin.getConsoleSender());
    log.addListener(sender);
    log.log("Starting.");
    if (!Bukkit.getPluginManager().isPluginEnabled("zPermissions")) {
        log.logError("Plugin not loaded.");
        return CommandResult.STATE_ERROR;
    }
    if (!Bukkit.getServicesManager().isProvidedFor(ZPermissionsService.class)) {
        log.logError("Plugin not loaded.");
        return CommandResult.STATE_ERROR;
    }
    ZPermissionsService service = Bukkit.getServicesManager().getRegistration(ZPermissionsService.class).getProvider();
    PermissionService internalService;
    try {
        Field psField = service.getClass().getDeclaredField("permissionService");
        psField.setAccessible(true);
        internalService = (PermissionService) psField.get(service);
    } catch (Exception e) {
        e.printStackTrace();
        return CommandResult.FAILURE;
    }
    // Migrate all groups
    log.log("Starting group migration.");
    Map<UUID, Set<Node>> userParents = new HashMap<>();
    AtomicInteger groupCount = new AtomicInteger(0);
    AtomicInteger maxWeight = new AtomicInteger(0);
    Iterators.iterate(internalService.getEntities(true), entity -> {
        String groupName = MigrationUtils.standardizeName(entity.getDisplayName());
        Group group = plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join();
        int weight = entity.getPriority();
        maxWeight.set(Math.max(maxWeight.get(), weight));
        migrateEntity(group, entity, weight);
        MigrationUtils.setGroupWeight(group, weight);
        // store user data for later
        Set<Membership> members = entity.getMemberships();
        for (Membership membership : members) {
            UUID uuid = BukkitMigrationUtils.lookupUuid(log, membership.getMember());
            if (uuid == null) {
                continue;
            }
            Set<Node> nodes = userParents.computeIfAbsent(uuid, u -> new HashSet<>());
            if (membership.getExpiration() == null) {
                nodes.add(NodeFactory.buildGroupNode(groupName).build());
            } else {
                long expiry = membership.getExpiration().toInstant().getEpochSecond();
                nodes.add(NodeFactory.buildGroupNode(groupName).setExpiry(expiry).build());
            }
        }
        plugin.getStorage().saveGroup(group);
        log.logAllProgress("Migrated {} groups so far.", groupCount.incrementAndGet());
    });
    log.log("Migrated " + groupCount.get() + " groups");
    // Migrate all tracks
    log.log("Starting track migration.");
    AtomicInteger trackCount = new AtomicInteger(0);
    Iterators.iterate(service.getAllTracks(), t -> {
        String trackName = MigrationUtils.standardizeName(t);
        Track track = plugin.getStorage().createAndLoadTrack(trackName, CreationCause.INTERNAL).join();
        track.setGroups(service.getTrackGroups(t));
        plugin.getStorage().saveTrack(track);
        log.logAllProgress("Migrated {} tracks so far.", trackCount.incrementAndGet());
    });
    log.log("Migrated " + trackCount.get() + " tracks");
    // Migrate all users.
    log.log("Starting user migration.");
    maxWeight.addAndGet(10);
    AtomicInteger userCount = new AtomicInteger(0);
    Set<UUID> usersToMigrate = new HashSet<>(userParents.keySet());
    usersToMigrate.addAll(service.getAllPlayersUUID());
    Iterators.iterate(usersToMigrate, u -> {
        PermissionEntity entity = internalService.getEntity(null, u, false);
        String username = null;
        if (entity != null) {
            username = entity.getDisplayName();
        }
        User user = plugin.getStorage().loadUser(u, username).join();
        // migrate permissions & meta
        if (entity != null) {
            migrateEntity(user, entity, maxWeight.get());
        }
        // migrate groups
        Set<Node> parents = userParents.get(u);
        if (parents != null) {
            parents.forEach(user::setPermission);
        }
        user.getPrimaryGroup().setStoredValue(MigrationUtils.standardizeName(service.getPlayerPrimaryGroup(u)));
        plugin.getUserManager().cleanup(user);
        plugin.getStorage().saveUser(user);
        log.logProgress("Migrated {} users so far.", userCount.incrementAndGet());
    });
    log.log("Migrated " + userCount.get() + " users.");
    log.log("Success! Migration complete.");
    return CommandResult.SUCCESS;
}
Also used : Group(me.lucko.luckperms.common.model.Group) HashSet(java.util.HashSet) Set(java.util.Set) User(me.lucko.luckperms.common.model.User) HashMap(java.util.HashMap) Node(me.lucko.luckperms.api.Node) ProgressLogger(me.lucko.luckperms.common.logging.ProgressLogger) PermissionService(org.tyrannyofheaven.bukkit.zPermissions.dao.PermissionService) Field(java.lang.reflect.Field) Membership(org.tyrannyofheaven.bukkit.zPermissions.model.Membership) UUID(java.util.UUID) PermissionEntity(org.tyrannyofheaven.bukkit.zPermissions.model.PermissionEntity) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Track(me.lucko.luckperms.common.model.Track) ZPermissionsService(org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService)

Example 7 with ProgressLogger

use of me.lucko.luckperms.common.logging.ProgressLogger in project LuckPerms by lucko.

the class MigrationBungeePerms method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List<String> args, String label) {
    ProgressLogger log = new ProgressLogger("BungeePerms");
    log.addListener(plugin.getConsoleSender());
    log.addListener(sender);
    log.log("Starting.");
    // Get BungeePerms instance
    BungeePerms bp = BungeePerms.getInstance();
    if (bp == null) {
        log.logError("Plugin not loaded.");
        return CommandResult.STATE_ERROR;
    }
    List<Group> groups = bp.getPermissionsManager().getBackEnd().loadGroups();
    log.log("Calculating group weightings.");
    int i = 0;
    for (Group group : groups) {
        i = Math.max(i, group.getRank());
    }
    int maxWeight = i + 5;
    // Migrate all groups.
    log.log("Starting group migration.");
    AtomicInteger groupCount = new AtomicInteger(0);
    Iterators.iterate(groups, g -> {
        int groupWeight = maxWeight - g.getRank();
        // Make a LuckPerms group for the one being migrated
        String groupName = MigrationUtils.standardizeName(g.getName());
        me.lucko.luckperms.common.model.Group group = plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join();
        MigrationUtils.setGroupWeight(group, groupWeight);
        migrateHolder(g, g.getInheritances(), groupWeight, group);
        plugin.getStorage().saveGroup(group);
        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);
    // Increment the max weight from the group migrations. All user meta should override.
    int userWeight = maxWeight + 5;
    Iterators.iterate(bp.getPermissionsManager().getBackEnd().loadUsers(), u -> {
        if (u.getUUID() == null) {
            log.logError("Could not parse UUID for user: " + u.getName());
            return;
        }
        // Make a LuckPerms user for the one being migrated.
        me.lucko.luckperms.common.model.User user = plugin.getStorage().loadUser(u.getUUID(), u.getName()).join();
        migrateHolder(u, u.getGroupsString(), userWeight, user);
        plugin.getStorage().saveUser(user);
        plugin.getUserManager().cleanup(user);
        log.logProgress("Migrated {} users so far.", userCount.incrementAndGet());
    });
    log.log("Migrated " + userCount.get() + " users.");
    log.log("Success! Migration complete.");
    return CommandResult.SUCCESS;
}
Also used : Group(net.alpenblock.bungeeperms.Group) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressLogger(me.lucko.luckperms.common.logging.ProgressLogger) BungeePerms(net.alpenblock.bungeeperms.BungeePerms)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 ProgressLogger (me.lucko.luckperms.common.logging.ProgressLogger)7 UUID (java.util.UUID)6 User (me.lucko.luckperms.common.model.User)6 Group (me.lucko.luckperms.common.model.Group)5 HashSet (java.util.HashSet)4 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 Collectors (java.util.stream.Collectors)2 Node (me.lucko.luckperms.api.Node)2 CreationCause (me.lucko.luckperms.api.event.cause.CreationCause)2 CommandResult (me.lucko.luckperms.common.command.CommandResult)2 SubCommand (me.lucko.luckperms.common.command.abstraction.SubCommand)2 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)2 MigrationUtils (me.lucko.luckperms.common.commands.migration.MigrationUtils)2 LocaleManager (me.lucko.luckperms.common.locale.LocaleManager)2 CommandSpec (me.lucko.luckperms.common.locale.command.CommandSpec)2