Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21.3] Add condition to validate feature flags enabled state #1712

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@
* Condition checking for the enabled state of a given {@link FeatureFlagSet}.
* <p>
* {@code requiredFeatures} - {@link FeatureFlagSet} containing all {@link FeatureFlag feature flags} to be validated.
* {@code checkEnabled} - Validates that all given {@link FeatureFlag feature flags} are enabled when {@code true} or disabled when {@code false}.
* {@code expectedResult} - Validates that all given {@link FeatureFlag feature flags} are enabled when {@code true} or disabled when {@code false}.
*
* @apiNote Mainly to be used when flagged content is not contained within the same feature pack which also enables said {@link FeatureFlag feature flags}.
*/
public final class FlagCondition implements ICondition {
public static final MapCodec<FlagCondition> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
FeatureFlags.CODEC.fieldOf("flags").forGetter(condition -> condition.requiredFeatures),
Codec.BOOL.lenientOptionalFieldOf("check_enabled", true).forGetter(condition -> condition.checkEnabled)).apply(instance, FlagCondition::new));
Codec.BOOL.lenientOptionalFieldOf("check_enabled", true).forGetter(condition -> condition.expectedResult)).apply(instance, FlagCondition::new));

private final FeatureFlagSet requiredFeatures;
private final boolean checkEnabled;
private final boolean expectedResult;

private FlagCondition(FeatureFlagSet requiredFeatures, boolean checkEnabled) {
private FlagCondition(FeatureFlagSet requiredFeatures, boolean expectedResult) {
this.requiredFeatures = requiredFeatures;
this.checkEnabled = checkEnabled;
this.expectedResult = expectedResult;
}

@Override
public boolean test(IContext context) {
var flagsEnabled = requiredFeatures.isSubsetOf(context.enabledFeatures());
// true if: 'checkEnabled' is true nd all given flags are enabled
// true if: 'expectedResult' is true nd all given flags are enabled
// false if: `enabledEnabled' is false and all given flags are disabled
return flagsEnabled == checkEnabled;
return flagsEnabled == expectedResult;
}

@Override
Expand Down