neoforged / Documentation

The repository containing Neo's documentation
MIT License
28 stars 53 forks source link

Block Entities doc for 1.21 is incorrect #175

Closed Rabbit0w0 closed 1 month ago

Rabbit0w0 commented 1 month ago

As title.

Code in doc:

public static final Supplier<BlockEntityType<MyBlockEntity>> MY_BLOCK_ENTITY = BLOCK_ENTITY_TYPES.register(
        "my_block_entity",
        // The block entity type, created using a builder.
        () -> BlockEntityType.Builder.of(
                // The supplier to use for constructing the block entity instances.
                MyBlockEntity::new,
                // A vararg of blocks that can have this block entity.
                // This assumes the existence of the referenced blocks as DeferredBlock<Block>s.
                MyBlocks.MY_BLOCK_1, MyBlocks.MY_BLOCK_2
        )
        // Build using null; vanilla does some datafixer shenanigans with the parameter that we don't need.
        .build(null)
);

but the register method actually accepts Block instead of DeferredBlock

Also, by following the doc to build a example mod, it actually doesn't work with the exception: java.lang.NullPointerException: Trying to access unbound value: ResourceKey[minecraft:block_entity_type / customfurniture:furniture_entity], which is thrown when constructing a new block entity and unboxing Supplier<BlockEntityType<MyBlockEntity>>

mc ver: 1.12.1 neo: 21.1.65

jananass commented 1 month ago

@Rabbit0w0 I had the exact same issue and just saw that it is caused because you need to register the newly created BLOCK_ENTITY_TYPES to the mod event bus. Like this, by using the starting ModDevGradle example repository.

// The constructor for the mod class is the first code that is run when your mod is loaded.
// FML will recognize some parameter types like IEventBus or ModContainer and pass them in automatically.
public ExtraSync(IEventBus modEventBus, ModContainer modContainer)
{
    // Register the commonSetup method for modloading
    modEventBus.addListener(this::commonSetup);

    // Register the Deferred Register to the mod event bus so blocks get registered
    BLOCKS.register(modEventBus);
    // Register the Deferred Register to the mod event bus so items get registered
    ITEMS.register(modEventBus);
    // Register the Deferred Register to the mod event bus so tabs get registered
    CREATIVE_MODE_TABS.register(modEventBus);
    // Register the Deferred Register to the mod event bus so block entities get registered
    BLOCK_ENTITY_TYPES.register(modEventBus);
    ...
}

Is it possible to add this mention to the documentation?

ChampionAsh5357 commented 1 month ago

@Rabbit0w0 I had the exact same issue and just saw that it is caused because you need to register the newly created BLOCK_ENTITY_TYPES to the mod event bus. Like this, by using the starting ModDevGradle example repository.

// The constructor for the mod class is the first code that is run when your mod is loaded. // FML will recognize some parameter types like IEventBus or ModContainer and pass them in automatically. public ExtraSync(IEventBus modEventBus, ModContainer modContainer) { // Register the commonSetup method for modloading modEventBus.addListener(this::commonSetup);

// Register the Deferred Register to the mod event bus so blocks get registered
BLOCKS.register(modEventBus);
// Register the Deferred Register to the mod event bus so items get registered
ITEMS.register(modEventBus);
// Register the Deferred Register to the mod event bus so tabs get registered
CREATIVE_MODE_TABS.register(modEventBus);
// Register the Deferred Register to the mod event bus so block entities get registered
BLOCK_ENTITY_TYPES.register(modEventBus);
...

}

Is it possible to add this mention to the documentation?

We do link to the relevant pages for registration using DeferredRegister in the documentation since it should be stated that if you are making a block entity, you have probably already done a similar process for blocks. However, after a quick read, I do think the link should be more explicitly highlight than just a loose mention. I would suggest creating a separate issue for this.