|
39 | 39 | import net.minecraft.tags.TagKey; |
40 | 40 | import net.minecraft.util.ExtraCodecs; |
41 | 41 | import net.minecraft.world.item.ItemStack; |
| 42 | +import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs; |
42 | 43 | import org.jetbrains.annotations.Nullable; |
43 | 44 |
|
44 | 45 | @NothingNullByDefault |
@@ -161,15 +162,19 @@ public void encode(RegistryFriendlyByteBuf buf, STACK stack) { |
161 | 162 | case SLURRY -> SlurryStack.MAP_CODEC; |
162 | 163 | }); |
163 | 164 | /** |
164 | | - * Codec to get any kind of chemical stack, based on a "chemicalType" field. |
| 165 | + * Codec to get any kind of chemical stack, based on a "chemicalType" field, serializing them as {@code {chemicalType:"chemicalType"}}. Falls back to the empty stack |
| 166 | + * of the given type. |
165 | 167 | * |
166 | 168 | * @see ChemicalType |
167 | 169 | * @see mekanism.api.chemical.merged.BoxedChemicalStack |
168 | 170 | * @since 10.6.0 |
169 | 171 | */ |
170 | | - //TODO - 1.20.5: Re-evaluate if we want this defaulting to an empty gas stack or to try and get the same stack type as it was? |
171 | | - public static final Codec<ChemicalStack<?>> BOXED_OPTIONAL_CODEC = ExtraCodecs.optionalEmptyMap(BOXED_CODEC).xmap(optional -> optional.orElse(GasStack.EMPTY), |
172 | | - stack -> stack.isEmpty() ? Optional.empty() : Optional.of(stack)); |
| 172 | + public static final Codec<ChemicalStack<?>> BOXED_OPTIONAL_CODEC = NeoForgeExtraCodecs.withAlternative(BOXED_CODEC, ChemicalType.CODEC.xmap(type -> switch (type) { |
| 173 | + case GAS -> GasStack.EMPTY; |
| 174 | + case INFUSION -> InfusionStack.EMPTY; |
| 175 | + case PIGMENT -> PigmentStack.EMPTY; |
| 176 | + case SLURRY -> SlurryStack.EMPTY; |
| 177 | + }, ChemicalType::getTypeFor)); |
173 | 178 | /** |
174 | 179 | * StreamCodec to get any kind of chemical stack (that does not accept empty stacks), based on a "chemicalType" field. |
175 | 180 | * |
|
0 commit comments