net.luckperms.api.context.MutableContextSet Java Examples

The following examples show how to use net.luckperms.api.context.MutableContextSet. 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: ContextSetJsonSerializer.java    From LuckPerms with MIT License 6 votes vote down vote up
public static ContextSet deserializeContextSet(JsonElement element) {
    Preconditions.checkArgument(element.isJsonObject());
    JsonObject data = element.getAsJsonObject();

    if (data.entrySet().isEmpty()) {
        return ImmutableContextSetImpl.EMPTY;
    }

    MutableContextSet map = new MutableContextSetImpl();
    for (Map.Entry<String, JsonElement> e : data.entrySet()) {
        String k = e.getKey();
        JsonElement v = e.getValue();
        if (v.isJsonArray()) {
            JsonArray values = v.getAsJsonArray();
            for (JsonElement value : values) {
                map.add(k, value.getAsString());
            }
        } else {
            map.add(k, v.getAsString());
        }
    }

    return map;
}
 
Example #2
Source File: ContextSetConfigurateSerializer.java    From LuckPerms with MIT License 6 votes vote down vote up
public static ContextSet deserializeContextSet(ConfigurationNode data) {
    Preconditions.checkArgument(data.hasMapChildren());
    Map<Object, ? extends ConfigurationNode> dataMap = data.getChildrenMap();

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

    MutableContextSet map = new MutableContextSetImpl();
    for (Map.Entry<Object, ? extends ConfigurationNode> e : dataMap.entrySet()) {
        String k = e.getKey().toString();
        ConfigurationNode v = e.getValue();

        if (v.hasListChildren()) {
            List<? extends ConfigurationNode> values = v.getChildrenList();
            for (ConfigurationNode value : values) {
                map.add(k, value.getString());
            }
        } else {
            map.add(k, v.getString());
        }
    }

    return map;
}
 
Example #3
Source File: LuckPerms5Hook.java    From BungeeChat2 with GNU General Public License v3.0 6 votes vote down vote up
private QueryOptions getQueryOptions(Optional<User> user) {
  final ContextManager contextManager = api.getContextManager();
  final QueryOptions queryOptions =
      user.flatMap(contextManager::getQueryOptions)
          .orElseGet(contextManager::getStaticQueryOptions);

  if (fixContexts && (queryOptions.mode() == QueryMode.CONTEXTUAL)) {
    final MutableContextSet context = queryOptions.context().mutableCopy();

    context
        .getValues(DefaultContextKeys.WORLD_KEY)
        .forEach(world -> context.add(DefaultContextKeys.SERVER_KEY, world));

    return queryOptions.toBuilder().context(context).build();
  } else {
    return queryOptions;
  }
}
 
Example #4
Source File: PermissionCheckInherits.java    From LuckPerms with MIT License 6 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.checkViewPerms(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);

    Optional<Node> match = target.resolveInheritedNodes(QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL).stream()
            .filter(n -> n.getKey().equalsIgnoreCase(node) && n.getContexts().equals(context))
            .findFirst();

    String location = match.map(n -> n.metadata(InheritanceOriginMetadata.KEY).getOrigin().getName()).orElse(null);

    if (location == null || location.equalsIgnoreCase(target.getObjectName())) {
        location = "self";
    }

    String s = MessageUtils.formatTristate(match.map(n -> Tristate.of(n.getValue())).orElse(Tristate.UNDEFINED));
    Message.CHECK_INHERITS_PERMISSION.send(sender, target.getFormattedDisplayName(), node, s, MessageUtils.contextSetToString(plugin.getLocaleManager(), context), location);
    return CommandResult.SUCCESS;
}
 
Example #5
Source File: PermissionCheck.java    From LuckPerms with MIT License 6 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.checkViewPerms(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);

    Tristate result = target.hasNode(DataType.NORMAL, NodeBuilders.determineMostApplicable(node).withContext(context).build(), NodeEqualityPredicate.IGNORE_VALUE_OR_IF_TEMPORARY);
    String s = MessageUtils.formatTristate(result);

    Message.CHECK_PERMISSION.send(sender, target.getFormattedDisplayName(), node, s, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
    return CommandResult.SUCCESS;
}
 
Example #6
Source File: MetaUnset.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 key = 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, key)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    if (target.removeIf(DataType.NORMAL, context, NodeType.META.predicate(n -> !n.hasExpiry() && n.getMetaKey().equalsIgnoreCase(key)), false)) {
        Message.UNSET_META_SUCCESS.send(sender, key, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

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

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.DOESNT_HAVE_META.send(sender, target.getFormattedDisplayName(), key, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example #7
Source File: ArgumentList.java    From LuckPerms with MIT License 5 votes vote down vote up
private MutableContextSet parseContext(int fromIndex) {
    MutableContextSet contextSet = new MutableContextSetImpl();
    List<String> entries = subList(fromIndex, size());
    for (int i = 0; i < entries.size(); i++) {
        String entry = entries.get(i);
        int sep = entry.indexOf('=');

        String key;
        String value;

        if (sep != -1) {
            key = entry.substring(0, sep);
            value = entry.substring(sep + 1);
        } else {
            key = i == 1 ? DefaultContextKeys.WORLD_KEY : DefaultContextKeys.SERVER_KEY;
            value = entry;
        }

        if (!Context.isValidKey(key) || !Context.isValidValue(value)) {
            continue;
        }

        contextSet.add(key, value);
    }

    return contextSet;
}
 
Example #8
Source File: MongoStorage.java    From LuckPerms with MIT License 5 votes vote down vote up
private static MutableContextSet docsToContextSet(List<Document> documents) {
    MutableContextSet map = new MutableContextSetImpl();
    for (Document doc : documents) {
        map.add(doc.getString("key"), doc.getString("value"));
    }
    return map;
}
 
Example #9
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 #10
Source File: ParentSet.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.LOADING_ERROR;
    }

    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;
    }

    target.removeIf(DataType.NORMAL, context, NodeType.INHERITANCE::matches, false);
    target.setNode(DataType.NORMAL, Inheritance.builder(group.getName()).withContext(context).build(), true);
    if (target.getType() == HolderType.USER) {
        ((User) target).getPrimaryGroup().setStoredValue(group.getName());
    }

    Message.SET_PARENT_SUCCESS.send(sender, target.getFormattedDisplayName(), group.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

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

    StorageAssistant.save(target, sender, plugin);
    return CommandResult.SUCCESS;
}
 
Example #11
Source File: ParentClear.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;
    }

    int before = target.normalData().immutable().size();

    MutableContextSet context = args.getContextOrDefault(0, plugin);

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

    if (context.isEmpty()) {
        target.removeIf(DataType.NORMAL, null, NodeType.INHERITANCE::matches, true);
    } else {
        target.removeIf(DataType.NORMAL, context, NodeType.INHERITANCE::matches, true);
    }

    int changed = before - target.normalData().immutable().size();
    if (changed == 1) {
        Message.PARENT_CLEAR_SUCCESS_SINGULAR.send(sender, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context), changed);
    } else {
        Message.PARENT_CLEAR_SUCCESS.send(sender, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context), changed);
    }

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

    StorageAssistant.save(target, sender, plugin);
    return CommandResult.SUCCESS;
}
 
Example #12
Source File: HolderClear.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T target, ArgumentList args, String label) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    int before = target.normalData().immutable().size();

    MutableContextSet context = args.getContextOrDefault(0, plugin);

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

    if (context.isEmpty()) {
        target.clearNodes(DataType.NORMAL, null, true);
    } else {
        target.clearNodes(DataType.NORMAL, context, true);
    }

    int changed = before - target.normalData().immutable().size();
    if (changed == 1) {
        Message.CLEAR_SUCCESS_SINGULAR.send(sender, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context), changed);
    } else {
        Message.CLEAR_SUCCESS.send(sender, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context), changed);
    }

    LoggedAction.build().source(sender).target(target)
            .description("clear", context)
            .build().submit(plugin, sender);

    StorageAssistant.save(target, sender, plugin);
    return CommandResult.SUCCESS;
}
 
Example #13
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public void addActiveContexts(Set<Context> contexts, GDPermissionHolder permissionHolder, GDPlayerData playerData, Claim claim) {
    if (playerData != null) {
        playerData.ignoreActiveContexts = true;
    }
    final PermissionHolder luckPermsHolder = this.getLuckPermsHolder(permissionHolder);
    if (luckPermsHolder instanceof Group) {
        contexts.addAll(this.getGDContexts(this.luckPermsApi.getContextManager().getStaticContext().mutableCopy()));
        return;
    }

    ImmutableContextSet contextSet = this.luckPermsApi.getContextManager().getContext((User) luckPermsHolder).orElse(null);
    if (contextSet == null) {
        contextSet = this.luckPermsApi.getContextManager().getStaticContext();
    }
    if (contextSet == null) {
        return;
    }
    MutableContextSet activeContexts = contextSet.mutableCopy();
    if (playerData != null && claim != null) {
        final Claim parent = claim.getParent().orElse(null);
        if (parent != null && claim.getData() != null && claim.getData().doesInheritParent()) {
            activeContexts.remove(parent.getContext().getKey(), parent.getContext().getValue());
        } else {
            activeContexts.remove(claim.getContext().getKey(), claim.getContext().getValue());
        }
    }
    contexts.addAll(this.getGDContexts(activeContexts));
}
 
Example #14
Source File: MetaUnsetTemp.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 key = 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, key)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }

    if (target.removeIf(DataType.NORMAL, context, NodeType.META.predicate(n -> n.hasExpiry() && n.getMetaKey().equalsIgnoreCase(key)), false)) {
        Message.UNSET_META_TEMP_SUCCESS.send(sender, key, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context));

        LoggedAction.build().source(sender).target(target)
                .description("meta", "unsettemp", key, context)
                .build().submit(plugin, sender);

        StorageAssistant.save(target, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.DOESNT_HAVE_TEMP_META.send(sender, target.getFormattedDisplayName(), key, MessageUtils.contextSetToString(plugin.getLocaleManager(), context));
        return CommandResult.STATE_ERROR;
    }
}
 
Example #15
Source File: PermissionClear.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;
    }

    int before = target.normalData().immutable().size();

    MutableContextSet context = args.getContextOrDefault(0, plugin);

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

    target.removeIf(DataType.NORMAL, context.isEmpty() ? null : context, NodeType.PERMISSION::matches, false);

    int changed = before - target.normalData().immutable().size();
    if (changed == 1) {
        Message.PERMISSION_CLEAR_SUCCESS_SINGULAR.send(sender, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context), changed);
    } else {
        Message.PERMISSION_CLEAR_SUCCESS.send(sender, target.getFormattedDisplayName(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context), changed);
    }

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

    StorageAssistant.save(target, sender, plugin);
    return CommandResult.SUCCESS;
}
 
Example #16
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public MutableContextSet getLPContexts(Set<Context> contexts) {
    MutableContextSet lpContexts = MutableContextSet.create();
    contexts.forEach(entry -> {
        lpContexts.add(entry.getKey(), entry.getValue());
    });

    return lpContexts;
}
 
Example #17
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public void addActiveContexts(Set<Context> contexts, GDPermissionHolder permissionHolder, GDPlayerData playerData, Claim claim) {
    if (playerData != null) {
        playerData.ignoreActiveContexts = true;
    }
    final PermissionHolder luckPermsHolder = this.getLuckPermsHolder(permissionHolder);
    if (luckPermsHolder instanceof Group) {
        contexts.addAll(this.getGDContexts(this.luckPermsApi.getContextManager().getStaticContext().mutableCopy()));
        return;
    }

    ImmutableContextSet contextSet = this.luckPermsApi.getContextManager().getContext((User) luckPermsHolder).orElse(null);
    if (contextSet == null) {
        contextSet = this.luckPermsApi.getContextManager().getStaticContext();
    }
    if (contextSet == null) {
        return;
    }
    MutableContextSet activeContexts = contextSet.mutableCopy();
    if (playerData != null && claim != null) {
        final Claim parent = claim.getParent().orElse(null);
        if (parent != null && claim.getData() != null && claim.getData().doesInheritParent()) {
            activeContexts.remove(parent.getContext().getKey(), parent.getContext().getValue());
        } else {
            activeContexts.remove(claim.getContext().getKey(), claim.getContext().getValue());
        }
    }
    contexts.addAll(this.getGDContexts(activeContexts));
}
 
Example #18
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 5 votes vote down vote up
public MutableContextSet getLPContexts(Set<Context> contexts) {
    MutableContextSet lpContexts = MutableContextSet.create();
    contexts.forEach(entry -> {
        lpContexts.add(entry.getKey(), entry.getValue());
    });

    return lpContexts;
}
 
Example #19
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 4 votes vote down vote up
public Tristate getPermissionValue(GDPermissionHolder holder, String permission, MutableContextSet contexts) {
    return this.getPermissionValue(holder, permission, this.getGDContexts(contexts));
}
 
Example #20
Source File: ArgumentList.java    From LuckPerms with MIT License 4 votes vote down vote up
public MutableContextSet getContextOrDefault(int fromIndex, LuckPermsPlugin plugin) throws CommandException {
    if (size() <= fromIndex) {
        return plugin.getConfiguration().getContextsFile().getDefaultContexts().mutableCopy();
    }
    return parseContext(fromIndex);
}
 
Example #21
Source File: LuckPermsProvider.java    From GriefDefender with MIT License 4 votes vote down vote up
public Tristate getPermissionValue(GDPermissionHolder holder, String permission, MutableContextSet contexts) {
    return this.getPermissionValue(holder, permission, this.getGDContexts(contexts));
}
 
Example #22
Source File: ImmutableContextSetImpl.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public @NonNull MutableContextSet mutableCopy() {
    return new MutableContextSetImpl(this.map);
}
 
Example #23
Source File: ApiContextSetFactory.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
public @NonNull MutableContextSet mutable() {
    return new MutableContextSetImpl();
}
 
Example #24
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 #25
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 #26
Source File: LuckPermsVaultPermission.java    From LuckPerms with MIT License 4 votes vote down vote up
QueryOptions getQueryOptions(@Nullable UUID uuid, @Nullable String world) {
    MutableContextSet context;

    Player player = Optional.ofNullable(uuid).flatMap(u -> this.plugin.getBootstrap().getPlayer(u)).orElse(null);
    if (player != null) {
        context = this.plugin.getContextManager().getContext(player).mutableCopy();
    } else {
        context = this.plugin.getContextManager().getStaticContext().mutableCopy();
    }

    String playerWorld = player == null ? null : player.getWorld().getName();

    // if world is null, we want to do a lookup in the players current context
    // if world is not null, we want to do a lookup in that specific world
    if (world != null && !world.isEmpty() && !world.equalsIgnoreCase(playerWorld)) {
        // remove already accumulated worlds
        context.removeAll(DefaultContextKeys.WORLD_KEY);
        // add the vault world
        context.add(DefaultContextKeys.WORLD_KEY, world.toLowerCase());
    }

    // if we're using a special vault server
    if (useVaultServer()) {
        // remove the normal server context from the set
        context.remove(DefaultContextKeys.SERVER_KEY, getServer());

        // add the vault specific server
        if (!getVaultServer().equals("global")) {
            context.add(DefaultContextKeys.SERVER_KEY, getVaultServer());
        }
    }

    boolean op = false;
    if (player != null) {
        op = player.isOp();
    } else if (uuid != null && uuid.version() == 2) { // npc
        op = this.plugin.getConfiguration().get(ConfigKeys.VAULT_NPC_OP_STATUS);
    }

    QueryOptions.Builder builder = QueryOptionsImpl.DEFAULT_CONTEXTUAL.toBuilder();
    builder.context(context);
    builder.flag(Flag.INCLUDE_NODES_WITHOUT_SERVER_CONTEXT, isIncludeGlobal());
    if (op) {
        builder.option(BukkitContextManager.OP_OPTION, true);
    }
    return builder.build();
}
 
Example #27
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 #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: MetaClear.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;
    }

    NodeType<?> type = null;
    if (!args.isEmpty()) {
        String typeId = args.get(0).toLowerCase();
        if (typeId.equals("any") || typeId.equals("all") || typeId.equals("*")) {
            type = NodeType.META_OR_CHAT_META;
        }
        if (typeId.equals("chat") || typeId.equals("chatmeta")) {
            type = NodeType.CHAT_META;
        }
        if (typeId.equals("meta")) {
            type = NodeType.META;
        }
        if (typeId.equals("prefix") || typeId.equals("prefixes")) {
            type = NodeType.PREFIX;
        }
        if (typeId.equals("suffix") || typeId.equals("suffixes")) {
            type = NodeType.SUFFIX;
        }

        if (type != null) {
            args.remove(0);
        }
    }

    if (type == null) {
        type = NodeType.META_OR_CHAT_META;
    }

    int before = target.normalData().immutable().size();

    MutableContextSet context = args.getContextOrDefault(0, plugin);

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

    if (context.isEmpty()) {
        target.removeIf(DataType.NORMAL, null, type::matches, false);
    } else {
        target.removeIf(DataType.NORMAL, context, type::matches, false);
    }

    int changed = before - target.normalData().immutable().size();
    if (changed == 1) {
        Message.META_CLEAR_SUCCESS_SINGULAR.send(sender, target.getFormattedDisplayName(), type.name().toLowerCase(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context), changed);
    } else {
        Message.META_CLEAR_SUCCESS.send(sender, target.getFormattedDisplayName(), type.name().toLowerCase(), MessageUtils.contextSetToString(plugin.getLocaleManager(), context), changed);
    }

    LoggedAction.build().source(sender).target(target)
            .description("meta", "clear", context)
            .build().submit(plugin, sender);

    StorageAssistant.save(target, sender, plugin);
    return CommandResult.SUCCESS;
}