Skip to content

Commit

Permalink
Merge branch 'releases/1.20.1' into releases/1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Sep 6, 2024
2 parents eb1cbb1 + 61bdbf8 commit 7528913
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

@SuppressWarnings("unused")
public class BlockBuilder<T extends Block> extends EntryBuilder<T> {
private final BlockEntry<T> entry;
private final Function<BlockBehaviour.Properties, T> factory;
Expand All @@ -30,7 +31,8 @@ public class BlockBuilder<T extends Block> extends EntryBuilder<T> {
};
private ItemBuilder<? extends BlockItem, ?> itemBuilder = new BlockItemBuilder<>(this.registrator, this, this.id, BlockItem::new);
private Supplier<ItemEntry<?>> dropOther = null;
private ItemEntry<?> itemEntry = null;
private ItemEntry<? extends BlockItem> itemEntry = null;
private BiConsumer<ItemEntry<? extends BlockItem>, RegistratorRecipeProvider> recipeFunction = null;

public BlockBuilder(AbstractRegistrator registrator, String id, Function<BlockBehaviour.Properties, T> factory) {
super(registrator, id);
Expand All @@ -49,9 +51,7 @@ public BlockBuilder<T> initialProperties(BlockEntry<? extends Block> entry) {
}

public BlockBuilder<T> defaultBlockState(){
return state((tBlockEntry, provider) -> {
provider.simpleBlock(tBlockEntry.get());
});
return state((tBlockEntry, provider) -> provider.simpleBlock(tBlockEntry.get()));
}

public BlockBuilder<T> initialProperties(Supplier<Block> supplier) {
Expand Down Expand Up @@ -84,6 +84,7 @@ public final BlockBuilder<T> tag(TagKey<Block>... tags) {
return this;
}

@SuppressWarnings("UnusedReturnValue")
public BlockBuilder<T> defaultLoot() {
return loot(BlockLootTableProvider::dropSelf);
}
Expand All @@ -104,9 +105,7 @@ public BlockBuilder<T> lang(String name) {
return this;
}
public BlockBuilder<T> defaultItem() {
this.itemBuilder = new BlockItemBuilder<>(this.registrator, this, this.id, BlockItem::new);
this.defaultLoot();
return this;
return this.blockItem().builder().defaultLoot();
}

public BlockItemBuilder<BlockItem, T> blockItem() {
Expand Down Expand Up @@ -137,8 +136,14 @@ public T build() {

@Override
public BlockEntry<T> register() {
if (this.recipeFunction != null){
this.itemBuilder.recipe(
(itemEntry1, registratorRecipeProvider) -> recipeFunction.accept(itemEntry1, registratorRecipeProvider)
);
}
this.itemEntry = itemBuilder.register();
this.entry.setBlockItem((ItemEntry<? extends BlockItem>) 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()));
Expand All @@ -147,7 +152,7 @@ public BlockEntry<T> register() {
}

public BlockBuilder<T> recipe(BiConsumer<ItemEntry<? extends BlockItem>, RegistratorRecipeProvider> fn) {
itemBuilder.recipe(fn::accept);
this.recipeFunction = fn;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,7 @@

public class BlockEntry<T extends Block> extends RegistryEntry<T> implements ItemLike {
private final BlockBuilder<T> blockBuilder;
@Getter
private ItemEntry<? extends BlockItem> blockItem = null;

public BlockEntry(BlockBuilder<T> blockBuilder) {
Expand Down
4 changes: 3 additions & 1 deletion common/src/main/resources/anvillib-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"package": "dev.anvilcraft.lib.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ItemPropertiesAccessor"
"ItemPropertiesAccessor",
"LootContextParamSetsAccessor",
"LootTableProviderAccessor"
],
"client": [
"BlockEntityRenderersAccessor"
Expand Down
10 changes: 10 additions & 0 deletions fabric/src/test/java/dev/anvilcraft/lib/test/TestRegisters.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public class TestRegisters {
public static final BlockEntry<Block> 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();

Expand All @@ -51,5 +60,6 @@ public class TestRegisters {
.register();

public static void register() {
System.out.println("TEST_BLOCK.getBlockItem().builder() = " + TEST_BLOCK.getBlockItem().getItemBuilder());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 7528913

Please sign in to comment.