[PR #7076] [MERGED] Native mods: read an attachment without creating its default (has, get_existing) #7319

Closed
opened 2026-11-03 06:37:39 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/7076
Author: @arismoko
Created: 11/2/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @ludovic111

Base: main ← Head: v2/ses_01M6DMWRZSPA9JSD7HP0ZN2DNJ-5468c48f1c40


📝 Commits (3)

  • 568e7e5 Add attachment reads that never insert the default
  • e160693 Document attachment has and get_existing and add their API fragment
  • cda5813 Format attachment existence reads

📊 Changes

3 files changed (+116 additions, -47 deletions)

View changed files

📝 crates/horizon-world/src/attachments.rs (+96 -40)
📝 docs/native-mods.md (+12 -7)
➕ tools/native-hook-fragments/attachment_existing.json (+8 -0)

📄 Description

🤖 Opened by Aria's agent swarm, Dyna, not by Aria.

Part of #6452 ("Attachments: read without creating the default").

What it does

AttachmentType::get inserts the default the first time it is read. So a check like "does this chunk carry my data?" marks the holder as carrying the default from then on, and when the type has a codec, that default gets saved. This PR adds two reads that never insert anything:

  • AttachmentType::has(host, holder) -> Result<bool, AttachmentError>: whether a value is stored. Saved values count before they decode.
  • AttachmentType::get_existing(host, holder) -> Result<Option<T>, AttachmentError>: the value if stored, decoded lazily as with get, else None.

Both are also on Attachments. A missing or non-server holder is still MissingHolder. Inside attachments.rs, get is now get_existing with the default as fallback. A private insert_decoded holds the encode-and-store step that get and set each repeated, and remove uses has. Saved NBT, unknown entries and undecodable values behave as before, and the existing tests are unchanged. A helper-only API fragment (attachment_existing) lets bundles name the API number that has these methods. docs/native-mods.md describes them.

Not in this PR: a read-only holder access path. Reading a chunk or block-entity holder still marks its chunk for saving, but adds nothing to it. Client sync and hasAttachments are not included either.

NeoForge counterpart

IAttachmentHolder.hasData(AttachmentType) and IAttachmentHolder.getExistingData(AttachmentType) (an empty Optional when absent), from the public javadoc and the data attachments page ("If attaching a default instance is not desired, a hasData check can be added"). Horizon's names drop the Data suffix, as get/set/remove already do.

Tests

Unit tests in horizon-world (attachments::tests):

  • an absent value stays absent after has and get_existing, and nothing gets encoded, while get does insert and encode;
  • a saved value reads back unchanged without changing the saved compound;
  • an undecodable saved value reports Decode and still round-trips.

Tested in game

https://github.com/arismoko/horizon-machine-sample (the recommended end-to-end route, used here), built at this head (cda58134c6) with the sample's tools/horizon.py build, plus a throwaway helper: when a machine is placed, it calls get_existing and has on one chunk attachment (probe) and get on another (legacy), both with codecs. On the server thread, through horizon-ctl:

  1. Fresh world, machine placed: before probe_has=false legacy_has=false, then after probe_has=false probe_existing=None legacy_has=true.
  2. The same world reopened: before probe_has=false legacy_has=true. So the get default was saved with the chunk and get_existing saved nothing. Setting probe then read back Some(7).

On a local dedicated server (--dedicated --nogui, one client joined, the machine placed with setblock), step 1 logged the same two lines. Step 2 wasn't repeated there.

pr_ready

pr_ready (tools/pr_ready.py --quick --base cabe8b5de7d0b45541c923c4a6b3c5cb3e3686c7 at cda58134c6, 4 min 30 s)

Check Result
Scope ✅ 3 files, +116 −47
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ❌ 610 run, FAILED (failures=1, skipped=1) (40 s)
rustfmt ✅ (10 s)
clippy ✅ (1 min 48 s)
tests: horizon-world ✅ 393 passed (1 min 49 s)
doc tests ✅ 0 passed (1 s)
content root tests ➖ content root untouched
mod API up to date ➖ native hooks untouched
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 371 affected, to run in CI (--quick)
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

The one Python failure is the base's test_world_input_capture.test_native_split_mouse_turns_only_its_owner, which fails the same way at cabe8b5de and passes on current main; this PR touches no Python or input code.

🤖 Generated with Dyna.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/7076 **Author:** [@arismoko](https://github.com/arismoko) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `v2/ses_01M6DMWRZSPA9JSD7HP0ZN2DNJ-5468c48f1c40` --- ### 📝 Commits (3) - [`568e7e5`](https://github.com/horizon-rust/horizon/commit/568e7e55f6e54f141898d8c2123101c24adacd33) Add attachment reads that never insert the default - [`e160693`](https://github.com/horizon-rust/horizon/commit/e16069360af946f9d614a07be40c35e45003b501) Document attachment has and get_existing and add their API fragment - [`cda5813`](https://github.com/horizon-rust/horizon/commit/cda58134c63249d722125bc28cb57d7f86f56526) Format attachment existence reads ### 📊 Changes **3 files changed** (+116 additions, -47 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-world/src/attachments.rs` (+96 -40) 📝 `docs/native-mods.md` (+12 -7) ➕ `tools/native-hook-fragments/attachment_existing.json` (+8 -0) </details> ### 📄 Description > 🤖 Opened by Aria's agent swarm, Dyna, not by Aria. Part of #6452 ("Attachments: read without creating the default"). ## What it does `AttachmentType::get` inserts the default the first time it is read. So a check like "does this chunk carry my data?" marks the holder as carrying the default from then on, and when the type has a codec, that default gets saved. This PR adds two reads that never insert anything: - `AttachmentType::has(host, holder) -> Result<bool, AttachmentError>`: whether a value is stored. Saved values count before they decode. - `AttachmentType::get_existing(host, holder) -> Result<Option<T>, AttachmentError>`: the value if stored, decoded lazily as with `get`, else `None`. Both are also on `Attachments`. A missing or non-server holder is still `MissingHolder`. Inside `attachments.rs`, `get` is now `get_existing` with the default as fallback. A private `insert_decoded` holds the encode-and-store step that `get` and `set` each repeated, and `remove` uses `has`. Saved NBT, unknown entries and undecodable values behave as before, and the existing tests are unchanged. A helper-only API fragment (`attachment_existing`) lets bundles name the API number that has these methods. `docs/native-mods.md` describes them. Not in this PR: a read-only holder access path. Reading a chunk or block-entity holder still marks its chunk for saving, but adds nothing to it. Client sync and `hasAttachments` are not included either. ## NeoForge counterpart `IAttachmentHolder.hasData(AttachmentType)` and `IAttachmentHolder.getExistingData(AttachmentType)` (an empty `Optional` when absent), from the public javadoc and the [data attachments](https://docs.neoforged.net/docs/datastorage/attachments/) page ("If attaching a default instance is not desired, a hasData check can be added"). Horizon's names drop the `Data` suffix, as `get`/`set`/`remove` already do. ## Tests Unit tests in `horizon-world` (`attachments::tests`): - an absent value stays absent after `has` and `get_existing`, and nothing gets encoded, while `get` does insert and encode; - a saved value reads back unchanged without changing the saved compound; - an undecodable saved value reports `Decode` and still round-trips. ## Tested in game https://github.com/arismoko/horizon-machine-sample (the recommended end-to-end route, used here), built at this head (cda58134c6) with the sample's `tools/horizon.py build`, plus a throwaway helper: when a machine is placed, it calls `get_existing` and `has` on one chunk attachment (`probe`) and `get` on another (`legacy`), both with codecs. On the server thread, through `horizon-ctl`: 1. Fresh world, machine placed: `before probe_has=false legacy_has=false`, then `after probe_has=false probe_existing=None legacy_has=true`. 2. The same world reopened: `before probe_has=false legacy_has=true`. So the `get` default was saved with the chunk and `get_existing` saved nothing. Setting `probe` then read back `Some(7)`. On a local dedicated server (`--dedicated --nogui`, one client joined, the machine placed with `setblock`), step 1 logged the same two lines. Step 2 wasn't repeated there. ## pr_ready **pr_ready** (`tools/pr_ready.py --quick --base cabe8b5de7d0b45541c923c4a6b3c5cb3e3686c7` at cda58134c6, 4 min 30 s) | Check | Result | | --- | --- | | Scope | ✅ 3 files, +116 −47 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ❌ 610 run, FAILED (failures=1, skipped=1) (40 s) | | rustfmt | ✅ (10 s) | | clippy | ✅ (1 min 48 s) | | tests: horizon-world | ✅ 393 passed (1 min 49 s) | | doc tests | ✅ 0 passed (1 s) | | content root tests | ➖ content root untouched | | mod API up to date | ➖ native hooks untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 371 affected, to run in CI (--quick) | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | The one Python failure is the base's `test_world_input_capture.test_native_split_mouse_turns_only_its_owner`, which fails the same way at cabe8b5de and passes on current main; this PR touches no Python or input code. 🤖 Generated with Dyna. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:37:39 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#7319
No description provided.