Java Code Examples for net.luckperms.api.model.data.DataMutateResult#SUCCESS

The following examples show how to use net.luckperms.api.model.data.DataMutateResult#SUCCESS . 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: PermissionHolder.java    From LuckPerms with MIT License 6 votes vote down vote up
public DataMutateResult setNode(DataType dataType, Node node, boolean callEvent) {
    if (hasNode(dataType, node, NodeEqualityPredicate.IGNORE_EXPIRY_TIME) != Tristate.UNDEFINED) {
        return DataMutateResult.FAIL_ALREADY_HAS;
    }

    NodeMap data = getData(dataType);

    ImmutableCollection<? extends Node> before = data.immutable().values();

    data.add(node);
    invalidateCache();

    ImmutableCollection<? extends Node> after = data.immutable().values();
    if (callEvent) {
        this.plugin.getEventDispatcher().dispatchNodeAdd(node, this, dataType, before, after);
    }

    return DataMutateResult.SUCCESS;
}
 
Example 2
Source File: ApiUser.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull DataMutateResult setPrimaryGroup(@NonNull String group) {
    Objects.requireNonNull(group, "group");
    if (getPrimaryGroup().equalsIgnoreCase(group)) {
        return DataMutateResult.FAIL_ALREADY_HAS;
    }

    if (!this.handle.hasNode(DataType.NORMAL, Inheritance.builder(group.toLowerCase()).build(), NodeEqualityPredicate.IGNORE_EXPIRY_TIME_AND_VALUE).asBoolean()) {
        return DataMutateResult.FAIL;
    }

    this.handle.getPrimaryGroup().setStoredValue(group.toLowerCase());
    return DataMutateResult.SUCCESS;
}
 
Example 3
Source File: Track.java    From LuckPerms with MIT License 5 votes vote down vote up
/**
 * Appends a group to the end of this track
 *
 * @param group the group to append
 * @return the result of the operation
 */
public DataMutateResult appendGroup(Group group) {
    if (containsGroup(group)) {
        return DataMutateResult.FAIL_ALREADY_HAS;
    }

    List<String> before = ImmutableList.copyOf(this.groups);
    this.groups.add(group.getName());
    List<String> after = ImmutableList.copyOf(this.groups);

    this.plugin.getEventDispatcher().dispatchTrackAddGroup(this, group.getName(), before, after);
    return DataMutateResult.SUCCESS;
}
 
Example 4
Source File: Track.java    From LuckPerms with MIT License 5 votes vote down vote up
/**
 * Inserts a group at a certain position on this track
 *
 * @param group    the group to be inserted
 * @param position the index position (a value of 0 inserts at the start)
 * @throws IndexOutOfBoundsException if the position is less than 0 or greater than the size of the track
 * @return the result of the operation
 */
public DataMutateResult insertGroup(Group group, int position) throws IndexOutOfBoundsException {
    if (containsGroup(group)) {
        return DataMutateResult.FAIL_ALREADY_HAS;
    }

    List<String> before = ImmutableList.copyOf(this.groups);
    this.groups.add(position, group.getName());
    List<String> after = ImmutableList.copyOf(this.groups);

    this.plugin.getEventDispatcher().dispatchTrackAddGroup(this, group.getName(), before, after);
    return DataMutateResult.SUCCESS;
}
 
Example 5
Source File: Track.java    From LuckPerms with MIT License 5 votes vote down vote up
/**
 * Removes a group from this track
 *
 * @param group the group to remove
 * @return the result of the operation
 */
public DataMutateResult removeGroup(String group) {
    if (!containsGroup(group)) {
        return DataMutateResult.FAIL_LACKS;
    }

    List<String> before = ImmutableList.copyOf(this.groups);
    this.groups.remove(group);
    List<String> after = ImmutableList.copyOf(this.groups);

    this.plugin.getEventDispatcher().dispatchTrackRemoveGroup(this, group, before, after);
    return DataMutateResult.SUCCESS;
}
 
Example 6
Source File: PermissionHolder.java    From LuckPerms with MIT License 5 votes vote down vote up
public DataMutateResult unsetNode(DataType dataType, Node node) {
    if (hasNode(dataType, node, NodeEqualityPredicate.IGNORE_EXPIRY_TIME_AND_VALUE) == Tristate.UNDEFINED) {
        return DataMutateResult.FAIL_LACKS;
    }

    ImmutableCollection<? extends Node> before = getData(dataType).immutable().values();

    getData(dataType).remove(node);
    invalidateCache();

    ImmutableCollection<? extends Node> after = getData(dataType).immutable().values();
    this.plugin.getEventDispatcher().dispatchNodeRemove(node, this, dataType, before, after);

    return DataMutateResult.SUCCESS;
}
 
Example 7
Source File: PermissionHolder.java    From LuckPerms with MIT License 5 votes vote down vote up
public DataMutateResult.WithMergedNode unsetNode(DataType dataType, Node node, @Nullable Duration duration) {
    if (node.getExpiry() != null && duration != null) {
        Node otherMatch = getData(dataType).immutable().values().stream()
                .filter(NodeEqualityPredicate.IGNORE_EXPIRY_TIME_AND_VALUE.equalTo(node))
                .findFirst().orElse(null);

        if (otherMatch != null && otherMatch.getExpiry() != null) {
            NodeMap data = getData(dataType);

            Instant newExpiry = otherMatch.getExpiry().minus(duration);

            if (newExpiry.isAfter(Instant.now())) {
                Node newNode = node.toBuilder().expiry(newExpiry).build();

                // Remove the old Node & add the new one.
                ImmutableCollection<? extends Node> before = data.immutable().values();

                data.replace(newNode, otherMatch);
                invalidateCache();

                ImmutableCollection<? extends Node> after = data.immutable().values();
                this.plugin.getEventDispatcher().dispatchNodeRemove(otherMatch, this, dataType, before, after);
                this.plugin.getEventDispatcher().dispatchNodeAdd(newNode, this, dataType, before, after);

                return new MergedNodeResult(DataMutateResult.SUCCESS, newNode);
            }
        }
    }

    // Fallback to the normal handling.
    return new MergedNodeResult(unsetNode(dataType, node), null);
}
 
Example 8
Source File: PermissionHolder.java    From LuckPerms with MIT License 4 votes vote down vote up
public DataMutateResult.WithMergedNode setNode(DataType dataType, Node node, TemporaryNodeMergeStrategy mergeStrategy) {
    if (node.getExpiry() != null && mergeStrategy != TemporaryNodeMergeStrategy.NONE) {
        Node otherMatch = getData(dataType).immutable().values().stream()
                .filter(NodeEqualityPredicate.IGNORE_EXPIRY_TIME_AND_VALUE.equalTo(node))
                .findFirst().orElse(null);

        if (otherMatch != null && otherMatch.getExpiry() != null) {
            NodeMap data = getData(dataType);

            Node newNode = null;
            switch (mergeStrategy) {
                case ADD_NEW_DURATION_TO_EXISTING: {
                    // Create a new Node with the same properties, but add the expiry dates together
                    Instant newExpiry = otherMatch.getExpiry().plus(Duration.between(Instant.now(), node.getExpiry()));
                    newNode = node.toBuilder().expiry(newExpiry).build();
                    break;
                }
                case REPLACE_EXISTING_IF_DURATION_LONGER: {
                    // Only replace if the new expiry time is greater than the old one.
                    if (node.getExpiry().compareTo(otherMatch.getExpiry()) > 0) {
                        newNode = node;
                    }
                    break;
                }
            }

            if (newNode != null) {
                // Remove the old Node & add the new one.
                ImmutableCollection<? extends Node> before = data.immutable().values();

                data.replace(newNode, otherMatch);
                invalidateCache();

                ImmutableCollection<? extends Node> after = data.immutable().values();
                this.plugin.getEventDispatcher().dispatchNodeAdd(newNode, this, dataType, before, after);

                return new MergedNodeResult(DataMutateResult.SUCCESS, newNode);
            }
        }
    }

    // Fallback to the normal handling.
    return new MergedNodeResult(setNode(dataType, node, true), node);
}