Skip to content

Commit

Permalink
feat(docs): Update block entities section
Browse files Browse the repository at this point in the history
  • Loading branch information
ChampionAsh5357 committed Oct 15, 2024
1 parent f0f7044 commit be95a9f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/blockentities/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public class MyBackpackContainer extends SimpleContainer {
And voilà, you have created an item-backed container! Call `new MyBackpackContainer(stack)` to create a container for a menu or other use case.

:::warning
Be aware that `Menu`s that directly interface with `Container`s must `#copy()` their `ItemStack`s when modifying them, as otherwise the immutability contract on data components is broken. To do this, NeoForge provides the `StackCopySlot` class for you.
Be aware that menus that directly interface with `Container`s must `#copy()` their `ItemStack`s when modifying them, as otherwise the immutability contract on data components is broken. To do this, NeoForge provides the `StackCopySlot` class for you.
:::

## `Container`s on `Entity`s
Expand Down
11 changes: 5 additions & 6 deletions docs/blockentities/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES =

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 block entity type.
() -> new BlockEntityType<>(
// 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.get(), MyBlocks.MY_BLOCK_2.get()
)
// Build using null; vanilla does some datafixer shenanigans with the parameter that we don't need.
.build(null)
);
```

Expand All @@ -54,7 +52,7 @@ public class MyBlockEntity extends BlockEntity {
```

:::info
The reason for this rather confusing setup process is that `BlockEntityType.Builder#of` expects a `BlockEntityType.BlockEntitySupplier<T extends BlockEntity>`, which is basically a `BiFunction<BlockPos, BlockState, T extends BlockEntity>`. As such, having a constructor we can directly reference using `::new` is highly beneficial. However, we also need to provide the constructed block entity type to the default and only constructor of `BlockEntity`, so we need to pass references around a bit.
The reason for this rather confusing setup process is that `BlockEntityType` expects a `BlockEntityType.BlockEntitySupplier<T extends BlockEntity>`, which is basically a `BiFunction<BlockPos, BlockState, T extends BlockEntity>`. As such, having a constructor we can directly reference using `::new` is highly beneficial. However, we also need to provide the constructed block entity type to the default and only constructor of `BlockEntity`, so we need to pass references around a bit.
:::

Finally, we need to modify the block class associated with the block entity. This means that we will not be able to attach block entities to simple instances of `Block`, instead, we need a subclass:
Expand All @@ -75,7 +73,7 @@ public class MyEntityBlock extends Block implements EntityBlock {
}
```

And then, you of course need to use this class as the type in your block registration:
And then, you of course need to use this class as the type in your [block registration][blockreg]:

```java
public static final DeferredBlock<MyEntityBlock> MY_BLOCK_1 =
Expand Down Expand Up @@ -232,6 +230,7 @@ It is important that you do safety checks, as the `BlockEntity` might already be
:::

[block]: ../blocks/index.md
[blockreg]: ../blocks/index.md#basic-blocks
[blockstate]: ../blocks/states.md
[dataattachments]: ../datastorage/attachments.md
[nbt]: ../datastorage/nbt.md
Expand Down

1 comment on commit be95a9f

@neoforged-pages-deployments
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploying with Cloudflare Pages

Name Result
Last commit: be95a9f12c3cf251810a52caf347b40d055e307a
Status: ✅ Deploy successful!
Preview URL: https://7cdc953a.neoforged-docs-previews.pages.dev
PR Preview URL: https://pr-178.neoforged-docs-previews.pages.dev

Please sign in to comment.