Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Using items on a server: play the first-person swing

oxi-does-devf5b36c5bac02
diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs
index 341082b6b..6a3f2541f 100644
--- a/crates/horizon-client/src/game/remote.rs
+++ b/crates/horizon-client/src/game/remote.rs
@@ -7640,6 +7640,8 @@ impl Game<'_> {
// Item.use: Equippable.swapWithEquipmentSlot runs on this client too, so
// the armor slot and the hand change at once (the server confirms).
if info.equip_slot.is_some() && self.with_hand(hand, |g| g.equip_from_hand(&held)) {
+ // Equippable.swapWithEquipmentSlot's SUCCESS (SwingSource.CLIENT).
+ self.with_hand(hand, Game::swing_arm);
return true;
}
// `ItemStack.use` runs on this client too: a bucket's own ray and its
@@ -7668,12 +7670,20 @@ impl Game<'_> {
};
self.audio.play(&name, Some(at), volume, pitch);
}
+ // BucketItem.use's SUCCESS (SwingSource.CLIENT) on this client.
+ if succeeded {
+ self.with_hand(hand, Game::swing_arm);
+ }
return succeeded;
}
// SpawnEggItem.use on the client succeeds after a SOURCE_ONLY block
// hit; the server alone tests LiquidBlock and the placement permission.
if held.item.ends_with("_spawn_egg") {
- return self.spawn_egg_allowed(&held.item) && self.bucket_hit().is_some();
+ let succeeded = self.spawn_egg_allowed(&held.item) && self.bucket_hit().is_some();
+ if succeeded {
+ self.with_hand(hand, Game::swing_arm);
+ }
+ return succeeded;
}
// Food starts being eaten (Consumable.startConsuming). A food that
// can't be eaten (full and not always-edible) fails the use
@@ -7697,6 +7707,8 @@ impl Game<'_> {
self.apply_use_cooldown(&held);
}
if self.with_hand(hand, |g| g.remote_use_book(&held)) {
+ // WritableBookItem.use's SUCCESS (SwingSource.CLIENT).
+ self.with_hand(hand, Game::swing_arm);
return true;
}
// BowItem.use and CrossbowItem.use FAIL with nothing to shoot: no
@@ -7706,7 +7718,16 @@ impl Game<'_> {
}
let gliding = self.player.as_ref().is_some_and(|p| p.fall_flying);
// A stack's own `blocks_attacks` raises it as a shield's does.
- use_ends_click(&info, gliding) || self.blocks_attacks(&held).is_some()
+ let blocks = self.blocks_attacks(&held).is_some();
+ let ends = use_ends_click(&info, gliding) || blocks;
+ // MultiPlayerGameMode.useItem: `ItemStack.use`'s SUCCESS swings
+ // (SwingSource.CLIENT); CONSUME does not — a bow, crossbow, trident
+ // or spear starting its charge, and `ItemUtils.startUsingInstantly`'s
+ // food, drink, spyglass, shield and goat horn.
+ if ends && !blocks && remote_use_swings(&held, info.consumable.is_some()) {
+ self.with_hand(hand, Game::swing_arm);
+ }
+ ends
}
/// Whether `held`'s `Item.use` needs a target this client sees none of
@@ -9605,6 +9626,27 @@ fn use_ends_click(info: &horizon_client::items::ItemInfo, fall_flying: bool) ->
item_has_use(info) || (fall_flying && &*info.id == "minecraft:firework_rocket")
}
+/// Whether `Item.use` of this item answers SUCCESS on this client, which
+/// swings (`MultiPlayerGameMode.useItem`'s `result.shouldSwing()`), rather
+/// than CONSUME, which only ends the click: a bow, crossbow, trident or
+/// spear starting its charge, `ItemUtils.startUsingInstantly`'s food, drink,
+/// spyglass, shield and goat horn, and the ender eye's SUCCESS_SERVER (the
+/// server's swing source, not this client's).
+fn remote_use_swings(held: &ItemStack, consumable: bool) -> bool {
+ let id = &*held.item;
+ !matches!(
+ id,
+ "minecraft:bow"
+ | "minecraft:crossbow"
+ | "minecraft:trident"
+ | "minecraft:spyglass"
+ | "minecraft:goat_horn"
+ | "minecraft:ender_eye"
+ | "minecraft:brush"
+ ) && !consumable
+ && horizon_client::spear::Kinetic::of(id).is_none()
+}
+
/// Whether `Item.use` usually acts on this item (eats, drinks, equips, raises,
/// throws, shoots, fills or places), which ends `Minecraft.startUseItem` at
/// its hand. Tools and materials pass, and the off hand gets its turn. The
@@ -9919,10 +9961,51 @@ fn client_millis() -> u64 {
#[cfg(test)]
mod tests {
- use super::{keep_client_state, synced_local_use};
- use horizon_client::containers::{Beacon, BlockEntity};
+ use super::{keep_client_state, remote_use_swings, synced_local_use};
+ use horizon_client::{
+ containers::{Beacon, BlockEntity},
+ items::ItemStack,
+ };
use horizon_world::nbt::Tag;
+ /// `MultiPlayerGameMode.useItem`'s `shouldSwing`: the SUCCESS answers
+ /// swing, CONSUME's do not (#6425).
+ #[test]
+ fn a_thrown_item_swinging_on_a_server_and_a_charge_not() {
+ let stack = |id: &str| ItemStack::new(id, 1);
+ // SUCCESS: the throwables, a bucket, a boat, a bundle, a book, a map.
+ for id in [
+ "minecraft:ender_pearl",
+ "minecraft:splash_potion",
+ "minecraft:wind_charge",
+ "minecraft:snowball",
+ "minecraft:experience_bottle",
+ "minecraft:bucket",
+ "minecraft:oak_boat",
+ "minecraft:bundle",
+ "minecraft:writable_book",
+ "minecraft:map",
+ ] {
+ assert!(remote_use_swings(&stack(id), false), "{id} swings");
+ }
+ // CONSUME: the charges and startUsingInstantly's uses, a drinkable
+ // potion among them (`consumable` set).
+ for id in [
+ "minecraft:bow",
+ "minecraft:crossbow",
+ "minecraft:trident",
+ "minecraft:spyglass",
+ "minecraft:goat_horn",
+ "minecraft:ender_eye",
+ ] {
+ assert!(!remote_use_swings(&stack(id), false), "{id} does not swing");
+ }
+ // The kinetic weapons charge like a trident.
+ assert!(!remote_use_swings(&stack("minecraft:iron_spear"), false));
+ // A consumable (food, a drink) starts its use without a swing.
+ assert!(!remote_use_swings(&stack("minecraft:apple"), true));
+ }
+
#[test]
fn a_beacon_update_tag_keeps_the_beam_the_client_walked() {
let old = BlockEntity::Beacon(Box::new(Beacon {