Search in sources :

Example 1 with FetchingPolicy

use of fr.neatmonster.nocheatplus.permissions.PermissionPolicy.FetchingPolicy in project NoCheatPlus by NoCheatPlus.

the class PlayerData method hasPermission.

@Override
public boolean hasPermission(final RegisteredPermission registeredPermission, final Player player) {
    // Check cache and policy.
    PermissionNode node = permissions.get(registeredPermission.getId());
    if (node == null) {
        node = getOrCreatePermissionNode(registeredPermission);
    }
    final FetchingPolicy fetchingPolicy = node.getFetchingPolicy();
    switch(fetchingPolicy) {
        case TRUE:
            return true;
        case FALSE:
            return false;
        default:
    }
    final AlmostBoolean lastState = node.getLastState();
    if (lastState != AlmostBoolean.MAYBE) {
        switch(fetchingPolicy) {
            case ONCE:
                return lastState.decide();
            case INTERVAL:
                if (System.currentTimeMillis() - node.getLastFetch() < node.getFetchInterval()) {
                    return lastState.decide();
                }
            // TODO: ALWAYS: Could still use cache within a check context.
            default:
                // Must fetch.
                break;
        }
    }
    // Permission not cached or needs to be updated with the PlayerTask.
    final AlmostBoolean fRes = fetchPermission(registeredPermission, player);
    if (fRes == AlmostBoolean.MAYBE) {
        // TODO: Slight risk on left-over meant-temporary permissions.
        return lastState.decide();
    } else {
        node.setState(fRes, System.currentTimeMillis());
        return fRes.decide();
    }
}
Also used : AlmostBoolean(fr.neatmonster.nocheatplus.compat.AlmostBoolean) FetchingPolicy(fr.neatmonster.nocheatplus.permissions.PermissionPolicy.FetchingPolicy) PermissionNode(fr.neatmonster.nocheatplus.permissions.PermissionNode)

Aggregations

AlmostBoolean (fr.neatmonster.nocheatplus.compat.AlmostBoolean)1 PermissionNode (fr.neatmonster.nocheatplus.permissions.PermissionNode)1 FetchingPolicy (fr.neatmonster.nocheatplus.permissions.PermissionPolicy.FetchingPolicy)1