From 7f7a05cbd093b87093267bcb88541744d9fd8591 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 22:49:31 +0200 Subject: [PATCH] Play with Friends on single player's server thread Sharing a world with an invite code refused with the integrated server on its own thread ("open a singleplayer world first"): the window is a client there. It now shares that server as Open to LAN publishes it: friends_start publishes it when it isn't, the invite tunnel points at its port, stopping sharing unpublishes it again, and the pause menu shows the Friends entry with the players the server lists. test_friends.py --server-thread runs the host's world on its server thread; the host's own move and blocks go through /tp, /fill and /setblock, which the server answers in both modes. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/friends.rs | 24 +++++++++++++++-------- tools/test_friends.py | 18 ++++++++++++++--- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/crates/horizon-client/src/game/friends.rs b/crates/horizon-client/src/game/friends.rs index 51dfe3fbf..69192265e 100644 --- a/crates/horizon-client/src/game/friends.rs +++ b/crates/horizon-client/src/game/friends.rs @@ -52,10 +52,12 @@ impl Game<'_> { if self.friends.is_some() { return Ok(()); } - if self.remote.is_some() || self.title.is_some() { + // Single player on its server thread shares that server, as Open to + // LAN publishes it; another server can't be shared. + if self.title.is_some() || (self.remote.is_some() && self.integrated.is_none()) { return Err("open a singleplayer world first".into()); } - let opened_lan = self.lan.is_none(); + let opened_lan = self.lan_port().is_none(); if opened_lan { // ShareToLanScreen's defaults, as the world options screen offers them. self.lan_open(LanSettings { @@ -171,14 +173,20 @@ impl Game<'_> { /// What the pause menu shows. pub(super) fn friends_view(&self) -> FriendsView { - let guests = self - .lan - .as_ref() - .map(|h| h.hub.guests.iter().map(|g| g.name.clone()).collect()) - .unwrap_or_default(); + let guests = match &self.lan { + Some(h) => h.hub.guests.iter().map(|g| g.name.clone()).collect(), + // On the server thread: the other players the server lists. + None if self.integrated.is_some() => { + let me = self.options.player_name(); + let mut names = self.remote_online_names().unwrap_or_default(); + names.retain(|n| *n != me); + names + } + None => Vec::new(), + }; let Some(s) = &self.friends else { return FriendsView { - shown: self.remote.is_none(), + shown: self.remote.is_none() || self.integrated.is_some(), ..FriendsView::default() }; }; diff --git a/tools/test_friends.py b/tools/test_friends.py index f89f3e599..3daeb28e7 100644 --- a/tools/test_friends.py +++ b/tools/test_friends.py @@ -15,6 +15,9 @@ is off: nothing leaves the machine. python3 tools/test_friends.py --client target/fast/horizon-client --relay target/fast/horizon-relay +`--server-thread` runs the host's world on its integrated server's own +thread (the window is a client of it, as Open to LAN publishes it). + `--role host|guest` runs one side only (two machines, e.g. over a tailnet): the host prints the code, the guest takes it with `--code` (`--relay-url` for a relay elsewhere, `--path relay` to force it). Needs a display on Linux @@ -86,8 +89,12 @@ class Game: # Extra words before and after the client (`--xvfb`, `--offscreen`). prefix = [] extra = [] + # The host's world on its server thread (`--server-thread`), else in the window. + server_thread = False def __init__(self, client, resources, work, name, env): + # Either way explicitly, whatever the game's default. + env = dict(env, HORIZON_SERVER_THREAD='1' if Game.server_thread and name == 'host' else '0') self.home = work / name self.home.mkdir() self.endpoint = self.home / 'c.sock' @@ -155,7 +162,9 @@ def play_together(host, guest, name, round_index): # The host moves; the guest sees the host's player there. hp = host('status')['player']['position'] target = [hp[0] + 3.0, hp[1], hp[2] + 1.0] - host(f'tp {target[0]} {target[1]} {target[2]}') + # The /tp command, not the control socket's local move: on the server thread the window is a + # client, and its server has the last word on where the host stands. + host(f'command tp @s {target[0]} {target[1]} {target[2]}') guest.wait('status', lambda v: any(math.dist(p, target) < 0.6 for p in v['server']['player_positions']), 20, 'host movement on the guest') # A block the host places, which the guest sees and then breaks. @@ -163,8 +172,9 @@ def play_together(host, guest, name, round_index): feet = [math.floor(gp[0]), math.floor(gp[1]), math.floor(gp[2])] block = [feet[0] + 2, feet[1] + 1, feet[2]] # Clear the line of sight (the test terrain has hills), then the block at eye level. - host(f'fill {feet[0] + 1} {feet[1]} {feet[2]} {feet[0] + 1} {feet[1] + 2} {feet[2]} minecraft:air') - host(f'setblock {block[0]} {block[1]} {block[2]} minecraft:gold_block') + # As commands, for the same reason as the /tp above. + host(f'command fill {feet[0] + 1} {feet[1]} {feet[2]} {feet[0] + 1} {feet[1] + 2} {feet[2]} minecraft:air') + host(f'command setblock {block[0]} {block[1]} {block[2]} minecraft:gold_block') guest.wait(f'getblock {block[0]} {block[1]} {block[2]}', lambda v: v['block'] == 'minecraft:gold_block', 20, 'the host block on the guest') eye = [gp[0], gp[1] + 1.62, gp[2]] @@ -343,10 +353,12 @@ def main(): parser.add_argument('--hold', type=float, default=0, help='guest: stay this many seconds before leaving') parser.add_argument('--baseline', action='store_true', help='first run the same checks over plain LAN') parser.add_argument('--offscreen', action='store_true', help='run the games without a window') + parser.add_argument('--server-thread', action='store_true', help="the host's world on its server thread") parser.add_argument('--xvfb', action='store_true', help='run the games under xvfb-run (headless Linux)') args = parser.parse_args() Game.prefix = ['xvfb-run', '-a'] if args.xvfb else [] Game.extra = ['--offscreen'] if args.offscreen else [] + Game.server_thread = args.server_thread args.client, args.resources = args.client.resolve(), args.resources.resolve() # Short: Unix socket paths are limited to about 100 bytes. with tempfile.TemporaryDirectory(prefix='hzf-', dir='/tmp') as directory: