diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 58b89a140..2acbfb4f9 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -156,6 +156,7 @@ mod server_packs; mod server_travel; mod shader_packs; mod shield; +mod shoulders; mod sign_edit; mod sniffers; mod spawner; @@ -4574,6 +4575,7 @@ impl Game<'_> { } self.powder_snow_fire()?; self.tick_totem(); + self.handle_shoulder_entities(); let Some(player) = self.player.as_mut() else { return Ok(()); }; diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 8eb4cc911..1a04ef0eb 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -49,6 +49,8 @@ pub(super) struct Creatures { pub elder_apparition: i32, /// The wandering trader spawner's clock. pub trader: super::wandering_trader::Spawner, + /// When each player last put a parrot on a shoulder (`timeEntitySatOnShoulder`). + pub shoulder_time: std::collections::HashMap<[i32; 4], i64>, } pub(super) struct ProjWorld<'a, 'w> { pub w: &'a MobWorld<'w>, @@ -992,6 +994,7 @@ impl Game<'_> { } MobEvent::BecomeStatue { mob } => self.golem_become_statue(mob), MobEvent::EnterHive { mob, hive } => self.bee_enter_hive(mob, hive), + MobEvent::ParrotToShoulder { mob } => self.parrot_to_shoulder(mob), MobEvent::Gift { target, item } => { if let Some(m) = self.mobs.iter_mut().find(|m| m.id == target) && m.saddle_item().is_none() diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 43bb573dc..6e7f63f9c 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -378,6 +378,7 @@ impl Game<'_> { std::mem::swap(&mut self.adv.changed, &mut g.advancements_changed); std::mem::swap(&mut self.adv.unlocked, &mut g.recipes_unlocked); std::mem::swap(&mut self.level.recipe_book, &mut g.recipe_book); + std::mem::swap(&mut self.level.player_extra, &mut g.saved); std::mem::swap(&mut self.stats, &mut play.stats); std::mem::swap(&mut self.hook, &mut play.hook); let adv_loaded = std::mem::replace(&mut self.adv.loaded, true); @@ -610,6 +611,7 @@ impl Game<'_> { std::mem::swap(&mut self.adv.changed, &mut g.advancements_changed); std::mem::swap(&mut self.adv.unlocked, &mut g.recipes_unlocked); std::mem::swap(&mut self.level.recipe_book, &mut g.recipe_book); + std::mem::swap(&mut self.level.player_extra, &mut g.saved); std::mem::swap(&mut self.stats, &mut play.stats); std::mem::swap(&mut self.hook, &mut play.hook); self.adv.loaded = adv_loaded; @@ -2178,6 +2180,7 @@ impl Game<'_> { if !g.effects.list.is_empty() { self.as_guest(host, conn, |game| game.tick_mob_effects()); } + self.as_guest(host, conn, |game| game.handle_shoulder_entities()); return; } let in_wall = !g.sleeping && self.in_wall(g.position, g.eye_height()); @@ -2279,6 +2282,7 @@ impl Game<'_> { // LivingEntity.baseTick: outside the world border, then tickEffects. game.border_damage(); game.tick_mob_effects(); + game.handle_shoulder_entities(); let Some(player) = game.player.as_mut() else { return (Vec::new(), None); }; diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 4f53cec60..4555474a5 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -80,6 +80,7 @@ pub(super) fn acts_on_player(e: &MobEvent) -> bool { } | MobEvent::EffectPlayer { .. } | MobEvent::AxolotlSupportPlayer + | MobEvent::ParrotToShoulder { .. } ) } diff --git a/crates/horizon-client/src/game/shoulders.rs b/crates/horizon-client/src/game/shoulders.rs new file mode 100644 index 000000000..602c5a5af --- /dev/null +++ b/crates/horizon-client/src/game/shoulders.rs @@ -0,0 +1,120 @@ +//! Parrots on a player's shoulder: a tame parrot lands on its owner +//! (`LandOnOwnersShoulderGoal`), is kept as the player's `ShoulderEntityLeft` +//! or `ShoulderEntityRight`, and flies off again when the player is hurt, +//! falls, swims, flies or sleeps (`ServerPlayer.handleShoulderEntities`). +use super::Game; +use horizon_client::mobs::Species; +use horizon_world::nbt::Tag; + +const KEYS: [&str; 2] = ["ShoulderEntityLeft", "ShoulderEntityRight"]; + +/// A shoulder slot holds a parrot when its compound is not empty. +fn occupied(extra: &Tag, key: &str) -> bool { + matches!(extra.get(key), Some(Tag::Compound(c)) if !c.is_empty()) +} + +impl Game<'_> { + /// `ServerPlayer.setEntityOnShoulder` for a parrot that touched its + /// owner: stored in the first free slot, the parrot gone from the world. + pub(super) fn parrot_to_shoulder(&mut self, mob: u32) { + let Some(i) = self + .mobs + .iter() + .position(|m| m.id == mob && m.alive() && !m.removed && m.species() == Species::Parrot) + else { + return; + }; + let Some(p) = self.player.as_ref() else { + return; + }; + if self.riding.is_some() + || !p.ground.on_ground + || p.fluids.was_touching_water + || p.in_powder_snow + || p.flying + { + return; + } + let extra = self + .level + .player_extra + .get_or_insert_with(|| Tag::Compound(Default::default())); + let Some(key) = KEYS.into_iter().find(|k| !occupied(extra, k)) else { + return; + }; + let tag = self.mob_tree_nbt(i); + if let Some(Tag::Compound(map)) = self.level.player_extra.as_mut() { + map.insert(key.into(), tag); + } + let (uuid, now) = (self.acting_uuid(), self.world.game_time); + self.creatures.shoulder_time.insert(uuid, now); + self.mobs[i].removed = true; + } + + /// `ServerPlayer.handleShoulderEntities`: the parrots leave the shoulder + /// of a player who falls, swims, flies, sleeps, is hurt, dying, a + /// spectator or spinning with a riptide. + pub(super) fn handle_shoulder_entities(&mut self) { + let Some(p) = self.player.as_ref() else { + return; + }; + let any = self + .level + .player_extra + .as_ref() + .is_some_and(|e| KEYS.iter().any(|k| occupied(e, k))); + if any + && (p.fall_distance > 0.5 + || p.fluids.was_touching_water + || p.flying + || p.spectator + || p.in_powder_snow + || p.auto_spin_attack_ticks > 0 + || p.health.hurt.hurt_time > 0 + || p.health.dead_or_dying() + || self.sleeping.is_some()) + { + self.remove_entities_on_shoulder(); + } + } + + /// `ServerPlayer.removeEntitiesOnShoulder` (not within a second of the + /// landing) and `respawnEntityOnShoulder`: the parrots come back at the + /// player, still owned by them. + fn remove_entities_on_shoulder(&mut self) { + let (uuid, now) = (self.acting_uuid(), self.world.game_time); + if self + .creatures + .shoulder_time + .get(&uuid) + .is_some_and(|t| t + 20 >= now) + { + return; + } + let Some(p) = self.player.as_ref().map(|p| p.position) else { + return; + }; + let Some(Tag::Compound(map)) = self.level.player_extra.as_mut() else { + return; + }; + let tags: Vec = KEYS + .iter() + .filter_map(|k| map.remove(&(*k).into())) + .collect(); + let pos = [p.x, p.y + f64::from(0.7_f32), p.z]; + for mut tag in tags { + let Tag::Compound(c) = &mut tag else { + continue; + }; + if c.is_empty() { + continue; + } + c.insert( + "Pos".into(), + Tag::List(pos.iter().map(|v| Tag::Double(v.to_bits())).collect()), + ); + c.insert("Owner".into(), Tag::IntArray(uuid.to_vec())); + self.load_mob(tag, None); + } + } +} diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 4b67f738f..b7e38bb01 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -894,6 +894,11 @@ pub enum MobEvent { mob: u32, hive: [i32; 3], }, + /// `LandOnOwnersShoulderGoal`: a tame parrot touched its owner and sits + /// on their shoulder (the host stores its tag and discards it). + ParrotToShoulder { + mob: u32, + }, /// `CopperGolem.turnToStatue`. BecomeStatue { mob: u32, diff --git a/crates/horizon-client/src/mobs/tame.rs b/crates/horizon-client/src/mobs/tame.rs index 0043ff9ef..82d808b7f 100644 --- a/crates/horizon-client/src/mobs/tame.rs +++ b/crates/horizon-client/src/mobs/tame.rs @@ -110,6 +110,35 @@ pub fn register_parrot(m: &mut Mob) { g.add(2, SitWhenOrdered); g.add(2, FollowOwner::new(1.0, 5.0, 1.0)); g.add(2, Stroll::avoiding_water(1.0)); + g.add(3, LandOnOwnersShoulder); +} +/// `LandOnOwnersShoulderGoal`: a standing parrot, 100 ticks old, lands on +/// the shoulder of the owner it touches; the host does the landing. +#[derive(Clone, Debug)] +struct LandOnOwnersShoulder; +impl Goal for LandOnOwnersShoulder { + goal_clone!(); + fn flags(&self) -> u8 { + 0 + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + // ShoulderRidingEntity.canSitOnShoulder: rideCooldownCounter > 100. + owner(m, cx.w).is_some_and(|(_, _, spectator)| !spectator) + && !m.extra.sitting + && m.age > 100 + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + if !m.extra.sitting + && m.extra.leash.is_none() + && cx + .w + .player + .as_ref() + .is_some_and(|p| m.bounds().intersects(p.bounds)) + { + cx.events.push(MobEvent::ParrotToShoulder { mob: m.id }); + } + } } /// `Wolf.PREY_SELECTOR`: sheep, rabbits and foxes. fn wolf_prey(s: &super::Seen) -> bool {