diff --git a/common/src/main/java/dev/anvilcraft/lib/registrator/builder/BlockBuilder.java b/common/src/main/java/dev/anvilcraft/lib/registrator/builder/BlockBuilder.java index a418577..8b055f5 100644 --- a/common/src/main/java/dev/anvilcraft/lib/registrator/builder/BlockBuilder.java +++ b/common/src/main/java/dev/anvilcraft/lib/registrator/builder/BlockBuilder.java @@ -22,6 +22,7 @@ import java.util.function.Function; import java.util.function.Supplier; +@SuppressWarnings("unused") public class BlockBuilder extends EntryBuilder { private final BlockEntry entry; private final Function factory; @@ -30,7 +31,8 @@ public class BlockBuilder extends EntryBuilder { }; private ItemBuilder itemBuilder = new BlockItemBuilder<>(this.registrator, this, this.id, BlockItem::new); private Supplier> dropOther = null; - private ItemEntry itemEntry = null; + private ItemEntry itemEntry = null; + private BiConsumer, RegistratorRecipeProvider> recipeFunction = null; public BlockBuilder(AbstractRegistrator registrator, String id, Function factory) { super(registrator, id); @@ -49,9 +51,7 @@ public BlockBuilder initialProperties(BlockEntry entry) { } public BlockBuilder defaultBlockState(){ - return state((tBlockEntry, provider) -> { - provider.simpleBlock(tBlockEntry.get()); - }); + return state((tBlockEntry, provider) -> provider.simpleBlock(tBlockEntry.get())); } public BlockBuilder initialProperties(Supplier supplier) { @@ -84,6 +84,7 @@ public final BlockBuilder tag(TagKey... tags) { return this; } + @SuppressWarnings("UnusedReturnValue") public BlockBuilder defaultLoot() { return loot(BlockLootTableProvider::dropSelf); } @@ -104,9 +105,7 @@ public BlockBuilder lang(String name) { return this; } public BlockBuilder defaultItem() { - this.itemBuilder = new BlockItemBuilder<>(this.registrator, this, this.id, BlockItem::new); - this.defaultLoot(); - return this; + return this.blockItem().builder().defaultLoot(); } public BlockItemBuilder blockItem() { @@ -137,8 +136,14 @@ public T build() { @Override public BlockEntry register() { + if (this.recipeFunction != null){ + this.itemBuilder.recipe( + (itemEntry1, registratorRecipeProvider) -> recipeFunction.accept(itemEntry1, registratorRecipeProvider) + ); + } this.itemEntry = itemBuilder.register(); - this.entry.setBlockItem((ItemEntry) itemEntry); + this.entry.setBlockItem(itemEntry); + this.registrator.addBuilder(BuiltInRegistries.BLOCK, this); if (dropOther != null) { this.loot((blockLootTableProvider, t) -> blockLootTableProvider.dropOther(this.entry.get(), dropOther.get())); @@ -147,7 +152,7 @@ public BlockEntry register() { } public BlockBuilder recipe(BiConsumer, RegistratorRecipeProvider> fn) { - itemBuilder.recipe(fn::accept); + this.recipeFunction = fn; return this; } diff --git a/common/src/main/java/dev/anvilcraft/lib/registrator/entry/BlockEntry.java b/common/src/main/java/dev/anvilcraft/lib/registrator/entry/BlockEntry.java index 8ee84a3..48e9cab 100644 --- a/common/src/main/java/dev/anvilcraft/lib/registrator/entry/BlockEntry.java +++ b/common/src/main/java/dev/anvilcraft/lib/registrator/entry/BlockEntry.java @@ -2,6 +2,7 @@ import dev.anvilcraft.lib.registrator.builder.BlockBuilder; import dev.anvilcraft.lib.registrator.builder.ItemBuilder; +import lombok.Getter; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; @@ -14,6 +15,7 @@ public class BlockEntry extends RegistryEntry implements ItemLike { private final BlockBuilder blockBuilder; + @Getter private ItemEntry blockItem = null; public BlockEntry(BlockBuilder blockBuilder) { diff --git a/common/src/main/resources/anvillib-common.mixins.json b/common/src/main/resources/anvillib-common.mixins.json index b0d9d42..4b46e9c 100644 --- a/common/src/main/resources/anvillib-common.mixins.json +++ b/common/src/main/resources/anvillib-common.mixins.json @@ -4,7 +4,9 @@ "package": "dev.anvilcraft.lib.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ - "ItemPropertiesAccessor" + "ItemPropertiesAccessor", + "LootContextParamSetsAccessor", + "LootTableProviderAccessor" ], "client": [ "BlockEntityRenderersAccessor" diff --git a/fabric/src/test/java/dev/anvilcraft/lib/test/TestRegisters.java b/fabric/src/test/java/dev/anvilcraft/lib/test/TestRegisters.java index 31cf783..44215ae 100644 --- a/fabric/src/test/java/dev/anvilcraft/lib/test/TestRegisters.java +++ b/fabric/src/test/java/dev/anvilcraft/lib/test/TestRegisters.java @@ -38,6 +38,15 @@ public class TestRegisters { public static final BlockEntry TEST_BLOCK = REGISTRATOR .block("test_block", Block::new) .tag(TEST_BLOCK_TAG) + .recipe((e, p) -> { + ShapedRecipeBuilder.shaped(RecipeCategory.MISC, e) + .pattern("xxx") + .pattern("xxx") + .pattern("xxx") + .define('x', TEST_ITEM.asItem()) + .unlockedBy("has_apple", RecipeProvider.has(Items.APPLE)) + .save(p); + }) .defaultItem() .register(); @@ -51,5 +60,6 @@ public class TestRegisters { .register(); public static void register() { + System.out.println("TEST_BLOCK.getBlockItem().builder() = " + TEST_BLOCK.getBlockItem().getItemBuilder()); } } diff --git a/neoforge/src/main/java/dev/anvilcraft/lib/registrator/forge/RegistratorImpl.java b/neoforge/src/main/java/dev/anvilcraft/lib/registrator/forge/RegistratorImpl.java index e16e9e5..05467de 100644 --- a/neoforge/src/main/java/dev/anvilcraft/lib/registrator/forge/RegistratorImpl.java +++ b/neoforge/src/main/java/dev/anvilcraft/lib/registrator/forge/RegistratorImpl.java @@ -6,6 +6,7 @@ import net.minecraft.core.Registry; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.RegisterEvent; import org.jetbrains.annotations.NotNull; @@ -16,10 +17,9 @@ private RegistratorImpl(String modid) { @Override public void init() { - MinecraftForge.EVENT_BUS.addListener(this::register); + FMLJavaModLoadingContext.get().getModEventBus().addListener(this::register); } - - @SubscribeEvent + public void register(RegisterEvent event) { for (Registry registry : this.manager) { this.init(event, registry);