diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 78efc708d..d36e0adec 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -2107,15 +2107,22 @@ impl Game<'_> { let from = pl.position; self.spawn_mob("minecraft:endermite", [from.x, from.y, from.z], false); } + // To where the pearl started its last tick (oldPosition). + let to = p.previous; if let Some(pl) = self.player.as_mut() { - pl.position = at; + pl.position = to; pl.velocity = Vec3::ZERO; pl.fall_distance = 0.0; + pl.teleported = true; } - self.previous_position = at; + self.previous_position = to; self.hurt_player_by("minecraft:ender_pearl", 5.0); - self.audio - .play("minecraft:entity.player.teleport", Some(atp), 1.0, 1.0); + self.audio.play( + "minecraft:entity.player.teleport", + Some([to.x, to.y, to.z]), + 1.0, + 1.0, + ); } } (ProjectileKind::SmallFireball, hit) => match hit { diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index b261e8ad2..c5cece06c 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -532,12 +532,14 @@ impl Game<'_> { g.health = m.health.health(); g.food = m.health.food.food; g.saturation = m.health.food.saturation; - // A teleport (commands) moves the guest. + // A teleport (commands, a pearl) moves the guest; a pearl's is + // sent even to where they stand (ServerPlayer.teleport). let at = [m.position.x, m.position.y, m.position.z]; m.sprinting = false; + let teleported = std::mem::take(&mut m.teleported); let moved: f64 = (0..3).map(|k| (at[k] - g.position[k]).powi(2)).sum(); g.motion = Some(Box::new(m)); - if moved > 0.0001 || turned { + if moved > 0.0001 || turned || teleported { host.hub.teleport(i, Some(at)); } } diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 192e900a1..b1088188c 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -270,6 +270,10 @@ pub struct PlayerMotion { pub steps: MovementDistances, pub needs_sync: bool, pub sync_position: bool, + /// Moved by an ender pearl (`ServerPlayer.teleport`): a server sends the + /// position packet even when the player lands where they stood. The + /// server takes it at the end of a guest's turn. + pub teleported: bool, pub events: Vec, pub attributes: LiveAttributes i32>>, pub impulse: ImpulseContext, @@ -440,6 +444,7 @@ impl PlayerMotion { }, needs_sync: false, sync_position: false, + teleported: false, events: Vec::new(), attributes, fluids: PlayerFluids::default(), diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java index 9a72ea224..d5e9c86fa 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java @@ -411,6 +411,12 @@ public final class VanillaProtocolClient { /** The entity mode's step, and how long it records the entities for (ms). */ private static final int ENTITIES = 1000; private static final long ENTITY_WAIT = 20_000; + /** + * How long a step waits at most for what it waits on (a villager to trade with, the ingot, a thrown + * snowball, the pearl's teleport and hurt): a busy machine can hold a server's ticks back for seconds, + * so the steps wait for those, not for a fixed time. + */ + private static final long PATIENCE = 30_000; static int moveEntityId(net.minecraft.network.protocol.game.ClientboundMoveEntityPacket p) { return (int) field(p, net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.class, "entityId"); @@ -464,6 +470,9 @@ public final class VanillaProtocolClient { int furnaceState = 0; // Snowballs the server spawned since the last right click. int snowballs = 0; + // When the first of them came, and when the furnace's ingot did. + long snowballAt = 0; + long smeltedAt = 0; // The player's own id, and what the ender pearl it throws does to it. int playerId = -1; boolean pearl = false; @@ -550,6 +559,7 @@ public final class VanillaProtocolClient { && furnaceWindow > 0 && p.getContainerId() == furnaceWindow) { furnaceState = p.getStateId(); record("play-furnace", "slot " + p.getSlot() + " " + p.getItem()); + if (p.getSlot() == 2 && !p.getItem().isEmpty() && smeltedAt == 0) smeltedAt = now; } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundContainerSetDataPacket p && furnaceWindow > 0 && p.getContainerId() == furnaceWindow && (p.getId() == 1 || p.getId() == 3)) { // Lit total and cooking total; the time left and the cooking timer follow the ticks. @@ -580,6 +590,7 @@ public final class VanillaProtocolClient { } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket p && p.getType() == net.minecraft.world.entity.EntityTypes.SNOWBALL) { snowballs++; + if (snowballAt == 0) snowballAt = now; // Projectile.getAddEntityPacket: its owner's id. record("play-use", "snowball owner is the player: " + (p.getData() == playerId)); } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket p @@ -666,8 +677,8 @@ public final class VanillaProtocolClient { send(new ServerboundChatCommandPacket(REPLIES[step - 2])); step++; stepAt = now; - } else if (step == 2 + REPLIES.length && now - stepAt > 1500) { - // Right-click the villager: the trading screen and its offers. + } else if (step == 2 + REPLIES.length && now - stepAt > 1500 && (villager >= 0 || now - stepAt > PATIENCE)) { + // Right-click the villager (once the server sent it): the trading screen and its offers. record("play-trade", "villager " + (villager >= 0)); if (villager >= 0) { send(new net.minecraft.network.protocol.game.ServerboundInteractPacket(villager, @@ -715,8 +726,9 @@ public final class VanillaProtocolClient { net.minecraft.core.Direction.NORTH, furnace, false), 1)); step++; stepAt = now; - } else if (step >= 8 + REPLIES.length && step <= 10 + REPLIES.length && furnaceWindow > 0 - && now - stepAt > (step == 10 + REPLIES.length ? 12_000 : 1500)) { + } else if (step >= 8 + REPLIES.length && step <= 10 + REPLIES.length && furnaceWindow > 0 && now - stepAt > 1500 + // The ingot out once the furnace made it (200 ticks), and its tick's other changes came. + && (step < 10 + REPLIES.length || smeltedAt > 0 && now - smeltedAt > 1000 || now - stepAt > 10_000 + PATIENCE)) { // Shift-click the ore (hotbar 0) into the input, the coal (hotbar 1) into the // fuel slot, and the smelted ingot out of the result; nothing claimed, so the // server sends every slot the click changed. @@ -743,9 +755,10 @@ public final class VanillaProtocolClient { useOn(respawnAt, net.minecraft.world.InteractionHand.MAIN_HAND, 2); use(position, net.minecraft.world.InteractionHand.MAIN_HAND, 3); snowballs = 0; + snowballAt = 0; step++; stepAt = now; - } else if (step == 14 + REPLIES.length && now - stepAt > 2000) { + } else if (step == 14 + REPLIES.length && thrown(snowballAt, stepAt, now)) { record("play-use", "main hand on a block: " + snowballs + " snowball(s)"); send(new ServerboundChatCommandPacket("item replace entity @s weapon.mainhand with minecraft:air")); send(new ServerboundChatCommandPacket("item replace entity @s weapon.offhand with minecraft:snowball 16")); @@ -757,9 +770,10 @@ public final class VanillaProtocolClient { useOn(respawnAt, net.minecraft.world.InteractionHand.OFF_HAND, 5); use(position, net.minecraft.world.InteractionHand.OFF_HAND, 6); snowballs = 0; + snowballAt = 0; step++; stepAt = now; - } else if (step == 16 + REPLIES.length && now - stepAt > 2000) { + } else if (step == 16 + REPLIES.length && thrown(snowballAt, stepAt, now)) { record("play-use", "off hand on a block: " + snowballs + " snowball(s)"); // An ender pearl thrown at its feet (no endermite: ThrownEnderpearl spawns one 5% of the time). send(new ServerboundChatCommandPacket("gamerule spawn_monsters false")); @@ -773,7 +787,8 @@ public final class VanillaProtocolClient { position.yRot(), 90.0F)); step++; stepAt = now; - } else if (step == 18 + REPLIES.length && now - stepAt > 2500) { + } else if (step == 18 + REPLIES.length && now - stepAt > 2500 + && (pearlTeleports > 0 && pearlHealth >= 0 || now - stepAt > PATIENCE)) { // ThrownEnderpearl.onHit: the thrower is teleported and takes 5 damage. record("play-pearl", "teleported " + pearlTeleports + " time(s), health " + pearlHealth); return; @@ -786,6 +801,14 @@ public final class VanillaProtocolClient { record("error", "timed out in play, step " + step + ", chunks " + chunks.size()); } + /** + * A right click's snowballs are counted: once the first came and a second could have come with it + * (the server spawns each in the tick it handles the click), or once the server had all the time. + */ + private static boolean thrown(long first, long clicked, long now) { + return now - clicked > 2000 && (first > 0 && now - first > 1000 || now - clicked > PATIENCE); + } + /** The death screen's Respawn button: true, the respawn being under way. */ private boolean respawn() throws Exception { send(new net.minecraft.network.protocol.game.ServerboundClientCommandPacket( @@ -821,10 +844,12 @@ public final class VanillaProtocolClient { */ private String respawnLine(Received r) { String t = r.type; - // Entities moving and the dead player's health land in the window between the death - // and the respawn as each server's ticks fall: left out. + // Entities moving, blocks the world's ticks change (a lava fall flows again once the game + // runs) and the dead player's health land in the window between the death and the respawn + // as each server's ticks fall: left out. if (t.startsWith("minecraft:move_entity") || t.equals("minecraft:rotate_head") || t.equals("minecraft:set_entity_motion") - || t.equals("minecraft:set_entity_data") || t.equals("minecraft:entity_position_sync") || t.equals("minecraft:set_health")) { + || t.equals("minecraft:set_entity_data") || t.equals("minecraft:entity_position_sync") || t.equals("minecraft:set_health") + || t.equals("minecraft:block_update") || t.equals("minecraft:section_blocks_update")) { return null; } if (t.equals("minecraft:keep_alive") || t.equals("minecraft:ping") || t.equals("minecraft:level_chunk_with_light")