Skip to content

Commit 84a3f2b

Browse files
committed
Update chemical stacks bringing them more in line with modern item and fluid stacks
1 parent 23c0ef8 commit 84a3f2b

File tree

97 files changed

+1299
-1240
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1299
-1240
lines changed

src/api/java/mekanism/api/JsonConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Class for storing constants that are used in various JSON serializers we have, to reduce the chances of typos
55
*/
6-
public class JsonConstants {
6+
public class JsonConstants {//TODO - 1.20.5: Combine with NBTConstants into a singular SerializationConstants, and then make all the values snake case
77

88
private JsonConstants() {
99
}

src/api/java/mekanism/api/SerializerHelper.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,10 @@
1414
import java.util.stream.Collectors;
1515
import java.util.stream.Stream;
1616
import mekanism.api.annotations.NothingNullByDefault;
17-
import mekanism.api.chemical.ChemicalStack;
18-
import mekanism.api.chemical.ChemicalType;
19-
import mekanism.api.chemical.ChemicalUtils;
2017
import net.minecraft.Util;
21-
import net.minecraft.core.Holder;
22-
import net.minecraft.core.component.DataComponentPatch;
23-
import net.minecraft.core.registries.BuiltInRegistries;
24-
import net.minecraft.util.ExtraCodecs;
25-
import net.minecraft.world.level.material.Fluid;
26-
import net.minecraft.world.level.material.Fluids;
27-
import net.neoforged.neoforge.common.crafting.CraftingHelper;
28-
import net.neoforged.neoforge.fluids.FluidStack;
2918
import org.jetbrains.annotations.NotNull;
3019

20+
//TODO - 1.20.5: Update the wiki docs to fix the syntax
3121
@NothingNullByDefault
3222
public class SerializerHelper {
3323

@@ -50,32 +40,6 @@ private SerializerHelper() {
5040
return Codec.LONG.flatXmap(checker, checker);
5141
});
5242

53-
/**
54-
* Fluid Codec which makes extra sure we don't end up with an empty/invalid fluid
55-
*/
56-
private static final Codec<Holder<Fluid>> NON_EMPTY_FLUID_CODEC = BuiltInRegistries.FLUID.holderByNameCodec().validate(holder ->
57-
holder.is(Fluids.EMPTY.builtInRegistryHolder()) ? DataResult.error(() -> "Fluid must not be minecraft:empty") : DataResult.success(holder));
58-
59-
/**
60-
* Fluidstack codec to maintain compatibility with our old json
61-
*/
62-
//TODO - 1.20.5: Re-evaluate this and probably switch to just using FluidStack#CODEC even if it changes the key from fluid to id
63-
public static final Codec<FluidStack> FLUIDSTACK_CODEC = RecordCodecBuilder.create(instance -> instance.group(
64-
NON_EMPTY_FLUID_CODEC.fieldOf(JsonConstants.FLUID).forGetter(FluidStack::getFluidHolder),
65-
ExtraCodecs.POSITIVE_INT.fieldOf(JsonConstants.AMOUNT).forGetter(FluidStack::getAmount),
66-
DataComponentPatch.CODEC.optionalFieldOf("components", DataComponentPatch.EMPTY).forGetter(stack -> stack.getComponents().asPatch())
67-
).apply(instance, FluidStack::new));
68-
69-
/**
70-
* Codec to get any kind of chemical stack, based on a "chemicalType" field. See also {@link ChemicalType}
71-
*/
72-
public static final Codec<ChemicalStack<?>> BOXED_CHEMICALSTACK_CODEC = ChemicalType.CODEC.dispatch(JsonConstants.CHEMICAL_TYPE, ChemicalType::getTypeFor, type -> switch (type) {
73-
case GAS -> ChemicalUtils.GAS_STACK_CODEC;
74-
case INFUSION -> ChemicalUtils.INFUSION_STACK_CODEC;
75-
case PIGMENT -> ChemicalUtils.PIGMENT_STACK_CODEC;
76-
case SLURRY -> ChemicalUtils.SLURRY_STACK_CODEC;
77-
});
78-
7943
/**
8044
* Generate a RecordCodecBuilder which is required only if the 'primary' is present. If this field is present, it will be returned regardless. Does not eat errors
8145
*

src/api/java/mekanism/api/chemical/BasicChemicalTank.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private void setStack(STACK stack, boolean validateStack) {
9191

9292
@Override
9393
public STACK insert(@NotNull STACK stack, Action action, AutomationType automationType) {
94-
if (stack.isEmpty() || !isValid(stack) || !canInsert.test(stack.getType(), automationType)) {
94+
if (stack.isEmpty() || !isValid(stack) || !canInsert.test(stack.getChemical(), automationType)) {
9595
//"Fail quick" if the given stack is empty, or we can never insert the chemical or currently are unable to insert it
9696
return stack;
9797
}
@@ -124,7 +124,7 @@ public STACK insert(@NotNull STACK stack, Action action, AutomationType automati
124124

125125
@Override
126126
public STACK extract(long amount, Action action, AutomationType automationType) {
127-
if (isEmpty() || amount < 1 || !canExtract.test(stored.getType(), automationType)) {
127+
if (isEmpty() || amount < 1 || !canExtract.test(stored.getChemical(), automationType)) {
128128
//"Fail quick" if we don't can never extract from this tank, have a chemical stored, or the amount being requested is less than one
129129
return getEmptyStack();
130130
}
@@ -145,7 +145,7 @@ public STACK extract(long amount, Action action, AutomationType automationType)
145145

146146
@Override
147147
public boolean isValid(STACK stack) {
148-
return getAttributeValidator().process(stack) && validator.test(stack.getType());
148+
return getAttributeValidator().process(stack) && validator.test(stack.getChemical());
149149
}
150150

151151
/**
@@ -222,7 +222,7 @@ public long getStored() {
222222
*/
223223
@Override
224224
public CHEMICAL getType() {
225-
return stored.getType();
225+
return stored.getChemical();
226226
}
227227

228228
/**
@@ -242,7 +242,7 @@ public boolean isTypeEqual(STACK other) {
242242
*/
243243
@Override
244244
public boolean isTypeEqual(CHEMICAL other) {
245-
return stored.isTypeEqual(other);
245+
return stored.is(other);
246246
}
247247

248248
@Override
@@ -271,7 +271,7 @@ public ChemicalAttributeValidator getAttributeValidator() {
271271
public CompoundTag serializeNBT(HolderLookup.Provider provider) {
272272
CompoundTag nbt = new CompoundTag();
273273
if (!isEmpty()) {
274-
nbt.put(NBTConstants.STORED, stored.write(new CompoundTag()));
274+
nbt.put(NBTConstants.STORED, stored.save(provider));
275275
}
276276
return nbt;
277277
}
@@ -283,7 +283,7 @@ public CompoundTag serializeNBT(HolderLookup.Provider provider) {
283283
*/
284284
@Override
285285
public boolean isCompatible(IChemicalTank<CHEMICAL, STACK> other) {
286-
return getClass() == other.getClass() && stored.isStackIdentical(((BasicChemicalTank<CHEMICAL, STACK>) other).stored);
286+
return getClass() == other.getClass() && stored.equals(other.getStack());
287287
}
288288

289289
@Override

src/api/java/mekanism/api/chemical/Chemical.java

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package mekanism.api.chemical;
22

3+
import com.mojang.serialization.Codec;
4+
import com.mojang.serialization.DataResult;
5+
import io.netty.handler.codec.DecoderException;
6+
import io.netty.handler.codec.EncoderException;
37
import java.util.Collection;
48
import java.util.HashMap;
59
import java.util.Map;
610
import java.util.stream.Stream;
11+
import mekanism.api.JsonConstants;
12+
import mekanism.api.MekanismAPI;
713
import mekanism.api.annotations.NothingNullByDefault;
814
import mekanism.api.chemical.attribute.ChemicalAttribute;
915
import mekanism.api.chemical.attribute.IChemicalAttributeContainer;
1016
import mekanism.api.chemical.gas.attribute.GasAttributes.Radiation;
1117
import mekanism.api.providers.IChemicalProvider;
1218
import mekanism.api.text.TextComponentUtil;
1319
import net.minecraft.core.Registry;
14-
import net.minecraft.nbt.CompoundTag;
20+
import net.minecraft.network.RegistryFriendlyByteBuf;
1521
import net.minecraft.network.chat.Component;
22+
import net.minecraft.network.codec.ByteBufCodecs;
23+
import net.minecraft.network.codec.StreamCodec;
1624
import net.minecraft.resources.ResourceLocation;
1725
import net.minecraft.tags.TagKey;
1826
import org.jetbrains.annotations.NotNull;
@@ -21,6 +29,67 @@
2129
@NothingNullByDefault
2230
public abstract class Chemical<CHEMICAL extends Chemical<CHEMICAL>> implements IChemicalProvider<CHEMICAL>, IChemicalAttributeContainer<CHEMICAL> {
2331

32+
/**
33+
* Codec to get any kind of chemical, based on a "chemicalType" field.
34+
*
35+
* @see ChemicalType
36+
* @see mekanism.api.chemical.merged.BoxedChemical
37+
* @since 10.6.0
38+
*/
39+
public static final Codec<Chemical<?>> BOXED_OPTIONAL_CODEC = ChemicalType.CODEC.dispatch(JsonConstants.CHEMICAL_TYPE, ChemicalType::getTypeFor, type -> switch (type) {
40+
case GAS -> MekanismAPI.GAS_REGISTRY.byNameCodec().fieldOf(JsonConstants.GAS);
41+
case INFUSION -> MekanismAPI.INFUSE_TYPE_REGISTRY.byNameCodec().fieldOf(JsonConstants.INFUSE_TYPE);
42+
case PIGMENT -> MekanismAPI.PIGMENT_REGISTRY.byNameCodec().fieldOf(JsonConstants.PIGMENT);
43+
case SLURRY -> MekanismAPI.SLURRY_REGISTRY.byNameCodec().fieldOf(JsonConstants.SLURRY);
44+
});
45+
/**
46+
* Codec to get any kind of chemical (that does not accept empty types), based on a "chemicalType" field.
47+
*
48+
* @see ChemicalType
49+
* @see mekanism.api.chemical.merged.BoxedChemical
50+
* @since 10.6.0
51+
*/
52+
public static final Codec<Chemical<?>> BOXED_CODEC = BOXED_OPTIONAL_CODEC.validate(chemical -> chemical.isEmptyType() ? DataResult.error(() -> "Chemical must not be mekanism:empty") : DataResult.success(chemical));
53+
/**
54+
* StreamCodec to get any kind of chemical stack, based on a "chemicalType" field.
55+
*
56+
* @see ChemicalType
57+
* @see mekanism.api.chemical.merged.BoxedChemical
58+
* @since 10.6.0
59+
*/
60+
public static final StreamCodec<RegistryFriendlyByteBuf, Chemical<?>> BOXED_OPTIONAL_STREAM_CODEC = ChemicalType.STREAM_CODEC.<RegistryFriendlyByteBuf>cast()
61+
.dispatch(ChemicalType::getTypeFor, type -> switch (type) {
62+
case GAS -> ByteBufCodecs.registry(MekanismAPI.GAS_REGISTRY_NAME);
63+
case INFUSION -> ByteBufCodecs.registry(MekanismAPI.INFUSE_TYPE_REGISTRY_NAME);
64+
case PIGMENT -> ByteBufCodecs.registry(MekanismAPI.PIGMENT_REGISTRY_NAME);
65+
case SLURRY -> ByteBufCodecs.registry(MekanismAPI.SLURRY_REGISTRY_NAME);
66+
});
67+
/**
68+
* StreamCodec to get any kind of chemical (that does not accept the empty type), based on a "chemicalType" field.
69+
*
70+
* @see ChemicalType
71+
* @see mekanism.api.chemical.merged.BoxedChemical
72+
* @since 10.6.0
73+
*/
74+
public static final StreamCodec<RegistryFriendlyByteBuf, Chemical<?>> BOXED_STREAM_CODEC = new StreamCodec<>() {
75+
@Override
76+
public Chemical<?> decode(RegistryFriendlyByteBuf buf) {
77+
Chemical<?> chemical = BOXED_OPTIONAL_STREAM_CODEC.decode(buf);
78+
if (chemical.isEmptyType()) {
79+
throw new DecoderException("Empty Chemicals are not allowed");
80+
}
81+
return chemical;
82+
}
83+
84+
@Override
85+
public void encode(RegistryFriendlyByteBuf buf, Chemical<?> chemical) {
86+
if (chemical.isEmptyType()) {
87+
throw new EncoderException("Empty Chemicals are not allowed");
88+
}
89+
BOXED_OPTIONAL_STREAM_CODEC.encode(buf, chemical);
90+
}
91+
};
92+
2493
private final Map<Class<? extends ChemicalAttribute>, ChemicalAttribute> attributeMap;
2594

2695
private final ResourceLocation iconLocation;
@@ -116,15 +185,6 @@ public Collection<Class<? extends ChemicalAttribute>> getAttributeTypes() {
116185
return attributeMap.keySet();
117186
}
118187

119-
/**
120-
* Writes this Chemical to a defined tag compound.
121-
*
122-
* @param nbtTags - tag compound to write this Chemical to
123-
*
124-
* @return the tag compound this Chemical was written to
125-
*/
126-
public abstract CompoundTag write(CompoundTag nbtTags);
127-
128188
/**
129189
* Gets the default translation key for this chemical.
130190
*/

0 commit comments

Comments
 (0)