fix(game): persist creative flight status across world save and reload (#777)
This commit is contained in:
@@ -3156,6 +3156,11 @@ impl Game<'_> {
|
||||
player.may_fly = self.creative || spectator;
|
||||
player.invulnerable = self.creative || spectator;
|
||||
player.spectator = spectator;
|
||||
if spectator {
|
||||
player.flying = true;
|
||||
} else if !player.may_fly {
|
||||
player.flying = false;
|
||||
}
|
||||
player.respiration = self.armor[0]
|
||||
.as_ref()
|
||||
.map_or(0, |s| s.enchantment_level("minecraft:respiration") as i32);
|
||||
@@ -8458,6 +8463,7 @@ impl Game<'_> {
|
||||
|
||||
level.food = p.health.food.food;
|
||||
level.saturation = p.health.food.saturation;
|
||||
level.flying = p.flying;
|
||||
}
|
||||
level.inventory = self
|
||||
.inventory
|
||||
|
||||
@@ -68,6 +68,7 @@ pub fn apply(guest: &mut Guest, level: &Level) {
|
||||
guest.xp_total = level.xp_total;
|
||||
guest.effects = crate::effects::PlayerEffects::from_saved(&level.active_effects);
|
||||
guest.game_mode = level.mode();
|
||||
guest.flying = level.flying;
|
||||
guest.selected = (level.selected_slot.max(0) as usize).min(8);
|
||||
guest.saved = level.player_extra.clone();
|
||||
guest.recipe_book = level.recipe_book.clone();
|
||||
@@ -128,6 +129,7 @@ pub fn store(guest: &Guest, level: &mut Level) {
|
||||
level.active_effects = guest.effects.saved();
|
||||
level.game_mode = guest.game_mode;
|
||||
level.creative = guest.game_mode == 1;
|
||||
level.flying = guest.flying;
|
||||
level.selected_slot = guest.selected as i32;
|
||||
level.on_ground = guest.on_ground;
|
||||
level.player_extra = guest.saved.clone();
|
||||
|
||||
@@ -309,6 +309,7 @@ impl PlayerMotion {
|
||||
self.set_health(level.health, false)?;
|
||||
self.health.food.food = level.food;
|
||||
self.health.food.saturation = level.saturation;
|
||||
self.flying = level.flying;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -2610,6 +2611,7 @@ mod tests {
|
||||
food: 11,
|
||||
saturation: 2.25,
|
||||
player_extra: Some(extra),
|
||||
flying: true,
|
||||
..Default::default()
|
||||
};
|
||||
let mut pending = true;
|
||||
@@ -2617,6 +2619,7 @@ mod tests {
|
||||
.load_saved_state_once(&mut pending, &level)
|
||||
.unwrap();
|
||||
assert_eq!((w.player.fire_ticks, w.player.air), (160, 8));
|
||||
assert!(w.player.flying);
|
||||
assert_eq!(
|
||||
(
|
||||
w.player.health.health(),
|
||||
@@ -2628,6 +2631,7 @@ mod tests {
|
||||
let mut respawn = PlayerMotion::new(&resources, Vec3::new(20.5, 4.0, 20.5)).unwrap();
|
||||
respawn.load_saved_state_once(&mut pending, &level).unwrap();
|
||||
assert_eq!((respawn.fire_ticks, respawn.air), (-20, 300));
|
||||
assert!(!respawn.flying);
|
||||
assert_eq!(
|
||||
(respawn.health.health(), respawn.health.food.food),
|
||||
(20.0, 20)
|
||||
|
||||
@@ -2146,6 +2146,8 @@ pub struct Level {
|
||||
pub on_ground: bool,
|
||||
pub food_exhaustion: f32,
|
||||
pub food_tick_timer: i32,
|
||||
/// Abilities.flying: whether creative/spectator flight is active.
|
||||
pub flying: bool,
|
||||
/// The player compound as loaded, for fields Horizon does not model
|
||||
/// (ender chest, effects, abilities, recipe book, attributes, ...).
|
||||
pub player_extra: Option<Tag>,
|
||||
@@ -2309,6 +2311,10 @@ impl Level {
|
||||
self.creative = mode == 1;
|
||||
self.game_mode = mode as i32;
|
||||
}
|
||||
self.flying = p
|
||||
.get("abilities")
|
||||
.and_then(|a| number(a.get("flying")))
|
||||
.is_some_and(|v| v != 0);
|
||||
if let Some(d) = text(p.get("Dimension")) {
|
||||
self.dimension = d;
|
||||
}
|
||||
@@ -2440,7 +2446,9 @@ impl Level {
|
||||
}
|
||||
if spectator {
|
||||
put(&mut abilities, "flying", Tag::Byte(1));
|
||||
} else if !creative {
|
||||
} else if creative {
|
||||
put(&mut abilities, "flying", Tag::Byte(i8::from(self.flying)));
|
||||
} else {
|
||||
put(&mut abilities, "flying", Tag::Byte(0));
|
||||
}
|
||||
if let Tag::Compound(map) = &mut abilities {
|
||||
@@ -3153,6 +3161,7 @@ mod tests {
|
||||
world_spawn: Some([8, 64, 8]),
|
||||
spawn: Some([20.0, 70.0, 20.0]),
|
||||
difficulty_locked: true,
|
||||
flying: true,
|
||||
..Level::default()
|
||||
};
|
||||
level.weather.raining = true;
|
||||
@@ -3164,6 +3173,7 @@ mod tests {
|
||||
assert_eq!(back.seed, -42);
|
||||
assert_eq!((back.time, back.day_time), (1234, 25000));
|
||||
assert!(back.creative);
|
||||
assert!(back.flying);
|
||||
assert_eq!(back.position, Some([1.5, 70.0, -3.25]));
|
||||
assert_eq!(back.rotation, [90.0, 10.0]);
|
||||
assert_eq!((back.health, back.food, back.saturation), (17.0, 15, 2.5));
|
||||
@@ -3246,4 +3256,33 @@ mod tests {
|
||||
level.active_effects.clear();
|
||||
assert!(level.player_tag().get("active_effects").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creative_flight_status_persists_in_save_and_resets_for_survival() {
|
||||
let mut level = Level {
|
||||
creative: true,
|
||||
flying: true,
|
||||
..Level::default()
|
||||
};
|
||||
let tag = level.player_tag();
|
||||
let flying_byte = tag
|
||||
.get("abilities")
|
||||
.and_then(|a| number(a.get("flying")))
|
||||
.unwrap_or(0);
|
||||
assert_eq!(flying_byte, 1);
|
||||
|
||||
let mut loaded = Level::default();
|
||||
loaded.read_player(&tag);
|
||||
assert!(loaded.flying);
|
||||
|
||||
// Survival mode resets flying ability to 0 in save.
|
||||
level.creative = false;
|
||||
level.game_mode = 0;
|
||||
let tag_survival = level.player_tag();
|
||||
let flying_survival = tag_survival
|
||||
.get("abilities")
|
||||
.and_then(|a| number(a.get("flying")))
|
||||
.unwrap_or(0);
|
||||
assert_eq!(flying_survival, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user