Merge origin/fix/lan-remote-item-use (#887) into main
Conflicts: the hub's player state keeps both main's attributes (#857) and #887's living_flags; remote.rs and lan.rs debug JSON keep swinging and using; the guests' model tick keeps both the swing update and the item in use; lan_e2e.py checks unioned. remote_player_eating reads main's Option<Consumable> (#799): any consumable with particles, its own use time and should_emit, as the local player does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -102,6 +102,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
equipment: Default::default(),
|
||||
health: 20.0,
|
||||
attributes: Vec::new(),
|
||||
living_flags: 0,
|
||||
};
|
||||
loop {
|
||||
world.game_time += 1;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! server threads and the play-state hub, feeds it the world's entities each
|
||||
//! tick and turns what the guests do into gameplay. A world that is not open
|
||||
//! costs one `Option` check per tick.
|
||||
use super::Game;
|
||||
use super::{Game, shield::OFFHAND_SLOT};
|
||||
use horizon_client::{
|
||||
Result,
|
||||
items::ItemStack,
|
||||
@@ -402,6 +402,8 @@ impl Game<'_> {
|
||||
}),
|
||||
// The host draws this guest's arm swinging.
|
||||
"model_swinging": h.models.get(&g.conn).map(|m| m.ai.swinging),
|
||||
// For how many ticks the host draws this guest using an item.
|
||||
"model_using": h.models.get(&g.conn).map(|m| m.extra.using_item),
|
||||
})).collect::<Vec<_>>(),
|
||||
}),
|
||||
_ => json!({ "open": false }),
|
||||
@@ -732,6 +734,7 @@ impl Game<'_> {
|
||||
equipment,
|
||||
health: p.health.health(),
|
||||
attributes: horizon_client::lan::attributes::syncable(&p.attributes),
|
||||
living_flags: living_flags(self.using),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -944,6 +947,7 @@ impl Game<'_> {
|
||||
}
|
||||
self.lan_flush_effects(&mut host);
|
||||
self.tick_guests(&mut host);
|
||||
self.lan_living_flags(&mut host);
|
||||
self.tick_server_travel(&mut host);
|
||||
self.lan_swings(&mut host);
|
||||
self.lan_update_streaming(&host);
|
||||
@@ -1204,6 +1208,11 @@ impl Game<'_> {
|
||||
// LivingEntity.updateSwingTime: the arm swing lan_swings started.
|
||||
m.anim.attack_anim_o = m.anim.attack_anim;
|
||||
horizon_client::mobs::ai::tick_swing(m);
|
||||
// LivingEntity.onSyncedDataUpdated(DATA_LIVING_ENTITY_FLAGS): the
|
||||
// item in use and its hand (the arm poses of a bow, a shield...).
|
||||
let using = host.play.get(&g.conn).and_then(|p| p.using());
|
||||
m.extra.using_item = using.map_or(0, |(_, ticks)| ticks as i32 + 1);
|
||||
m.extra.using_off_hand = using.is_some_and(|(slot, _)| slot == OFFHAND_SLOT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1272,3 +1281,9 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `LivingEntity.DATA_LIVING_ENTITY_FLAGS` for an item in use: 1, and 2 when
|
||||
/// it is the off hand's.
|
||||
pub(super) fn living_flags(using: Option<(usize, u32)>) -> i8 {
|
||||
using.map_or(0, |(slot, _)| if slot == OFFHAND_SLOT { 3 } else { 1 })
|
||||
}
|
||||
|
||||
@@ -127,6 +127,13 @@ pub(super) struct GuestSwap {
|
||||
swings: u32,
|
||||
}
|
||||
|
||||
impl GuestPlay {
|
||||
/// The item in use: its slot and for how many ticks.
|
||||
pub(super) fn using(&self) -> Option<(usize, u32)> {
|
||||
self.using
|
||||
}
|
||||
}
|
||||
|
||||
struct ItemRules<'a>(&'a horizon_client::items::Items);
|
||||
impl Rules for ItemRules<'_> {
|
||||
fn max_stack(&self, item: &str) -> u32 {
|
||||
@@ -1861,6 +1868,15 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The guests' items in use, for the players around (`LivingEntity.startUsingItem`
|
||||
/// and `stopUsingItem` set DATA_LIVING_ENTITY_FLAGS).
|
||||
pub(super) fn lan_living_flags(&mut self, host: &mut LanHost) {
|
||||
for g in &mut host.hub.guests {
|
||||
let using = host.play.get(&g.conn).and_then(GuestPlay::using);
|
||||
g.living_flags = super::lan::living_flags(using);
|
||||
}
|
||||
}
|
||||
|
||||
/// A use the server ran swung the guest's hand: their own client is told
|
||||
/// too (`swing(hand, true)` for a SwingSource.SERVER result). Horizon's
|
||||
/// client does not run uses itself, so it does not predict the swing; a
|
||||
|
||||
@@ -1505,6 +1505,25 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// LivingEntity.onSyncedDataUpdated(DATA_LIVING_ENTITY_FLAGS):
|
||||
// another player starts using the item in a hand (eating, a
|
||||
// bow drawn, a shield up), or stops.
|
||||
let other = self
|
||||
.remote
|
||||
.as_ref()
|
||||
.is_some_and(|r| r.level_players.contains_key(&p.id) && r.entity_id != p.id);
|
||||
if other && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) {
|
||||
for (index, _, value) in &p.values {
|
||||
if let (8, Value::Byte(flags)) = (index, value) {
|
||||
m.extra.using_off_hand = flags & 2 != 0;
|
||||
if flags & 1 == 0 {
|
||||
m.extra.using_item = 0;
|
||||
} else if m.extra.using_item == 0 {
|
||||
m.extra.using_item = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): this
|
||||
// player lies down in the bed, or gets up.
|
||||
if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) {
|
||||
@@ -2686,6 +2705,7 @@ impl Game<'_> {
|
||||
let mut lerps = std::mem::take(&mut r.lerps);
|
||||
let head_lerps = &mut r.head_lerps;
|
||||
let players = &r.level_players;
|
||||
let mut using = Vec::new();
|
||||
for m in &mut self.mobs {
|
||||
let player = players.contains_key(&(m.id as i32));
|
||||
if !running && !player {
|
||||
@@ -2737,6 +2757,28 @@ impl Game<'_> {
|
||||
m.head_yaw += wrap_degrees(*yaw - m.head_yaw) / *steps as f32;
|
||||
*steps -= 1;
|
||||
}
|
||||
// LivingEntity.updatingUsingItem for another player: the ticks the
|
||||
// item has been used before this one.
|
||||
if player && m.extra.using_item > 0 {
|
||||
let item = if m.extra.using_off_hand {
|
||||
m.extra.off_hand.clone()
|
||||
} else {
|
||||
m.extra.main_hand.clone()
|
||||
};
|
||||
if let Some(item) = item {
|
||||
let eye = if m.extra.crouching { 1.27 } else { 1.62 };
|
||||
let at = [m.position.x, m.position.y, m.position.z];
|
||||
using.push((
|
||||
item,
|
||||
m.extra.using_item as u32 - 1,
|
||||
at,
|
||||
eye,
|
||||
m.head_pitch,
|
||||
m.yaw,
|
||||
));
|
||||
}
|
||||
m.extra.using_item += 1;
|
||||
}
|
||||
// LivingEntity.tick's tickHeadTurn: a mob's BodyRotationControl,
|
||||
// a player's body following its walk and look.
|
||||
if player {
|
||||
@@ -2784,6 +2826,9 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
head_lerps.retain(|_, (_, steps)| *steps > 0);
|
||||
for (item, ticks, at, eye, pitch, yaw) in using {
|
||||
self.remote_player_eating(&item, ticks, at, eye, pitch, yaw);
|
||||
}
|
||||
if running {
|
||||
// Boats, minecarts, armor stands, projectiles and the rest
|
||||
// interpolate like mobs.
|
||||
@@ -2809,6 +2854,47 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `Consumable.onConsumeTick` for another player using a consumable: the
|
||||
/// crumbs at their mouth every 4 ticks once 21.875% of its use has passed
|
||||
/// (`shouldEmitParticlesAndSounds`; the sounds come from the server).
|
||||
fn remote_player_eating(
|
||||
&mut self,
|
||||
item: &str,
|
||||
ticks: u32,
|
||||
at: [f64; 3],
|
||||
eye: f64,
|
||||
pitch: f32,
|
||||
yaw: f32,
|
||||
) {
|
||||
let Some(consumable) = self
|
||||
.res
|
||||
.items()
|
||||
.get(item)
|
||||
.and_then(|i| i.consumable.as_ref())
|
||||
.filter(|c| c.particles)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let duration = consumable.ticks().max(0) as u32;
|
||||
let remaining = duration - ticks.min(duration);
|
||||
if !consumable.should_emit(remaining as i32) {
|
||||
return;
|
||||
}
|
||||
let options = self.item_particle(&ItemStack::new(item, 1));
|
||||
let mut spawned = Vec::new();
|
||||
horizon_client::particles::item_use_particles(
|
||||
&mut self.world.random,
|
||||
pitch,
|
||||
yaw,
|
||||
at,
|
||||
at[1] + eye,
|
||||
5,
|
||||
|p, v| spawned.push((p, v)),
|
||||
);
|
||||
for (p, v) in spawned {
|
||||
self.particles.add(&options, p, v);
|
||||
}
|
||||
}
|
||||
/// LocalPlayer.sendPosition plus input, sprint and held-slot updates.
|
||||
fn remote_send_state(&mut self, input: u8) {
|
||||
let Some(p) = self.player.as_ref() else {
|
||||
@@ -3859,6 +3945,7 @@ impl Game<'_> {
|
||||
serde_json::json!({
|
||||
"id": m.id,
|
||||
"swinging": m.ai.swinging,
|
||||
"using": m.extra.using_item,
|
||||
"armor": armor,
|
||||
"main_hand": e.main_hand.as_deref(),
|
||||
"off_hand": e.off_hand.as_deref(),
|
||||
|
||||
@@ -75,6 +75,9 @@ pub struct Guest {
|
||||
pub fall_flying: bool,
|
||||
pub flying: bool,
|
||||
pub sleeping: bool,
|
||||
/// `LivingEntity.DATA_LIVING_ENTITY_FLAGS`: 1 while using an item, 2 if
|
||||
/// it is in the off hand.
|
||||
pub living_flags: i8,
|
||||
/// The bed (its head) the player sleeps in (`LivingEntity.getSleepingPos`),
|
||||
/// `Player.sleepCounter`, and the bed last told to the player itself.
|
||||
pub bed: Option<[i32; 3]>,
|
||||
@@ -222,6 +225,7 @@ impl Guest {
|
||||
fall_flying: false,
|
||||
flying: false,
|
||||
sleeping: false,
|
||||
living_flags: 0,
|
||||
bed: None,
|
||||
sleep_timer: 0,
|
||||
sent_bed: None,
|
||||
|
||||
@@ -82,6 +82,8 @@ pub struct HostState {
|
||||
pub health: f32,
|
||||
/// The host player's client-syncable attribute instances.
|
||||
pub attributes: Vec<play::AttributeSnapshot>,
|
||||
/// `LivingEntity.DATA_LIVING_ENTITY_FLAGS` (the item in use).
|
||||
pub living_flags: i8,
|
||||
}
|
||||
|
||||
/// Everything the hub needs from the game for one tick.
|
||||
@@ -1391,6 +1393,7 @@ impl Hub {
|
||||
rot: (f32, f32, f32),
|
||||
on_ground: bool,
|
||||
flags: (bool, bool, bool, bool),
|
||||
living_flags: i8,
|
||||
skin: u8,
|
||||
health: f32,
|
||||
equipment: Vec<(u8, Option<horizon_protocol::item::Stack>)>,
|
||||
@@ -1423,6 +1426,9 @@ impl Hub {
|
||||
if pose != 0 {
|
||||
meta.push((6, serializer::POSE, Value::Id(pose)));
|
||||
}
|
||||
// Always there: the trackers send the entries that changed, and an
|
||||
// item no longer in use is a change back to 0.
|
||||
meta.push((8, serializer::BYTE, Value::Byte(living_flags)));
|
||||
if (health - 1.0).abs() > f32::EPSILON && health != 20.0 {
|
||||
meta.push((9, serializer::FLOAT, Value::Float(health)));
|
||||
}
|
||||
@@ -1487,6 +1493,7 @@ impl Hub {
|
||||
(h.yaw, h.pitch, h.head_yaw),
|
||||
h.on_ground,
|
||||
(h.sneaking, h.sprinting, false, false),
|
||||
h.living_flags,
|
||||
0x7F,
|
||||
h.health,
|
||||
equipment,
|
||||
@@ -1517,6 +1524,7 @@ impl Hub {
|
||||
(g.yaw, g.pitch, g.head_yaw),
|
||||
g.on_ground,
|
||||
(g.sneaking, g.sprinting, g.fall_flying, g.sleeping),
|
||||
g.living_flags,
|
||||
g.info.model_customisation,
|
||||
g.health,
|
||||
equipment,
|
||||
|
||||
@@ -182,6 +182,7 @@ impl Server {
|
||||
equipment: Default::default(),
|
||||
health: 20.0,
|
||||
attributes: Vec::new(),
|
||||
living_flags: 0,
|
||||
};
|
||||
let mut ctx = TickCtx {
|
||||
world: &mut self.world,
|
||||
|
||||
+40
-2
@@ -55,6 +55,8 @@ address. The script then checks, from both sides, what a guest's game used to ge
|
||||
pushes it, as in vanilla), not ever faster across the ground.
|
||||
- eat: holding right click with bread, the guest's own screen plays the eating (the item in use
|
||||
counts up) and the server eats one loaf and feeds the guest.
|
||||
- eating: each side sees the other player eat: the item in use (LivingEntity's flags) on the
|
||||
player it draws, and no longer once the bread is eaten (#803);
|
||||
- xp: experience orbs 4 blocks away fly to the guest, show on the guest's screen and give the
|
||||
guest their points (not the host).
|
||||
- objects: boats, minecarts, armor stands, item frames and paintings the host summons reach the
|
||||
@@ -103,7 +105,7 @@ ROOT = Path(__file__).resolve().parents[1]
|
||||
GUEST_NAME = 'Guest'
|
||||
# The host plays single player on its server thread (--server-thread).
|
||||
SERVER_THREAD = False
|
||||
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'light', 'time', 'swings', 'plates', 'effects', 'portals', 'distance']
|
||||
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'trials', 'clicks', 'sleep', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'light', 'time', 'swings', 'plates', 'effects', 'portals', 'distance']
|
||||
|
||||
|
||||
class Game:
|
||||
@@ -1329,6 +1331,42 @@ def check_swings(host, guest, out):
|
||||
return detail
|
||||
|
||||
|
||||
def check_eating(host, guest, out):
|
||||
for g in (host, guest):
|
||||
g('command', command='gamemode survival')
|
||||
g('command', command='clear @s')
|
||||
# Hungry: Player.canEat.
|
||||
g('command', command='effect give @s minecraft:hunger 3 255')
|
||||
host.wait(70)
|
||||
for g in (host, guest):
|
||||
g('command', command='effect clear @s')
|
||||
g('command', command='give @s minecraft:bread 2')
|
||||
host.wait(5)
|
||||
face_each_other(host, guest)
|
||||
|
||||
def using(game):
|
||||
# On its server thread the host's window is a client of its server too.
|
||||
if game is host and not SERVER_THREAD:
|
||||
return lan_guest(host).get('model_using') or 0
|
||||
return max((e.get('using') or 0 for e in remote(game).get('player_equipment', [])), default=0)
|
||||
seen = {}
|
||||
for name, eater, watcher in (('host eats', host, guest), ('guest eats', guest, host)):
|
||||
eater.raw('select', 'minecraft:bread')
|
||||
hold = threading.Thread(target=eater.raw, args=('use-item', '--hold', 2.2))
|
||||
hold.start()
|
||||
samples = []
|
||||
for _ in range(4):
|
||||
time.sleep(0.3)
|
||||
samples.append(using(watcher))
|
||||
watcher.shot(out, 'eating-' + name.replace(' ', '-'))
|
||||
hold.join()
|
||||
eater.wait(10)
|
||||
seen[name] = {'during': samples, 'after': using(watcher)}
|
||||
for name, s in seen.items():
|
||||
assert sum(1 for u in s['during'] if u > 0) >= 3 and s['after'] == 0, f'{name}: {seen}'
|
||||
return seen
|
||||
|
||||
|
||||
def check_xp(host, guest, out):
|
||||
for g in (host, guest):
|
||||
g('command', command='gamemode survival')
|
||||
@@ -1549,7 +1587,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name
|
||||
'commands': check_commands,
|
||||
'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls,
|
||||
'effects': check_effects, 'portals': check_portals, 'distance': check_distance, 'clicks': check_clicks,
|
||||
'sleep': check_sleep, 'push': check_push, 'eat': check_eat,
|
||||
'sleep': check_sleep, 'push': check_push, 'eat': check_eat, 'eating': check_eating,
|
||||
'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles,
|
||||
'light': check_light, 'plates': check_plates,
|
||||
'swings': check_swings,
|
||||
|
||||
Reference in New Issue
Block a user