Search in sources :

Example 6 with ChatMetaType

use of me.lucko.luckperms.api.ChatMetaType in project LuckPerms by lucko.

the class MigrationBPermissions method migrateHolder.

private static void migrateHolder(World world, Calculable c, PermissionHolder holder) {
    // Migrate the groups permissions in this world
    for (Permission p : c.getPermissions()) {
        if (p.name().isEmpty()) {
            continue;
        }
        holder.setPermission(NodeFactory.make(p.name(), p.isTrue(), "global", world.getName()));
        // Include any child permissions
        for (Map.Entry<String, Boolean> child : p.getChildren().entrySet()) {
            if (child.getKey().isEmpty()) {
                continue;
            }
            holder.setPermission(NodeFactory.make(child.getKey(), child.getValue(), "global", world.getName()));
        }
    }
    // Migrate any inherited groups
    c.getGroups().forEach(parent -> {
        String parentName = MigrationUtils.standardizeName(parent.getName());
        if (parent.getName().equalsIgnoreCase(world.getDefaultGroup())) {
            parentName = NodeFactory.DEFAULT_GROUP_NAME;
        }
        holder.setPermission(NodeFactory.make(NodeFactory.groupNode(parentName), true, "global", world.getName()));
    });
    // Migrate existing meta
    for (Map.Entry<String, String> meta : c.getMeta().entrySet()) {
        if (meta.getKey().isEmpty() || meta.getValue().isEmpty()) {
            continue;
        }
        if (meta.getKey().equalsIgnoreCase(NodeFactory.PREFIX_KEY) || meta.getKey().equalsIgnoreCase(NodeFactory.SUFFIX_KEY)) {
            ChatMetaType type = ChatMetaType.valueOf(meta.getKey().toUpperCase());
            holder.setPermission(NodeFactory.buildChatMetaNode(type, c.getPriority(), meta.getValue()).setWorld(world.getName()).build());
            continue;
        }
        holder.setPermission(NodeFactory.buildMetaNode(meta.getKey(), meta.getValue()).setWorld(world.getName()).build());
    }
}
Also used : Permission(de.bananaco.bpermissions.api.Permission) ChatMetaType(me.lucko.luckperms.api.ChatMetaType) Map(java.util.Map)

Example 7 with ChatMetaType

use of me.lucko.luckperms.api.ChatMetaType in project LuckPerms by lucko.

the class MigrationGroupManager method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List<String> args, String label) {
    ProgressLogger log = new ProgressLogger("GroupManager");
    log.addListener(plugin.getConsoleSender());
    log.addListener(sender);
    log.log("Starting.");
    if (!args.get(0).equalsIgnoreCase("true") && !args.get(0).equalsIgnoreCase("false")) {
        log.logError("Was expecting true/false, but got " + args.get(0) + " instead.");
        return CommandResult.STATE_ERROR;
    }
    final boolean migrateAsGlobal = Boolean.parseBoolean(args.get(0));
    final Function<String, String> worldMappingFunc = s -> migrateAsGlobal ? null : s;
    if (!Bukkit.getPluginManager().isPluginEnabled("GroupManager")) {
        log.logError("Plugin not loaded.");
        return CommandResult.STATE_ERROR;
    }
    List<String> worlds = Bukkit.getWorlds().stream().map(World::getName).map(String::toLowerCase).collect(Collectors.toList());
    GroupManager gm = (GroupManager) Bukkit.getPluginManager().getPlugin("GroupManager");
    // Migrate Global Groups
    log.log("Starting global group migration.");
    GlobalGroups gg = GroupManager.getGlobalGroups();
    AtomicInteger globalGroupCount = new AtomicInteger(0);
    Iterators.iterate(gg.getGroupList(), g -> {
        String groupName = MigrationUtils.standardizeName(g.getName());
        Group group = plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join();
        for (String node : g.getPermissionList()) {
            if (node.isEmpty())
                continue;
            group.setPermission(MigrationUtils.parseNode(node, true).build());
        }
        for (String s : g.getInherits()) {
            if (s.isEmpty())
                continue;
            group.setPermission(NodeFactory.make(NodeFactory.groupNode(MigrationUtils.standardizeName(s))));
        }
        plugin.getStorage().saveGroup(group);
        log.logAllProgress("Migrated {} groups so far.", globalGroupCount.incrementAndGet());
    });
    log.log("Migrated " + globalGroupCount.get() + " global groups");
    // Collect data
    Map<UUID, Set<Node>> users = new HashMap<>();
    Map<UUID, String> primaryGroups = new HashMap<>();
    Map<String, Set<Node>> groups = new HashMap<>();
    WorldsHolder wh = gm.getWorldsHolder();
    // Collect data for all users and groups.
    log.log("Collecting user and group data.");
    Iterators.iterate(worlds, String::toLowerCase, world -> {
        log.log("Querying world " + world);
        WorldDataHolder wdh = wh.getWorldData(world);
        AtomicInteger groupWorldCount = new AtomicInteger(0);
        Iterators.iterate(wdh.getGroupList(), group -> {
            String groupName = MigrationUtils.standardizeName(group.getName());
            groups.putIfAbsent(groupName, new HashSet<>());
            for (String node : group.getPermissionList()) {
                if (node.isEmpty())
                    continue;
                groups.get(groupName).add(MigrationUtils.parseNode(node, true).setWorld(worldMappingFunc.apply(world)).build());
            }
            for (String s : group.getInherits()) {
                if (s.isEmpty())
                    continue;
                groups.get(groupName).add(NodeFactory.make(NodeFactory.groupNode(MigrationUtils.standardizeName(s)), true, null, worldMappingFunc.apply(world)));
            }
            String[] metaKeys = group.getVariables().getVarKeyList();
            for (String key : metaKeys) {
                String value = group.getVariables().getVarString(key);
                key = key.toLowerCase();
                if (key.isEmpty() || value.isEmpty())
                    continue;
                if (key.equals("build"))
                    continue;
                if (key.equals(NodeFactory.PREFIX_KEY) || key.equals(NodeFactory.SUFFIX_KEY)) {
                    ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase());
                    groups.get(groupName).add(NodeFactory.buildChatMetaNode(type, 50, value).setWorld(worldMappingFunc.apply(world)).build());
                } else {
                    groups.get(groupName).add(NodeFactory.buildMetaNode(key, value).setWorld(worldMappingFunc.apply(world)).build());
                }
            }
            log.logAllProgress("Migrated {} groups so far in world " + world, groupWorldCount.incrementAndGet());
        });
        log.log("Migrated " + groupWorldCount.get() + " groups in world " + world);
        AtomicInteger userWorldCount = new AtomicInteger(0);
        Iterators.iterate(wdh.getUserList(), user -> {
            UUID uuid = BukkitMigrationUtils.lookupUuid(log, user.getUUID());
            if (uuid == null) {
                return;
            }
            users.putIfAbsent(uuid, new HashSet<>());
            for (String node : user.getPermissionList()) {
                if (node.isEmpty())
                    continue;
                users.get(uuid).add(MigrationUtils.parseNode(node, true).setWorld(worldMappingFunc.apply(world)).build());
            }
            // Collect sub groups
            String finalWorld = worldMappingFunc.apply(world);
            users.get(uuid).addAll(user.subGroupListStringCopy().stream().filter(n -> !n.isEmpty()).map(n -> NodeFactory.groupNode(MigrationUtils.standardizeName(n))).map(n -> NodeFactory.make(n, true, null, finalWorld)).collect(Collectors.toSet()));
            // Get primary group
            primaryGroups.put(uuid, MigrationUtils.standardizeName(user.getGroupName()));
            String[] metaKeys = user.getVariables().getVarKeyList();
            for (String key : metaKeys) {
                String value = user.getVariables().getVarString(key);
                key = key.toLowerCase();
                if (key.isEmpty() || value.isEmpty())
                    continue;
                if (key.equals("build"))
                    continue;
                if (key.equals(NodeFactory.PREFIX_KEY) || key.equals(NodeFactory.SUFFIX_KEY)) {
                    ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase());
                    users.get(uuid).add(NodeFactory.buildChatMetaNode(type, 100, value).setWorld(worldMappingFunc.apply(world)).build());
                } else {
                    users.get(uuid).add(NodeFactory.buildMetaNode(key, value).setWorld(worldMappingFunc.apply(world)).build());
                }
            }
            log.logProgress("Migrated {} users so far in world " + world, userWorldCount.incrementAndGet());
        });
        log.log("Migrated " + userWorldCount.get() + " users in world " + world);
    });
    log.log("All data has now been processed, now starting the import process.");
    log.log("Found a total of " + users.size() + " users and " + groups.size() + " groups.");
    log.log("Starting group migration.");
    AtomicInteger groupCount = new AtomicInteger(0);
    Iterators.iterate(groups.entrySet(), e -> {
        Group group = plugin.getStorage().createAndLoadGroup(e.getKey(), CreationCause.INTERNAL).join();
        for (Node node : e.getValue()) {
            group.setPermission(node);
        }
        plugin.getStorage().saveGroup(group);
        log.logAllProgress("Migrated {} groups so far.", groupCount.incrementAndGet());
    });
    log.log("Migrated " + groupCount.get() + " groups");
    log.log("Starting user migration.");
    AtomicInteger userCount = new AtomicInteger(0);
    Iterators.iterate(users.entrySet(), e -> {
        User user = plugin.getStorage().loadUser(e.getKey(), null).join();
        for (Node node : e.getValue()) {
            user.setPermission(node);
        }
        String primaryGroup = primaryGroups.get(e.getKey());
        if (primaryGroup != null && !primaryGroup.isEmpty()) {
            user.setPermission(NodeFactory.buildGroupNode(primaryGroup).build());
            user.getPrimaryGroup().setStoredValue(primaryGroup);
            user.unsetPermission(NodeFactory.buildGroupNode(NodeFactory.DEFAULT_GROUP_NAME).build());
        }
        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 : SubCommand(me.lucko.luckperms.common.command.abstraction.SubCommand) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) HashMap(java.util.HashMap) GlobalGroups(org.anjocaido.groupmanager.GlobalGroups) Function(java.util.function.Function) HashSet(java.util.HashSet) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) ProgressLogger(me.lucko.luckperms.common.logging.ProgressLogger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WorldsHolder(org.anjocaido.groupmanager.dataholder.worlds.WorldsHolder) World(org.bukkit.World) Map(java.util.Map) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) Bukkit(org.bukkit.Bukkit) CreationCause(me.lucko.luckperms.api.event.cause.CreationCause) MigrationUtils(me.lucko.luckperms.common.commands.migration.MigrationUtils) Set(java.util.Set) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) Sender(me.lucko.luckperms.common.sender.Sender) UUID(java.util.UUID) Iterators(me.lucko.luckperms.common.utils.Iterators) Collectors(java.util.stream.Collectors) CommandResult(me.lucko.luckperms.common.command.CommandResult) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) List(java.util.List) GroupManager(org.anjocaido.groupmanager.GroupManager) ChatMetaType(me.lucko.luckperms.api.ChatMetaType) Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) User(me.lucko.luckperms.common.model.User) Predicates(me.lucko.luckperms.common.utils.Predicates) WorldDataHolder(org.anjocaido.groupmanager.dataholder.WorldDataHolder) Group(me.lucko.luckperms.common.model.Group) HashSet(java.util.HashSet) Set(java.util.Set) User(me.lucko.luckperms.common.model.User) WorldsHolder(org.anjocaido.groupmanager.dataholder.worlds.WorldsHolder) HashMap(java.util.HashMap) Node(me.lucko.luckperms.api.Node) ChatMetaType(me.lucko.luckperms.api.ChatMetaType) ProgressLogger(me.lucko.luckperms.common.logging.ProgressLogger) World(org.bukkit.World) GroupManager(org.anjocaido.groupmanager.GroupManager) WorldDataHolder(org.anjocaido.groupmanager.dataholder.WorldDataHolder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GlobalGroups(org.anjocaido.groupmanager.GlobalGroups) UUID(java.util.UUID)

Example 8 with ChatMetaType

use of me.lucko.luckperms.api.ChatMetaType in project LuckPerms by lucko.

the class VaultChatHook method setChatMeta.

private void setChatMeta(PermissionHolder holder, ChatMetaType type, String value, String world) {
    if (log()) {
        logMsg("#setChatMeta: %s - %s - %s - %s", holder.getFriendlyName(), type, value, world);
    }
    this.permissionHook.getExecutor().execute(() -> {
        // remove all prefixes/suffixes directly set on the user/group
        holder.removeIf(type::matches);
        if (value == null) {
            this.permissionHook.holderSave(holder);
            return;
        }
        // find the max inherited priority & add 10
        MetaAccumulator metaAccumulator = holder.accumulateMeta(null, createContextForWorldSet(world));
        int priority = (type == ChatMetaType.PREFIX ? metaAccumulator.getPrefixes() : metaAccumulator.getSuffixes()).keySet().stream().mapToInt(e -> e).max().orElse(0) + 10;
        Node.Builder chatMetaNode = NodeFactory.buildChatMetaNode(type, priority, value);
        chatMetaNode.setServer(this.permissionHook.getVaultServer());
        chatMetaNode.setWorld(world);
        holder.setPermission(chatMetaNode.build());
        this.permissionHook.holderSave(holder);
    });
}
Also used : CommandManager(me.lucko.luckperms.common.command.CommandManager) Contexts(me.lucko.luckperms.api.Contexts) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) UUID(java.util.UUID) ConfigKeys(me.lucko.luckperms.common.config.ConfigKeys) MetaCache(me.lucko.luckperms.common.caching.type.MetaCache) Objects(java.util.Objects) Strings(com.google.common.base.Strings) LPBukkitPlugin(me.lucko.luckperms.bukkit.LPBukkitPlugin) ChatMetaType(me.lucko.luckperms.api.ChatMetaType) Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) Chat(net.milkbowl.vault.chat.Chat) User(me.lucko.luckperms.common.model.User) MetaAccumulator(me.lucko.luckperms.common.caching.type.MetaAccumulator) MetaAccumulator(me.lucko.luckperms.common.caching.type.MetaAccumulator) Node(me.lucko.luckperms.api.Node)

Example 9 with ChatMetaType

use of me.lucko.luckperms.api.ChatMetaType in project LuckPerms by lucko.

the class HolderSubjectData method setOption.

@Override
public CompletableFuture<Boolean> setOption(ImmutableContextSet contexts, String key, String value) {
    Objects.requireNonNull(contexts, "contexts");
    Objects.requireNonNull(key, "key");
    Objects.requireNonNull(value, "value");
    Node node;
    if (key.equalsIgnoreCase(NodeFactory.PREFIX_KEY) || key.equalsIgnoreCase(NodeFactory.SUFFIX_KEY)) {
        // special handling.
        ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase());
        // remove all prefixes/suffixes from the user
        List<Node> toRemove = streamNodes().filter(type::matches).filter(n -> n.getFullContexts().equals(contexts)).collect(Collectors.toList());
        toRemove.forEach(n -> this.type.run(() -> this.holder.unsetPermission(n), () -> this.holder.unsetTransientPermission(n)));
        MetaAccumulator metaAccumulator = this.holder.accumulateMeta(null, this.service.getPlugin().getContextManager().formContexts(contexts));
        int priority = metaAccumulator.getChatMeta(type).keySet().stream().mapToInt(e -> e).max().orElse(0);
        priority += 10;
        node = NodeFactory.buildChatMetaNode(type, priority, value).withExtraContext(contexts).build();
    } else {
        // standard remove
        List<Node> toRemove = streamNodes().filter(n -> n.isMeta() && n.getMeta().getKey().equals(key)).filter(n -> n.getFullContexts().equals(contexts)).collect(Collectors.toList());
        toRemove.forEach(n -> this.type.run(() -> this.holder.unsetPermission(n), () -> this.holder.unsetTransientPermission(n)));
        node = NodeFactory.buildMetaNode(key, value).withExtraContext(contexts).build();
    }
    this.type.run(() -> this.holder.setPermission(node), () -> this.holder.setTransientPermission(node));
    return objectSave(this.holder).thenApply(v -> true);
}
Also used : DataMutateResult(me.lucko.luckperms.api.DataMutateResult) LuckPermsService(me.lucko.luckperms.sponge.service.LuckPermsService) Tristate(me.lucko.luckperms.api.Tristate) LPSubject(me.lucko.luckperms.sponge.service.model.LPSubject) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PermissionService(org.spongepowered.api.service.permission.PermissionService) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) LPSubjectReference(me.lucko.luckperms.sponge.service.model.LPSubjectReference) SubjectData(org.spongepowered.api.service.permission.SubjectData) ProxyFactory(me.lucko.luckperms.sponge.service.proxy.ProxyFactory) ImmutableMap(com.google.common.collect.ImmutableMap) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) Collection(java.util.Collection) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) LPSubjectData(me.lucko.luckperms.sponge.service.model.LPSubjectData) List(java.util.List) Stream(java.util.stream.Stream) ChatMetaType(me.lucko.luckperms.api.ChatMetaType) Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) NodeMapType(me.lucko.luckperms.common.model.NodeMapType) User(me.lucko.luckperms.common.model.User) MetaAccumulator(me.lucko.luckperms.common.caching.type.MetaAccumulator) MetaAccumulator(me.lucko.luckperms.common.caching.type.MetaAccumulator) Node(me.lucko.luckperms.api.Node) ChatMetaType(me.lucko.luckperms.api.ChatMetaType)

Aggregations

ChatMetaType (me.lucko.luckperms.api.ChatMetaType)9 Map (java.util.Map)6 Node (me.lucko.luckperms.api.Node)5 Group (me.lucko.luckperms.common.model.Group)5 NodeFactory (me.lucko.luckperms.common.node.NodeFactory)5 List (java.util.List)4 HashSet (java.util.HashSet)3 Objects (java.util.Objects)3 Set (java.util.Set)3 UUID (java.util.UUID)3 Collectors (java.util.stream.Collectors)3 ImmutableContextSet (me.lucko.luckperms.api.context.ImmutableContextSet)3 User (me.lucko.luckperms.common.model.User)3 Maps (com.google.common.collect.Maps)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Function (java.util.function.Function)2 LogEntry (me.lucko.luckperms.api.LogEntry)2 MetaAccumulator (me.lucko.luckperms.common.caching.type.MetaAccumulator)2 CommandManager (me.lucko.luckperms.common.command.CommandManager)2