From 6d36d468e70d0aea524e61f32081879ea064fa3b Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:29:55 +0100 Subject: [PATCH] Fix empty-hand drop animations --- .../examples/held_drop_fixture.rs | 11 +++++- crates/horizon-client/src/game/remote.rs | 23 ++++++----- .../parity/VanillaHeldDropOracle.java | 39 +++++++++++++++++-- tools/test_held_drop_parity.py | 10 +++-- 4 files changed, 65 insertions(+), 18 deletions(-) diff --git a/crates/horizon-client/examples/held_drop_fixture.rs b/crates/horizon-client/examples/held_drop_fixture.rs index 87ca9870c..a0c5d92ba 100644 --- a/crates/horizon-client/examples/held_drop_fixture.rs +++ b/crates/horizon-client/examples/held_drop_fixture.rs @@ -14,13 +14,22 @@ fn main() { for count in [0, 1, 4, 64] { let mut slot = Some(ItemStack::new("minecraft:stone", count)); let mut dropped = 0; + let mut swings = 0; + let mut packets = 0; for _ in 0..2 { if let Some(stack) = take_held_drop(&mut slot, all, mode == GameMode::Spectator) { dropped += stack.count; + swings += 1; } - println!("{} {}", slot.as_ref().map_or(0, |s| s.count), dropped); + if mode != GameMode::Spectator { + packets += 1; + } + println!( + "{} {dropped} {swings} {packets}", + slot.as_ref().map_or(0, |s| s.count), + ); } } } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 7c96e7bad..6614c68a3 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -7,7 +7,7 @@ //! client predicts its own movement; the server stays authoritative for //! everything else. use super::{Dim, Game, Mining, Screen}; -use horizon_client::first_person::Hand; +use horizon_client::first_person::{Hand, SwingAnimation}; use horizon_client::{ Result, chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section, block_strategy}, @@ -3836,23 +3836,26 @@ impl Game<'_> { Ok(true) } KeyCode::KeyQ => { + if self.spectator() { + return Ok(true); + } let all = self.keys.contains(&KeyCode::ControlLeft); + // MultiPlayerGameMode.dropItem: predict removal before sending + // the action, then swing only if a nonempty stack was removed. + let dropped = horizon_client::items::take_held_drop( + &mut self.inventory[self.selected], + all, + false, + ); self.send(S::PlayerAction(play::PlayerAction { action: if all { 4 } else { 5 }, pos: [0; 3], direction: 0, sequence: 0, })); - // Inventory.removeFromSelected (prediction). - let slot = &mut self.inventory[self.selected]; - if let Some(s) = slot { - if all || s.count <= 1 { - *slot = None; - } else { - s.count -= 1; - } + if dropped.is_some() { + self.swing_hand(Hand::Main, SwingAnimation::DEFAULT); } - self.swing_arm(); Ok(true) } KeyCode::KeyE => { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaHeldDropOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaHeldDropOracle.java index 8034968a5..b2cdb3383 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaHeldDropOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaHeldDropOracle.java @@ -6,9 +6,14 @@ import java.io.PrintWriter; import net.minecraft.client.*; import net.minecraft.client.gui.Gui; import net.minecraft.client.multiplayer.MultiPlayerGameMode; +import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.client.player.LocalPlayer; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket; +import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.player.*; import net.minecraft.world.item.*; +import net.minecraft.world.item.component.SwingAnimation; import net.minecraft.world.level.GameType; /** Runs the real key handler through its drop loop; stops before item-use handling. */ @@ -41,16 +46,36 @@ public final class VanillaHeldDropOracle { } static final class Actor extends LocalPlayer { GameType mode; + int swings; Actor() { super(null, null, null, null, null, null, false, null, null); } @Override public GameType gameMode() { return mode; } @Override public boolean isUsingItem() { throw new Done(); } + @Override public boolean swing(InteractionHand hand, SwingAnimation animation, boolean sendPacket) { + if (hand != InteractionHand.MAIN_HAND || !animation.equals(SwingAnimation.DEFAULT) || sendPacket) + throw new AssertionError("unexpected drop swing"); + swings++; + return true; + } + } + static final class Connection extends ClientPacketListener { + int drops; + ServerboundPlayerActionPacket.Action expected; + Connection() { super(null, null, null); } + @Override public void send(Packet packet) { + if (packet instanceof ServerboundPlayerActionPacket action) { + if (action.getAction() != expected) throw new AssertionError("unexpected drop action"); + drops++; + } + } } static final class Controller extends MultiPlayerGameMode { int dropped; Controller() { super(null, null); } - // Controlled dispatch boundary: no networking, swings, statistics or item entities. + // Run vanilla's actual prediction, packet dispatch and conditional swing. @Override public void dropItem(LocalPlayer player, boolean all) { - dropped += player.getInventory().removeFromSelected(all).getCount(); + int before = player.getInventory().getSelectedItem().getCount(); + super.dropItem(player, all); + dropped += before - player.getInventory().getSelectedItem().getCount(); } } public static void main(String[] args) throws Exception { @@ -81,6 +106,9 @@ public final class VanillaHeldDropOracle { field(Player.class, "inventory").set(actor, inventory); field(Minecraft.class, "player").set(client, actor); var controller = make(Controller.class); + var connection = make(Connection.class); + field(MultiPlayerGameMode.class, "minecraft").set(controller, client); + field(MultiPlayerGameMode.class, "connection").set(controller, connection); field(Minecraft.class, "gameMode").set(client, controller); try (var out = new PrintWriter(Files.newBufferedWriter(Path.of(args[0])))) { for (var mode : GameType.values()) for (boolean all : new boolean[]{false, true}) { @@ -91,6 +119,10 @@ public final class VanillaHeldDropOracle { inventory.setSelectedSlot(selected); inventory.setItem(selected, new ItemStack(Items.STONE, count)); controller.dropped = 0; + actor.swings = 0; + connection.drops = 0; + connection.expected = all ? ServerboundPlayerActionPacket.Action.DROP_ALL_ITEMS + : ServerboundPlayerActionPacket.Action.DROP_ITEM; // Two consecutive key presses also cover an emptied stack and repeated spectator drops. for (int press = 0; press < 2; press++) { ((Key) options.keyDrop).clicks = 1; @@ -100,7 +132,8 @@ public final class VanillaHeldDropOracle { } catch (InvocationTargetException e) { if (!(e.getCause() instanceof Done)) throw e; } - out.println(inventory.getSelectedItem().getCount() + " " + controller.dropped); + out.println(inventory.getSelectedItem().getCount() + " " + controller.dropped + + " " + actor.swings + " " + connection.drops); } } } diff --git a/tools/test_held_drop_parity.py b/tools/test_held_drop_parity.py index c546c5987..842ad3d8a 100644 --- a/tools/test_held_drop_parity.py +++ b/tools/test_held_drop_parity.py @@ -2,10 +2,12 @@ """Compare selected-slot drops with Minecraft.handleKeybinds in all four game modes. The oracle invokes the unmodified native key handler, consuming real drop-key -clicks. Its controlled dropItem callback removes from the real player Inventory; -other keys are idle and execution stops at the following item-use boundary. +clicks, and MultiPlayerGameMode.dropItem's real prediction and swing dispatch. +Other keys are idle and execution stops at the following item-use boundary. Checks Q/Ctrl+Q, slots 0/8, empty/single/partial/full stacks and repeated drops. -Packet delivery, item entities, swing animation and statistics are outside scope. +Checks swing attempts and drop packet counts, including packets without swings +for empty slots. Packet delivery, animation ticks, item entities and statistics +are outside scope. """ import os import subprocess @@ -35,7 +37,7 @@ def main(): subprocess.run(['cargo', 'run', '--quiet', '-p', 'horizon-client', '--example', 'held_drop_fixture'], cwd=lab.ROOT, stdout=out, check=True) matched = base.compare(build / 'vanilla.txt', build / 'rust.txt') - print(f'Held drop parity passed: {matched} selected-stack/drop-count snapshots match vanilla') + print(f'Held drop parity passed: {matched} inventory/drop/swing/packet snapshots match vanilla') if __name__ == '__main__':