Skip to content

Commit

Permalink
修复:数据生成中不支持处理null的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MCZME committed Aug 16, 2024
1 parent 39925ed commit 6519e91
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/mczme/lingshi/common/utility/ModCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mczme.lingshi.common.utility;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.OptionalFieldCodec;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.fluids.FluidStack;

import java.util.Optional;

public class ModCodec {

// 序列化工具,可以对null进行处理
public static MapCodec<ItemStack> ModoptionalField(final String name, final Codec<ItemStack> codec, ItemStack defaultValue) {
return new OptionalFieldCodec<>(name, codec, false).xmap(
o -> o.orElse(defaultValue),
a -> a==null ? Optional.empty() : Optional.of(a)
);
}

public static MapCodec<FluidStack> ModoptionalField(final String name, final Codec<FluidStack> codec, FluidStack defaultValue) {
return new OptionalFieldCodec<>(name, codec, false).xmap(
o -> o.orElse(defaultValue),
a -> a==null ? Optional.empty() : Optional.of(a)
);
}

}

0 comments on commit 6519e91

Please sign in to comment.