Skip to content

Commit

Permalink
Fix conflicting resource conditions
Browse files Browse the repository at this point in the history
Fixes #154
Fixes #129
  • Loading branch information
Su5eD committed Sep 22, 2024
1 parent ba60825 commit 59440bc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static MapCodec<Ingredient> makeIngredientMapCodec(MapCodec<Ingredient> o
CustomIngredientImpl.CODEC,
CustomIngredient::getSerializer,
s -> s.getCodec(true),
original
original,
"type" // NeoForge ingredient type spec
)
.xmap(
e -> e.map(c -> new NeoCustomIngredientWrapper(c).toVanilla(), Function.identity()),
Expand All @@ -46,7 +47,7 @@ public static MapCodec<Ingredient> makeIngredientMapCodec(MapCodec<Ingredient> o
}

// Stolen from NeoForgeExtraCodecs because they don't support custom type keys
public static <A, E, B> MapCodec<Either<E, B>> dispatchMapOrElse(String typeKey, Codec<A> typeCodec, Function<? super E, ? extends A> type, Function<? super A, ? extends MapCodec<? extends E>> codec, MapCodec<B> fallbackCodec) {
public static <A, E, B> MapCodec<Either<E, B>> dispatchMapOrElse(String typeKey, Codec<A> typeCodec, Function<? super E, ? extends A> type, Function<? super A, ? extends MapCodec<? extends E>> codec, MapCodec<B> fallbackCodec, String ignoreKey) {
var dispatchCodec = typeCodec.dispatchMap(typeKey, type, codec);
return new MapCodec<>() {
@Override
Expand All @@ -56,7 +57,7 @@ public <T> Stream<T> keys(DynamicOps<T> ops) {

@Override
public <T> DataResult<Either<E, B>> decode(DynamicOps<T> ops, MapLike<T> input) {
if (input.get(typeKey) != null) {
if (input.get(typeKey) != null && input.get(ignoreKey) == null) {
return dispatchCodec.decode(ops, input).map(Either::left);
} else {
return fallbackCodec.decode(ops, input).map(Either::right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.gson.JsonObject;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import net.neoforged.neoforge.common.conditions.ConditionalOps;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -62,7 +63,7 @@ public void onInitialize() {
public static boolean applyResourceConditions(JsonObject obj, String dataType, ResourceLocation key, @Nullable HolderLookup.Provider registryLookup) {
boolean debugLogEnabled = ResourceConditionsImpl.LOGGER.isDebugEnabled();

if (obj.has(ResourceConditions.CONDITIONS_KEY)) {
if (obj.has(ResourceConditions.CONDITIONS_KEY) && !obj.has(ConditionalOps.DEFAULT_CONDITIONS_KEY)) {
DataResult<ResourceCondition> conditions = ResourceCondition.CONDITION_CODEC.parse(JsonOps.INSTANCE, obj.get(ResourceConditions.CONDITIONS_KEY));

if (conditions.isSuccess()) {
Expand Down

0 comments on commit 59440bc

Please sign in to comment.