Run single player on an integrated server thread (opt-in)

With --server-thread or HORIZON_SERVER_THREAD=1, opening a single-player
world starts a game of its own on a "Server thread" over the shared
resources, as vanilla's IntegratedServer does, and the window joins it as a
player over a local connection. The world tick then never runs inside a
frame.

- The server listens on 127.0.0.1 only, lets in the window's profile alone,
  without compression, and makes it an operator when the world allows
  commands.
- The window's player is the world's own player: it loads from and saves to
  players/data/<singleplayer_uuid>.dat, so the world opens the same way in
  either mode.
- A pause screen pauses the server (IntegratedServer.tickServer), and
  leaving the world or quitting waits for the server to save.
- The integrated server does not mesh sections it never draws.
- Worlds whose player is outside the Overworld open the usual way for now.
- The bench summary reports the 0.1% low.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-16 17:18:03 +02:00
parent c2f4ecc188
commit 56dc4352d2
8 changed files with 434 additions and 36 deletions
+34 -21
View File
@@ -45,6 +45,13 @@ pub fn load(guest: &mut Guest, world: &Path) -> bool {
..Level::default()
};
level.read_player(&tag);
level.player_extra = Some(tag);
apply(guest, &level);
true
}
/// Give a guest the player state read into `level`.
pub fn apply(guest: &mut Guest, level: &Level) {
if let Some(p) = level.position {
guest.position = p;
guest.previous_position = p;
@@ -61,7 +68,7 @@ pub fn load(guest: &mut Guest, world: &Path) -> bool {
guest.xp_total = level.xp_total;
guest.game_mode = level.mode();
guest.selected = (level.selected_slot.max(0) as usize).min(8);
guest.saved = Some(tag);
guest.saved = level.player_extra.clone();
if level.ender_items.len() == 27 {
guest.ender_items = level.ender_items.clone();
}
@@ -85,30 +92,41 @@ pub fn load(guest: &mut Guest, world: &Path) -> bool {
_ => {}
}
}
true
}
/// Save a guest (PlayerDataStorage.save).
pub fn save(guest: &Guest, world: &Path) {
let mut level = Level {
player_uuid: Some(uuid_ints(guest.uuid)),
position: Some(guest.position),
rotation: [guest.yaw, guest.pitch],
health: guest.health,
food: guest.food,
saturation: guest.saturation,
xp_level: guest.xp_level,
xp_progress: guest.xp_progress,
xp_total: guest.xp_total,
game_mode: guest.game_mode,
creative: guest.game_mode == 1,
selected_slot: guest.selected as i32,
on_ground: guest.on_ground,
dimension: "minecraft:overworld".into(),
player_extra: guest.saved.clone(),
ender_items: guest.ender_items.clone(),
..Level::default()
};
store(guest, &mut level);
let file = path(world, guest.uuid);
if let Some(dir) = file.parent() {
let _ = std::fs::create_dir_all(dir);
}
let _ = write_dat(&file, level.player_tag());
}
/// Put a guest's state in `level`'s player fields.
pub fn store(guest: &Guest, level: &mut Level) {
level.position = Some(guest.position);
level.rotation = [guest.yaw, guest.pitch];
level.health = guest.health;
level.food = guest.food;
level.saturation = guest.saturation;
level.xp_level = guest.xp_level;
level.xp_progress = guest.xp_progress;
level.xp_total = guest.xp_total;
level.game_mode = guest.game_mode;
level.creative = guest.game_mode == 1;
level.selected_slot = guest.selected as i32;
level.on_ground = guest.on_ground;
level.player_extra = guest.saved.clone();
level.ender_items = guest.ender_items.clone();
level.inventory.clear();
level.inventory_components.clear();
let mut put = |slot: u8, stack: &Option<ItemStack>| {
if let Some(s) = stack {
level
@@ -127,9 +145,4 @@ pub fn save(guest: &Guest, world: &Path) {
put(101, &guest.armor[2]);
put(100, &guest.armor[3]);
put(150, &guest.offhand);
let file = path(world, guest.uuid);
if let Some(dir) = file.parent() {
let _ = std::fs::create_dir_all(dir);
}
let _ = write_dat(&file, level.player_tag());
}