/recipe gives and takes each target's own recipes

cmd_recipe changed self.level.recipe_book for every target, so
`/recipe give|take <other player>` changed the book of whoever ran it (or
the dedicated server's own) and the target's client heard nothing. Each
target now runs with its state swapped in (for_player), as
ServerPlayer.awardRecipes/resetRecipes do on the player's own
ServerRecipeBook: the guest's book changes and its client gets
ClientboundRecipeBookAdd/RemovePacket.

lan_e2e recipe_targets: the host gives and takes the guest's recipes and,
on the server thread, the guest gives the host one; each open book must
show exactly its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 17:18:47 +01:00
parent 747c22a5c2
commit d8e68148be
2 changed files with 85 additions and 20 deletions
@@ -49,26 +49,12 @@ impl Game<'_> {
let mut tracker = Tracker::default();
for &t in &targets {
let h = holder(self.target_name(t));
// ServerRecipeBook.addRecipes/removeRecipes count recipe book
// displays (a transmute recipe has one per material count).
let mut n = 0;
// ServerPlayer.awardRecipes / resetRecipes: the displays changed.
let index = &self.res.stations().recipe_index;
n += if give {
let (count, unlocked) = self.level.recipe_book.add_recipes(index, &recipes);
self.recipe_unlocked(&unlocked);
count as i32
} else {
let removed = self.level.recipe_book.remove_recipes(index, &recipes);
let count = removed.len() as i32;
// A network player's client drops them from its book.
if self.lan_acting.is_some() && !removed.is_empty() {
self.guest_packets.push(C::RecipeBookRemove(
horizon_protocol::packets::recipes::RecipeBookRemove { ids: removed },
));
}
count
};
// ServerPlayer.awardRecipes / resetRecipes on the target's own
// ServerRecipeBook: a guest's is swapped in for the call, and the
// packets go to its client.
let n = self
.for_player(t, |g| g.change_recipes(give, &recipes))
.unwrap_or(0);
tracker.track(&h, n);
}
let verb = if give { "give" } else { "take" };
@@ -92,6 +78,27 @@ impl Game<'_> {
Ok(total)
}
/// ServerRecipeBook.addRecipes/removeRecipes for the player in place: how
/// many recipe book displays changed (a transmute recipe has one per
/// material count).
fn change_recipes(&mut self, give: bool, recipes: &[String]) -> i32 {
let index = &self.res.stations().recipe_index;
if give {
let (count, unlocked) = self.level.recipe_book.add_recipes(index, recipes);
self.recipe_unlocked(&unlocked);
return count as i32;
}
let removed = self.level.recipe_book.remove_recipes(index, recipes);
let count = removed.len() as i32;
// A network player's client drops them from its book.
if self.lan_acting.is_some() && !removed.is_empty() {
self.guest_packets.push(C::RecipeBookRemove(
horizon_protocol::packets::recipes::RecipeBookRemove { ids: removed },
));
}
count
}
// ---- /damage ----------------------------------------------------------------
fn damage_kind(ty: &str) -> DamageKind {
+58
View File
@@ -91,6 +91,8 @@ address. The script then checks, from both sides, what a guest's game used to ge
places a log in the inventory's crafting grid (the server places it).
- recipe_removal: the host and guest each forget all and individual recipes while the book
is open; it stays empty when reopened, and a later grant appears (#1495).
- recipe_targets: `/recipe give|take <other player>` changes that player's book, the host's
for the guest and (server thread) the guest's for the host; their open book shows it.
- feedback: the guest's command feedback names the guest and shows the numbers, and the host (an
operator) is told of it as `[Guest: ...]`.
- clicks: with real mouse presses (not click-slot, which skips the screen's press and drag
@@ -6222,6 +6224,62 @@ CHECKS += ['recipe_removal']
CHECK_FNS.update(recipe_removal=check_recipe_removal)
def check_recipe_targets(host, guest, out):
"""RecipeCommand gives and takes the recipes of each target's own ServerRecipeBook: a
`/recipe give|take <other player>` changes that player's book and reaches their client,
not the book of the player who typed it. An in-frame host isn't one of the players a guest's
command can name (`command_players` lists the guests), so the guest gives the host a recipe
only when the host is a client of its server thread."""
host_name = host.raw('lan', 'status').get('host', 'Player')
books = {}
for game in (host, guest):
back_to_game(game)
game('command', command='clear @s')
game('command', command='recipe take @s *')
host.wait(20)
for game in (host, guest):
game.raw('key', 'e', 'tap')
game.wait(5)
book = game('screen').get('recipe_book')
assert book, f'{game.name}: the inventory has no recipe book'
if len(book['targets']) == 1:
game.raw('click-at', *centre(book['targets'][0]))
game.wait(5)
def known():
return {game.name: game('screen')['recipe_book']['known'] for game in (host, guest)}
try:
books['empty'] = known()
assert books['empty'] == {host.name: 0, guest.name: 0}, books
host('command', command=f'recipe give {GUEST_NAME} minecraft:oak_planks')
host('command', command=f'recipe give {GUEST_NAME} minecraft:stick')
host.wait(20)
books['host gives guest'] = known()
if SERVER_THREAD:
guest('command', command=f'recipe give {host_name} minecraft:stick')
guest.wait(20)
books['guest gives host'] = known()
host('command', command=f'recipe take {GUEST_NAME} minecraft:stick')
host.wait(20)
books['host takes from guest'] = known()
host.shot(out, 'recipe-targets-host')
guest.shot(out, 'recipe-targets-guest')
by_guest = int(SERVER_THREAD)
expected = {'empty': (0, 0), 'host gives guest': (0, 2), 'guest gives host': (by_guest, 2),
'host takes from guest': (by_guest, 1)}
for step, (in_host, in_guest) in expected.items():
assert books[step] == {host.name: in_host, guest.name: in_guest}, (step, books)
finally:
back_to_game(host)
back_to_game(guest)
return books
CHECKS += ['recipe_targets']
CHECK_FNS.update(recipe_targets=check_recipe_targets)
def check_shulker_bullets(host, guest, out):
"""ShulkerAttackGoal's bullets reach ClientPacketListener.handleAddEntity, then the existing
projectile renderer and interpolation, with distinct ids from the server's entity counter."""