net.luckperms.api.model.data.DataMutateResult Java Examples

The following examples show how to use net.luckperms.api.model.data.DataMutateResult. 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: LuckPermsHandler.java    From AuthMeReloaded with GNU General Public License v3.0 8 votes vote down vote up
@Override
public boolean addToGroup(OfflinePlayer player, String group) {
    Group newGroup = luckPerms.getGroupManager().getGroup(group);
    if (newGroup == null) {
        return false;
    }

    String playerName = player.getName();
    if (playerName == null) {
        return false;
    }
    User user = luckPerms.getUserManager().getUser(playerName);
    if (user == null) {
        return false;
    }

    InheritanceNode node = InheritanceNode.builder(group).build();
    DataMutateResult result = user.data().add(node);
    if (result == DataMutateResult.FAIL) {
        return false;
    }

    luckPerms.getUserManager().saveUser(user);
    return true;
}
 
Example #2
Source File: LuckPermsHandler.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean removeFromGroup(OfflinePlayer player, String group) {
    String playerName = player.getName();
    if (playerName == null) {
        return false;
    }
    User user = luckPerms.getUserManager().getUser(playerName);
    if (user == null) {
        logger.warning("LuckPermsHandler: tried to remove group for offline user "
            + player.getName() + " but it isn't loaded!");
        return false;
    }

    InheritanceNode groupNode = InheritanceNode.builder(group).build();
    boolean result = user.data().remove(groupNode) != DataMutateResult.FAIL;

    luckPerms.getUserManager().saveUser(user);
    return result;
}
 
Example #3
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 #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: LuckPermsHandler.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean setGroup(OfflinePlayer player, String group) {
    String playerName = player.getName();
    if (playerName == null) {
        return false;
    }
    User user = luckPerms.getUserManager().getUser(playerName);
    if (user == null) {
        logger.warning("LuckPermsHandler: tried to set group for offline user "
            + player.getName() + " but it isn't loaded!");
        return false;
    }
    InheritanceNode groupNode = InheritanceNode.builder(group).build();
    DataMutateResult result = user.data().add(groupNode);
    if (result == DataMutateResult.FAIL) {
        return false;
    }
    user.data().clear(node -> {
        if (!(node instanceof InheritanceNode)) {
            return false;
        }
        InheritanceNode inheritanceNode = (InheritanceNode) node;
        return !inheritanceNode.equals(groupNode);
    });

    luckPerms.getUserManager().saveUser(user);
    return true;
}
 
Example #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
Source File: ApiTrack.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public @NonNull DataMutateResult removeGroup(@NonNull Group group) {
    Objects.requireNonNull(group, "group");
    return this.handle.removeGroup(ApiGroup.cast(group));
}
 
Example #22
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 #23
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;
    }
}
 
Example #24
Source File: ApiTrack.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public @NonNull DataMutateResult appendGroup(@NonNull Group group) {
    Objects.requireNonNull(group, "group");
    return this.handle.appendGroup(ApiGroup.cast(group));
}
 
Example #25
Source File: PermissionHolder.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public @NonNull DataMutateResult getResult() {
    return this.result;
}
 
Example #26
Source File: PermissionHolder.java    From LuckPerms with MIT License 4 votes vote down vote up
private MergedNodeResult(DataMutateResult result, Node mergedNode) {
    this.result = result;
    this.mergedNode = mergedNode;
}
 
Example #27
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);
}
 
Example #28
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 #29
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 #30
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;
    }
}