-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.21.3] Add condition to validate feature flags enabled state (#1712)
Co-authored-by: Marc Hermans <[email protected]>
- Loading branch information
1 parent
cc64d63
commit c0dbe21
Showing
11 changed files
with
329 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/net/neoforged/neoforge/common/conditions/FlagCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.common.conditions; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.world.flag.FeatureFlag; | ||
import net.minecraft.world.flag.FeatureFlagSet; | ||
import net.minecraft.world.flag.FeatureFlags; | ||
|
||
/** | ||
* 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 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("expected_result", true).forGetter(condition -> condition.expectedResult)).apply(instance, FlagCondition::new)); | ||
|
||
private final FeatureFlagSet requiredFeatures; | ||
private final boolean expectedResult; | ||
|
||
private FlagCondition(FeatureFlagSet requiredFeatures, boolean expectedResult) { | ||
this.requiredFeatures = requiredFeatures; | ||
this.expectedResult = expectedResult; | ||
} | ||
|
||
@Override | ||
public boolean test(IContext context) { | ||
var flagsEnabled = requiredFeatures.isSubsetOf(context.enabledFeatures()); | ||
// true if: 'expectedResult' is true nd all given flags are enabled | ||
// false if: `enabledEnabled' is false and all given flags are disabled | ||
return flagsEnabled == expectedResult; | ||
} | ||
|
||
@Override | ||
public MapCodec<? extends ICondition> codec() { | ||
return CODEC; | ||
} | ||
|
||
public static ICondition isEnabled(FeatureFlagSet requiredFeatures) { | ||
return new FlagCondition(requiredFeatures, true); | ||
} | ||
|
||
public static ICondition isEnabled(FeatureFlag requiredFlag) { | ||
return isEnabled(FeatureFlagSet.of(requiredFlag)); | ||
} | ||
|
||
public static ICondition isEnabled(FeatureFlag requiredFlag, FeatureFlag... requiredFlags) { | ||
return isEnabled(FeatureFlagSet.of(requiredFlag, requiredFlags)); | ||
} | ||
|
||
public static ICondition isDisabled(FeatureFlagSet requiredFeatures) { | ||
return new FlagCondition(requiredFeatures, false); | ||
} | ||
|
||
public static ICondition isDisabled(FeatureFlag requiredFlag) { | ||
return isDisabled(FeatureFlagSet.of(requiredFlag)); | ||
} | ||
|
||
public static ICondition isDisabled(FeatureFlag requiredFlag, FeatureFlag... requiredFlags) { | ||
return isDisabled(FeatureFlagSet.of(requiredFlag, requiredFlags)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ources/data/neotests_test_flag_condition/advancement/recipes/misc/diamonds_from_dirt.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"neoforge:conditions": [ | ||
{ | ||
"type": "neoforge:feature_flags", | ||
"flags": [ | ||
"custom_feature_flags_pack_test:test_flag" | ||
] | ||
} | ||
], | ||
"parent": "minecraft:recipes/root", | ||
"criteria": { | ||
"has_dirt": { | ||
"conditions": { | ||
"items": [ | ||
{ | ||
"items": "#minecraft:dirt" | ||
} | ||
] | ||
}, | ||
"trigger": "minecraft:inventory_changed" | ||
}, | ||
"has_the_recipe": { | ||
"conditions": { | ||
"recipe": "neotests_test_flag_condition:diamonds_from_dirt" | ||
}, | ||
"trigger": "minecraft:recipe_unlocked" | ||
} | ||
}, | ||
"requirements": [ | ||
[ | ||
"has_the_recipe", | ||
"has_dirt" | ||
] | ||
], | ||
"rewards": { | ||
"recipes": [ | ||
"neotests_test_flag_condition:diamonds_from_dirt" | ||
] | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"neoforge:conditions": [ | ||
{ | ||
"type": "neoforge:feature_flags", | ||
"expected_result": false, | ||
"flags": [ | ||
"custom_feature_flags_pack_test:test_flag" | ||
] | ||
} | ||
], | ||
"parent": "minecraft:recipes/root", | ||
"criteria": { | ||
"has_diamond": { | ||
"conditions": { | ||
"items": [ | ||
{ | ||
"items": "#c:gems/diamond" | ||
} | ||
] | ||
}, | ||
"trigger": "minecraft:inventory_changed" | ||
}, | ||
"has_the_recipe": { | ||
"conditions": { | ||
"recipe": "neotests_test_flag_condition:dirt_from_diamonds" | ||
}, | ||
"trigger": "minecraft:recipe_unlocked" | ||
} | ||
}, | ||
"requirements": [ | ||
[ | ||
"has_the_recipe", | ||
"has_diamond" | ||
] | ||
], | ||
"rewards": { | ||
"recipes": [ | ||
"neotests_test_flag_condition:dirt_from_diamonds" | ||
] | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
.../src/generated/resources/data/neotests_test_flag_condition/recipe/diamonds_from_dirt.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"neoforge:conditions": [ | ||
{ | ||
"type": "neoforge:feature_flags", | ||
"flags": [ | ||
"custom_feature_flags_pack_test:test_flag" | ||
] | ||
} | ||
], | ||
"type": "minecraft:crafting_shapeless", | ||
"category": "misc", | ||
"ingredients": [ | ||
"#minecraft:dirt" | ||
], | ||
"result": { | ||
"count": 1, | ||
"id": "minecraft:diamond" | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
.../src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"neoforge:conditions": [ | ||
{ | ||
"type": "neoforge:feature_flags", | ||
"expected_result": false, | ||
"flags": [ | ||
"custom_feature_flags_pack_test:test_flag" | ||
] | ||
} | ||
], | ||
"type": "minecraft:crafting_shapeless", | ||
"category": "misc", | ||
"ingredients": [ | ||
"#c:gems/diamond" | ||
], | ||
"result": { | ||
"count": 1, | ||
"id": "minecraft:dirt" | ||
} | ||
} |
Oops, something went wrong.