Run the agent's commands, give and time on the server thread

With single player on its server thread, the window is a client of the
server. The agent's `command` went out as the owner's chat and returned no
output, so harnesses that read a command's feedback (lan_e2e trials) failed.
Control `give` and `time` changed only the window's copy of the world.

The server now runs them as single player's console does without a server
thread (`as_console`): as the world's owner, in the owner's level. The
feedback goes to the owner's chat and comes back to the agent. `give` runs
`give @s`, and `time` sets the server's clock too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-18 16:28:48 +02:00
parent 176da24f2e
commit 60bc15a63e
3 changed files with 77 additions and 1 deletions
+7
View File
@@ -1534,6 +1534,13 @@ impl Game<'_> {
return Err("command TEXT".into());
}
let command = format!("/{text}");
// Single player's console is its server thread's.
if let Some(server) = &self.integrated {
let chat = server
.call(move |g| g.owner_console_command(&command))
.ok_or("the integrated server stopped")?;
return ok(json!({ "ok": true, "chat": chat }));
}
if self.remote.is_some() {
self.send_chat(&command);
return ok(json!({ "ok": true }));
+16 -1
View File
@@ -349,6 +349,14 @@ impl Game<'_> {
.get(&item)
.ok_or_else(|| format!("unknown item {item}"))?;
let count: u32 = words.get(2).map_or(Ok(1), |c| c.parse())?;
// Single player's inventory is its server thread's player's.
if let Some(server) = &self.integrated {
let command = format!("give @s {item} {count}");
server
.call(move |g| g.owner_console_command(&command))
.ok_or("the integrated server stopped")?;
return Ok(Some(json!({ "ok": true, "given": count })));
}
let max = info.max_stack;
let mut given = 0;
// Stop once the inventory is full.
@@ -379,7 +387,14 @@ impl Game<'_> {
ok()
}
"time" => {
self.world.day_time = arg(1)?.parse()?;
let time: i64 = arg(1)?.parse()?;
self.world.day_time = time;
// Single player's clock is its server thread's.
if let Some(server) = &self.integrated {
server
.call(move |g| g.world.day_time = time)
.ok_or("the integrated server stopped")?;
}
ok()
}
"summon" => {
@@ -409,6 +409,60 @@ impl Game<'_> {
pub(super) fn serving(&self) -> bool {
self.serving.is_some()
}
/// The window's agent runs a command as single player's console does
/// (`as_console`, as without a server thread): as the world's owner, in
/// the owner's level, at the owner permission level. The feedback goes to
/// the owner's chat and is returned.
pub(super) fn owner_console_command(&mut self, command: &str) -> Vec<String> {
let Some(owner) = self.serving.as_ref().map(|s| s.owner) else {
return Vec::new();
};
let Some(mut host) = self.lan.take() else {
return Vec::new();
};
let mut out = Vec::new();
if let Some(g) = host.hub.guests.iter().find(|g| g.uuid == owner && g.placed) {
let conn = g.conn;
// Commands.performCommand runs in the player's level.
let (back, theirs) = (self.dim, super::Dim::from_id(&g.dimension));
let entered = theirs == back || self.enter_level(Some(&mut host), theirs).is_ok();
if entered {
let command = command.trim_start_matches('/').to_owned();
let lines = self.as_guest(&mut host, conn, |g| {
g.as_console(|g| {
g.chat_capture = Some(Vec::new());
g.run_command(&command);
g.chat_capture.take().unwrap_or_default()
})
});
// A teleport into another level happens before its feedback is sent.
self.run_level_teleports(&mut host);
for line in lines.unwrap_or_default() {
out.push(line.plain());
host.hub.send_to(
conn,
&horizon_protocol::packets::play::Clientbound::SystemChat(
horizon_protocol::packets::play::SystemChat {
content: horizon_client::lan::data::component_nbt(
&line.component(),
),
overlay: false,
},
),
);
}
self.send_admin_feedback(&mut host, Some(conn));
}
if theirs != back
&& let Err(e) = self.enter_level(Some(&mut host), back)
{
log::error!("could not reopen {}: {e}", back.id());
}
}
self.lan = Some(host);
out
}
/// The window's requests, between two ticks.
fn run_tasks(&mut self) {
let tasks: Vec<Task> = match &self.serving {