Search in sources :

Example 1 with PermissionNode

use of fr.neatmonster.nocheatplus.permissions.PermissionNode 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)

Example 2 with PermissionNode

use of fr.neatmonster.nocheatplus.permissions.PermissionNode in project NoCheatPlus by NoCheatPlus.

the class PlayerData method handleTimeRanBackwards.

public void handleTimeRanBackwards(final Collection<Class<? extends IData>> dataTypes) {
    // Permissions.
    final Iterator<Entry<Integer, PermissionNode>> it = permissions.iterator();
    final long timeNow = System.currentTimeMillis();
    while (it.hasNext()) {
        final PermissionNode node = it.next().getValue();
        switch(node.getFetchingPolicy()) {
            case INTERVAL:
                node.invalidate();
                break;
            default:
                if (node.getLastFetch() > timeNow) {
                    node.setState(node.getLastState(), timeNow);
                }
                break;
        }
    }
    // TODO: Register explicitly or not? (+ auto register?)...
    for (final Class<? extends IData> type : dataTypes) {
        final IData obj = dataCache.get(type);
        if (obj != null && obj instanceof ICanHandleTimeRunningBackwards) {
            ((ICanHandleTimeRunningBackwards) obj).handleTimeRanBackwards();
        }
    }
}
Also used : Entry(java.util.Map.Entry) IData(fr.neatmonster.nocheatplus.components.data.IData) ICanHandleTimeRunningBackwards(fr.neatmonster.nocheatplus.components.data.ICanHandleTimeRunningBackwards) PermissionNode(fr.neatmonster.nocheatplus.permissions.PermissionNode)

Example 3 with PermissionNode

use of fr.neatmonster.nocheatplus.permissions.PermissionNode in project NoCheatPlus by NoCheatPlus.

the class PlayerData method invalidateOffline.

private void invalidateOffline() {
    final Iterator<Entry<Integer, PermissionNode>> it = permissions.iterator();
    // TODO: More efficient: get unmodifiable collection from registry?
    while (it.hasNext()) {
        final PermissionNode node = it.next().getValue();
        final PermissionInfo info = node.getPermissionInfo();
        if (info.invalidationOffline() || /*
                     * TODO: world based should only be invalidated with world
                     * changing. Therefore store the last world info
                     * (UUID/name?) in PlayerData and use on login for
                     * comparison.
                     */
        info.invalidationWorld()) {
            // TODO: Really count leave as world change?
            node.invalidate();
        }
    }
}
Also used : Entry(java.util.Map.Entry) PermissionInfo(fr.neatmonster.nocheatplus.permissions.PermissionInfo) PermissionNode(fr.neatmonster.nocheatplus.permissions.PermissionNode)

Example 4 with PermissionNode

use of fr.neatmonster.nocheatplus.permissions.PermissionNode in project NoCheatPlus by NoCheatPlus.

the class PlayerData method getOrCreatePermissionNode.

private PermissionNode getOrCreatePermissionNode(final RegisteredPermission registeredPermission) {
    // Optimistic creation (concurrency).
    final PermissionNode node = new PermissionNode(permissionRegistry.getPermissionInfo(registeredPermission.getId()));
    final PermissionNode oldNode = permissions.putIfAbsent(registeredPermission.getId(), node);
    return oldNode == null ? node : oldNode;
}
Also used : PermissionNode(fr.neatmonster.nocheatplus.permissions.PermissionNode)

Example 5 with PermissionNode

use of fr.neatmonster.nocheatplus.permissions.PermissionNode in project NoCheatPlus by NoCheatPlus.

the class PlayerData method onPlayerChangedWorld.

/**
 * Early adaption on world change.
 *
 * @param oldWorld
 * @param newWorld
 * @param types
 */
void onPlayerChangedWorld(final Player player, final World oldWorld, final World newWorld, final WorldDataManager worldDataManager, final Collection<Class<? extends IDataOnWorldChange>> types) {
    updateCurrentWorld(newWorld, worldDataManager);
    // TODO: Double-invalidation (previous policy and target world policy)
    final Iterator<Entry<Integer, PermissionNode>> it = permissions.iterator();
    // TODO: More efficient: get unmodifiable collection from registry?
    while (it.hasNext()) {
        final PermissionNode node = it.next().getValue();
        final PermissionInfo info = node.getPermissionInfo();
        if (info.invalidationWorld()) {
            node.invalidate();
        }
    }
    requestLazyPermissionUpdate(permissionRegistry.getPreferKeepUpdatedWorld());
    for (final Class<? extends IDataOnWorldChange> type : types) {
        final IDataOnWorldChange instance = dataCache.get(type);
        if (instance != null && instance.dataOnWorldChange(player, this, oldWorld, newWorld)) {
            dataCache.remove(type);
        }
    }
}
Also used : Entry(java.util.Map.Entry) PermissionInfo(fr.neatmonster.nocheatplus.permissions.PermissionInfo) IDataOnWorldChange(fr.neatmonster.nocheatplus.components.data.IDataOnWorldChange) PermissionNode(fr.neatmonster.nocheatplus.permissions.PermissionNode)

Aggregations

PermissionNode (fr.neatmonster.nocheatplus.permissions.PermissionNode)5 Entry (java.util.Map.Entry)3 PermissionInfo (fr.neatmonster.nocheatplus.permissions.PermissionInfo)2 AlmostBoolean (fr.neatmonster.nocheatplus.compat.AlmostBoolean)1 ICanHandleTimeRunningBackwards (fr.neatmonster.nocheatplus.components.data.ICanHandleTimeRunningBackwards)1 IData (fr.neatmonster.nocheatplus.components.data.IData)1 IDataOnWorldChange (fr.neatmonster.nocheatplus.components.data.IDataOnWorldChange)1 FetchingPolicy (fr.neatmonster.nocheatplus.permissions.PermissionPolicy.FetchingPolicy)1