Skip to content

Commit

Permalink
feat: Take serializer when registering a recipe (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
edralzar authored and BlayTheNinth committed Dec 9, 2023
1 parent cca763b commit e036411
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;

import java.util.function.Supplier;

public class FabricBalmRecipes implements BalmRecipes {

@Override
public <T extends Recipe<?>> DeferredObject<RecipeType<T>> registerRecipeType(Supplier<RecipeType<T>> supplier, ResourceLocation identifier) {
public <T extends Recipe<?>> DeferredObject<RecipeType<T>> registerRecipeType(Supplier<RecipeType<T>> typeSupplier, Supplier<RecipeSerializer<T>> serializerSupplier, ResourceLocation identifier) {
return new DeferredObject<>(identifier, () -> {
RecipeType<T> recipeType = supplier.get();
Registry.register(BuiltInRegistries.RECIPE_SERIALIZER, identifier, serializerSupplier.get());
RecipeType<T> recipeType = typeSupplier.get();
recipeType = Registry.register(BuiltInRegistries.RECIPE_TYPE, identifier, recipeType);
return recipeType;
}).resolveImmediately();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.blay09.mods.balm.forge.DeferredRegisters;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
Expand All @@ -14,10 +15,14 @@

public class ForgeBalmRecipes implements BalmRecipes {
@Override
public <T extends Recipe<?>> DeferredObject<RecipeType<T>> registerRecipeType(Supplier<RecipeType<T>> supplier, ResourceLocation identifier) {
DeferredRegister<RecipeType<?>> register = DeferredRegisters.get(ForgeRegistries.RECIPE_TYPES, identifier.getNamespace());
RegistryObject<RecipeType<T>> registryObject = register.register(identifier.getPath(), supplier);
return new DeferredObject<>(identifier, registryObject, registryObject::isPresent);
public <T extends Recipe<?>> DeferredObject<RecipeType<T>> registerRecipeType(Supplier<RecipeType<T>> typeSupplier, Supplier<RecipeSerializer<T>> serializerSupplier, ResourceLocation identifier) {
DeferredRegister<RecipeSerializer<?>> serializerRegister = DeferredRegisters.get(ForgeRegistries.RECIPE_SERIALIZERS, identifier.getNamespace());
RegistryObject<RecipeSerializer<T>> serializerObject = serializerRegister.register(identifier.getPath(), serializerSupplier);

DeferredRegister<RecipeType<?>> typeRegister = DeferredRegisters.get(ForgeRegistries.RECIPE_TYPES, identifier.getNamespace());
RegistryObject<RecipeType<T>> typeObject = typeRegister.register(identifier.getPath(), typeSupplier);

return new DeferredObject<>(identifier, typeObject, () -> typeObject.isPresent() && serializerObject.isPresent());
}

}
5 changes: 5 additions & 0 deletions shared/src/main/java/net/blay09/mods/balm/api/Balm.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.blay09.mods.balm.api.network.BalmNetworking;
import net.blay09.mods.balm.api.provider.BalmProviders;
import net.blay09.mods.balm.api.proxy.SidedProxy;
import net.blay09.mods.balm.api.recipe.BalmRecipes;
import net.blay09.mods.balm.api.sound.BalmSounds;
import net.blay09.mods.balm.api.stats.BalmStats;
import net.blay09.mods.balm.api.world.BalmWorldGen;
Expand Down Expand Up @@ -99,6 +100,10 @@ public static BalmHooks getHooks() {
return runtime.getHooks();
}

public static BalmRecipes getRecipes() {
return runtime.getRecipes();
}

public static BalmRegistries getRegistries() {
return runtime.getRegistries();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import net.blay09.mods.balm.api.DeferredObject;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;

import java.util.function.Supplier;

public interface BalmRecipes {
<T extends Recipe<?>> DeferredObject<RecipeType<T>> registerRecipeType(Supplier<RecipeType<T>> supplier, ResourceLocation identifier);
<T extends Recipe<?>> DeferredObject<RecipeType<T>> registerRecipeType(Supplier<RecipeType<T>> typeSupplier, Supplier<RecipeSerializer<T>> serializerSupplier, ResourceLocation identifier);
}

0 comments on commit e036411

Please sign in to comment.