generated from NeoForgeMDKs/MDK-1.21-NeoGradle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MCZME
committed
Aug 16, 2024
1 parent
39925ed
commit 6519e91
Showing
1 changed file
with
28 additions
and
0 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
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) | ||
); | ||
} | ||
|
||
} |