Skip to content

Commit

Permalink
the codec-ification begins
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Nov 3, 2023
1 parent b09834d commit 029d52c
Show file tree
Hide file tree
Showing 36 changed files with 206 additions and 282 deletions.
3 changes: 3 additions & 0 deletions src/api/java/mekanism/api/chemical/gas/Gas.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mekanism.api.chemical.gas;

import com.mojang.serialization.Codec;
import mekanism.api.MekanismAPI;
import mekanism.api.NBTConstants;
import mekanism.api.annotations.NothingNullByDefault;
Expand All @@ -21,6 +22,8 @@
@NothingNullByDefault
public class Gas extends Chemical<Gas> implements IGasProvider {

public static final Codec<Gas> CODEC = MekanismAPI.gasRegistry().getCodec();

public Gas(GasBuilder builder) {
super(builder, ChemicalTags.GAS);
}
Expand Down
7 changes: 7 additions & 0 deletions src/api/java/mekanism/api/chemical/gas/GasStack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mekanism.api.chemical.gas;

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import mekanism.api.MekanismAPI;
import mekanism.api.NBTConstants;
import mekanism.api.annotations.NothingNullByDefault;
Expand All @@ -18,6 +20,11 @@
@NothingNullByDefault
public final class GasStack extends ChemicalStack<Gas> {

public static final Codec<GasStack> CODEC = Codec.pair(Gas.CODEC, Codec.LONG).xmap(
pair->new GasStack(pair.getFirst(), pair.getSecond()),
stack-> Pair.of(stack.getRaw(), stack.getAmount())
);

/**
* Empty GasStack instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class ChemicalCrystallizerRecipe extends MekanismRecipe implemen
* @param output Output.
*/
public ChemicalCrystallizerRecipe(ResourceLocation id, ChemicalStackIngredient<?, ?> input, ItemStack output) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
this.chemicalType = ChemicalType.getTypeFor(input);
Objects.requireNonNull(output, "Output cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class ChemicalDissolutionRecipe extends MekanismRecipe implement
* @param output Output.
*/
public ChemicalDissolutionRecipe(ResourceLocation id, ItemStackIngredient itemInput, GasStackIngredient gasInput, ChemicalStack<?> output) {
super(id);
super();
this.itemInput = Objects.requireNonNull(itemInput, "Item input cannot be null.");
this.gasInput = Objects.requireNonNull(gasInput, "Gas input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
Expand Down
2 changes: 1 addition & 1 deletion src/api/java/mekanism/api/recipes/CombinerRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class CombinerRecipe extends MekanismRecipe implements BiPredica
* @param output Output.
*/
public CombinerRecipe(ResourceLocation id, ItemStackIngredient mainInput, ItemStackIngredient extraInput, ItemStack output) {
super(id);
super();
this.mainInput = Objects.requireNonNull(mainInput, "Main input cannot be null.");
this.extraInput = Objects.requireNonNull(extraInput, "Secondary/Extra input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
Expand Down
2 changes: 1 addition & 1 deletion src/api/java/mekanism/api/recipes/ElectrolysisRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class ElectrolysisRecipe extends MekanismRecipe implements Predi
* @param rightGasOutput Right output.
*/
public ElectrolysisRecipe(ResourceLocation id, FluidStackIngredient input, FloatingLong energyMultiplier, GasStack leftGasOutput, GasStack rightGasOutput) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
this.energyMultiplier = Objects.requireNonNull(energyMultiplier, "Energy multiplier cannot be null.").copyAsConst();
if (energyMultiplier.smallerThan(FloatingLong.ONE)) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/java/mekanism/api/recipes/FluidToFluidRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class FluidToFluidRecipe extends MekanismRecipe implements Predi
* @param output Output.
*/
public FluidToFluidRecipe(ResourceLocation id, FluidStackIngredient input, FluidStack output) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
if (output.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class ItemStackToEnergyRecipe extends MekanismRecipe implements
* @param output Output, must be greater than zero.
*/
public ItemStackToEnergyRecipe(ResourceLocation id, ItemStackIngredient input, FloatingLong output) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
if (output.isZero()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class ItemStackToFluidRecipe extends MekanismRecipe implements P
* @param output Output.
*/
public ItemStackToFluidRecipe(ResourceLocation id, ItemStackIngredient input, FluidStack output) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
if (output.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class ItemStackToItemStackRecipe extends MekanismRecipe implemen
* @param output Output.
*/
public ItemStackToItemStackRecipe(ResourceLocation id, ItemStackIngredient input, ItemStack output) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
if (output.isEmpty()) {
Expand Down
16 changes: 0 additions & 16 deletions src/api/java/mekanism/api/recipes/MekanismRecipe.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package mekanism.api.recipes;

import java.util.Objects;
import mekanism.api.inventory.IgnoredIInventory;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;
Expand All @@ -15,27 +13,13 @@
*/
public abstract class MekanismRecipe implements Recipe<IgnoredIInventory> {//TODO: Should we make implementations override equals and hashcode?

private final ResourceLocation id;

/**
* @param id Recipe name.
*/
protected MekanismRecipe(ResourceLocation id) {
this.id = Objects.requireNonNull(id, "Recipe name cannot be null.");
}

/**
* Writes this recipe to a PacketBuffer.
*
* @param buffer The buffer to write to.
*/
public abstract void write(FriendlyByteBuf buffer);

@NotNull
public ResourceLocation getId() {
return id;
}

@Override
public boolean matches(@NotNull IgnoredIInventory inv, @NotNull Level world) {
//TODO: Decide if we ever want to make use of this method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public abstract class PressurizedReactionRecipe extends MekanismRecipe implement
*/
public PressurizedReactionRecipe(ResourceLocation id, ItemStackIngredient inputSolid, FluidStackIngredient inputFluid, GasStackIngredient inputGas,
FloatingLong energyRequired, int duration, ItemStack outputItem, GasStack outputGas) {
super(id);
super();
this.inputSolid = Objects.requireNonNull(inputSolid, "Item input cannot be null.");
this.inputFluid = Objects.requireNonNull(inputFluid, "Fluid input cannot be null.");
this.inputGas = Objects.requireNonNull(inputGas, "Gas input cannot be null.");
Expand Down
27 changes: 10 additions & 17 deletions src/api/java/mekanism/api/recipes/RotaryRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import mekanism.api.recipes.ingredients.ChemicalStackIngredient.GasStackIngredient;
import mekanism.api.recipes.ingredients.FluidStackIngredient;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.fluids.FluidStack;
import org.jetbrains.annotations.Contract;

Expand Down Expand Up @@ -37,16 +36,14 @@ public abstract class RotaryRecipe extends MekanismRecipe {
/**
* Rotary recipe that converts a fluid into a gas.
*
* @param id Recipe name.
* @param fluidInput Fluid input.
* @param gasOutput Gas output.
*
* @apiNote It is recommended to use {@link #RotaryRecipe(ResourceLocation, FluidStackIngredient, GasStackIngredient, GasStack, FluidStack)} over this constructor in
* combination with {@link #RotaryRecipe(ResourceLocation, GasStackIngredient, FluidStack)} and making two separate recipes if the conversion will be possible in both
* @apiNote It is recommended to use {@link #RotaryRecipe(FluidStackIngredient, GasStackIngredient, GasStack, FluidStack)} over this constructor in
* combination with {@link #RotaryRecipe(GasStackIngredient, FluidStack)} and making two separate recipes if the conversion will be possible in both
* directions.
*/
public RotaryRecipe(ResourceLocation id, FluidStackIngredient fluidInput, GasStack gasOutput) {
super(id);
public RotaryRecipe(FluidStackIngredient fluidInput, GasStack gasOutput) {
this.fluidInput = Objects.requireNonNull(fluidInput, "Fluid input cannot be null.");
Objects.requireNonNull(gasOutput, "Gas output cannot be null.");
if (gasOutput.isEmpty()) {
Expand All @@ -63,16 +60,14 @@ public RotaryRecipe(ResourceLocation id, FluidStackIngredient fluidInput, GasSta
/**
* Rotary recipe that converts a gas into a fluid.
*
* @param id Recipe name.
* @param gasInput Gas input.
* @param fluidOutput Fluid output.
*
* @apiNote It is recommended to use {@link #RotaryRecipe(ResourceLocation, FluidStackIngredient, GasStackIngredient, GasStack, FluidStack)} over this constructor in
* combination with {@link #RotaryRecipe(ResourceLocation, FluidStackIngredient, GasStack)} and making two separate recipes if the conversion will be possible in both
* directions.
* @apiNote It is recommended to use {@link #RotaryRecipe(FluidStackIngredient, GasStackIngredient, GasStack, FluidStack)} over this constructor in combination with
* {@link #RotaryRecipe(FluidStackIngredient, GasStack)} and making two separate recipes if the conversion will be possible in both directions.
*/
public RotaryRecipe(ResourceLocation id, GasStackIngredient gasInput, FluidStack fluidOutput) {
super(id);
public RotaryRecipe(GasStackIngredient gasInput, FluidStack fluidOutput) {
super();
this.gasInput = Objects.requireNonNull(gasInput, "Gas input cannot be null.");
Objects.requireNonNull(fluidOutput, "Fluid output cannot be null.");
if (fluidOutput.isEmpty()) {
Expand All @@ -89,18 +84,16 @@ public RotaryRecipe(ResourceLocation id, GasStackIngredient gasInput, FluidStack
/**
* Rotary recipe that is capable of converting a fluid into a gas and a gas into a fluid.
*
* @param id Recipe name.
* @param fluidInput Fluid input.
* @param gasInput Gas input.
* @param gasOutput Gas output.
* @param fluidOutput Fluid output.
*
* @apiNote It is recommended to use this constructor over using {@link #RotaryRecipe(ResourceLocation, FluidStackIngredient, GasStack)} and
* {@link #RotaryRecipe(ResourceLocation, GasStackIngredient, FluidStack)} in combination and creating two recipes if the conversion will be possible in both
* @apiNote It is recommended to use this constructor over using {@link #RotaryRecipe(FluidStackIngredient, GasStack)} and
* {@link #RotaryRecipe(GasStackIngredient, FluidStack)} in combination and creating two recipes if the conversion will be possible in both
* directions.
*/
public RotaryRecipe(ResourceLocation id, FluidStackIngredient fluidInput, GasStackIngredient gasInput, GasStack gasOutput, FluidStack fluidOutput) {
super(id);
public RotaryRecipe(FluidStackIngredient fluidInput, GasStackIngredient gasInput, GasStack gasOutput, FluidStack fluidOutput) {
this.gasInput = Objects.requireNonNull(gasInput, "Gas input cannot be null.");
this.fluidInput = Objects.requireNonNull(fluidInput, "Fluid input cannot be null.");
Objects.requireNonNull(gasOutput, "Gas output cannot be null.");
Expand Down
2 changes: 1 addition & 1 deletion src/api/java/mekanism/api/recipes/SawmillRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class SawmillRecipe extends MekanismRecipe implements Predicate<
* @apiNote At least one output must not be empty.
*/
public SawmillRecipe(ResourceLocation id, ItemStackIngredient input, ItemStack mainOutput, ItemStack secondaryOutput, double secondaryChance) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
Objects.requireNonNull(mainOutput, "Main output cannot be null.");
Objects.requireNonNull(secondaryOutput, "Secondary output cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class ChemicalChemicalToChemicalRecipe<CHEMICAL extends Chemical
* @apiNote The order of the inputs does not matter.
*/
public ChemicalChemicalToChemicalRecipe(ResourceLocation id, INGREDIENT leftInput, INGREDIENT rightInput, STACK output) {
super(id);
super();
this.leftInput = Objects.requireNonNull(leftInput, "Left input cannot be null.");
this.rightInput = Objects.requireNonNull(rightInput, "Right input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class ChemicalToChemicalRecipe<CHEMICAL extends Chemical<CHEMICA
* @param output Output.
*/
public ChemicalToChemicalRecipe(ResourceLocation id, INGREDIENT input, STACK output) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
if (output.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class FluidChemicalToChemicalRecipe<CHEMICAL extends Chemical<CH
* @param output Output.
*/
public FluidChemicalToChemicalRecipe(ResourceLocation id, FluidStackIngredient fluidInput, INGREDIENT chemicalInput, STACK output) {
super(id);
super();
this.fluidInput = Objects.requireNonNull(fluidInput, "Fluid input cannot be null.");
this.chemicalInput = Objects.requireNonNull(chemicalInput, "Chemical input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class ItemStackChemicalToItemStackRecipe<CHEMICAL extends Chemic
* @param output Output.
*/
public ItemStackChemicalToItemStackRecipe(ResourceLocation id, ItemStackIngredient itemInput, INGREDIENT chemicalInput, ItemStack output) {
super(id);
super();
this.itemInput = Objects.requireNonNull(itemInput, "Item input cannot be null.");
this.chemicalInput = Objects.requireNonNull(chemicalInput, "Chemical input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class ItemStackToChemicalRecipe<CHEMICAL extends Chemical<CHEMIC
* @param output Output.
*/
public ItemStackToChemicalRecipe(ResourceLocation id, ItemStackIngredient input, STACK output) {
super(id);
super();
this.input = Objects.requireNonNull(input, "Input cannot be null.");
Objects.requireNonNull(output, "Output cannot be null.");
if (output.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mekanism.api.recipes.ingredients;

import com.google.gson.JsonElement;
import com.mojang.serialization.Codec;
import java.util.List;
import java.util.function.Predicate;
import net.minecraft.MethodsReturnNonnullByDefault;
Expand Down Expand Up @@ -73,4 +74,10 @@ default boolean hasNoMatchingInstances() {
* @return JsonElement representation of this ingredient.
*/
JsonElement serialize();

/**
*
* @return the codec for this ingredient
*/
Codec<InputIngredient<TYPE>> codec();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mekanism.api.recipes.ingredients.creator;

import com.google.gson.JsonElement;
import com.mojang.serialization.Codec;
import java.util.stream.Stream;
import mekanism.api.annotations.NothingNullByDefault;
import mekanism.api.recipes.ingredients.InputIngredient;
Expand Down Expand Up @@ -62,6 +63,8 @@ public interface IIngredientCreator<TYPE, STACK, INGREDIENT extends InputIngredi
*/
INGREDIENT deserialize(@Nullable JsonElement json);

Codec<INGREDIENT> codec();

/**
* Combines multiple Ingredients into a single Ingredient.
*
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/mekanism/common/Mekanism.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import mekanism.common.item.loot.MekanismLootFunctions;
import mekanism.common.item.predicate.FullCanteenItemPredicate;
import mekanism.common.item.predicate.MaxedModuleContainerItemPredicate;
import mekanism.common.item.predicate.MekanismItemPredicates;
import mekanism.common.lib.MekAnnotationScanner;
import mekanism.common.lib.Version;
import mekanism.common.lib.frequency.FrequencyManager;
Expand All @@ -69,6 +70,7 @@
import mekanism.common.network.to_client.PacketTransmitterUpdate;
import mekanism.common.recipe.MekanismRecipeType;
import mekanism.common.recipe.condition.ConditionExistsCondition;
import mekanism.common.recipe.condition.MekanismRecipeConditions;
import mekanism.common.recipe.condition.ModVersionLoadedCondition;
import mekanism.common.registries.MekanismBlocks;
import mekanism.common.registries.MekanismContainerTypes;
Expand Down Expand Up @@ -222,6 +224,8 @@ public Mekanism() {
MekanismSlurries.SLURRIES.createAndRegisterChemical(modEventBus);
MekanismRobitSkins.createAndRegisterDatapack(modEventBus);
MekanismModules.MODULES.createAndRegister(modEventBus);
MekanismRecipeConditions.CONDITION_CODECS.register(modEventBus);
MekanismItemPredicates.PREDICATES.register(modEventBus);
modEventBus.addListener(this::registerEventListener);
//Set our version number to match the mods.toml file, which matches the one in our build.gradle
versionNumber = new Version(ModLoadingContext.get().getActiveContainer());
Expand All @@ -245,11 +249,6 @@ private void registerEventListener(RegisterEvent event) {
event.register(MekanismAPI.INFUSE_TYPE_REGISTRY_NAME, emptyName, () -> MekanismAPI.EMPTY_INFUSE_TYPE);
event.register(MekanismAPI.PIGMENT_REGISTRY_NAME, emptyName, () -> MekanismAPI.EMPTY_PIGMENT);
event.register(MekanismAPI.SLURRY_REGISTRY_NAME, emptyName, () -> MekanismAPI.EMPTY_SLURRY);
//Register our custom serializer condition
if (event.getRegistryKey().equals(ForgeRegistries.Keys.RECIPE_SERIALIZERS)) {
CraftingHelper.register(ConditionExistsCondition.Serializer.INSTANCE);
CraftingHelper.register(ModVersionLoadedCondition.Serializer.INSTANCE);
}
}

public static ResourceLocation rl(String path) {
Expand Down Expand Up @@ -349,9 +348,6 @@ private void commonSetup(FMLCommonSetupEvent event) {
registerDispenseBehavior(new ModuleDispenseBehavior(), MekanismItems.MEKA_TOOL);
registerDispenseBehavior(new MekaSuitDispenseBehavior(), MekanismItems.MEKASUIT_HELMET, MekanismItems.MEKASUIT_BODYARMOR, MekanismItems.MEKASUIT_PANTS,
MekanismItems.MEKASUIT_BOOTS);
//Register custom item predicates
ItemPredicate.register(FullCanteenItemPredicate.ID, json -> FullCanteenItemPredicate.INSTANCE);
ItemPredicate.register(MaxedModuleContainerItemPredicate.ID, MaxedModuleContainerItemPredicate::fromJson);
//Add any extra game event frequencies
MekanismGameEvents.addFrequencies();
});
Expand Down
Loading

0 comments on commit 029d52c

Please sign in to comment.