Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Inventory article #246

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Block Inventory article #246

wants to merge 2 commits into from

Conversation

natri0
Copy link
Contributor

@natri0 natri0 commented Dec 30, 2024

Added an article about implementing Inventory in block entities. Some stuff (e.g. ImplementedInventory) copied over from wiki's "tutorial:inventory".

Tutorial covers using the ImplementedInventory & implementing SidedInventory, as well as some basic user interaction (inserting items on right-click).

@natri0 natri0 requested a review from a team as a code owner December 30, 2024 20:50
Copy link

netlify bot commented Dec 30, 2024

Deploy Preview for nimble-elf-d9d491 failed.

Name Link
🔨 Latest commit 972b248
🔍 Latest deploy log https://app.netlify.com/sites/nimble-elf-d9d491/deploys/6788ca1c46725800087b3817

@IMB11 IMB11 marked this pull request as draft December 30, 2024 20:52
@IMB11 IMB11 added new-content A new page or set of pages stage:verification This should be verified labels Dec 30, 2024
@its-miroma
Copy link
Member

Thank you for your contribution!

We are temporarily not accepting PRs until we finish the update process. Follow #242 for more information.

You may keep this PR open.

@IMB11
Copy link
Member

IMB11 commented Jan 15, 2025

Hi! The documentation has been updated to version 1.21.4. Please rebase your PR and update your code to align with the latest version.

Alternatively, you can create a new branch based on the latest commit from the FabricMC:main branch, copy over your changes from the old natri0:inventory branch, update it, and open a new PR - this approach is often simpler than dealing with complex git commands if you're not the best git wrangler.

Once you've completed the updates and are confident everything is ready, feel free to unmark this as a draft. Thanks again for your contribution!

Basically the same content as the article on the fabric wiki: https://wiki.fabricmc.net/tutorial:inventory

Uses the same `ImplementedInventory` class as that one to implement `Inventory` on the custom block entity

Now updated for latest!
Maybe we should get a Checkstyle rule to allow comments after blocks? It's making me do stuff like this:

// :::1
if (condition) {
    // code...
}

// :::1

...which is IMO kinda ugly
@natri0
Copy link
Contributor Author

natri0 commented Jan 16, 2025

updated for 1.21.4 :)

@natri0 natri0 marked this pull request as ready for review January 16, 2025 08:59

<VideoPlayer src="/assets/develop/blocks/inventory_2.mp4" />

## Sided Inventories
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
## Sided Inventories
## Sided Inventories {#sided-inventories}


Now, when you right-click the block with an item, you'll no longer have an item! If you run `/data get block` on the block, you'll see the item in the `Items` field in the NBT.

![Duplicator block & /data get block output showing the item in the inventory](/assets/develop/blocks/inventory_1.png)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
![Duplicator block & /data get block output showing the item in the inventory](/assets/develop/blocks/inventory_1.png)
![Duplicator block and output of `/data get block` showing the item in the inventory](/assets/develop/blocks/inventory_1.png)


Here, if the player is holding an item and there is an empty slot, we move the item from the player's hand to the block's inventory and return `ItemActionResult.SUCCESS`.

Now, when you right-click the block with an item, you'll no longer have an item! If you run `/data get block` on the block, you'll see the item in the `Items` field in the NBT.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Now, when you right-click the block with an item, you'll no longer have an item! If you run `/data get block` on the block, you'll see the item in the `Items` field in the NBT.
Now, when you right-click the block with an item, you'll no longer have it! If you run `/data get block` on the block, you'll see the item in the `Items` field in the NBT.

Copy link
Contributor

@JR1811 JR1811 left a comment

Choose a reason for hiding this comment

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

This looks pretty good so far!

I think it would be interesting to also expand this page to explain how storing things like ItemStacks, Fluids, Energy, and so on, could be stored and retrieved. Also on that note, an example on how to use the Fabric Transfer API might be useful for that too. Or this could get its extra page, if necessary.


@[code transcludeWith=:::3](@/reference/latest/src/main/java/com/example/docs/block/entity/custom/DuplicatorBlockEntity.java)

The `DuplicatorBlock` should now have a `getTicker` method that returns a reference to `DuplicatorBlockEntity::tick`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need a ticking functionality here?
From what I can see, it's just copying the slot and is returning it asap.

So this could be handled in the block interaction method in the custom block class, right?

Or do something like duplicating the slot every 10 ticks and then storing it in the inventory, until it's being retrieved or the stack has hit its max size.


# Block Inventories {#inventories}

In Minecraft, all blocks that can store items have an `Inventory`. This includes blocks like chests, furnaces, and hoppers.
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be just me, but this kind of makes it sound like the Inventory interface is always needed to store any kind of ItemStack. Maybe mention it as "best practice" or something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-content A new page or set of pages stage:verification This should be verified
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants