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

The following examples show how to use net.luckperms.api.model.data.DataMutateResult#WithMergedNode . 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: ApiPermissionHolder.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public DataMutateResult.@NonNull WithMergedNode add(@NonNull Node node, @NonNull TemporaryNodeMergeStrategy temporaryNodeMergeStrategy) {
    Objects.requireNonNull(node, "node");
    Objects.requireNonNull(temporaryNodeMergeStrategy, "temporaryNodeMergeStrategy");
    DataMutateResult.WithMergedNode result = ApiPermissionHolder.this.handle.setNode(this.dataType, node, temporaryNodeMergeStrategy);
    if (result.getResult().wasSuccessful()) {
        onNodeChange();
    }
    return result;
}
 
Example 2
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 3
Source File: PermissionSetTemp.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder target, ArgumentList args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    String node = args.get(0);
    boolean value = args.getBooleanOrInsert(1, true);
    Duration duration = args.getDuration(2);
    TemporaryNodeMergeStrategy modifier = args.getTemporaryModifierAndRemove(3).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
    MutableContextSet context = args.getContextOrDefault(3, plugin);

    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) ||
            ArgumentPermissions.checkGroup(plugin, sender, target, context) ||
            ArgumentPermissions.checkArguments(plugin, sender, permission, node)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    Node builtNode = NodeBuilders.determineMostApplicable(node).value(value).withContext(context).expiry(duration).build();

    if (builtNode instanceof InheritanceNode) {
        if (ArgumentPermissions.checkGroup(plugin, sender, ((InheritanceNode) builtNode).getGroupName(), context)) {
            Message.COMMAND_NO_PERMISSION.send(sender);
            return CommandResult.NO_PERMISSION;
        }
    }

    DataMutateResult.WithMergedNode result = target.setNode(DataType.NORMAL, builtNode, modifier);

    if (result.getResult().wasSuccessful()) {
        duration = result.getMergedNode().getExpiryDuration();
        Message.SETPERMISSION_TEMP_SUCCESS.send(sender, node, value, target.getFormattedDisplayName(), DurationFormatter.LONG.format(duration), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

        LoggedAction.build().source(sender).target(target)
                .description("permission", "settemp", node, value, duration, context)
                .build().submit(plugin, sender);

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.ALREADY_HAS_TEMP_PERMISSION.send(sender, target.getFormattedDisplayName(), node, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example 4
Source File: PermissionUnsetTemp.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder target, ArgumentList args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    String node = args.get(0);
    Duration duration = args.getDurationOrDefault(1, null);
    int fromIndex = duration == null ? 1 : 2;
    MutableContextSet context = args.getContextOrDefault(fromIndex, plugin);

    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) ||
            ArgumentPermissions.checkGroup(plugin, sender, target, context) ||
            ArgumentPermissions.checkArguments(plugin, sender, permission, node)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    Node builtNode = NodeBuilders.determineMostApplicable(node).expiry(10L).withContext(context).build();

    if (builtNode instanceof InheritanceNode) {
        if (ArgumentPermissions.checkGroup(plugin, sender, ((InheritanceNode) builtNode).getGroupName(), context)) {
            Message.COMMAND_NO_PERMISSION.send(sender);
            return CommandResult.NO_PERMISSION;
        }
    }

    DataMutateResult.WithMergedNode result = target.unsetNode(DataType.NORMAL, builtNode, duration);
    if (result.getResult().wasSuccessful()) {
        Node mergedNode = result.getMergedNode();
        //noinspection ConstantConditions
        if (mergedNode != null) {
            Message.UNSET_TEMP_PERMISSION_SUBTRACT_SUCCESS.send(sender,
                    mergedNode.getKey(),
                    mergedNode.getValue(),
                    target.getFormattedDisplayName(),
                    DurationFormatter.LONG.format(mergedNode.getExpiryDuration()),
                    MessageUtils.contextSetToString(plugin.getLocaleManager(), context),
                    DurationFormatter.LONG.format(duration)
            );

            LoggedAction.build().source(sender).target(target)
                    .description("permission", "unsettemp", node, duration, context)
                    .build().submit(plugin, sender);
        } else {
            Message.UNSET_TEMP_PERMISSION_SUCCESS.send(sender, node, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

            LoggedAction.build().source(sender).target(target)
                    .description("permission", "unsettemp", node, context)
                    .build().submit(plugin, sender);
        }

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.DOES_NOT_HAVE_TEMP_PERMISSION.send(sender, target.getFormattedDisplayName(), node, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example 5
Source File: ParentRemoveTemp.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder target, ArgumentList args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    String groupName = args.getLowercase(0, DataConstraints.GROUP_NAME_TEST_ALLOW_SPACE);
    Duration duration = args.getDurationOrDefault(1, null);
    int fromIndex = duration == null ? 1 : 2;
    MutableContextSet context = args.getContextOrDefault(fromIndex, plugin);

    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) ||
            ArgumentPermissions.checkGroup(plugin, sender, target, context) ||
            ArgumentPermissions.checkGroup(plugin, sender, groupName, context) ||
            ArgumentPermissions.checkArguments(plugin, sender, permission, groupName)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    DataMutateResult.WithMergedNode result = target.unsetNode(DataType.NORMAL, Inheritance.builder(groupName).expiry(10L).withContext(context).build(), duration);
    if (result.getResult().wasSuccessful()) {
        Node mergedNode = result.getMergedNode();
        //noinspection ConstantConditions
        if (mergedNode != null) {
            Message.UNSET_TEMP_INHERIT_SUBTRACT_SUCCESS.send(sender,
                    target.getFormattedDisplayName(),
                    groupName,
                    DurationFormatter.LONG.format(mergedNode.getExpiryDuration()),
                    MessageUtils.contextSetToString(plugin.getLocaleManager(), context),
                    DurationFormatter.LONG.format(duration)
            );

            LoggedAction.build().source(sender).target(target)
                    .description("parent", "removetemp", groupName, duration, context)
                    .build().submit(plugin, sender);
        } else {
            Message.UNSET_TEMP_INHERIT_SUCCESS.send(sender, target.getFormattedDisplayName(), groupName, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

            LoggedAction.build().source(sender).target(target)
                    .description("parent", "removetemp", groupName, context)
                    .build().submit(plugin, sender);
        }

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.DOES_NOT_TEMP_INHERIT.send(sender, target.getFormattedDisplayName(), groupName, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example 6
Source File: ParentAddTemp.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder target, ArgumentList args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    String groupName = args.getLowercase(0, DataConstraints.GROUP_NAME_TEST);
    Duration duration = args.getDuration(1);
    TemporaryNodeMergeStrategy modifier = args.getTemporaryModifierAndRemove(2).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
    MutableContextSet context = args.getContextOrDefault(2, plugin);

    Group group = StorageAssistant.loadGroup(groupName, sender, plugin, false);
    if (group == null) {
        return CommandResult.INVALID_ARGS;
    }

    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) ||
            ArgumentPermissions.checkGroup(plugin, sender, target, context) ||
            ArgumentPermissions.checkGroup(plugin, sender, group, context) ||
            ArgumentPermissions.checkArguments(plugin, sender, permission, group.getName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    if (group.getName().equalsIgnoreCase(target.getObjectName())) {
        Message.ALREADY_TEMP_INHERITS.send(sender, target.getFormattedDisplayName(), group.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }

    DataMutateResult.WithMergedNode result = target.setNode(DataType.NORMAL, Inheritance.builder(group.getName()).expiry(duration).withContext(context).build(), modifier);

    if (result.getResult().wasSuccessful()) {
        duration = result.getMergedNode().getExpiryDuration();
        Message.SET_TEMP_INHERIT_SUCCESS.send(sender, target.getFormattedDisplayName(), group.getFormattedDisplayName(), DurationFormatter.LONG.format(duration), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

        LoggedAction.build().source(sender).target(target)
                .description("parent", "addtemp", group.getName(), duration, context)
                .build().submit(plugin, sender);

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.ALREADY_TEMP_INHERITS.send(sender, target.getFormattedDisplayName(), group.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example 7
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);
}