[PR #6396] [MERGED] Native mods: a mod loot kind sees where the roll happens (origin, block entity, level) #6845

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/6396
Author: @ErikKalikas
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/1/2026
Merged by: @ludovic111

Base: main ← Head: mod-loot-context


📝 Commits (1)

  • 26103c1 Native mods: a mod loot kind sees where the roll happens

📊 Changes

7 files changed (+232 additions, -33 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+62 -17)
📝 crates/horizon-client/src/game/creatures.rs (+5 -1)
📝 crates/horizon-client/src/game/explosion.rs (+15 -9)
📝 crates/horizon-items/src/items.rs (+1 -0)
📝 crates/horizon-world/src/loot.rs (+135 -6)
📝 docs/native-mods.md (+6 -0)
➕ tools/native-hook-fragments/loot_context_place.json (+8 -0)

📄 Description

🤖 Opened by ErikKalikas's AI agent (Claude Code, Claude Opus 5.5), not by ErikKalikas personally.

What and why

A loot function or condition of a mod's own kind (#6142) saw the entity, the damage, the killer, the tool, the block, the luck, the biome and a seed, but not where the roll happens. So a mod couldn't write a condition that drops something only at night or in one dimension, or read the broken block's entity.

NativeLootContext gains place: Option<Rc<NativeLootPlace>>:

  • origin: LootContextParams.ORIGIN, the broken block's centre or the dead mob's position;
  • block_entity: LootContextParams.BLOCK_ENTITY, the broken block's entity as it was before the break, saved with its id, position and Items (saveWithFullMetadata, through save::encode_block_entity);
  • level: what vanilla's own conditions read from LootContext.getLevel: the dimension, the game time, the time of day, rain and thunder (TimeCheck, WeatherCheck).

The game sets it with loot::with_native_loot_place(place, || roll) around three rolls: a block a player breaks, a block an explosion breaks, and a mob's death loot. Both loot engines put it in the context they hand a mod: the items engine (block drops) and the main engine (death loot). It is a thread-local for the length of the roll, so none of the ~60 callers of the two engines' entry points change. The place before comes back when the roll returns or panics. Other rolls (chests, fishing, /loot) carry None for now, as the docs say.

The block entity is captured before player_destroy_block removes it, the same point where pending.record already copies it for the drops. In creative it isn't captured, since nothing drops. A place changes nothing a table rolls: same items, same random.

One limit, unchanged here: in block drops only the condition hook answers, not the function hook (#6199). So after this PR a mod can test a machine's contents in its drop's condition, but carrying them into the drop needs #6199.

API fragment loot_context_place (no hooks, a new field and helper). Fixes #6200.

Kind

  • feature: something new for players (listed under New in the release notes)

Vanilla reference

LootContextParams.ORIGIN, LootContextParams.BLOCK_ENTITY and LootContext.getLevel, as read by TimeCheck.test (the clock's total ticks), WeatherCheck.test (isRaining, isThundering) and LocationCheck.test (ORIGIN). Block.getDrops rolls a block's table at Vec3.atCenterOf(pos). Level.isRaining and isThundering use the same thresholds as Horizon's level access (with_level: rain above 0.2, thunder above 0.9). Horizon-only API, no parity harness: vanilla's own tables never reach a mod's kind. Two unit tests in loot.rs (native_kinds_tests): a place holds for its roll only (nested, and after a panic), and a place leaves a roll as it was, with and without a mod's kind in the table.

Other projects' code

none

Self-check

pr_ready (tools/pr_ready.py --quick --base upstream/main on kased-h610mkddr4, against upstream/main at 2c9b58e72, 4 min 22 s)

Check Result
Scope ✅ 7 files, +232 −33
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 594 passed (26 s)
rustfmt ✅ (6 s)
clippy ✅ (57 s)
tests: horizon-client, horizon-items, horizon-world ✅ 999 passed (1 min 40 s)
doc tests ✅ 0 passed (2 s)
content root tests ✅ 21 passed (1 min 7 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 310 affected, to run in CI (--quick): ambient_sound, armor_stand_frost, arrow, arrow_wind_charge, audio_attributes, auto_jump, blending, block_entity_anim, block_entity_render, block_entity_tickers, block_particles, block_tint and 298 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

In the game. A throwaway world data pack replaced blocks/stone, blocks/chest and entities/cow with tables behind a mod condition (testmod:night, testmod:has_items, testmod:log), and a throwaway body in native_loot_condition logged ctx.place and answered. The same results on the server thread and on a dedicated server (horizon-client --dedicated --nogui, a client joined with horizon-ctl launch --server):

Roll What the condition saw Drop
Stone broken at noon origin [2.5, y + 0.5, 0.5], overworld, day_time 6000 none (condition false)
Stone broken at midnight day_time 18000 cobblestone
Chest with 3 diamonds block_entity: id minecraft:chest, Items with the 3 diamonds chest + 3 diamonds
Cow killed origin [4.5, y, 2.5], its position leather
Stone in a TNT blast origin at the stone's centre, night cobblestone

Found while testing, not changed here: when a block's table has a random_sequence (vanilla's tables all do), a player's break rolls the game's own table, not the world's data pack one (block_loot_at reads the sequence from the world's tables, then rolls self.res.items()). The check's block tables leave random_sequence out to reach the data pack; the bug is left for its own issue.

🤖 Generated with Claude Code


🔄 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/6396 **Author:** [@ErikKalikas](https://github.com/ErikKalikas) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `mod-loot-context` --- ### 📝 Commits (1) - [`26103c1`](https://github.com/horizon-rust/horizon/commit/26103c104e682c11942822c31a5c4e7671bccc75) Native mods: a mod loot kind sees where the roll happens ### 📊 Changes **7 files changed** (+232 additions, -33 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+62 -17) 📝 `crates/horizon-client/src/game/creatures.rs` (+5 -1) 📝 `crates/horizon-client/src/game/explosion.rs` (+15 -9) 📝 `crates/horizon-items/src/items.rs` (+1 -0) 📝 `crates/horizon-world/src/loot.rs` (+135 -6) 📝 `docs/native-mods.md` (+6 -0) ➕ `tools/native-hook-fragments/loot_context_place.json` (+8 -0) </details> ### 📄 Description > 🤖 Opened by ErikKalikas's AI agent (Claude Code, Claude Opus 5.5), not by ErikKalikas personally. ## What and why A loot function or condition of a mod's own kind (#6142) saw the entity, the damage, the killer, the tool, the block, the luck, the biome and a seed, but not where the roll happens. So a mod couldn't write a condition that drops something only at night or in one dimension, or read the broken block's entity. `NativeLootContext` gains `place: Option<Rc<NativeLootPlace>>`: - `origin`: `LootContextParams.ORIGIN`, the broken block's centre or the dead mob's position; - `block_entity`: `LootContextParams.BLOCK_ENTITY`, the broken block's entity as it was before the break, saved with its id, position and `Items` (`saveWithFullMetadata`, through `save::encode_block_entity`); - `level`: what vanilla's own conditions read from `LootContext.getLevel`: the dimension, the game time, the time of day, rain and thunder (`TimeCheck`, `WeatherCheck`). The game sets it with `loot::with_native_loot_place(place, || roll)` around three rolls: a block a player breaks, a block an explosion breaks, and a mob's death loot. Both loot engines put it in the context they hand a mod: the items engine (block drops) and the main engine (death loot). It is a thread-local for the length of the roll, so none of the ~60 callers of the two engines' entry points change. The place before comes back when the roll returns or panics. Other rolls (chests, fishing, `/loot`) carry `None` for now, as the docs say. The block entity is captured before `player_destroy_block` removes it, the same point where `pending.record` already copies it for the drops. In creative it isn't captured, since nothing drops. A place changes nothing a table rolls: same items, same random. One limit, unchanged here: in block drops only the condition hook answers, not the function hook (#6199). So after this PR a mod can *test* a machine's contents in its drop's condition, but carrying them into the drop needs #6199. API fragment `loot_context_place` (no hooks, a new field and helper). Fixes #6200. ## Kind - [x] feature: something new for players (listed under New in the release notes) ## Vanilla reference `LootContextParams.ORIGIN`, `LootContextParams.BLOCK_ENTITY` and `LootContext.getLevel`, as read by `TimeCheck.test` (the clock's total ticks), `WeatherCheck.test` (`isRaining`, `isThundering`) and `LocationCheck.test` (`ORIGIN`). `Block.getDrops` rolls a block's table at `Vec3.atCenterOf(pos)`. `Level.isRaining` and `isThundering` use the same thresholds as Horizon's level access (`with_level`: rain above 0.2, thunder above 0.9). Horizon-only API, no parity harness: vanilla's own tables never reach a mod's kind. Two unit tests in `loot.rs` (`native_kinds_tests`): a place holds for its roll only (nested, and after a panic), and a place leaves a roll as it was, with and without a mod's kind in the table. ## Other projects' code none ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base upstream/main` on kased-h610mkddr4, against upstream/main at 2c9b58e72, 4 min 22 s) | Check | Result | | --- | --- | | Scope | ✅ 7 files, +232 −33 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 594 passed (26 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (57 s) | | tests: horizon-client, horizon-items, horizon-world | ✅ 999 passed (1 min 40 s) | | doc tests | ✅ 0 passed (2 s) | | content root tests | ✅ 21 passed (1 min 7 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 310 affected, to run in CI (--quick): ambient_sound, armor_stand_frost, arrow, arrow_wind_charge, audio_attributes, auto_jump, blending, block_entity_anim, block_entity_render, block_entity_tickers, block_particles, block_tint and 298 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | **In the game.** A throwaway world data pack replaced `blocks/stone`, `blocks/chest` and `entities/cow` with tables behind a mod condition (`testmod:night`, `testmod:has_items`, `testmod:log`), and a throwaway body in `native_loot_condition` logged `ctx.place` and answered. The same results on the server thread and on a dedicated server (`horizon-client --dedicated --nogui`, a client joined with `horizon-ctl launch --server`): | Roll | What the condition saw | Drop | | --- | --- | --- | | Stone broken at noon | origin `[2.5, y + 0.5, 0.5]`, `overworld`, `day_time` 6000 | none (condition false) | | Stone broken at midnight | `day_time` 18000 | cobblestone | | Chest with 3 diamonds | `block_entity`: `id` `minecraft:chest`, `Items` with the 3 diamonds | chest + 3 diamonds | | Cow killed | origin `[4.5, y, 2.5]`, its position | leather | | Stone in a TNT blast | origin at the stone's centre, night | cobblestone | Found while testing, not changed here: when a block's table has a `random_sequence` (vanilla's tables all do), a player's break rolls the game's own table, not the world's data pack one (`block_loot_at` reads the sequence from the world's tables, then rolls `self.res.items()`). The check's block tables leave `random_sequence` out to reach the data pack; the bug is left for its own issue. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:27:55 +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#6845
No description provided.