Search in sources :

Example 1 with AlmostBooleanWithOverride

use of fr.neatmonster.nocheatplus.components.config.value.AlmostBooleanWithOverride in project NoCheatPlus by NoCheatPlus.

the class BaseCheckNode method update.

/**
 * Update towards the given rawConfiguration instance.
 *
 * @param rawConfiguration
 * @param forceUpdateChildren
 * @param access
 */
protected void update(final ConfigFile rawConfiguration, final boolean forceUpdateChildren, final IConfigFlagAccess<N> access) {
    @SuppressWarnings("unchecked") final N // TODO
    thisNode = (N) this;
    final AlmostBooleanWithOverride configActivation = access.getConfigState(thisNode);
    // First attempt to override by config.
    if (rawConfiguration != null) {
        if (configActivation.allowsOverrideBy(OverrideType.SPECIFIC)) {
            // TODO: SPECIFIC for inherited !?
            final String configPath = access.getConfigPath(thisNode);
            final AlmostBoolean setValue;
            if (configPath == null) {
                setValue = AlmostBoolean.MAYBE;
            } else {
                // TODO: Contract? Either config is null, or path must exist.
                setValue = rawConfiguration.getAlmostBoolean(configPath, AlmostBoolean.MAYBE);
            }
            configActivation.setValue(setValue, configOverrideType);
        }
    }
    // TODO: else -> set to MAYBE ?
    final boolean oldState = access.getState(thisNode);
    // Update in-place.
    update(false, access);
    final boolean changed = oldState ^ access.getState(thisNode);
    if (forceUpdateChildren) {
        // Update children with configuration (forced).
        for (final N node : this.getChildren()) {
            node.update(rawConfiguration, forceUpdateChildren, access);
        }
    } else if (changed) {
        // Update children just for the changed parent.
        for (final N node : this.getChildren()) {
            // Only update, if the state depends on the parent.
            if (access.getConfigState(node).getValue() == AlmostBoolean.MAYBE) {
                node.update(false, access);
            }
        }
    }
}
Also used : AlmostBoolean(fr.neatmonster.nocheatplus.compat.AlmostBoolean) AlmostBooleanWithOverride(fr.neatmonster.nocheatplus.components.config.value.AlmostBooleanWithOverride)

Example 2 with AlmostBooleanWithOverride

use of fr.neatmonster.nocheatplus.components.config.value.AlmostBooleanWithOverride in project NoCheatPlus by NoCheatPlus.

the class BaseCheckNode method update.

/**
 * Update in place according to tree state.
 *
 * @param forceUpdateChildren
 * @param access
 */
protected void update(final boolean forceUpdateChildren, final IConfigFlagAccess<N> access) {
    @SuppressWarnings("unchecked") final N // TODO
    thisNode = (N) this;
    final boolean previousActive = access.getState(thisNode);
    final AlmostBooleanWithOverride configActivation = access.getConfigState(thisNode);
    AlmostBoolean newActive = configActivation.getValue();
    if (newActive != AlmostBoolean.MAYBE) {
        access.setState(thisNode, newActive.decide());
    } else {
        // Fetch from parent.
        if (newActive == AlmostBoolean.MAYBE) {
            N parent = getParent();
            if (parent == null) {
                access.setState(thisNode, access.getMissingParentState());
            } else {
                // Assume top-down updating always.
                access.setState(thisNode, access.getState(parent));
            }
        } else {
            access.setState(thisNode, newActive.decide());
        }
    }
    // Update on changes, or if forced.
    if (forceUpdateChildren || previousActive != access.getState(thisNode)) {
        // Update children.
        for (final N node : this.getChildren()) {
            // Only update, if the state depends on the parent.
            if (forceUpdateChildren || access.getConfigState(node).getValue() == AlmostBoolean.MAYBE) {
                node.update(forceUpdateChildren, access);
            }
        }
    }
}
Also used : AlmostBoolean(fr.neatmonster.nocheatplus.compat.AlmostBoolean) AlmostBooleanWithOverride(fr.neatmonster.nocheatplus.components.config.value.AlmostBooleanWithOverride)

Example 3 with AlmostBooleanWithOverride

use of fr.neatmonster.nocheatplus.components.config.value.AlmostBooleanWithOverride in project NoCheatPlus by NoCheatPlus.

the class TestConfigValueWithOverride method testAlmostBoolean.

@Test
public void testAlmostBoolean() {
    AlmostBooleanWithOverride val1 = new AlmostBooleanWithOverride();
    if (!val1.setValue(AlmostBoolean.NO, OverrideType.SPECIFIC)) {
        fail("Override to SPECIFIC after init is expected to work.");
    }
    expectValueIdentity(val1, AlmostBoolean.NO);
    if (!val1.setValue(AlmostBoolean.YES, OverrideType.SPECIFIC)) {
        fail("Override SPECIFIC -> SPECIFIC is expected to work.");
    }
    expectValueIdentity(val1, AlmostBoolean.YES);
}
Also used : AlmostBooleanWithOverride(fr.neatmonster.nocheatplus.components.config.value.AlmostBooleanWithOverride) Test(org.junit.Test)

Aggregations

AlmostBooleanWithOverride (fr.neatmonster.nocheatplus.components.config.value.AlmostBooleanWithOverride)3 AlmostBoolean (fr.neatmonster.nocheatplus.compat.AlmostBoolean)2 Test (org.junit.Test)1