Skip to content

Commit 9615886

Browse files
committed
Further, reduce duplicate code relating to codecs for the various chemical ingredient types
1 parent 8237564 commit 9615886

24 files changed

+84
-90
lines changed

src/api/java/mekanism/api/recipes/ingredients/chemical/CompoundChemicalIngredient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package mekanism.api.recipes.ingredients.chemical;
22

3+
import com.mojang.serialization.MapCodec;
34
import java.util.List;
5+
import java.util.function.Function;
46
import java.util.stream.Stream;
7+
import mekanism.api.JsonConstants;
58
import mekanism.api.annotations.NothingNullByDefault;
69
import mekanism.api.chemical.Chemical;
10+
import mekanism.api.recipes.ingredients.creator.IChemicalIngredientCreator;
711
import net.neoforged.neoforge.common.crafting.CompoundIngredient;
12+
import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs;
813
import org.jetbrains.annotations.ApiStatus.Internal;
914
import org.jetbrains.annotations.Nullable;
1015

@@ -19,6 +24,17 @@
1924
public abstract non-sealed class CompoundChemicalIngredient<CHEMICAL extends Chemical<CHEMICAL>, INGREDIENT extends IChemicalIngredient<CHEMICAL, INGREDIENT>>
2025
extends ChemicalIngredient<CHEMICAL, INGREDIENT> {
2126

27+
/**
28+
* Helper to create the codec for compound ingredients.
29+
*/
30+
@Internal
31+
protected static <INGREDIENT extends IChemicalIngredient<?, INGREDIENT>, COMPOUND extends CompoundChemicalIngredient<?, INGREDIENT>> MapCodec<COMPOUND> codec(
32+
IChemicalIngredientCreator<?, INGREDIENT> creator, Function<List<INGREDIENT>, COMPOUND> constructor) {
33+
return NeoForgeExtraCodecs.aliasedFieldOf(creator.listCodecMultipleElements(), JsonConstants.CHILDREN, JsonConstants.INGREDIENTS).xmap(
34+
constructor, CompoundChemicalIngredient::children
35+
);
36+
}
37+
2238
private final List<INGREDIENT> children;
2339

2440
/**

src/api/java/mekanism/api/recipes/ingredients/chemical/DifferenceChemicalIngredient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package mekanism.api.recipes.ingredients.chemical;
22

3+
import com.mojang.serialization.MapCodec;
4+
import com.mojang.serialization.codecs.RecordCodecBuilder;
35
import java.util.Objects;
6+
import java.util.function.BiFunction;
47
import java.util.stream.Stream;
8+
import mekanism.api.JsonConstants;
59
import mekanism.api.annotations.NothingNullByDefault;
610
import mekanism.api.chemical.Chemical;
11+
import mekanism.api.recipes.ingredients.creator.IChemicalIngredientCreator;
712
import net.neoforged.neoforge.common.crafting.DifferenceIngredient;
813
import org.jetbrains.annotations.ApiStatus.Internal;
914
import org.jetbrains.annotations.Nullable;
@@ -19,6 +24,18 @@
1924
public abstract non-sealed class DifferenceChemicalIngredient<CHEMICAL extends Chemical<CHEMICAL>, INGREDIENT extends IChemicalIngredient<CHEMICAL, INGREDIENT>>
2025
extends ChemicalIngredient<CHEMICAL, INGREDIENT> {
2126

27+
/**
28+
* Helper to create the codec for difference ingredients.
29+
*/
30+
@Internal
31+
protected static <INGREDIENT extends IChemicalIngredient<?, INGREDIENT>, DIFFERENCE extends DifferenceChemicalIngredient<?, INGREDIENT>> MapCodec<DIFFERENCE> codec(
32+
IChemicalIngredientCreator<?, INGREDIENT> creator, BiFunction<INGREDIENT, INGREDIENT, DIFFERENCE> constructor) {
33+
return RecordCodecBuilder.mapCodec(builder -> builder.group(
34+
creator.codecNonEmpty().fieldOf(JsonConstants.BASE).forGetter(DifferenceChemicalIngredient::base),
35+
creator.codecNonEmpty().fieldOf(JsonConstants.SUBTRACTED).forGetter(DifferenceChemicalIngredient::subtracted)
36+
).apply(builder, constructor));
37+
}
38+
2239
private final INGREDIENT base;
2340
private final INGREDIENT subtracted;
2441

src/api/java/mekanism/api/recipes/ingredients/chemical/IntersectionChemicalIngredient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package mekanism.api.recipes.ingredients.chemical;
22

3+
import com.mojang.serialization.MapCodec;
4+
import com.mojang.serialization.codecs.RecordCodecBuilder;
35
import java.util.List;
46
import java.util.Objects;
7+
import java.util.function.Function;
58
import java.util.stream.Stream;
9+
import mekanism.api.JsonConstants;
610
import mekanism.api.annotations.NothingNullByDefault;
711
import mekanism.api.chemical.Chemical;
12+
import mekanism.api.recipes.ingredients.creator.IChemicalIngredientCreator;
813
import org.jetbrains.annotations.ApiStatus.Internal;
914
import org.jetbrains.annotations.Nullable;
1015

@@ -17,6 +22,17 @@
1722
public abstract non-sealed class IntersectionChemicalIngredient<CHEMICAL extends Chemical<CHEMICAL>, INGREDIENT extends IChemicalIngredient<CHEMICAL, INGREDIENT>>
1823
extends ChemicalIngredient<CHEMICAL, INGREDIENT> {
1924

25+
/**
26+
* Helper to create the codec for intersection ingredients.
27+
*/
28+
@Internal
29+
protected static <INGREDIENT extends IChemicalIngredient<?, INGREDIENT>, INTERSECTION extends IntersectionChemicalIngredient<?, INGREDIENT>> MapCodec<INTERSECTION> codec(
30+
IChemicalIngredientCreator<?, INGREDIENT> creator, Function<List<INGREDIENT>, INTERSECTION> constructor) {
31+
return RecordCodecBuilder.mapCodec(builder -> builder.group(
32+
creator.listCodecMultipleElements().fieldOf(JsonConstants.CHILDREN).forGetter(IntersectionChemicalIngredient::children)
33+
).apply(builder, constructor));
34+
}
35+
2036
private final List<INGREDIENT> children;
2137

2238
/**

src/api/java/mekanism/api/recipes/ingredients/chemical/TagChemicalIngredient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package mekanism.api.recipes.ingredients.chemical;
22

3+
import com.mojang.serialization.MapCodec;
4+
import java.util.function.Function;
35
import java.util.stream.Stream;
6+
import mekanism.api.JsonConstants;
47
import mekanism.api.annotations.NothingNullByDefault;
58
import mekanism.api.chemical.Chemical;
69
import net.minecraft.core.Holder;
710
import net.minecraft.core.HolderSet;
811
import net.minecraft.core.Registry;
12+
import net.minecraft.resources.ResourceKey;
913
import net.minecraft.tags.TagKey;
1014
import org.jetbrains.annotations.ApiStatus.Internal;
1115
import org.jetbrains.annotations.Nullable;
@@ -22,6 +26,15 @@
2226
public abstract non-sealed class TagChemicalIngredient<CHEMICAL extends Chemical<CHEMICAL>, INGREDIENT extends IChemicalIngredient<CHEMICAL, INGREDIENT>>
2327
extends ChemicalIngredient<CHEMICAL, INGREDIENT> {
2428

29+
/**
30+
* Helper to create the codec for tag ingredients.
31+
*/
32+
@Internal
33+
protected static <CHEMICAL extends Chemical<CHEMICAL>, TAG extends TagChemicalIngredient<CHEMICAL, ?>> MapCodec<TAG> codec(
34+
ResourceKey<? extends Registry<CHEMICAL>> registryName, Function<TagKey<CHEMICAL>, TAG> constructor) {
35+
return TagKey.codec(registryName).xmap(constructor, TagChemicalIngredient::tag).fieldOf(JsonConstants.TAG);
36+
}
37+
2538
private final TagKey<CHEMICAL> tag;
2639

2740
/**

src/main/java/mekanism/common/recipe/ingredients/gas/CompoundGasIngredient.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22

33
import com.mojang.serialization.MapCodec;
44
import java.util.List;
5-
import mekanism.api.JsonConstants;
65
import mekanism.api.annotations.NothingNullByDefault;
76
import mekanism.api.chemical.gas.Gas;
87
import mekanism.api.recipes.ingredients.chemical.CompoundChemicalIngredient;
98
import mekanism.api.recipes.ingredients.chemical.IGasIngredient;
109
import mekanism.api.recipes.ingredients.creator.IngredientCreatorAccess;
1110
import mekanism.common.registries.MekanismGasIngredientTypes;
12-
import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs;
1311

1412
@NothingNullByDefault
1513
public final class CompoundGasIngredient extends CompoundChemicalIngredient<Gas, IGasIngredient> implements IGasIngredient {
1614

17-
public static final MapCodec<CompoundGasIngredient> CODEC = NeoForgeExtraCodecs.aliasedFieldOf(IngredientCreatorAccess.gas().listCodecMultipleElements(),
18-
JsonConstants.CHILDREN, JsonConstants.INGREDIENTS).xmap(
19-
CompoundGasIngredient::new, CompoundGasIngredient::children
20-
);
15+
public static final MapCodec<CompoundGasIngredient> CODEC = codec(IngredientCreatorAccess.gas(), CompoundGasIngredient::new);
2116

2217
CompoundGasIngredient(List<IGasIngredient> children) {
2318
super(children);

src/main/java/mekanism/common/recipe/ingredients/gas/DifferenceGasIngredient.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package mekanism.common.recipe.ingredients.gas;
22

33
import com.mojang.serialization.MapCodec;
4-
import com.mojang.serialization.codecs.RecordCodecBuilder;
5-
import mekanism.api.JsonConstants;
64
import mekanism.api.annotations.NothingNullByDefault;
75
import mekanism.api.chemical.gas.Gas;
86
import mekanism.api.recipes.ingredients.chemical.DifferenceChemicalIngredient;
@@ -13,10 +11,7 @@
1311
@NothingNullByDefault
1412
public final class DifferenceGasIngredient extends DifferenceChemicalIngredient<Gas, IGasIngredient> implements IGasIngredient {
1513

16-
public static final MapCodec<DifferenceGasIngredient> CODEC = RecordCodecBuilder.mapCodec(builder -> builder.group(
17-
IngredientCreatorAccess.gas().codecNonEmpty().fieldOf(JsonConstants.BASE).forGetter(DifferenceGasIngredient::base),
18-
IngredientCreatorAccess.gas().codecNonEmpty().fieldOf(JsonConstants.SUBTRACTED).forGetter(DifferenceGasIngredient::subtracted)
19-
).apply(builder, DifferenceGasIngredient::new));
14+
public static final MapCodec<DifferenceGasIngredient> CODEC = codec(IngredientCreatorAccess.gas(), DifferenceGasIngredient::new);
2015

2116
DifferenceGasIngredient(IGasIngredient base, IGasIngredient subtracted) {
2217
super(base, subtracted);

src/main/java/mekanism/common/recipe/ingredients/gas/EmptyGasIngredient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
public final class EmptyGasIngredient extends EmptyChemicalIngredient<Gas, IGasIngredient> implements IGasIngredient {
1212

1313
public static final EmptyGasIngredient INSTANCE = new EmptyGasIngredient();
14-
1514
public static final MapCodec<EmptyGasIngredient> CODEC = MapCodec.unit(INSTANCE);
1615

1716
private EmptyGasIngredient() {

src/main/java/mekanism/common/recipe/ingredients/gas/IntersectionGasIngredient.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package mekanism.common.recipe.ingredients.gas;
22

33
import com.mojang.serialization.MapCodec;
4-
import com.mojang.serialization.codecs.RecordCodecBuilder;
54
import java.util.List;
6-
import mekanism.api.JsonConstants;
75
import mekanism.api.annotations.NothingNullByDefault;
86
import mekanism.api.chemical.gas.Gas;
97
import mekanism.api.recipes.ingredients.chemical.IGasIngredient;
@@ -14,9 +12,7 @@
1412
@NothingNullByDefault
1513
public final class IntersectionGasIngredient extends IntersectionChemicalIngredient<Gas, IGasIngredient> implements IGasIngredient {
1614

17-
public static final MapCodec<IntersectionGasIngredient> CODEC = RecordCodecBuilder.mapCodec(builder -> builder.group(
18-
IngredientCreatorAccess.gas().listCodecMultipleElements().fieldOf(JsonConstants.CHILDREN).forGetter(IntersectionGasIngredient::children)
19-
).apply(builder, IntersectionGasIngredient::new));
15+
public static final MapCodec<IntersectionGasIngredient> CODEC = codec(IngredientCreatorAccess.gas(), IntersectionGasIngredient::new);
2016

2117
IntersectionGasIngredient(List<IGasIngredient> children) {
2218
super(children);

src/main/java/mekanism/common/recipe/ingredients/gas/TagGasIngredient.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mekanism.common.recipe.ingredients.gas;
22

33
import com.mojang.serialization.MapCodec;
4-
import mekanism.api.JsonConstants;
54
import mekanism.api.MekanismAPI;
65
import mekanism.api.annotations.NothingNullByDefault;
76
import mekanism.api.chemical.gas.Gas;
@@ -14,8 +13,7 @@
1413
@NothingNullByDefault
1514
public final class TagGasIngredient extends TagChemicalIngredient<Gas, IGasIngredient> implements IGasIngredient {
1615

17-
public static final MapCodec<TagGasIngredient> CODEC = TagKey.codec(MekanismAPI.GAS_REGISTRY_NAME).xmap(TagGasIngredient::new, TagGasIngredient::tag)
18-
.fieldOf(JsonConstants.TAG);
16+
public static final MapCodec<TagGasIngredient> CODEC = codec(MekanismAPI.GAS_REGISTRY_NAME, TagGasIngredient::new);
1917

2018
TagGasIngredient(TagKey<Gas> tag) {
2119
super(tag);
@@ -27,7 +25,7 @@ public MapCodec<TagGasIngredient> codec() {
2725
}
2826

2927
@Override
30-
public Registry<Gas> registry() {
28+
protected Registry<Gas> registry() {
3129
return MekanismAPI.GAS_REGISTRY;
3230
}
3331
}

src/main/java/mekanism/common/recipe/ingredients/infusion/CompoundInfusionIngredient.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22

33
import com.mojang.serialization.MapCodec;
44
import java.util.List;
5-
import mekanism.api.JsonConstants;
65
import mekanism.api.annotations.NothingNullByDefault;
76
import mekanism.api.chemical.infuse.InfuseType;
87
import mekanism.api.recipes.ingredients.chemical.CompoundChemicalIngredient;
98
import mekanism.api.recipes.ingredients.chemical.IInfusionIngredient;
109
import mekanism.api.recipes.ingredients.creator.IngredientCreatorAccess;
1110
import mekanism.common.registries.MekanismInfusionIngredientTypes;
12-
import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs;
1311

1412
@NothingNullByDefault
1513
public final class CompoundInfusionIngredient extends CompoundChemicalIngredient<InfuseType, IInfusionIngredient> implements IInfusionIngredient {
1614

17-
public static final MapCodec<CompoundInfusionIngredient> CODEC = NeoForgeExtraCodecs.aliasedFieldOf(IngredientCreatorAccess.infusion().listCodecMultipleElements(),
18-
JsonConstants.CHILDREN, JsonConstants.INGREDIENTS).xmap(
19-
CompoundInfusionIngredient::new, CompoundInfusionIngredient::children
20-
);
15+
public static final MapCodec<CompoundInfusionIngredient> CODEC = codec(IngredientCreatorAccess.infusion(), CompoundInfusionIngredient::new);
2116

2217
CompoundInfusionIngredient(List<IInfusionIngredient> children) {
2318
super(children);

0 commit comments

Comments
 (0)