From 4084bcdf753c4e1c517ea1667b632016cfc7b110 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 18:54:11 +0100 Subject: [PATCH] Let a brush dust suspicious sand and gravel --- crates/horizon-client/src/game.rs | 25 ++ crates/horizon-client/src/game/brush.rs | 246 ++++++++++++++++++++ crates/horizon-client/src/game/item_draw.rs | 5 +- crates/horizon-client/src/game/lan.rs | 5 +- crates/horizon-client/src/game/remote.rs | 33 +++ crates/horizon-world/src/level/mod.rs | 3 +- crates/horizon-world/src/level/tick.rs | 7 + 7 files changed, 319 insertions(+), 5 deletions(-) create mode 100644 crates/horizon-client/src/game/brush.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 64d5b2766..b20de6ea8 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -18,6 +18,7 @@ mod boats; mod books; mod bossbars; mod breezes; +mod brush; mod campfire; mod campfire_douse; mod cartography; @@ -1653,6 +1654,8 @@ struct Game<'a> { boss_overlay: horizon_client::boss_overlay::BossOverlay, exec: commands::execute::ExecState, rand_sequences: horizon_client::random_sequences::RandomSequences, + /// `BrushableBlockEntity`'s brush counts of the blocks being brushed. + brushing: brush::Brushing, border: horizon_client::worldborder::WorldBorder, /// `WorldBorderRenderer` and its render state. border_renderer: horizon_client::border_render::BorderRenderer, @@ -2796,6 +2799,11 @@ impl Game<'_> { data, } => self.sculk_charge_event(pos, data), LevelEvent::LevelEvent { id: 3007, pos, .. } => self.shriek_event(pos), + LevelEvent::LevelEvent { + id: 3008, + pos, + data, + } => self.brush_complete_event([pos.x, pos.y, pos.z], data), LevelEvent::LevelEvent { id: 2004, pos, .. } => { self.spawner_burst([pos.x, pos.y, pos.z]) } @@ -2972,6 +2980,11 @@ impl Game<'_> { kind: "command_block", .. } => self.command_block_tick(pos), + LevelEvent::BlockEvent { + pos, + kind: "brushable", + .. + } => self.brushable_tick([pos.x, pos.y, pos.z]), LevelEvent::BlockEvent { pos, kind: "hatch", .. } => { @@ -3674,6 +3687,13 @@ impl Game<'_> { { return Ok(true); } + // BrushItem.useOn: brushing starts on the block aimed at (CONSUME). + if block_hit.is_some() && &*stack.item == "minecraft:brush" { + if self.using.is_none() { + self.using = Some((self.held_slot(), 0)); + } + return Ok(true); + } if !on_block && (self.raise_shield(self.held_slot()) || self.use_fishing_rod(&stack) @@ -3962,6 +3982,10 @@ impl Game<'_> { } return; } + if &*stack.item == "minecraft:brush" { + self.tick_brush(slot, ticks); + return; + } let Some(consumable) = info.consumable.as_ref() else { self.using = None; return; @@ -12540,6 +12564,7 @@ impl<'a> Game<'a> { boss_overlay: Default::default(), exec: Default::default(), rand_sequences: Default::default(), + brushing: Default::default(), border: Default::default(), border_renderer: Default::default(), border_state: Default::default(), diff --git a/crates/horizon-client/src/game/brush.rs b/crates/horizon-client/src/game/brush.rs new file mode 100644 index 000000000..c064a3153 --- /dev/null +++ b/crates/horizon-client/src/game/brush.rs @@ -0,0 +1,246 @@ +//! Brushing suspicious sand and gravel: `BrushItem.onUseTick` and +//! `BrushableBlockEntity`. The block's loot table stays in its saved block +//! entity (like a decorated pot's) until the first counted stroke rolls it. +use super::{Game, Hit, decorated_pot, spill, view_vector}; +use horizon_client::{ + particles::{ParticleOptions, ParticleType}, + save, +}; +use horizon_core::random::RandomSource; +use std::collections::HashMap; + +/// `BrushItem.USE_DURATION`. +const USE_DURATION: u32 = 200; + +/// A brushable block's counters. Vanilla never saves them: the block's +/// `dusted` state is all that is left after a reload. +#[derive(Default)] +pub(super) struct BrushState { + count: i32, + resets_at: i64, + cooldown_ends_at: i64, + hit_face: Option<[i32; 3]>, +} + +/// `BrushableBlockEntity.getCompletionState`: the `dusted` property. +fn completion_state(count: i32) -> i32 { + match count { + 0 => 0, + 1..=2 => 1, + 3..=5 => 2, + _ => 3, + } +} + +pub(super) type Brushing = HashMap<[i32; 3], BrushState>; + +impl Game<'_> { + /// `BrushItem.onUseTick` for a brush used `ticks` ticks so far, the + /// hand of `slot` acting. + pub(super) fn tick_brush(&mut self, slot: usize, ticks: u32) { + // No block under the crosshair: releaseUsingItem. + let Some(hit) = self.pick() else { + self.using = None; + return; + }; + let elapsed = ticks + 1; + // The last tick before the backswing. + if elapsed % 10 == 5 { + self.brush_stroke(&hit, slot); + } + // The use ends after 200 ticks; the held button starts it again. + self.using = (elapsed < USE_DURATION).then_some((slot, elapsed)); + } + + /// One stroke on the block aimed at: dust (this window only), the brush + /// sound, and the block entity's count. The brush wears when the block + /// changed state. + fn brush_stroke(&mut self, hit: &Hit, slot: usize) { + let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); + let name = self.res.facts().get(state).name.clone(); + let (sound, brushable) = match &*name { + "minecraft:suspicious_sand" => ("minecraft:item.brush.brushing.sand", true), + "minecraft:suspicious_gravel" => ("minecraft:item.brush.brushing.gravel", true), + _ => ("minecraft:item.brush.brushing.generic", false), + }; + if self.lan_acting.is_none() { + self.brush_dust(hit, slot == super::shield::OFFHAND_SLOT); + } + let at = hit.pos.map(|v| f64::from(v) + 0.5); + self.audio.play(sound, Some(at), 1.0, 1.0); + if brushable && self.brush_block(hit.pos, hit.face) { + self.damage_held(1); + } + } + + /// `BrushItem.spawnDustParticles`. + pub(super) fn brush_dust(&mut self, hit: &Hit, off_hand: bool) { + let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); + let Some((sprite, tint)) = self.particle_sprite(state, hit.pos) else { + return; + }; + let options = ParticleOptions::block(ParticleType::Block, sprite, tint); + // The arm that brushes: the main arm, or the other one for the off hand. + let flip = if off_hand == self.settings.main_hand_left { + 1.0 + } else { + -1.0 + }; + let view = view_vector(self.yaw, self.pitch); + // DustParticlesDelta.fromDirection. + let (dx, dz) = match hit.face { + [0, _, 0] => (view[2], -view[0]), + [0, 0, -1] => (1.0, -0.1), + [0, 0, 1] => (-1.0, 0.1), + [-1, 0, 0] => (-0.1, -1.0), + _ => (0.1, 1.0), + }; + let count = 7 + self.particles.level_random().next_int_bound(5).unwrap_or(0); + let x = hit.point[0] - if hit.face == [-1, 0, 0] { 1.0e-6 } else { 0.0 }; + let z = hit.point[2] - if hit.face == [0, 0, -1] { 1.0e-6 } else { 0.0 }; + for _ in 0..count { + let random = self.particles.level_random(); + let v = [ + dx * flip * 3.0 * random.next_double(), + 0.0, + dz * flip * 3.0 * random.next_double(), + ]; + self.particles.add(&options, [x, hit.point[1], z], v); + } + } + + /// `BrushableBlockEntity.brush`: whether the block changed (the brush wears). + fn brush_block(&mut self, pos: [i32; 3], face: [i32; 3]) -> bool { + let time = self.world.game_time; + let mut brush = self.brushing.remove(&pos).unwrap_or_default(); + brush.hit_face.get_or_insert(face); + brush.resets_at = time + 40; + if time < brush.cooldown_ends_at { + self.brushing.insert(pos, brush); + return false; + } + brush.cooldown_ends_at = time + 10; + self.unpack_brush_loot(pos); + let before = completion_state(brush.count); + brush.count += 1; + if brush.count >= 10 { + self.brushing_completed(pos, brush.hit_face); + return true; + } + self.schedule_brushable_tick(pos); + let after = completion_state(brush.count); + self.brushing.insert(pos, brush); + if before != after { + self.set_dusted(pos, after); + } + false + } + + /// `BrushableBlockEntity.unpackLootTable`: the table's roll becomes the + /// block entity's item. + fn unpack_brush_loot(&mut self, pos: [i32; 3]) { + let Some(mut tag) = spill::saved_entity(&self.world, pos) else { + return; + }; + if tag.get("LootTable").is_none() { + return; + } + let loot = horizon_client::structure_blocks::loot_data(&self.options.resources); + decorated_pot::unpack_loot( + &mut tag, + loot.as_deref(), + &mut self.world.random, + &mut self.rand_sequences, + self.level.seed, + ); + spill::put_saved_entity(&mut self.world, pos, tag); + } + + /// `BrushableBlockEntity.brushingCompleted`: the item drops out of the + /// brushed face and the block turns into its sand or gravel. + fn brushing_completed(&mut self, pos: [i32; 3], face: Option<[i32; 3]>) { + let state = self.world.block(pos[0], pos[1], pos[2]); + let turns_into = match &*self.res.facts().get(state).name { + "minecraft:suspicious_sand" => "minecraft:sand", + "minecraft:suspicious_gravel" => "minecraft:gravel", + _ => "minecraft:air", + }; + // dropContent: the item at the middle of the block beside that face. + let item = spill::saved_entity(&self.world, pos) + .and_then(|tag| tag.get("item").and_then(save::item_from_nbt)); + if let Some(mut item) = item { + let face = face.unwrap_or([0, 1, 0]); + let take = 10 + self.world.random.next_int_bound(21).unwrap_or(0); + item.count = item.count.min(take as u32); + let at = [ + f64::from(pos[0] + face[0]) + 0.5, + f64::from(pos[1] + face[1]) + 0.625, + f64::from(pos[2] + face[2]) + 0.5, + ]; + self.spawn_item(item, at, [0.0; 3], 0); + } + // Level event 3008 for every player, then the block change. + self.level_event_at(3008, pos, state.0 as i32); + if let Some(base) = self.pack.assets.registry.default_state(turns_into) { + let _ = self.level_set(pos, base); + } + } + + fn set_dusted(&mut self, pos: [i32; 3], dusted: i32) { + let registry = self.pack.assets.registry; + let Some(mut state) = registry + .state(self.world.block(pos[0], pos[1], pos[2])) + .cloned() + else { + return; + }; + state.properties.insert("dusted".into(), dusted.to_string()); + if let Ok(state) = registry.resolve(&state) { + let _ = self.level_set(pos, state); + } + } + + fn schedule_brushable_tick(&mut self, pos: [i32; 3]) { + let at = horizon_core::position::BlockPos::new(pos[0], pos[1], pos[2]); + self.with_level(|b, level, ticks| b.schedule_tick(level, ticks, at, 2)); + } + + /// `BrushableBlockEntity.checkReset`, from `BrushableBlock.tick`: a block + /// left alone loses two counts, again every four ticks. + pub(super) fn brushable_tick(&mut self, pos: [i32; 3]) { + let time = self.world.game_time; + let Some(mut brush) = self.brushing.remove(&pos) else { + return; + }; + let before = completion_state(brush.count); + if brush.count != 0 && time >= brush.resets_at { + brush.count = (brush.count - 2).max(0); + brush.resets_at = time + 4; + } + let after = completion_state(brush.count); + if brush.count != 0 { + self.brushing.insert(pos, brush); + self.schedule_brushable_tick(pos); + } + if before != after { + self.set_dusted(pos, after); + } + } + + /// Level event 3008 (`LevelEventHandler`): the finished block's sound + /// and break particles. + pub(super) fn brush_complete_event(&mut self, pos: [i32; 3], data: i32) { + let state = horizon_world::registry::BlockStateId(data as u32); + let sound = match &*self.res.facts().get(state).name { + "minecraft:suspicious_sand" => "minecraft:item.brush.brushing.sand.complete", + "minecraft:suspicious_gravel" => "minecraft:item.brush.brushing.gravel.complete", + _ => "", + }; + if !sound.is_empty() { + let at = pos.map(|v| f64::from(v) + 0.5); + // Each client plays it from the event itself. + self.audio.play_unshared(sound, Some(at), 1.0, 1.0); + } + self.destroy_particles(pos, state); + } +} diff --git a/crates/horizon-client/src/game/item_draw.rs b/crates/horizon-client/src/game/item_draw.rs index b6b7e2fb7..8deb483c1 100644 --- a/crates/horizon-client/src/game/item_draw.rs +++ b/crates/horizon-client/src/game/item_draw.rs @@ -179,8 +179,9 @@ fn use_duration(components: &Components) -> i32 { | "minecraft:crossbow" | "minecraft:trident" | "minecraft:spyglass" - | "minecraft:goat_horn" - | "minecraft:brush" => 72_000, + | "minecraft:goat_horn" => 72_000, + // BrushItem.USE_DURATION. + "minecraft:brush" => 200, _ => 0, } } diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 5292cd31b..e4c9d19a7 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -1187,9 +1187,10 @@ impl Game<'_> { match e { horizon_world::level::LevelEvent::LevelEvent { id, pos, data } => { // The acting guest does not predict the egg's teleport - // particles, bone meal's growth, or anvil result sounds + // particles, bone meal's growth, anvil result sounds, or a + // finished brushing // (these levelEvent calls name no excluded player). - let except = if matches!(*id, 2015 | 1505 | 1029 | 1030) { + let except = if matches!(*id, 2015 | 1505 | 1029 | 1030 | 3008) { None } else { self.lan_acting diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 3cfbd15e3..5c25fc9b8 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -4063,6 +4063,15 @@ impl Game<'_> { } if reacts { self.predict_use(&changes, seq); + } else if held.as_ref().is_some_and(|s| &*s.item == "minecraft:brush") { + // BrushItem.useOn: brushing starts here (CONSUME, no swing); the + // server brushes and the dust is this client's own. + self.with_hand(hand, |g| { + if g.using.is_none() { + g.using = Some((g.held_slot(), 0)); + } + }); + return true; } else if let Some(stack) = held.as_ref() && self.predict_item_on_block(stack, &hit) { @@ -4383,6 +4392,30 @@ impl Game<'_> { // Another hotbar slot picked: LivingEntity.updatingUsingItem sees a // different stack in the hand. The off hand keeps its item. let in_hand = slot == self.selected || slot == super::shield::OFFHAND_SLOT; + // BrushItem.onUseTick: with no block aimed the use ends (the server + // ends it too); a stroke shows its dust. Its sounds are the server's. + if stack + .as_ref() + .is_some_and(|s| &*s.item == "minecraft:brush") + { + let elapsed = ticks + 1; + let hit = self.pick().filter(|_| in_hand); + if let Some(hit) = &hit + && elapsed % 10 == 5 + { + self.brush_dust(hit, slot == super::shield::OFFHAND_SLOT); + } + if hit.is_none() && in_hand { + self.send(S::PlayerAction(play::PlayerAction { + action: 6, + pos: [0; 3], + direction: 0, + sequence: 0, + })); + } + self.using = hit.and(Some((slot, elapsed))).filter(|_| elapsed < 200); + return; + } // A drawn bow, a charging crossbow, a raised shield...: held until // the button lets go. if stack.as_ref().is_some_and(|s| self.held_until_released(s)) { diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index a384b38b7..d9c63a493 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -252,7 +252,8 @@ pub enum LevelEvent { /// event follow as their own events; the caller removes the block. PrimeTnt { pos: BlockPos, rot: f64 }, /// Block-entity or entity side effects the caller implements: `"dispense"` - /// (dispenser/dropper/crafter scheduled tick), `"hatch"` (frogspawn + /// (dispenser/dropper/crafter scheduled tick), `"brushable"` (a suspicious + /// block's scheduled tick: `checkReset`), `"hatch"` (frogspawn /// hatched into tadpoles; the block is already destroyed), `"portal_spawn"` /// (a nether portal won its zombified piglin roll; the caller checks /// `anyPlayerCloseEnoughForSpawning` and spawns below the portal). diff --git a/crates/horizon-world/src/level/tick.rs b/crates/horizon-world/src/level/tick.rs index 29f53e053..186f64bd7 100644 --- a/crates/horizon-world/src/level/tick.rs +++ b/crates/horizon-world/src/level/tick.rs @@ -127,6 +127,13 @@ impl Sim<'_, L> { } Class::FallingBlock => self.falling_block_tick(pos, state), Class::Brushable => { + // BrushableBlock.tick checks the brush reset first (the + // caller keeps the brush counts). + self.emit(LevelEvent::BlockEvent { + pos, + state, + kind: "brushable", + }); if self.is_free(self.below_state(pos)) && pos.y >= self.level.min_y() { self.falling_block_fall(pos, state); }