[PR #6914] [MERGED] Native mods: hear each villager trade (TradeWithVillagerEvent) #7188

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

📋 Pull Request Information

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

Base: main ← Head: native/trade-with-villager


📝 Commits (2)

  • 0dd9865 Native mods: hear each villager trade (native_trade_with_villager)
  • 60191c6 Native trade hook: each trade's own offer snapshot; xtask stub; docs

📊 Changes

8 files changed (+127 additions, -17 deletions)

View changed files

📝 crates/horizon-client/src/game/native_hooks.rs (+19 -0)
📝 crates/horizon-client/src/game/trading.rs (+19 -14)
📝 crates/horizon-client/src/merchant.rs (+27 -3)
📝 crates/horizon-client/src/mod_api.rs (+1 -0)
📝 crates/horizon-client/src/mod_api/generated.rs (+37 -0)
📝 docs/native-mods.md (+7 -0)
➕ tools/native-hook-fragments/trade_with_villager.json (+16 -0)
📝 xtask/src/mod_api.rs (+1 -0)

📄 Description

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

Part of #4953: NeoForge's TradeWithVillagerEvent.

What

New native hook native_trade_with_villager(merchant, kind, offer), side server, contract listen.

It fires once per trade a server player makes with a villager or a wandering trader (vanilla MerchantResultSlot.onTake → AbstractVillager.notifyTrade). A shift-click that trades several times fires once for each trade.

  • merchant is the trader's entity id.
  • kind is minecraft:villager or minecraft:wandering_trader.
  • offer is the MerchantOffer, now re-exported from mod_api, as it was right after that trade: its costs, result, uses with this trade counted, max_uses and xp.

The acting player's state is swapped in, so native_acting_player names them. It fires on the server thread, for LAN guests and on the dedicated server. A pure client stays silent (gated on lan_acting).

How it works: merchant::Event::Traded now carries a copy of the offer as notify_trade left it. Before this, the game read the session's final offers after the click. Then a shift-click's earlier trades would have reported the final uses, and the prices from after a level-up the click triggered. merchant_apply passes those copies to merchant_traded. That function keeps the existing statistic and villager_trade trigger exactly as they were, then calls the hook.

Differences from NeoForge, both documented:

  • The hook runs once the whole click is done, with its payments taken. NeoForge fires during each take.
  • The hook is not called if the trader has already left the level (as the trigger already behaved).

Also: xtask's mod API check gets a MerchantOffer stand-in.

Tests

  • Real game, server thread and dedicated server, using a throwaway trait mod (not committed) driven by horizon-ctl in a private nested KWin. A NoAI farmer villager trades 20 wheat → 1 emerald. The player holds 60 wheat, selects the offer and shift-clicks the result.
    • single player (server thread): the server counted 0 wheat and 3 emeralds; the hook ran 3 times with kind=minecraft:villager, uses = 1, 2, 3, result=minecraft:emerald x1. PASS
    • dedicated server: same counts, and the same three hook calls with the same uses (1, 2, 3). PASS
    • Both runs were repeated after the snapshot change (final code), and both passed again.
  • Unit test merchant::tests::each_trade_event_keeps_its_own_offer: three trades over two offers; each event keeps its own uses and cost.
  • python3 tools/pr_ready.py --quick --base hr/main passes Python tests, clippy, 2053 horizon-client/-lib/xtask tests, doc tests and the mod API check. rustfmt failed on the first run; I fixed it in the same commit, and cargo fmt --all --check is now clean.
  • An independent Opus review found no blockers. Its findings are addressed: per-trade snapshots instead of rewinding uses, accurate comments on the stat/trigger order, and wandering_trader in the docs.

Not run: parity harnesses (--quick; CI's parity job picks the affected ones).

🤖 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/6914 **Author:** [@levzzz5154](https://github.com/levzzz5154) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `native/trade-with-villager` --- ### 📝 Commits (2) - [`0dd9865`](https://github.com/horizon-rust/horizon/commit/0dd9865328c37201f50236ad85f9b9c4770075c2) Native mods: hear each villager trade (native_trade_with_villager) - [`60191c6`](https://github.com/horizon-rust/horizon/commit/60191c6b70eed6decfa55e23fd2da6018289846a) Native trade hook: each trade's own offer snapshot; xtask stub; docs ### 📊 Changes **8 files changed** (+127 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/native_hooks.rs` (+19 -0) 📝 `crates/horizon-client/src/game/trading.rs` (+19 -14) 📝 `crates/horizon-client/src/merchant.rs` (+27 -3) 📝 `crates/horizon-client/src/mod_api.rs` (+1 -0) 📝 `crates/horizon-client/src/mod_api/generated.rs` (+37 -0) 📝 `docs/native-mods.md` (+7 -0) ➕ `tools/native-hook-fragments/trade_with_villager.json` (+16 -0) 📝 `xtask/src/mod_api.rs` (+1 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 personally. Part of #4953: NeoForge's `TradeWithVillagerEvent`. ## What New native hook `native_trade_with_villager(merchant, kind, offer)`, side server, contract `listen`. It fires once per trade a server player makes with a villager or a wandering trader (vanilla `MerchantResultSlot.onTake` → `AbstractVillager.notifyTrade`). A shift-click that trades several times fires once for each trade. - `merchant` is the trader's entity id. - `kind` is `minecraft:villager` or `minecraft:wandering_trader`. - `offer` is the `MerchantOffer`, now re-exported from `mod_api`, as it was right after that trade: its costs, `result`, `uses` with this trade counted, `max_uses` and `xp`. The acting player's state is swapped in, so `native_acting_player` names them. It fires on the server thread, for LAN guests and on the dedicated server. A pure client stays silent (gated on `lan_acting`). How it works: `merchant::Event::Traded` now carries a copy of the offer as `notify_trade` left it. Before this, the game read the session's final offers after the click. Then a shift-click's earlier trades would have reported the final `uses`, and the prices from after a level-up the click triggered. `merchant_apply` passes those copies to `merchant_traded`. That function keeps the existing statistic and `villager_trade` trigger exactly as they were, then calls the hook. Differences from NeoForge, both documented: - The hook runs once the whole click is done, with its payments taken. NeoForge fires during each take. - The hook is not called if the trader has already left the level (as the trigger already behaved). Also: xtask's mod API check gets a `MerchantOffer` stand-in. ## Tests - **Real game, server thread and dedicated server**, using a throwaway trait mod (not committed) driven by horizon-ctl in a private nested KWin. A NoAI farmer villager trades 20 wheat → 1 emerald. The player holds 60 wheat, selects the offer and shift-clicks the result. - single player (server thread): the server counted 0 wheat and 3 emeralds; the hook ran 3 times with `kind=minecraft:villager`, `uses` = 1, 2, 3, `result=minecraft:emerald x1`. **PASS** - dedicated server: same counts, and the same three hook calls with the same `uses` (1, 2, 3). **PASS** - Both runs were repeated after the snapshot change (final code), and both passed again. - Unit test `merchant::tests::each_trade_event_keeps_its_own_offer`: three trades over two offers; each event keeps its own uses and cost. - `python3 tools/pr_ready.py --quick --base hr/main` passes Python tests, clippy, 2053 horizon-client/-lib/xtask tests, doc tests and the mod API check. rustfmt failed on the first run; I fixed it in the same commit, and `cargo fmt --all --check` is now clean. - An independent Opus review found no blockers. Its findings are addressed: per-trade snapshots instead of rewinding `uses`, accurate comments on the stat/trigger order, and `wandering_trader` in the docs. Not run: parity harnesses (`--quick`; CI's parity job picks the affected ones). 🤖 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:35:26 +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#7188
No description provided.