Java Code Examples for net.luckperms.api.model.data.DataMutateResult#wasSuccessful()

The following examples show how to use net.luckperms.api.model.data.DataMutateResult#wasSuccessful() . 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: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public PermissionResult setOptionValue(GDPermissionHolder holder, String key, String value, Set<Context> contexts, boolean check) {
    DataMutateResult result = null;
    if (check) {
        // 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) {
        new GDPermissionResult(ResultTypes.FAILURE);
    }

    final Option option = OptionRegistryModule.getInstance().getById(key).orElse(null);
    if (option == null) {
        new GDPermissionResult(ResultTypes.FAILURE);
    }

    final Node node = MetaNode.builder().key(key).value(value).context(set).build();
    if (!value.equalsIgnoreCase("undefined")) {
        if (!option.multiValued()) {
            this.clearMeta(permissionHolder, key, set);
        }
        result = permissionHolder.data().add(node);
    } else {
        this.clearMeta(permissionHolder, key, set);
        this.savePermissionHolder(permissionHolder);
        return new GDPermissionResult(ResultTypes.SUCCESS);
    }
    if (result != null) {
        if (result.wasSuccessful()) {
            this.savePermissionHolder(permissionHolder);
            return new GDPermissionResult(ResultTypes.SUCCESS, TextComponent.builder().append(result.name()).build());
        }
        return new GDPermissionResult(ResultTypes.FAILURE, TextComponent.builder().append(result.name()).build());
    }

    return new GDPermissionResult(ResultTypes.FAILURE);
}
 
Example 2
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save) {
    DataMutateResult result = null;
    if (check) {
        // If no server context exists, add global
        this.checkServerContext(contexts);
    }
    ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
    final Node node = this.luckPermsApi.getNodeBuilderRegistry().forPermission().permission(permission).value(value.asBoolean()).context(set).build();
    final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
    if (permissionHolder == null) {
        return new GDPermissionResult(ResultTypes.FAILURE);
    }

    if (value == Tristate.UNDEFINED) {
        result = permissionHolder.data().remove(node);
    } else {
        result = permissionHolder.data().add(node);
    }

    if (result.wasSuccessful()) {
        if (permissionHolder instanceof Group) {
            // If a group is changed, we invalidate all cache
            PermissionHolderCache.getInstance().invalidateAllPermissionCache();
        } else {
            // We need to invalidate cache outside of LP listener so we can guarantee proper result returns
            PermissionHolderCache.getInstance().getOrCreatePermissionCache(holder).invalidateAll();
        }

        if (save) {
            this.savePermissionHolder(permissionHolder);
        }

        return new GDPermissionResult(ResultTypes.SUCCESS, TextComponent.builder().append(result.name()).build());
    }

    return new GDPermissionResult(ResultTypes.FAILURE, TextComponent.builder().append(result.name()).build());
}
 
Example 3
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public PermissionResult setOptionValue(GDPermissionHolder holder, String key, String value, Set<Context> contexts, boolean check) {
    DataMutateResult result = null;
    if (check) {
        // 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) {
        new GDPermissionResult(ResultTypes.FAILURE);
    }

    final Option option = OptionRegistryModule.getInstance().getById(key).orElse(null);
    if (option == null) {
        new GDPermissionResult(ResultTypes.FAILURE);
    }

    final Node node = MetaNode.builder().key(key).value(value).context(set).build();
    if (!value.equalsIgnoreCase("undefined")) {
        if (!option.multiValued()) {
            this.clearMeta(permissionHolder, key, set);
        }
        result = permissionHolder.data().add(node);
    } else {
        this.clearMeta(permissionHolder, key, set);
        this.savePermissionHolder(permissionHolder);
        return new GDPermissionResult(ResultTypes.SUCCESS);
    }
    if (result != null) {
        if (result.wasSuccessful()) {
            this.savePermissionHolder(permissionHolder);
            return new GDPermissionResult(ResultTypes.SUCCESS, TextComponent.builder().append(result.name()).build());
        }
        return new GDPermissionResult(ResultTypes.FAILURE, TextComponent.builder().append(result.name()).build());
    }

    return new GDPermissionResult(ResultTypes.FAILURE);
}
 
Example 4
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save) {
    DataMutateResult result = null;
    if (check) {
        // If no server context exists, add global
        this.checkServerContext(contexts);
    }
    ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
    final Node node = this.luckPermsApi.getNodeBuilderRegistry().forPermission().permission(permission).value(value.asBoolean()).context(set).build();
    final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
    if (permissionHolder == null) {
        return new GDPermissionResult(ResultTypes.FAILURE);
    }

    if (value == Tristate.UNDEFINED) {
        result = permissionHolder.data().remove(node);
    } else {
        result = permissionHolder.data().add(node);
    }

    if (result.wasSuccessful()) {
        if (permissionHolder instanceof Group) {
            // If a group is changed, we invalidate all cache
            PermissionHolderCache.getInstance().invalidateAllPermissionCache();
        } else {
            // We need to invalidate cache outside of LP listener so we can guarantee proper result returns
            PermissionHolderCache.getInstance().getOrCreatePermissionCache(holder).invalidateAll();
        }

        if (save) {
            this.savePermissionHolder(permissionHolder);
        }

        return new GDPermissionResult(ResultTypes.SUCCESS, TextComponent.builder().append(result.name()).build());
    }

    return new GDPermissionResult(ResultTypes.FAILURE, TextComponent.builder().append(result.name()).build());
}
 
Example 5
Source File: ParentAdd.java    From LuckPerms with MIT License 5 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);
    MutableContextSet context = args.getContextOrDefault(1, 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;
    }

    DataMutateResult result = target.setNode(DataType.NORMAL, Inheritance.builder(group.getName()).withContext(context).build(), true);

    if (result.wasSuccessful()) {
        Message.SET_INHERIT_SUCCESS.send(sender, target.getFormattedDisplayName(), group.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

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

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.ALREADY_INHERITS.send(sender, target.getFormattedDisplayName(), group.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example 6
Source File: TrackAppend.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track target, ArgumentList args, String label) {
    String groupName = args.get(0).toLowerCase();
    if (!DataConstraints.GROUP_NAME_TEST.test(groupName)) {
        sendDetailedUsage(sender, label);
        return CommandResult.INVALID_ARGS;
    }

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

    DataMutateResult result = target.appendGroup(group);

    if (result.wasSuccessful()) {
        Message.TRACK_APPEND_SUCCESS.send(sender, group.getName(), target.getName());
        if (target.getGroups().size() > 1) {
            Message.BLANK.send(sender, MessageUtils.listToArrowSep(target.getGroups(), group.getName()));
        }

        LoggedAction.build().source(sender).target(target)
                .description("append", group.getName())
                .build().submit(plugin, sender);

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.TRACK_ALREADY_CONTAINS.send(sender, target.getName(), group.getName());
        return CommandResult.STATE_ERROR;
    }
}
 
Example 7
Source File: TrackRemove.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track target, ArgumentList args, String label) {
    String groupName = args.get(0).toLowerCase();
    if (!DataConstraints.GROUP_NAME_TEST.test(groupName)) {
        sendDetailedUsage(sender, label);
        return CommandResult.INVALID_ARGS;
    }

    DataMutateResult result = target.removeGroup(groupName);

    if (result.wasSuccessful()) {
        Message.TRACK_REMOVE_SUCCESS.send(sender, groupName, target.getName());
        if (target.getGroups().size() > 1) {
            Message.BLANK.send(sender, MessageUtils.listToArrowSep(target.getGroups()));
        }

        LoggedAction.build().source(sender).target(target)
                .description("remove", groupName)
                .build().submit(plugin, sender);

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.TRACK_DOES_NOT_CONTAIN.send(sender, target.getName(), groupName);
        return CommandResult.STATE_ERROR;
    }
}
 
Example 8
Source File: ApiPermissionHolder.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull DataMutateResult add(@NonNull Node node) {
    Objects.requireNonNull(node, "node");
    DataMutateResult result = ApiPermissionHolder.this.handle.setNode(this.dataType, node, true);
    if (result.wasSuccessful()) {
        onNodeChange();
    }
    return result;
}
 
Example 9
Source File: ApiPermissionHolder.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull DataMutateResult remove(@NonNull Node node) {
    Objects.requireNonNull(node, "node");
    DataMutateResult result = ApiPermissionHolder.this.handle.unsetNode(this.dataType, node);
    if (result.wasSuccessful()) {
        onNodeChange();
    }
    return result;
}
 
Example 10
Source File: PermissionUnset.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);
    MutableContextSet context = args.getContextOrDefault(1, 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).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 result = target.unsetNode(DataType.NORMAL, builtNode);

    if (result.wasSuccessful()) {
        Message.UNSETPERMISSION_SUCCESS.send(sender, node, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

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

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.DOES_NOT_HAVE_PERMISSION.send(sender, target.getFormattedDisplayName(), node, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example 11
Source File: PermissionSet.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);
    MutableContextSet context = args.getContextOrDefault(2, 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).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 result = target.setNode(DataType.NORMAL, builtNode, true);

    if (result.wasSuccessful()) {
        Message.SETPERMISSION_SUCCESS.send(sender, node, value, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

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

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.ALREADY_HASPERMISSION.send(sender, target.getFormattedDisplayName(), node, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example 12
Source File: ParentRemove.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);
    MutableContextSet context = args.getContextOrDefault(1, 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;
    }

    if (target.getType() == HolderType.USER) {
        User user = (User) target;

        boolean shouldPrevent = plugin.getConfiguration().get(ConfigKeys.PREVENT_PRIMARY_GROUP_REMOVAL) &&
                context.isEmpty() &&
                plugin.getConfiguration().get(ConfigKeys.PRIMARY_GROUP_CALCULATION_METHOD).equals("stored") &&
                user.getPrimaryGroup().getStoredValue().orElse(GroupManager.DEFAULT_GROUP_NAME).equalsIgnoreCase(groupName);

        if (shouldPrevent) {
            Message.USER_REMOVEGROUP_ERROR_PRIMARY.send(sender);
            return CommandResult.STATE_ERROR;
        }
    }

    DataMutateResult result = target.unsetNode(DataType.NORMAL, Inheritance.builder(groupName).withContext(context).build());
    if (result.wasSuccessful()) {
        Message.UNSET_INHERIT_SUCCESS.send(sender, target.getFormattedDisplayName(), groupName, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

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

        if (target.getType() == HolderType.USER) {
            plugin.getUserManager().giveDefaultIfNeeded(((User) target), false);
        }

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.DOES_NOT_INHERIT.send(sender, target.getFormattedDisplayName(), groupName, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}