Saved state: burning, air and falling survive a reopen; InLove for every animal
- Guests' player data saves Fire, Air and fall_distance and puts them back on their body when they come back (single player's players are guests of the integrated server), so a burning player still burns after a reopen (#1323); the old single player's level.dat keeps fall_distance too (#1327). - Every Animal saves and loads InLove, so a fed animal stays in love across a reopen and is not fed again (#1396). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -175,12 +175,15 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
|
||||
for (key, value) in [
|
||||
("Age", Tag::Int(0)),
|
||||
("ForcedAge", Tag::Int(0)),
|
||||
("InLove", Tag::Int(0)),
|
||||
("AgeLocked", Tag::Byte(0)),
|
||||
] {
|
||||
map.entry(key.into()).or_insert(value);
|
||||
}
|
||||
}
|
||||
// Animal.addAdditionalSaveData: the love ticks left, whatever the save had.
|
||||
if m.kind.flags.animal {
|
||||
map.insert("InLove".into(), Tag::Int(m.extra.in_love.max(0)));
|
||||
}
|
||||
match id {
|
||||
"minecraft:creeper" => {
|
||||
for (key, value) in [("Fuse", Tag::Short(30)), ("ExplosionRadius", Tag::Byte(3))] {
|
||||
@@ -621,6 +624,9 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap<String, Arc<MobType>>, id: u32) -
|
||||
m.extra.forced_age = number(tag.get("ForcedAge")).unwrap_or(0) as i32;
|
||||
m.extra.age_locked = flag(tag, "AgeLocked");
|
||||
}
|
||||
if m.kind.flags.animal {
|
||||
m.extra.in_love = number(tag.get("InLove")).unwrap_or(0) as i32;
|
||||
}
|
||||
m.name_visible = flag(tag, "CustomNameVisible");
|
||||
m.no_ai = flag(tag, "NoAI");
|
||||
m.no_gravity = flag(tag, "NoGravity");
|
||||
|
||||
@@ -9534,6 +9534,7 @@ impl Game<'_> {
|
||||
);
|
||||
map.insert("Air".into(), horizon_world::nbt::Tag::Short(p.air as i16));
|
||||
}
|
||||
level.fall_distance = p.fall_distance;
|
||||
|
||||
level.food = p.health.food.food;
|
||||
level.saturation = p.health.food.saturation;
|
||||
|
||||
@@ -217,6 +217,12 @@ impl Game<'_> {
|
||||
host.hub.guests[i].position[2],
|
||||
);
|
||||
host.hub.guests[i].motion = PlayerMotion::new(self.motion, p).ok().map(Box::new);
|
||||
// The first body after joining burns, breathes and falls on as saved.
|
||||
if let Some(saved) = host.hub.guests[i].loaded_body.take()
|
||||
&& let Some(m) = host.hub.guests[i].motion.as_mut()
|
||||
{
|
||||
saved.apply(m);
|
||||
}
|
||||
let kept = host.hub.guests[i].logs_kept.take();
|
||||
if let (Some(m), Some((hurts, attacks))) = (host.hub.guests[i].motion.as_mut(), kept) {
|
||||
m.hurt_log = hurts;
|
||||
|
||||
@@ -18,6 +18,31 @@ pub const CREATIVE: i32 = 1;
|
||||
pub const ADVENTURE: i32 = 2;
|
||||
pub const SPECTATOR: i32 = 3;
|
||||
|
||||
/// The body state a player's file keeps that lives on `PlayerMotion`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct SavedBody {
|
||||
pub fire_ticks: i32,
|
||||
pub air: i32,
|
||||
pub fall_distance: f64,
|
||||
}
|
||||
|
||||
impl SavedBody {
|
||||
/// The body state of `motion`, to save.
|
||||
pub fn of(motion: &PlayerMotion) -> Self {
|
||||
Self {
|
||||
fire_ticks: motion.fire_ticks,
|
||||
air: motion.air,
|
||||
fall_distance: motion.fall_distance,
|
||||
}
|
||||
}
|
||||
/// Put it on a new body.
|
||||
pub fn apply(self, motion: &mut PlayerMotion) {
|
||||
motion.fire_ticks = self.fire_ticks;
|
||||
motion.air = self.air;
|
||||
motion.fall_distance = self.fall_distance;
|
||||
}
|
||||
}
|
||||
|
||||
/// An effect as last sent in ClientboundUpdateMobEffectPacket.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct SentEffect {
|
||||
@@ -133,6 +158,9 @@ pub struct Guest {
|
||||
pub tracker: Tracker,
|
||||
// Player state the gameplay code reads and writes.
|
||||
pub motion: Option<Box<PlayerMotion>>,
|
||||
/// What the player's file says of the body (`Entity.load`: the fire
|
||||
/// and air ticks and the fall distance), for the first body it gets.
|
||||
pub loaded_body: Option<SavedBody>,
|
||||
/// The hurt and attack logs of the body it had before it respawned,
|
||||
/// until the new body takes them over (the control socket's `hurtlog`
|
||||
/// reads across a death).
|
||||
@@ -275,6 +303,7 @@ impl Guest {
|
||||
batch_quota: 0.0,
|
||||
tracker: Tracker::default(),
|
||||
motion: None,
|
||||
loaded_body: None,
|
||||
logs_kept: None,
|
||||
inventory: vec![None; 36],
|
||||
armor: [None, None, None, None],
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Guests' saved state: `players/data/<uuid>.dat` in the vanilla player
|
||||
//! compound, through the same code that saves the host.
|
||||
use super::guest::Guest;
|
||||
use super::guest::{Guest, SavedBody};
|
||||
use crate::{
|
||||
items::ItemStack,
|
||||
save::{Level, read_dat, write_dat},
|
||||
@@ -74,6 +74,17 @@ pub fn apply(guest: &mut Guest, level: &Level) {
|
||||
guest.spawn = level.spawn.map(|s| [s[0] + 0.5, s[1], s[2] + 0.5]);
|
||||
guest.respawn_elsewhere = level.respawn_elsewhere.clone();
|
||||
guest.saved = level.player_extra.clone();
|
||||
let extra = level.player_extra.as_ref();
|
||||
let short = |key: &str, default: i64| {
|
||||
extra
|
||||
.and_then(|t| crate::save::number(t.get(key)))
|
||||
.unwrap_or(default) as i32
|
||||
};
|
||||
guest.loaded_body = Some(SavedBody {
|
||||
fire_ticks: short("Fire", -20),
|
||||
air: short("Air", 300),
|
||||
fall_distance: level.fall_distance,
|
||||
});
|
||||
guest.recipe_book = level.recipe_book.clone();
|
||||
guest.dimension = match level.dimension.as_str() {
|
||||
d @ ("minecraft:the_nether" | "minecraft:the_end") => d.to_owned(),
|
||||
@@ -139,6 +150,24 @@ pub fn store(guest: &Guest, level: &mut Level) {
|
||||
level.respawn_elsewhere = guest.respawn_elsewhere.clone();
|
||||
level.on_ground = guest.on_ground;
|
||||
level.player_extra = guest.saved.clone();
|
||||
// The live body's fire, air and fall distance (Entity.saveWithoutId),
|
||||
// or what the file had while the player has no body yet.
|
||||
let body = guest
|
||||
.motion
|
||||
.as_deref()
|
||||
.map(SavedBody::of)
|
||||
.or(guest.loaded_body);
|
||||
if let Some(body) = body {
|
||||
level.fall_distance = body.fall_distance;
|
||||
let extra = level
|
||||
.player_extra
|
||||
.get_or_insert_with(|| horizon_world::nbt::Tag::Compound(Default::default()));
|
||||
if let horizon_world::nbt::Tag::Compound(map) = extra {
|
||||
let short = |v: i32| horizon_world::nbt::Tag::Short(v.clamp(-32768, 32767) as i16);
|
||||
map.insert("Fire".into(), short(body.fire_ticks));
|
||||
map.insert("Air".into(), short(body.air));
|
||||
}
|
||||
}
|
||||
level.recipe_book = guest.recipe_book.clone();
|
||||
level.ender_items = guest.ender_items.clone();
|
||||
level.inventory.clear();
|
||||
@@ -162,3 +191,52 @@ pub fn store(guest: &Guest, level: &mut Level) {
|
||||
put(100, &guest.armor[3]);
|
||||
put(150, &guest.offhand);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lan::{Joined, PlayerSender};
|
||||
use horizon_protocol::packets::configuration::ClientInformation;
|
||||
use std::sync::{Arc, atomic::AtomicI32, mpsc};
|
||||
|
||||
fn guest() -> Guest {
|
||||
let (tx, _rx) = mpsc::channel();
|
||||
Guest::new(
|
||||
Joined {
|
||||
conn: 2,
|
||||
uuid: 2,
|
||||
name: "Alex".into(),
|
||||
properties: Vec::new(),
|
||||
info: ClientInformation::new(8),
|
||||
brand: "test".into(),
|
||||
sender: PlayerSender::new(tx, Arc::default()),
|
||||
latency: Arc::new(AtomicI32::new(0)),
|
||||
address: "127.0.0.1:25565".parse().unwrap(),
|
||||
},
|
||||
2,
|
||||
[0.0, 64.0, 0.0],
|
||||
8,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_saved_burn_and_fall_come_back() {
|
||||
// A player saved burning in mid-fall (#1323, #1327): Entity.saveWithoutId's
|
||||
// Fire, Air and fall_distance, read back for the next body.
|
||||
let mut g = guest();
|
||||
g.loaded_body = Some(SavedBody {
|
||||
fire_ticks: 96,
|
||||
air: 250,
|
||||
fall_distance: 9.5,
|
||||
});
|
||||
let mut level = Level::default();
|
||||
store(&g, &mut level);
|
||||
let tag = level.player_tag();
|
||||
let mut read = Level::default();
|
||||
read.read_player(&tag);
|
||||
read.player_extra = Some(tag);
|
||||
let mut back = guest();
|
||||
apply(&mut back, &read);
|
||||
assert_eq!(back.loaded_body, g.loaded_body);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,6 +310,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn love_ticks_survive_a_save() {
|
||||
// Animal.addAdditionalSaveData / readAdditionalSaveData: "InLove" (#1396).
|
||||
let mut m = rabbit();
|
||||
m.extra.in_love = 570;
|
||||
let tag = crate::entity_nbt::mob_to_nbt(&m, [0, 0, 0, 1], None);
|
||||
assert_eq!(tag.get("InLove"), Some(&horizon_world::nbt::Tag::Int(570)));
|
||||
let types = HashMap::from([(m.kind.id.clone(), m.kind.clone())]);
|
||||
let back = crate::entity_nbt::mob_from_nbt(&tag, &types, 2).unwrap();
|
||||
assert_eq!(back.extra.in_love, 570);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn landing_waits_then_real_hop_clock_moves_the_model() {
|
||||
let mut m = rabbit();
|
||||
|
||||
@@ -337,6 +337,7 @@ impl PlayerMotion {
|
||||
self.air = extra
|
||||
.and_then(|t| crate::save::number(t.get("Air")))
|
||||
.unwrap_or(300) as i32;
|
||||
self.fall_distance = level.fall_distance;
|
||||
self.set_health(level.health, false)?;
|
||||
self.health.food.food = level.food;
|
||||
self.health.food.saturation = level.saturation;
|
||||
|
||||
Reference in New Issue
Block a user