Search in sources :

Example 1 with DataQueryOrderFunction

use of net.luckperms.api.query.dataorder.DataQueryOrderFunction in project LuckPerms by lucko.

the class DataSelector method selectOrder.

public static DataType[] selectOrder(QueryOptions queryOptions, PermissionHolder.Identifier identifier) {
    final DataQueryOrderFunction orderFunc = queryOptions.option(DataQueryOrderFunction.KEY).orElse(null);
    final DataTypeFilterFunction filterFunc = queryOptions.option(DataTypeFilterFunction.KEY).orElse(null);
    if (orderFunc == null && filterFunc == null) {
        return TRANSIENT_NORMAL;
    }
    Predicate<DataType> predicate = filterFunc != null ? filterFunc.getTypeFilter(identifier) : DataTypeFilter.ALL;
    boolean normal = predicate.test(DataType.NORMAL);
    boolean trans = predicate.test(DataType.TRANSIENT);
    // if both are included - we need to consult the order comparator
    if (normal && trans) {
        Comparator<DataType> comparator = orderFunc != null ? orderFunc.getOrderComparator(identifier) : DataQueryOrder.TRANSIENT_FIRST;
        int compare = comparator.compare(DataType.TRANSIENT, DataType.NORMAL);
        if (compare == 0) {
            throw new IllegalStateException("Comparator " + comparator + " does not define an order between DataType.NORMAL and DataType.TRANSIENT!");
        }
        return compare > 0 ? TRANSIENT_NORMAL : NORMAL_TRANSIENT;
    }
    // otherwise, no need to worry about order
    if (normal) {
        return NORMAL;
    } else if (trans) {
        return TRANSIENT;
    } else {
        return NONE;
    }
}
Also used : DataQueryOrderFunction(net.luckperms.api.query.dataorder.DataQueryOrderFunction) DataType(net.luckperms.api.model.data.DataType) DataTypeFilterFunction(net.luckperms.api.query.dataorder.DataTypeFilterFunction)

Aggregations

DataType (net.luckperms.api.model.data.DataType)1 DataQueryOrderFunction (net.luckperms.api.query.dataorder.DataQueryOrderFunction)1 DataTypeFilterFunction (net.luckperms.api.query.dataorder.DataTypeFilterFunction)1