Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Native mods: a player's item losing durability, changeable (native_item_damaged)

Detimese8b62addbedb
diff --git a/crates/horizon-client/src/game/enchant_effects.rs b/crates/horizon-client/src/game/enchant_effects.rs
index 1828ff9ce..019ce3d64 100644
--- a/crates/horizon-client/src/game/enchant_effects.rs
+++ b/crates/horizon-client/src/game/enchant_effects.rs
@@ -89,7 +89,17 @@ impl Game<'_> {
/// `ItemStack.hurtAndBreak` for the player's `stack`: the wear Unbreaking
/// leaves, and `ItemDurabilityTrigger` when some is left (`applyDamage`).
pub(super) fn wear(&mut self, stack: &ItemStack, amount: u32) -> u32 {
- let loss = self.durability_loss(stack, amount);
+ let mut loss = self.durability_loss(stack, amount);
+ // The mods hear a server player's wear once Unbreaking has had its say,
+ // and may change it or stop it.
+ if loss > 0
+ && let (Some(uuid), Some(name)) = (self.lan_acting_uuid, self.lan_acting_name.clone())
+ {
+ let mut points = loss as i32;
+ let mut cancel = false;
+ self.native_item_damaged(uuid, &name, &stack.item, &mut points, &mut cancel);
+ loss = if cancel { 0 } else { points.max(0) as u32 };
+ }
if loss > 0 {
self.trigger_item_durability(stack, stack.damage + loss);
}
diff --git a/crates/horizon-client/src/game/native_hooks.rs b/crates/horizon-client/src/game/native_hooks.rs
index 14b884c4a..3a3941d9c 100644
--- a/crates/horizon-client/src/game/native_hooks.rs
+++ b/crates/horizon-client/src/game/native_hooks.rs
@@ -3485,6 +3485,27 @@ impl Game<'_> {
) {
let _ = (context, drops);
}
+ /// The item `item` of a server player is about to lose `amount` points of
+ /// durability (`ItemStack.hurtAndBreak`; Bukkit's `PlayerItemDamageEvent`):
+ /// the tool that mines or hits, the armour that takes a hit, a shield, a
+ /// rod, an elytra. `amount` is what is left after Unbreaking; change it
+ /// (0 or less leaves the item whole) or set `cancel` for no wear at all. It
+ /// is not heard for a creative player's tools (they take no wear) nor when
+ /// Unbreaking already saved the item, nor for the wear of a mob's armour or
+ /// of a tool a dispenser uses, nor the host of a LAN world. The player's
+ /// state is swapped in, so `native_acting_player`, `native_tell` and the
+ /// item helpers reach them. Once for each wear, on a server.
+ /// @API:native_item_damaged@.
+ pub(super) fn native_item_damaged(
+ &mut self,
+ player: u128,
+ name: &str,
+ item: &str,
+ amount: &mut i32,
+ cancel: &mut bool,
+ ) {
+ let _ = (player, name, item, amount, cancel);
+ }
/// `player` threw `count` of `item` (`Player.drop`: the drop key, a
/// menu's throw...), not what scatters when they die. It only hears.
/// Server-side. API 40.
diff --git a/docs/native-mods.md b/docs/native-mods.md
index 00898d73a..0e5eeccde 100644
--- a/docs/native-mods.md
+++ b/docs/native-mods.md
@@ -1212,6 +1212,11 @@ For mods that run where the world is simulated: a dedicated server, a LAN host,
`native_broadcast(text)`, to every player and the server's
log; `native_feedback(text)`, the command's answer to the player whose command it is; `native_world_dir()`, the
open world's folder, where a server mod keeps its data in a folder of its own so the world's backups carry it.
+- **`native_item_damaged(player, name, item, amount, cancel)`**: an item of a player is about to lose durability, on a
+ server (`ItemStack.hurtAndBreak`: tools, armour, shield, rod, elytra), after Unbreaking has had its say. Change
+ `amount` (0 leaves the item whole) or set `cancel` for no wear. Not heard for a creative player or when Unbreaking
+ saved the item, nor for a mob's armour, a dispenser's tool or the host of a LAN world. The player's state is swapped
+ in, so `native_acting_player`, `native_tell` and the item helpers reach them.
- Items (API 9), of the acting player (inside `native_perform_command`, whose player is swapped in): `native_held()`
(the main hand's item, count, and whether it is plain), `native_count_items(item)`, `native_carried_items()`,
`native_take_items(item, count)` (the main hand first, then as `Inventory.add` orders slots),
diff --git a/tools/native-hook-fragments/native_item_damaged.json b/tools/native-hook-fragments/native_item_damaged.json
new file mode 100644
index 000000000..17ebc4bf9
--- /dev/null
+++ b/tools/native-hook-fragments/native_item_damaged.json
@@ -0,0 +1,12 @@
+{
+ "hooks": [
+ {
+ "name": "item_damaged",
+ "target": "horizon_client::game::native_hooks::Game<'_>::native_item_damaged",
+ "neoforge": "none (vanilla: ItemStack.hurtAndBreak; PlayerDestroyItemEvent is only the break)",
+ "side": "server"
+ }
+ ],
+ "rust_summary": "a player's item losing durability, changeable and cancellable (`native_item_damaged`).",
+ "doc_summary": "a player's item losing durability: `native_item_damaged(player, name, item, amount, cancel)` is heard where `ItemStack.hurtAndBreak` wears the item of a player of a server (Bukkit's `PlayerItemDamageEvent`), after Unbreaking, with the points about to be lost. A mod changes `amount` or sets `cancel` for no wear. The player's state is swapped in. Bundles that use it need `\"hooks\": @API:native_item_damaged@`."
+}