Skip to content

Commit 261daad

Browse files
committed
Make ChemicalStack#BOXED_OPTIONAL_CODEC persist the type of the chemical for empty stacks
1 parent 7199e81 commit 261daad

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/api/java/mekanism/api/chemical/ChemicalStack.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import net.minecraft.tags.TagKey;
4040
import net.minecraft.util.ExtraCodecs;
4141
import net.minecraft.world.item.ItemStack;
42+
import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs;
4243
import org.jetbrains.annotations.Nullable;
4344

4445
@NothingNullByDefault
@@ -161,15 +162,19 @@ public void encode(RegistryFriendlyByteBuf buf, STACK stack) {
161162
case SLURRY -> SlurryStack.MAP_CODEC;
162163
});
163164
/**
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.
165167
*
166168
* @see ChemicalType
167169
* @see mekanism.api.chemical.merged.BoxedChemicalStack
168170
* @since 10.6.0
169171
*/
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));
173178
/**
174179
* StreamCodec to get any kind of chemical stack (that does not accept empty stacks), based on a "chemicalType" field.
175180
*

src/main/java/mekanism/common/CommonPlayerTracker.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import mekanism.common.block.BlockCardboardBox;
77
import mekanism.common.block.BlockMekanism;
88
import mekanism.common.lib.radiation.RadiationManager;
9-
import mekanism.common.network.PacketUtils;
109
import mekanism.common.network.to_client.player_data.PacketPlayerData;
1110
import mekanism.common.network.to_client.player_data.PacketResetPlayerClient;
1211
import mekanism.common.network.to_client.radiation.PacketPlayerRadiationData;

0 commit comments

Comments
 (0)