diff --git a/crates/horizon-client/src/advancements.rs b/crates/horizon-client/src/advancements.rs index f052bd202..1ed11da4d 100644 --- a/crates/horizon-client/src/advancements.rs +++ b/crates/horizon-client/src/advancements.rs @@ -437,6 +437,8 @@ pub struct EntityView { /// mainhand, offhand, head, chest, legs, feet, body. pub equipment: [Option; 7], pub vehicle: Option>, + /// Player: what it looks at within 100 blocks (`PlayerPredicate.lookingAt`). + pub looking_at: Option>, pub effects: Vec<(String, i32, i32)>, /// Player: game mode name. pub gamemode: Option, @@ -458,7 +460,7 @@ fn range_f(v: &Value, x: f64) -> bool { _ => false, } } -fn range_i(v: &Value, x: i64) -> bool { +pub fn range_i(v: &Value, x: i64) -> bool { range_f(v, x as f64) } /// A registry id, `#tag`, or list matched against one id. @@ -659,6 +661,18 @@ fn components(expected: &Value, have: &[(String, String)]) -> bool { }) }) } +/// `DistancePredicate.matches` between two points. +pub fn distance(v: &Value, a: [f64; 3], b: [f64; 3]) -> bool { + let d = [a[0] - b[0], a[1] - b[1], a[2] - b[2]]; + range_f(&v["x"], d[0].abs()) + && range_f(&v["y"], d[1].abs()) + && range_f(&v["z"], d[2].abs()) + && range_f(&v["horizontal"], (d[0] * d[0] + d[2] * d[2]).sqrt()) + && range_f( + &v["absolute"], + (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt(), + ) +} /// `EntityPredicate` (26.x keyed sub-predicates). pub fn entity(v: &Value, e: &EntityView, w: &dyn World, origin: [f64; 3]) -> bool { if v.is_null() { @@ -674,21 +688,7 @@ pub fn entity(v: &Value, e: &EntityView, w: &dyn World, origin: [f64; 3]) -> boo "minecraft:stepping_on" => { location(p, [e.position[0], e.position[1] - 0.5, e.position[2]], w) } - "minecraft:distance" | "distance" => { - let d = [ - e.position[0] - origin[0], - e.position[1] - origin[1], - e.position[2] - origin[2], - ]; - range_f(&p["x"], d[0].abs()) - && range_f(&p["y"], d[1].abs()) - && range_f(&p["z"], d[2].abs()) - && range_f(&p["horizontal"], (d[0] * d[0] + d[2] * d[2]).sqrt()) - && range_f( - &p["absolute"], - (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt(), - ) - } + "minecraft:distance" | "distance" => distance(p, e.position, origin), "minecraft:flags" | "flags" => { let f = |k: &str, have: bool| p[k].as_bool().is_none_or(|want| want == have); f("is_on_fire", e.on_fire) @@ -723,11 +723,16 @@ pub fn entity(v: &Value, e: &EntityView, w: &dyn World, origin: [f64; 3]) -> boo }) }) }), - "minecraft:type_specific/player" => p["gamemode"].as_array().is_none_or(|modes| { - e.gamemode - .as_deref() - .is_some_and(|g| modes.iter().any(|m| m.as_str() == Some(g))) - }), + "minecraft:type_specific/player" => { + p["gamemode"].as_array().is_none_or(|modes| { + e.gamemode + .as_deref() + .is_some_and(|g| modes.iter().any(|m| m.as_str() == Some(g))) + }) && (p["looking_at"].is_null() + || e.looking_at + .as_ref() + .is_some_and(|t| entity(&p["looking_at"], t, w, origin))) + } "minecraft:type_specific/lightning" => { range_i(&p["blocks_set_on_fire"], 0) && (p["entity_struck"].is_null() diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a9a946b99..959b3426b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4365,6 +4365,8 @@ impl Game<'_> { self.release_used_item(&stack, ticks, hand_changed); return; } + // ServerPlayer.updateUsingItem: USING_ITEM every tick of the use. + self.trigger_using_item(&stack); let Some(info) = self.res.items().get(&stack.item).cloned() else { self.using = None; return; diff --git a/crates/horizon-client/src/game/advancements.rs b/crates/horizon-client/src/game/advancements.rs index 56cc5df79..912bf7c33 100644 --- a/crates/horizon-client/src/game/advancements.rs +++ b/crates/horizon-client/src/game/advancements.rs @@ -9,6 +9,7 @@ use horizon_client::{ text::{self, Style, Text}, toasts::Slide, }; +use horizon_core::geometry::Vec3; use serde_json::Value; use std::{ collections::{BTreeMap, BTreeSet, VecDeque}, @@ -82,6 +83,8 @@ pub(super) struct AdvState { pub(super) quiet: bool, /// The inventory last seen by `inventory_changed`. pub(super) last_inventory: u64, + /// `ServerPlayer.levitationStartPos` and `levitationStartTime`. + pub(super) levitation: Option<([f64; 3], u64)>, /// While the game acts for a network player (`as_guest`): the /// advancements whose progress changed (`progressChanged`), the recipes /// unlocked and the announcements, for their client and everyone's. @@ -655,6 +658,28 @@ impl Game<'_> { }; adv::conditions(&c["player"], &ctx, w) }); + // ServerPlayer.onEffectAdded and onEffectsRemoved keep the start of a + // levitation; tick fires LEVITATION while there is one. + let levitating = self + .potions + .effects + .list + .iter() + .any(|e| e.id == "levitation"); + match (levitating, self.adv.levitation) { + (true, None) => { + let at = self + .player + .as_ref() + .map(|p| [p.position.x, p.position.y, p.position.z]); + self.adv.levitation = at.map(|at| (at, self.ticks)); + } + (false, Some(_)) => self.adv.levitation = None, + _ => {} + } + if let Some((start, since)) = self.adv.levitation { + self.trigger_levitation(start, (self.ticks - since) as i64); + } if self.ticks.is_multiple_of(20) { let me = self.player_entity(); self.trigger("minecraft:location", |c, w| { @@ -689,6 +714,60 @@ impl Game<'_> { adv::conditions(&c["player"], &pctx, w) && adv::conditions(&c[&key], &ectx, w) }); } + /// `LevitationTrigger`: how far the player rose from where it started + /// levitating, and for how long. + fn trigger_levitation(&mut self, start: [f64; 3], duration: i64) { + let me = self.player_entity(); + self.trigger("minecraft:levitation", move |c, w| { + let pctx = adv::Entities { + this: Some(&me), + origin: me.position, + tool: None, + block_pos: None, + }; + adv::conditions(&c["player"], &pctx, w) + && (c["distance"].is_null() || adv::distance(&c["distance"], start, me.position)) + && adv::range_i(&c["duration"], duration) + }); + } + /// `ServerPlayer.updateUsingItem`: USING_ITEM for the item in use, with + /// what the player looks at (`PlayerPredicate.lookingAt`). + pub(super) fn trigger_using_item(&mut self, stack: &ItemStack) { + if self.remote.is_some() || self.no_player_here() { + return; + } + let mut me = self.player_entity(); + me.looking_at = self.looking_at_entity().map(Box::new); + let it = item_view(stack); + self.trigger("minecraft:using_item", move |c, w| { + let pctx = adv::Entities { + this: Some(&me), + origin: me.position, + tool: None, + block_pos: None, + }; + adv::conditions(&c["player"], &pctx, w) && adv::item(&c["item"], &it, w) + }); + } + /// The mob a 100 block ray from the eyes meets first, if no block is in + /// the way (`ProjectileUtil.getEntityHitResult` and `hasLineOfSight`). + fn looking_at_entity(&self) -> Option { + let eye = self.eye_position()?; + let dir = super::view_vector(self.yaw, self.pitch); + let (m, _) = self + .mobs + .iter() + .filter(|m| m.alive()) + .filter_map(|m| Some((m, super::ray_box(eye, dir, 100.0, m.bounds())?))) + .min_by(|a, b| a.1.total_cmp(&b.1))?; + // `LivingEntity.hasLineOfSight`: eye to the target's eye height, + // `ClipContext.Block.COLLIDER`, no fluids, within 128 blocks. + let from = Vec3::new(eye[0], eye[1], eye[2]); + let to = Vec3::new(m.position.x, m.eye_y(), m.position.z); + let world = self.mob_world_base(&[]); + let clear = to.distance(from) <= 128.0 && world.clear_between(from, to, false); + clear.then(|| self.mob_entity(m)) + } /// `PlayerHurtEntityTrigger`: the player, the damage and the victim. pub(super) fn trigger_player_hurt_entity( &mut self, diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 33c22055f..dde9b590d 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -78,6 +78,8 @@ pub(super) struct GuestPlay { /// way out, the statistic is awarded once (`LanHost::award_leave_game`). pub(super) left_game: bool, last_inventory: u64, + /// `ServerPlayer.levitationStartPos` and `levitationStartTime`. + levitation: Option<([f64; 3], u64)>, /// `Entity.portalProcess` and `portalCooldown` (game/server_travel.rs). pub(super) portal: horizon_client::nether_portal::PortalState, /// `ServerPlayer.wonGame`: the End poem is showing, the player waits for @@ -161,6 +163,7 @@ pub(super) struct GuestSwap { host_ender: Option<(i32, i32, i32)>, adv_loaded: bool, last_inventory: u64, + levitation: Option<([f64; 3], u64)>, death_cause: Option, /// The guest's own damage record, in place while they act. combat: super::combat::CombatTracker, @@ -389,6 +392,7 @@ impl Game<'_> { std::mem::swap(&mut self.hook, &mut play.hook); let adv_loaded = std::mem::replace(&mut self.adv.loaded, true); let last_inventory = std::mem::replace(&mut self.adv.last_inventory, play.last_inventory); + let levitation = std::mem::replace(&mut self.adv.levitation, play.levitation); let death_cause = std::mem::replace(&mut self.ui.death_cause, play.death_cause.take()); let acting = ( self.lan_acting.replace(conn), @@ -460,6 +464,7 @@ impl Game<'_> { host_ender, adv_loaded, last_inventory, + levitation, death_cause, acting, packets, @@ -519,6 +524,7 @@ impl Game<'_> { host_ender, adv_loaded, last_inventory, + levitation, death_cause, combat, acting, @@ -641,6 +647,7 @@ impl Game<'_> { std::mem::swap(&mut self.hook, &mut play.hook); self.adv.loaded = adv_loaded; play.last_inventory = std::mem::replace(&mut self.adv.last_inventory, last_inventory); + play.levitation = std::mem::replace(&mut self.adv.levitation, levitation); let stat_events = std::mem::take(&mut play.stats.events); let name = g.name.clone(); self.potions.swap_player(&mut g.effects, &mut play.potions);