net.luckperms.api.context.ImmutableContextSet Java Examples

The following examples show how to use net.luckperms.api.context.ImmutableContextSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParentAdd.java    From LuckPerms with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, ArgumentList args, String label) {
    String collection = args.get(0);
    String name = args.get(1);
    ImmutableContextSet contextSet = args.getContextOrEmpty(2);

    LPPermissionService service = Sponge.getServiceManager().provideUnchecked(LPPermissionService.class);
    if (service.getLoadedCollections().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(collection))) {
        Message.BLANK.send(sender, "Warning: SubjectCollection '&4" + collection + "&c' doesn't already exist.");
    }

    LPSubjectCollection c = service.getCollection(collection);
    if (!c.hasRegistered(name).join()) {
        Message.BLANK.send(sender, "Warning: Subject '&4" + name + "&c' doesn't already exist.");
    }

    LPSubject subject = c.loadSubject(name).join();

    if (subjectData.addParent(contextSet, subject.toReference()).join()) {
        Message.BLANK.send(sender, "&aAdded parent &b" + subject.getParentCollection().getIdentifier() +
                    "&a/&b" + subject.getIdentifier() + "&a in context " + SpongeCommandUtils.contextToString(contextSet, plugin.getLocaleManager()));
    } else {
        Message.BLANK.send(sender, "Unable to add parent. Does the Subject already have it added?");
    }
    return CommandResult.SUCCESS;
}
 
Example #2
Source File: ParentRemove.java    From LuckPerms with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, ArgumentList args, String label) {
    String collection = args.get(0);
    String name = args.get(1);
    ImmutableContextSet contextSet = args.getContextOrEmpty(2);

    LPPermissionService service = Sponge.getServiceManager().provideUnchecked(LPPermissionService.class);
    if (service.getLoadedCollections().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(collection))) {
        Message.BLANK.send(sender, "Warning: SubjectCollection '&4" + collection + "&c' doesn't exist.");
    }

    LPSubjectCollection c = service.getCollection(collection);
    if (!c.hasRegistered(name).join()) {
        Message.BLANK.send(sender, "Warning: Subject '&4" + name + "&c' doesn't exist.");
    }

    LPSubject subject = c.loadSubject(name).join();

    if (subjectData.removeParent(contextSet, subject.toReference()).join()) {
        Message.BLANK.send(sender, "&aRemoved parent &b" + subject.getParentCollection().getIdentifier() +
                    "&a/&b" + subject.getIdentifier() + "&a in context " + SpongeCommandUtils.contextToString(contextSet, plugin.getLocaleManager()));
    } else {
        Message.BLANK.send(sender, "Unable to remove parent. Are you sure the Subject has it added?");
    }
    return CommandResult.SUCCESS;
}
 
Example #3
Source File: NodeMap.java    From LuckPerms with MIT License 6 votes vote down vote up
public void forEach(QueryOptions filter, Consumer<? super Node> consumer) {
    for (Map.Entry<ImmutableContextSet, SortedSet<Node>> e : this.map.entrySet()) {
        if (!filter.satisfies(e.getKey(), defaultSatisfyMode())) {
            continue;
        }

        if (normalNodesExcludeTest(filter, e.getKey())) {
            if (inheritanceNodesIncludeTest(filter, e.getKey())) {
                // only copy inheritance nodes.
                SortedSet<InheritanceNode> inheritanceNodes = this.inheritanceMap.get(e.getKey());
                if (inheritanceNodes != null) {
                    inheritanceNodes.forEach(consumer);
                }
            }
        } else {
            e.getValue().forEach(consumer);
        }
    }
}
 
Example #4
Source File: NodeMap.java    From LuckPerms with MIT License 6 votes vote down vote up
void add(Node node) {
    ImmutableContextSet context = node.getContexts();
    Node n = localise(node);

    SortedSet<Node> nodesInContext = this.map.computeIfAbsent(context, VALUE_SET_SUPPLIER);
    nodesInContext.removeIf(e -> e.equals(node, NodeEqualityPredicate.IGNORE_EXPIRY_TIME_AND_VALUE));
    nodesInContext.add(n);

    if (n instanceof InheritanceNode) {
        SortedSet<InheritanceNode> inheritanceNodesInContext = this.inheritanceMap.computeIfAbsent(context, INHERITANCE_VALUE_SET_SUPPLIER);
        inheritanceNodesInContext.removeIf(e -> e.equals(node, NodeEqualityPredicate.IGNORE_EXPIRY_TIME_AND_VALUE));
        if (n.getValue()) {
            inheritanceNodesInContext.add((InheritanceNode) n);
        }
    }
}
 
Example #5
Source File: NodeMap.java    From LuckPerms with MIT License 6 votes vote down vote up
boolean removeIf(ContextSet contextSet, Predicate<? super Node> predicate) {
    ImmutableContextSet context = contextSet.immutableCopy();

    boolean success = false;

    SortedSet<Node> nodesInContext = this.map.get(context);
    if (nodesInContext != null) {
        success = nodesInContext.removeIf(predicate);
    }

    SortedSet<InheritanceNode> inheritanceNodesInContext = this.inheritanceMap.get(context);
    if (inheritanceNodesInContext != null) {
        inheritanceNodesInContext.removeIf(predicate);
    }

    return success;
}
 
Example #6
Source File: CompatibilityUtil.java    From LuckPerms with MIT License 6 votes vote down vote up
public static ImmutableContextSet convertContexts(Set<Context> contexts) {
    Objects.requireNonNull(contexts, "contexts");

    if (contexts instanceof ForwardingContextSet) {
        return ((ForwardingContextSet) contexts).delegate().immutableCopy();
    }

    if (contexts.isEmpty()) {
        return ImmutableContextSetImpl.EMPTY;
    }

    ImmutableContextSet.Builder builder = new ImmutableContextSetImpl.BuilderImpl();
    for (Map.Entry<String, String> entry : contexts) {
        builder.add(new ContextImpl(entry.getKey(), entry.getValue()));
    }
    return builder.build();
}
 
Example #7
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 6 votes vote down vote up
@Override
public List<String> getOptionValueList(GDPermissionHolder holder, Option option, Set<Context> contexts) {
    // If no server context exists, add global
    this.checkServerContext(contexts);
    ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
    final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
    if (permissionHolder == null) {
        return null;
    }

    final QueryOptions query = QueryOptions.builder(QueryMode.CONTEXTUAL).option(DataQueryOrderFunction.KEY, DEFAULT_DATA_QUERY_ORDER).context(set).build();
    CachedMetaData metaData = permissionHolder.getCachedData().getMetaData(query);
    List<String> list = metaData.getMeta().get(option.getPermission());
    if (list == null) {
        return new ArrayList<>();
    }
    return list;
}
 
Example #8
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 6 votes vote down vote up
public Map<String, String> getOptions(GDPermissionHolder holder, Set<Context> contexts) {
    ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
    final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
    if (permissionHolder == null) {
        return new HashMap<>();
    }

    final QueryOptions query = QueryOptions.builder(QueryMode.CONTEXTUAL).option(DataQueryOrderFunction.KEY, DEFAULT_DATA_QUERY_ORDER).context(set).build();
    CachedMetaData cachedData = permissionHolder.getCachedData().getMetaData(query);
    // TODO
    Map<String, String> metaMap = new HashMap<>();
    for (Map.Entry<String, List<String>> mapEntry : cachedData.getMeta().entrySet()) {
        metaMap.put(mapEntry.getKey(), mapEntry.getValue().get(0));
    }
    return metaMap;
}
 
Example #9
Source File: PermissionHolderSubjectData.java    From LuckPerms with MIT License 6 votes vote down vote up
@Override
public CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, LPSubjectReference subject) {
    Objects.requireNonNull(contexts, "contexts");
    Objects.requireNonNull(subject, "subject");

    if (!subject.getCollectionIdentifier().equals(PermissionService.SUBJECTS_GROUP)) {
        return CompletableFuture.completedFuture(false);
    }

    Node node = Inheritance.builder(subject.getSubjectIdentifier())
            .withContext(contexts)
            .build();

    if (!this.holder.setNode(this.type, node, true).wasSuccessful()) {
        return CompletableFuture.completedFuture(false);
    }

    return save(this.holder).thenApply(v -> true);
}
 
Example #10
Source File: PermissionHolderSubjectData.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts) {
    ImmutableList.Builder<LPSubjectReference> builder = ImmutableList.builder();
    for (InheritanceNode n : this.holder.getData(this.type).immutableInheritance().get(contexts)) {
        builder.add(this.service.getGroupSubjects().loadSubject(n.getGroupName()).join().toReference());
    }
    return builder.build();
}
 
Example #11
Source File: OptionSet.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, ArgumentList args, String label) {
    String key = args.get(0);
    String value = args.get(1);
    ImmutableContextSet contextSet = args.getContextOrEmpty(2);

    if (subjectData.setOption(contextSet, key, value).join()) {
        Message.BLANK.send(sender, "&aSet &f\"" + key + "&f\"&a to &f\"" + value + "&f\"&a in context " + SpongeCommandUtils.contextToString(contextSet, plugin.getLocaleManager()));
    } else {
        Message.BLANK.send(sender, "Unable to set option. Does the Subject already have it set?");
    }

    return CommandResult.SUCCESS;
}
 
Example #12
Source File: NukkitContextManager.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public QueryOptions formQueryOptions(Player subject, ImmutableContextSet contextSet) {
    QueryOptions.Builder queryOptions = this.plugin.getConfiguration().get(ConfigKeys.GLOBAL_QUERY_OPTIONS).toBuilder();
    if (subject.isOp()) {
        queryOptions.option(OP_OPTION, true);
    }

    return queryOptions.context(contextSet).build();
}
 
Example #13
Source File: OptionUnset.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, ArgumentList args, String label) {
    String key = args.get(0);
    ImmutableContextSet contextSet = args.getContextOrEmpty(1);

    if (subjectData.unsetOption(contextSet, key).join()) {
        Message.BLANK.send(sender, "&aUnset &f\"" + key + "&f\"&a in context " + SpongeCommandUtils.contextToString(contextSet, plugin.getLocaleManager()));
    } else {
        Message.BLANK.send(sender, "Unable to unset option. Are you sure the Subject has it set?");
    }
    return CommandResult.SUCCESS;
}
 
Example #14
Source File: AbstractConfigurateStorage.java    From LuckPerms with MIT License 5 votes vote down vote up
private static Node readMetaAttributes(ConfigurationNode attributes, Function<ConfigurationNode, NodeBuilder<?, ?>> permissionFunction) {
    long expiryVal = attributes.getNode("expiry").getLong(0L);
    Instant expiry = expiryVal == 0L ? null : Instant.ofEpochSecond(expiryVal);
    ImmutableContextSet context = readContexts(attributes);

    return permissionFunction.apply(attributes)
            .expiry(expiry)
            .context(context)
            .build();
}
 
Example #15
Source File: CalculatedSubjectData.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public CompletableFuture<Boolean> clearOptions(ImmutableContextSet contexts) {
    Map<String, String> map = this.options.get(contexts);
    if (map == null) {
        return CompletableFuture.completedFuture(false);
    }

    this.options.remove(contexts);
    this.service.invalidateAllCaches();
    return CompletableFuture.completedFuture(!map.isEmpty());
}
 
Example #16
Source File: BackendServerCalculator.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public ContextSet estimatePotentialContexts() {
    Collection<RegisteredServer> servers = this.plugin.getBootstrap().getProxy().getAllServers();
    ImmutableContextSet.Builder builder = new ImmutableContextSetImpl.BuilderImpl();
    for (RegisteredServer server : servers) {
        builder.add(DefaultContextKeys.WORLD_KEY, server.getServerInfo().getName().toLowerCase());
    }
    return builder.build();
}
 
Example #17
Source File: LuckPermsVaultChat.java    From LuckPerms with MIT License 5 votes vote down vote up
private QueryOptions createQueryOptionsForWorldSet(String world) {
    ImmutableContextSet.Builder context = new ImmutableContextSetImpl.BuilderImpl();
    if (world != null && !world.equals("") && !world.equalsIgnoreCase("global")) {
        context.add(DefaultContextKeys.WORLD_KEY, world.toLowerCase());
    }
    context.add(DefaultContextKeys.SERVER_KEY, this.vaultPermission.getVaultServer());

    QueryOptions.Builder builder = QueryOptionsImpl.DEFAULT_CONTEXTUAL.toBuilder();
    builder.context(context.build());
    builder.flag(Flag.INCLUDE_NODES_WITHOUT_SERVER_CONTEXT, this.vaultPermission.isIncludeGlobal());
    return builder.build();
}
 
Example #18
Source File: PermissionHolderSubjectData.java    From LuckPerms with MIT License 5 votes vote down vote up
@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(Prefix.NODE_KEY) || key.equalsIgnoreCase(Suffix.NODE_KEY)) {
        // special handling.
        ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase());

        // remove all prefixes/suffixes from the user
        this.holder.removeIf(this.type, contexts, type.nodeType()::matches, false);

        MetaAccumulator metaAccumulator = this.holder.accumulateMeta(QueryOptionsImpl.DEFAULT_CONTEXTUAL.toBuilder().context(contexts).build());
        int priority = metaAccumulator.getChatMeta(type).keySet().stream().mapToInt(e -> e).max().orElse(0);
        priority += 10;

        node = type.builder(value, priority).withContext(contexts).build();
    } else {
        // standard remove
        this.holder.removeIf(this.type, contexts, NodeType.META.predicate(n -> n.getMetaKey().equals(key)), false);
        node = Meta.builder(key, value).withContext(contexts).build();
    }

    this.holder.setNode(this.type, node, true);
    return save(this.holder).thenApply(v -> true);
}
 
Example #19
Source File: SqlNode.java    From LuckPerms with MIT License 5 votes vote down vote up
private SqlNode(String permission, boolean value, String server, String world, long expiry, ImmutableContextSet contexts, long sqlId) {
    this.permission = Objects.requireNonNull(permission, "permission");
    this.value = value;
    this.server = Objects.requireNonNull(server, "server");
    this.world = Objects.requireNonNull(world, "world");
    this.expiry = expiry;
    this.contexts = Objects.requireNonNull(contexts, "contexts");
    this.sqlId = sqlId;
}
 
Example #20
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public void clearPermissions(GDPermissionHolder holder, Set<Context> contexts) {
    final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
    if (permissionHolder == null) {
        return;
    }

    ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
    permissionHolder.data().clear(set);
    this.savePermissionHolder(permissionHolder);
}
 
Example #21
Source File: WorldCalculator.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public ContextSet estimatePotentialContexts() {
    Game game = this.plugin.getBootstrap().getGame();
    if (!game.isServerAvailable()) {
        return ImmutableContextSetImpl.EMPTY;
    }

    Collection<World> worlds = game.getServer().getWorlds();
    ImmutableContextSet.Builder builder = new ImmutableContextSetImpl.BuilderImpl();
    for (World world : worlds) {
        builder.add(DefaultContextKeys.WORLD_KEY, world.getName().toLowerCase());
    }
    return builder.build();
}
 
Example #22
Source File: SpongeGroupManager.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public ImmutableMap<LPSubject, Boolean> getLoadedWithPermission(ImmutableContextSet contexts, String permission) {
    return getAll().values().stream()
            .map(SpongeGroup::sponge)
            .map(sub -> Maps.immutableEntry(sub, sub.getPermissionValue(contexts, permission)))
            .filter(pair -> pair.getValue() != Tristate.UNDEFINED)
            .collect(ImmutableCollectors.toMap(Map.Entry::getKey, sub -> sub.getValue().asBoolean()));
}
 
Example #23
Source File: SpongeUserManager.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public ImmutableMap<LPSubject, Boolean> getLoadedWithPermission(ImmutableContextSet contexts, String permission) {
    return getAll().values().stream()
            .map(SpongeUser::sponge)
            .map(sub -> Maps.immutableEntry(sub, sub.getPermissionValue(contexts, permission)))
            .filter(pair -> pair.getValue() != Tristate.UNDEFINED)
            .collect(ImmutableCollectors.toMap(Map.Entry::getKey, sub -> sub.getValue().asBoolean()));
}
 
Example #24
Source File: PersistedCollection.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public ImmutableMap<LPSubject, Boolean> getLoadedWithPermission(ImmutableContextSet contexts, String permission) {
    ImmutableMap.Builder<LPSubject, Boolean> m = ImmutableMap.builder();
    for (LPSubject subject : this.subjects.values()) {
        Tristate ts = subject.getPermissionValue(contexts, permission);
        if (ts != Tristate.UNDEFINED) {
            m.put(subject, ts.asBoolean());
        }

    }
    return m.build();
}
 
Example #25
Source File: PermissionHolderSubject.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public Optional<String> getOption(ImmutableContextSet contexts, String s) {
    MetaCache data = this.parent.getCachedData().getMetaData(this.plugin.getContextManager().formQueryOptions(contexts));
    if (s.equalsIgnoreCase(Prefix.NODE_KEY)) {
        String prefix = data.getPrefix(MetaCheckEvent.Origin.PLATFORM_API);
        if (prefix != null) {
            return Optional.of(prefix);
        }
    }

    if (s.equalsIgnoreCase(Suffix.NODE_KEY)) {
        String suffix = data.getSuffix(MetaCheckEvent.Origin.PLATFORM_API);
        if (suffix != null) {
            return Optional.of(suffix);
        }
    }

    String val = data.getMetaValue(s, MetaCheckEvent.Origin.PLATFORM_API);
    if (val != null) {
        return Optional.of(val);
    }

    Optional<String> v = getParentCollection().getDefaults().getOption(contexts, s);
    if (v.isPresent()) {
        return v;
    }

    return this.plugin.getService().getRootDefaults().getOption(contexts, s);
}
 
Example #26
Source File: CalculatedSubjectData.java    From LuckPerms with MIT License 5 votes vote down vote up
public void replacePermissions(Map<ImmutableContextSet, Map<String, Boolean>> map) {
    this.permissions.clear();
    for (Map.Entry<ImmutableContextSet, Map<String, Boolean>> e : map.entrySet()) {
        this.permissions.put(e.getKey(), new ConcurrentHashMap<>(e.getValue()));
    }
    this.service.invalidateAllCaches();
}
 
Example #27
Source File: CalculatedSubjectData.java    From LuckPerms with MIT License 5 votes vote down vote up
public void replaceParents(Map<ImmutableContextSet, List<LPSubjectReference>> map) {
    this.parents.clear();
    for (Map.Entry<ImmutableContextSet, List<LPSubjectReference>> e : map.entrySet()) {
        Set<LPSubjectReference> set = ConcurrentHashMap.newKeySet();
        set.addAll(e.getValue());
        this.parents.put(e.getKey(), set);
    }
    this.service.invalidateAllCaches();
}
 
Example #28
Source File: NodeMap.java    From LuckPerms with MIT License 5 votes vote down vote up
private void removeExact(Node node) {
    ImmutableContextSet context = node.getContexts();
    SortedSet<Node> nodesInContext = this.map.get(context);
    if (nodesInContext != null) {
        nodesInContext.remove(node);
    }

    if (node instanceof InheritanceNode && node.getValue()) {
        SortedSet<InheritanceNode> inheritanceNodesInContext = this.inheritanceMap.get(context);
        if (inheritanceNodesInContext != null) {
            inheritanceNodesInContext.remove(node);
        }
    }
}
 
Example #29
Source File: ImmutableContextSetImpl.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull ImmutableContextSet build() {
    if (this.builder == null) {
        return EMPTY;
    } else {
        return new ImmutableContextSetImpl(this.builder.build());
    }
}
 
Example #30
Source File: CalculatedSubjectData.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public ImmutableMap<ImmutableContextSet, ImmutableList<LPSubjectReference>> getAllParents() {
    ImmutableMap.Builder<ImmutableContextSet, ImmutableList<LPSubjectReference>> map = ImmutableMap.builder();
    for (Map.Entry<ImmutableContextSet, Set<LPSubjectReference>> e : this.parents.entrySet()) {
        map.put(e.getKey(), ImmutableList.copyOf(e.getValue()));
    }
    return map.build();
}