From 48f3934c5b30049aba0708e49c89a2b7332d621e Mon Sep 17 00:00:00 2001
From: Technici4n <13494793+Technici4n@users.noreply.github.com>
Date: Wed, 27 Nov 2024 12:26:25 +0100
Subject: [PATCH 1/4] Simplify FlagCondition
---
.../common/conditions/FlagCondition.java | 53 +++++++------------
.../common/conditions/IConditionBuilder.java | 24 ++-------
.../recipes/misc/dirt_from_diamonds.json | 12 +++--
.../recipe/dirt_from_diamonds.json | 12 +++--
.../debug/data/CustomFeatureFlagsTests.java | 5 +-
5 files changed, 40 insertions(+), 66 deletions(-)
diff --git a/src/main/java/net/neoforged/neoforge/common/conditions/FlagCondition.java b/src/main/java/net/neoforged/neoforge/common/conditions/FlagCondition.java
index 2b59ed1584..a36f96db34 100644
--- a/src/main/java/net/neoforged/neoforge/common/conditions/FlagCondition.java
+++ b/src/main/java/net/neoforged/neoforge/common/conditions/FlagCondition.java
@@ -5,40 +5,34 @@
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;
+import org.apache.commons.lang3.ArrayUtils;
/**
- * Condition checking for the enabled state of a given {@link FeatureFlagSet}.
- *
- * {@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}.
+ * Condition checking that a set of {@link FeatureFlag feature flags} are enabled.
*
* @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 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));
+ FeatureFlags.CODEC.fieldOf("flags").forGetter(condition -> condition.requiredFeatures)).apply(instance, FlagCondition::new));
private final FeatureFlagSet requiredFeatures;
- private final boolean expectedResult;
- private FlagCondition(FeatureFlagSet requiredFeatures, boolean expectedResult) {
+ private FlagCondition(FeatureFlagSet requiredFeatures) {
+ if (requiredFeatures.isEmpty()) {
+ throw new IllegalArgumentException("FlagCondition requires a non-empty feature flag set");
+ }
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;
+ return requiredFeatures.isSubsetOf(context.enabledFeatures());
}
@Override
@@ -46,27 +40,18 @@ public MapCodec extends ICondition> codec() {
return CODEC;
}
- public static ICondition isEnabled(FeatureFlagSet requiredFeatures) {
- return new FlagCondition(requiredFeatures, true);
+ public static ICondition of(FeatureFlagSet requiredFeatures) {
+ return new FlagCondition(requiredFeatures);
}
- 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));
+ public static ICondition of(FeatureFlag... requiredFlags) {
+ if (requiredFlags.length == 0) {
+ throw new IllegalArgumentException("FlagCondition requires at least one feature flag.");
+ }
+ if (requiredFlags.length == 1) {
+ return new FlagCondition(FeatureFlagSet.of(requiredFlags[0]));
+ } else {
+ return new FlagCondition(FeatureFlagSet.of(requiredFlags[1], ArrayUtils.remove(requiredFlags, 0)));
+ }
}
}
diff --git a/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java b/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
index f0a532ac16..51504fb7dd 100644
--- a/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
+++ b/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
@@ -44,27 +44,11 @@ default ICondition tagEmpty(TagKey- tag) {
return new TagEmptyCondition(tag.location());
}
- default ICondition isFeatureEnabled(FeatureFlagSet requiredFeatures) {
- return FlagCondition.isEnabled(requiredFeatures);
+ default ICondition featureFlags(FeatureFlagSet requiredFeatures) {
+ return FlagCondition.of(requiredFeatures);
}
- default ICondition isFeatureEnabled(FeatureFlag requiredFlag) {
- return FlagCondition.isEnabled(requiredFlag);
- }
-
- default ICondition isFeatureEnabled(FeatureFlag requiredFlag, FeatureFlag... requiredFlags) {
- return FlagCondition.isEnabled(requiredFlag, requiredFlags);
- }
-
- default ICondition isFeatureDisabled(FeatureFlagSet requiredFeatures) {
- return FlagCondition.isDisabled(requiredFeatures);
- }
-
- default ICondition isFeatureDisabled(FeatureFlag requiredFlag) {
- return FlagCondition.isDisabled(requiredFlag);
- }
-
- default ICondition isFeatureDisabled(FeatureFlag requiredFlag, FeatureFlag... requiredFlags) {
- return FlagCondition.isDisabled(requiredFlag, requiredFlags);
+ default ICondition isFeatureEnabled(FeatureFlag... requiredFlags) {
+ return FlagCondition.of(requiredFlags);
}
}
diff --git a/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json b/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
index bde8245ef5..a1ce5d6894 100644
--- a/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
+++ b/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
@@ -1,11 +1,13 @@
{
"neoforge:conditions": [
{
- "type": "neoforge:feature_flags",
- "expected_result": false,
- "flags": [
- "custom_feature_flags_pack_test:test_flag"
- ]
+ "type": "neoforge:not",
+ "value": {
+ "type": "neoforge:feature_flags",
+ "flags": [
+ "custom_feature_flags_pack_test:test_flag"
+ ]
+ }
}
],
"parent": "minecraft:recipes/root",
diff --git a/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json b/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
index d1984cf97c..8979c4c29d 100644
--- a/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
+++ b/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
@@ -1,11 +1,13 @@
{
"neoforge:conditions": [
{
- "type": "neoforge:feature_flags",
- "expected_result": false,
- "flags": [
- "custom_feature_flags_pack_test:test_flag"
- ]
+ "type": "neoforge:not",
+ "value": {
+ "type": "neoforge:feature_flags",
+ "flags": [
+ "custom_feature_flags_pack_test:test_flag"
+ ]
+ }
}
],
"type": "minecraft:crafting_shapeless",
diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java b/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
index 0e852b9840..023534631a 100644
--- a/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
+++ b/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
@@ -25,6 +25,7 @@
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.common.conditions.FlagCondition;
+import net.neoforged.neoforge.common.conditions.NotCondition;
import net.neoforged.neoforge.event.AddPackFindersEvent;
import net.neoforged.neoforge.event.server.ServerStartedEvent;
import net.neoforged.neoforge.registries.DeferredItem;
@@ -122,13 +123,13 @@ protected void buildRecipes() {
shapeless(RecipeCategory.MISC, Items.DIAMOND)
.requires(ItemTags.DIRT)
.unlockedBy("has_dirt", has(ItemTags.DIRT))
- .save(output.withConditions(FlagCondition.isEnabled(flag)), enabledRecipeName);
+ .save(output.withConditions(FlagCondition.of(flag)), enabledRecipeName);
// recipe available when above flag is disabled
shapeless(RecipeCategory.MISC, Items.DIRT)
.requires(Tags.Items.GEMS_DIAMOND)
.unlockedBy("has_diamond", has(Tags.Items.GEMS_DIAMOND))
- .save(output.withConditions(FlagCondition.isDisabled(flag)), disabledRecipeName);
+ .save(output.withConditions(new NotCondition(FlagCondition.of(flag))), disabledRecipeName);
}
};
}
From 8a1b5d3b1884dad772a45d10a005ad448c8a5f22 Mon Sep 17 00:00:00 2001
From: Technici4n <13494793+Technici4n@users.noreply.github.com>
Date: Wed, 27 Nov 2024 17:02:18 +0100
Subject: [PATCH 2/4] Rename condition
---
.../neoforged/neoforge/common/NeoForgeMod.java | 4 ++--
....java => FeatureFlagsEnabledCondition.java} | 18 +++++++++---------
.../common/conditions/IConditionBuilder.java | 8 ++++----
.../recipes/misc/diamonds_from_dirt.json | 2 +-
.../recipes/misc/dirt_from_diamonds.json | 2 +-
.../recipe/diamonds_from_dirt.json | 2 +-
.../recipe/dirt_from_diamonds.json | 2 +-
.../debug/data/CustomFeatureFlagsTests.java | 6 +++---
8 files changed, 22 insertions(+), 22 deletions(-)
rename src/main/java/net/neoforged/neoforge/common/conditions/{FlagCondition.java => FeatureFlagsEnabledCondition.java} (61%)
diff --git a/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java b/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java
index bbdffc2b55..d9ad69fcf7 100644
--- a/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java
+++ b/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java
@@ -87,7 +87,7 @@
import net.neoforged.neoforge.common.advancements.critereon.SnowBootsEntityPredicate;
import net.neoforged.neoforge.common.conditions.AndCondition;
import net.neoforged.neoforge.common.conditions.FalseCondition;
-import net.neoforged.neoforge.common.conditions.FlagCondition;
+import net.neoforged.neoforge.common.conditions.FeatureFlagsEnabledCondition;
import net.neoforged.neoforge.common.conditions.ICondition;
import net.neoforged.neoforge.common.conditions.ItemExistsCondition;
import net.neoforged.neoforge.common.conditions.ModLoadedCondition;
@@ -386,7 +386,7 @@ public class NeoForgeMod {
public static final DeferredHolder, MapCodec> OR_CONDITION = CONDITION_CODECS.register("or", () -> OrCondition.CODEC);
public static final DeferredHolder, MapCodec> TAG_EMPTY_CONDITION = CONDITION_CODECS.register("tag_empty", () -> TagEmptyCondition.CODEC);
public static final DeferredHolder, MapCodec> TRUE_CONDITION = CONDITION_CODECS.register("true", () -> TrueCondition.CODEC);
- public static final DeferredHolder, MapCodec> FEATURE_FLAG_CONDITION = CONDITION_CODECS.register("feature_flags", () -> FlagCondition.CODEC);
+ public static final DeferredHolder, MapCodec> FEATURE_FLAGS_ENABLED_CONDITION = CONDITION_CODECS.register("feature_flags_enabled", () -> FeatureFlagsEnabledCondition.CODEC);
private static final DeferredRegister> ENTITY_PREDICATE_CODECS = DeferredRegister.create(Registries.ENTITY_SUB_PREDICATE_TYPE, NeoForgeVersion.MOD_ID);
public static final DeferredHolder, MapCodec> PIGLIN_NEUTRAL_ARMOR_PREDICATE = ENTITY_PREDICATE_CODECS.register("piglin_neutral_armor", () -> PiglinNeutralArmorEntityPredicate.CODEC);
diff --git a/src/main/java/net/neoforged/neoforge/common/conditions/FlagCondition.java b/src/main/java/net/neoforged/neoforge/common/conditions/FeatureFlagsEnabledCondition.java
similarity index 61%
rename from src/main/java/net/neoforged/neoforge/common/conditions/FlagCondition.java
rename to src/main/java/net/neoforged/neoforge/common/conditions/FeatureFlagsEnabledCondition.java
index a36f96db34..347cebd8d9 100644
--- a/src/main/java/net/neoforged/neoforge/common/conditions/FlagCondition.java
+++ b/src/main/java/net/neoforged/neoforge/common/conditions/FeatureFlagsEnabledCondition.java
@@ -17,15 +17,15 @@
*
* @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 CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
- FeatureFlags.CODEC.fieldOf("flags").forGetter(condition -> condition.requiredFeatures)).apply(instance, FlagCondition::new));
+public final class FeatureFlagsEnabledCondition implements ICondition {
+ public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
+ FeatureFlags.CODEC.fieldOf("flags").forGetter(condition -> condition.requiredFeatures)).apply(instance, FeatureFlagsEnabledCondition::new));
private final FeatureFlagSet requiredFeatures;
- private FlagCondition(FeatureFlagSet requiredFeatures) {
+ private FeatureFlagsEnabledCondition(FeatureFlagSet requiredFeatures) {
if (requiredFeatures.isEmpty()) {
- throw new IllegalArgumentException("FlagCondition requires a non-empty feature flag set");
+ throw new IllegalArgumentException("FeatureFlagsEnabledCondition requires a non-empty feature flag set");
}
this.requiredFeatures = requiredFeatures;
}
@@ -41,17 +41,17 @@ public MapCodec extends ICondition> codec() {
}
public static ICondition of(FeatureFlagSet requiredFeatures) {
- return new FlagCondition(requiredFeatures);
+ return new FeatureFlagsEnabledCondition(requiredFeatures);
}
public static ICondition of(FeatureFlag... requiredFlags) {
if (requiredFlags.length == 0) {
- throw new IllegalArgumentException("FlagCondition requires at least one feature flag.");
+ throw new IllegalArgumentException("FeatureFlagsEnabledCondition requires at least one feature flag.");
}
if (requiredFlags.length == 1) {
- return new FlagCondition(FeatureFlagSet.of(requiredFlags[0]));
+ return new FeatureFlagsEnabledCondition(FeatureFlagSet.of(requiredFlags[0]));
} else {
- return new FlagCondition(FeatureFlagSet.of(requiredFlags[1], ArrayUtils.remove(requiredFlags, 0)));
+ return new FeatureFlagsEnabledCondition(FeatureFlagSet.of(requiredFlags[0], ArrayUtils.remove(requiredFlags, 0)));
}
}
}
diff --git a/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java b/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
index 51504fb7dd..a1a0f9e0d3 100644
--- a/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
+++ b/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
@@ -44,11 +44,11 @@ default ICondition tagEmpty(TagKey
- tag) {
return new TagEmptyCondition(tag.location());
}
- default ICondition featureFlags(FeatureFlagSet requiredFeatures) {
- return FlagCondition.of(requiredFeatures);
+ default ICondition featureFlagsEnabled(FeatureFlagSet requiredFeatures) {
+ return FeatureFlagsEnabledCondition.of(requiredFeatures);
}
- default ICondition isFeatureEnabled(FeatureFlag... requiredFlags) {
- return FlagCondition.of(requiredFlags);
+ default ICondition featureFlagsEnabled(FeatureFlag... requiredFlags) {
+ return FeatureFlagsEnabledCondition.of(requiredFlags);
}
}
diff --git a/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/diamonds_from_dirt.json b/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/diamonds_from_dirt.json
index 2228d43a8b..e0d40e6e34 100644
--- a/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/diamonds_from_dirt.json
+++ b/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/diamonds_from_dirt.json
@@ -1,7 +1,7 @@
{
"neoforge:conditions": [
{
- "type": "neoforge:feature_flags",
+ "type": "neoforge:feature_flags_enabled",
"flags": [
"custom_feature_flags_pack_test:test_flag"
]
diff --git a/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json b/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
index a1ce5d6894..6e59abc2fd 100644
--- a/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
+++ b/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
@@ -3,7 +3,7 @@
{
"type": "neoforge:not",
"value": {
- "type": "neoforge:feature_flags",
+ "type": "neoforge:feature_flags_enabled",
"flags": [
"custom_feature_flags_pack_test:test_flag"
]
diff --git a/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/diamonds_from_dirt.json b/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/diamonds_from_dirt.json
index 51550166a3..5accc57d95 100644
--- a/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/diamonds_from_dirt.json
+++ b/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/diamonds_from_dirt.json
@@ -1,7 +1,7 @@
{
"neoforge:conditions": [
{
- "type": "neoforge:feature_flags",
+ "type": "neoforge:feature_flags_enabled",
"flags": [
"custom_feature_flags_pack_test:test_flag"
]
diff --git a/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json b/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
index 8979c4c29d..e915b1c0e5 100644
--- a/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
+++ b/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
@@ -3,7 +3,7 @@
{
"type": "neoforge:not",
"value": {
- "type": "neoforge:feature_flags",
+ "type": "neoforge:feature_flags_enabled",
"flags": [
"custom_feature_flags_pack_test:test_flag"
]
diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java b/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
index 023534631a..dd14798f79 100644
--- a/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
+++ b/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
@@ -24,7 +24,7 @@
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.Tags;
-import net.neoforged.neoforge.common.conditions.FlagCondition;
+import net.neoforged.neoforge.common.conditions.FeatureFlagsEnabledCondition;
import net.neoforged.neoforge.common.conditions.NotCondition;
import net.neoforged.neoforge.event.AddPackFindersEvent;
import net.neoforged.neoforge.event.server.ServerStartedEvent;
@@ -123,13 +123,13 @@ protected void buildRecipes() {
shapeless(RecipeCategory.MISC, Items.DIAMOND)
.requires(ItemTags.DIRT)
.unlockedBy("has_dirt", has(ItemTags.DIRT))
- .save(output.withConditions(FlagCondition.of(flag)), enabledRecipeName);
+ .save(output.withConditions(FeatureFlagsEnabledCondition.of(flag)), enabledRecipeName);
// recipe available when above flag is disabled
shapeless(RecipeCategory.MISC, Items.DIRT)
.requires(Tags.Items.GEMS_DIAMOND)
.unlockedBy("has_diamond", has(Tags.Items.GEMS_DIAMOND))
- .save(output.withConditions(new NotCondition(FlagCondition.of(flag))), disabledRecipeName);
+ .save(output.withConditions(new NotCondition(FeatureFlagsEnabledCondition.of(flag))), disabledRecipeName);
}
};
}
From fc5a7a94a4bc06f7e9af4104a62d4fc81fad5a1c Mon Sep 17 00:00:00 2001
From: Technici4n <13494793+Technici4n@users.noreply.github.com>
Date: Wed, 27 Nov 2024 17:09:09 +0100
Subject: [PATCH 3/4] More cleanup
---
.../FeatureFlagsEnabledCondition.java | 29 ++++---------------
.../common/conditions/IConditionBuilder.java | 12 ++++++--
.../debug/data/CustomFeatureFlagsTests.java | 16 ++++++----
3 files changed, 25 insertions(+), 32 deletions(-)
diff --git a/src/main/java/net/neoforged/neoforge/common/conditions/FeatureFlagsEnabledCondition.java b/src/main/java/net/neoforged/neoforge/common/conditions/FeatureFlagsEnabledCondition.java
index 347cebd8d9..6eb0aef8ab 100644
--- a/src/main/java/net/neoforged/neoforge/common/conditions/FeatureFlagsEnabledCondition.java
+++ b/src/main/java/net/neoforged/neoforge/common/conditions/FeatureFlagsEnabledCondition.java
@@ -10,48 +10,29 @@
import net.minecraft.world.flag.FeatureFlag;
import net.minecraft.world.flag.FeatureFlagSet;
import net.minecraft.world.flag.FeatureFlags;
-import org.apache.commons.lang3.ArrayUtils;
/**
* Condition checking that a set of {@link FeatureFlag feature flags} are enabled.
*
* @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 FeatureFlagsEnabledCondition implements ICondition {
+public record FeatureFlagsEnabledCondition(FeatureFlagSet flags) implements ICondition {
public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
- FeatureFlags.CODEC.fieldOf("flags").forGetter(condition -> condition.requiredFeatures)).apply(instance, FeatureFlagsEnabledCondition::new));
+ FeatureFlags.CODEC.fieldOf("flags").forGetter(condition -> condition.flags)).apply(instance, FeatureFlagsEnabledCondition::new));
- private final FeatureFlagSet requiredFeatures;
-
- private FeatureFlagsEnabledCondition(FeatureFlagSet requiredFeatures) {
- if (requiredFeatures.isEmpty()) {
+ public FeatureFlagsEnabledCondition {
+ if (flags.isEmpty()) {
throw new IllegalArgumentException("FeatureFlagsEnabledCondition requires a non-empty feature flag set");
}
- this.requiredFeatures = requiredFeatures;
}
@Override
public boolean test(IContext context) {
- return requiredFeatures.isSubsetOf(context.enabledFeatures());
+ return flags.isSubsetOf(context.enabledFeatures());
}
@Override
public MapCodec extends ICondition> codec() {
return CODEC;
}
-
- public static ICondition of(FeatureFlagSet requiredFeatures) {
- return new FeatureFlagsEnabledCondition(requiredFeatures);
- }
-
- public static ICondition of(FeatureFlag... requiredFlags) {
- if (requiredFlags.length == 0) {
- throw new IllegalArgumentException("FeatureFlagsEnabledCondition requires at least one feature flag.");
- }
- if (requiredFlags.length == 1) {
- return new FeatureFlagsEnabledCondition(FeatureFlagSet.of(requiredFlags[0]));
- } else {
- return new FeatureFlagsEnabledCondition(FeatureFlagSet.of(requiredFlags[0], ArrayUtils.remove(requiredFlags, 0)));
- }
- }
}
diff --git a/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java b/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
index a1a0f9e0d3..ef10000e02 100644
--- a/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
+++ b/src/main/java/net/neoforged/neoforge/common/conditions/IConditionBuilder.java
@@ -10,6 +10,7 @@
import net.minecraft.world.flag.FeatureFlag;
import net.minecraft.world.flag.FeatureFlagSet;
import net.minecraft.world.item.Item;
+import org.apache.commons.lang3.ArrayUtils;
public interface IConditionBuilder {
default ICondition and(ICondition... values) {
@@ -45,10 +46,17 @@ default ICondition tagEmpty(TagKey
- tag) {
}
default ICondition featureFlagsEnabled(FeatureFlagSet requiredFeatures) {
- return FeatureFlagsEnabledCondition.of(requiredFeatures);
+ return new FeatureFlagsEnabledCondition(requiredFeatures);
}
default ICondition featureFlagsEnabled(FeatureFlag... requiredFlags) {
- return FeatureFlagsEnabledCondition.of(requiredFlags);
+ if (requiredFlags.length == 0) {
+ throw new IllegalArgumentException("FeatureFlagsEnabledCondition requires at least one feature flag.");
+ }
+ if (requiredFlags.length == 1) {
+ return new FeatureFlagsEnabledCondition(FeatureFlagSet.of(requiredFlags[0]));
+ } else {
+ return new FeatureFlagsEnabledCondition(FeatureFlagSet.of(requiredFlags[0], ArrayUtils.remove(requiredFlags, 0)));
+ }
}
}
diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java b/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
index dd14798f79..510a3d8c5a 100644
--- a/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
+++ b/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
@@ -24,8 +24,7 @@
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.Tags;
-import net.neoforged.neoforge.common.conditions.FeatureFlagsEnabledCondition;
-import net.neoforged.neoforge.common.conditions.NotCondition;
+import net.neoforged.neoforge.common.conditions.IConditionBuilder;
import net.neoforged.neoforge.event.AddPackFindersEvent;
import net.neoforged.neoforge.event.server.ServerStartedEvent;
import net.neoforged.neoforge.registries.DeferredItem;
@@ -116,22 +115,27 @@ static void testFlagCondition(DynamicTest test, RegistrationHelper reg) {
reg.addProvider(event -> new RecipeProvider.Runner(event.getGenerator().getPackOutput(), event.getLookupProvider()) {
@Override
protected RecipeProvider createRecipeProvider(HolderLookup.Provider registries, RecipeOutput output) {
- return new RecipeProvider(registries, output) {
+ class Provider extends RecipeProvider implements IConditionBuilder {
+ protected Provider(HolderLookup.Provider p_360573_, RecipeOutput p_360872_) {
+ super(p_360573_, p_360872_);
+ }
+
@Override
protected void buildRecipes() {
// recipe available when above flag is enabled
shapeless(RecipeCategory.MISC, Items.DIAMOND)
.requires(ItemTags.DIRT)
.unlockedBy("has_dirt", has(ItemTags.DIRT))
- .save(output.withConditions(FeatureFlagsEnabledCondition.of(flag)), enabledRecipeName);
+ .save(output.withConditions(featureFlagsEnabled(flag)), enabledRecipeName);
// recipe available when above flag is disabled
shapeless(RecipeCategory.MISC, Items.DIRT)
.requires(Tags.Items.GEMS_DIAMOND)
.unlockedBy("has_diamond", has(Tags.Items.GEMS_DIAMOND))
- .save(output.withConditions(new NotCondition(FeatureFlagsEnabledCondition.of(flag))), disabledRecipeName);
+ .save(output.withConditions(not(featureFlagsEnabled(flag))), disabledRecipeName);
}
- };
+ }
+ return new Provider(registries, output);
}
@Override
From 58eb1a52723d5a3c270f32146b25d0e0be51c12e Mon Sep 17 00:00:00 2001
From: Technici4n <13494793+Technici4n@users.noreply.github.com>
Date: Wed, 27 Nov 2024 23:06:12 +0100
Subject: [PATCH 4/4] Remove redundant test
---
.../recipes/misc/dirt_from_diamonds.json | 43 -------------------
.../recipe/dirt_from_diamonds.json | 22 ----------
.../debug/data/CustomFeatureFlagsTests.java | 15 -------
3 files changed, 80 deletions(-)
delete mode 100644 tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
delete mode 100644 tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
diff --git a/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json b/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
deleted file mode 100644
index 6e59abc2fd..0000000000
--- a/tests/src/generated/resources/data/neotests_test_flag_condition/advancement/recipes/misc/dirt_from_diamonds.json
+++ /dev/null
@@ -1,43 +0,0 @@
-{
- "neoforge:conditions": [
- {
- "type": "neoforge:not",
- "value": {
- "type": "neoforge:feature_flags_enabled",
- "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"
- ]
- }
-}
\ No newline at end of file
diff --git a/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json b/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
deleted file mode 100644
index e915b1c0e5..0000000000
--- a/tests/src/generated/resources/data/neotests_test_flag_condition/recipe/dirt_from_diamonds.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "neoforge:conditions": [
- {
- "type": "neoforge:not",
- "value": {
- "type": "neoforge:feature_flags_enabled",
- "flags": [
- "custom_feature_flags_pack_test:test_flag"
- ]
- }
- }
- ],
- "type": "minecraft:crafting_shapeless",
- "category": "misc",
- "ingredients": [
- "#c:gems/diamond"
- ],
- "result": {
- "count": 1,
- "id": "minecraft:dirt"
- }
-}
\ No newline at end of file
diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java b/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
index 510a3d8c5a..10419da329 100644
--- a/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
+++ b/tests/src/main/java/net/neoforged/neoforge/debug/data/CustomFeatureFlagsTests.java
@@ -23,7 +23,6 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
-import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.common.conditions.IConditionBuilder;
import net.neoforged.neoforge.event.AddPackFindersEvent;
import net.neoforged.neoforge.event.server.ServerStartedEvent;
@@ -110,7 +109,6 @@ static void testFlagCondition(DynamicTest test, RegistrationHelper reg) {
var modId = reg.modId();
var enabledRecipeName = ResourceKey.create(Registries.RECIPE, ResourceLocation.fromNamespaceAndPath(modId, "diamonds_from_dirt"));
- var disabledRecipeName = ResourceKey.create(Registries.RECIPE, ResourceLocation.fromNamespaceAndPath(modId, "dirt_from_diamonds"));
reg.addProvider(event -> new RecipeProvider.Runner(event.getGenerator().getPackOutput(), event.getLookupProvider()) {
@Override
@@ -127,12 +125,6 @@ protected void buildRecipes() {
.requires(ItemTags.DIRT)
.unlockedBy("has_dirt", has(ItemTags.DIRT))
.save(output.withConditions(featureFlagsEnabled(flag)), enabledRecipeName);
-
- // recipe available when above flag is disabled
- shapeless(RecipeCategory.MISC, Items.DIRT)
- .requires(Tags.Items.GEMS_DIAMOND)
- .unlockedBy("has_diamond", has(Tags.Items.GEMS_DIAMOND))
- .save(output.withConditions(not(featureFlagsEnabled(flag))), disabledRecipeName);
}
}
return new Provider(registries, output);
@@ -149,22 +141,15 @@ public String getName() {
var isFlagEnabled = server.getWorldData().enabledFeatures().contains(flag);
var recipeMap = server.getRecipeManager().recipeMap();
var hasEnabledRecipe = recipeMap.byKey(enabledRecipeName) != null;
- var hasDisabledRecipe = recipeMap.byKey(disabledRecipeName) != null;
if (isFlagEnabled) {
if (!hasEnabledRecipe) {
test.fail("Missing recipe '" + enabledRecipeName.location() + "', This should be enabled due to our flag '" + flagName + "' being enabled");
}
- if (hasDisabledRecipe) {
- test.fail("Found recipe '" + disabledRecipeName.location() + "', This should be disabled due to our flag '" + flagName + "' being disabled");
- }
} else {
if (hasEnabledRecipe) {
test.fail("Found recipe '" + enabledRecipeName.location() + "', This should be disabled due to our flag '" + flagName + "' being enabled");
}
- if (!hasDisabledRecipe) {
- test.fail("Missing recipe '" + disabledRecipeName.location() + "', This should be enabled due to our flag '" + flagName + "' being disabled");
- }
}
test.pass();