Merge commit 'd6dc08098a30a08be7ec168fa3e6b756aa3ac493' into fix/worldgen-clippy-bool-20261023

This commit is contained in:
Ganer
2026-10-23 17:12:21 -05:00
10 changed files with 293 additions and 38 deletions
+26 -9
View File
@@ -123,6 +123,7 @@ mod raids;
mod recipe_book;
mod remote;
mod remote_mob_data;
mod remote_mob_use;
mod remote_objects;
mod remote_projectiles;
mod remote_stations;
@@ -505,6 +506,9 @@ enum FallStep {
enum GuestUse {
OnBlock(Hand),
Item(Hand),
/// `handleInteract`: `Player.interactOn` with the hand, nothing else (the
/// client sends UseItem itself when the entity passed).
Entity(Hand),
}
/// The client world as seen by the vanilla block-behaviour library.
@@ -3239,6 +3243,9 @@ impl Game<'_> {
// A guest's use packets name one hand and touch no entity: the client
// sends one per hand and part of its click, and Interact for entities.
let guest = self.guest_use;
if let Some(GuestUse::Entity(hand)) = guest {
return Ok(self.with_hand(hand, Game::interact_entity)?.then_some(hand));
}
let on_block = matches!(guest, Some(GuestUse::OnBlock(_)));
if !on_block
&& self.riding.is_some()
@@ -3247,14 +3254,7 @@ impl Game<'_> {
{
return Ok(Some(Hand::Main));
}
if guest.is_none()
&& (self.interact_frame()
|| self.interact_armor_stand()
|| self.interact_boat()
|| self.interact_cart()
|| self.interact_mob()?
|| self.interact_knot())
{
if guest.is_none() && self.interact_entity()? {
return Ok(Some(Hand::Main));
}
// An EntityHitResult may fall through to Item.use, never to useItemOn.
@@ -3264,16 +3264,33 @@ impl Game<'_> {
self.pick()
};
let hands = match guest {
Some(GuestUse::OnBlock(hand) | GuestUse::Item(hand)) => vec![hand],
Some(GuestUse::OnBlock(hand) | GuestUse::Item(hand) | GuestUse::Entity(hand)) => {
vec![hand]
}
None => Hand::BOTH.to_vec(),
};
let entity = guest.is_none() && self.entity_targeted();
for hand in hands {
// The off hand's turn starts on the entity too (Minecraft.startUseItem).
if entity && hand == Hand::Off && self.with_hand(hand, Game::interact_entity)? {
return Ok(Some(hand));
}
if self.with_hand(hand, |game| game.use_hand(block_hit.as_ref()))? {
return Ok(Some(hand));
}
}
Ok(None)
}
/// `Player.interactOn` for the entity under the crosshair with the
/// acting hand: whether it took the click.
fn interact_entity(&mut self) -> Result<bool> {
Ok(self.interact_frame()
|| self.interact_armor_stand()
|| self.interact_boat()
|| self.interact_cart()
|| self.interact_mob()?
|| self.interact_knot())
}
/// One hand's turn in `startUseItem`: `useItemOn` the block aimed at,
/// then `useItem`. Whether the click ends here (it acted or failed).
fn use_hand(&mut self, block_hit: Option<&Hit>) -> Result<bool> {
@@ -461,14 +461,14 @@ impl Game<'_> {
count: 1,
..h.clone()
});
self.inventory[self.selected] = Some(ItemStack {
*self.held_mut() = Some(ItemStack {
count: h.count - 1,
..h
});
}
h => {
self.armor_stands[i].equipment[slot] = h;
self.inventory[self.selected] = current;
*self.held_mut() = current;
}
}
let p = self.armor_stands[i].position;
+18 -13
View File
@@ -2413,13 +2413,14 @@ impl Game<'_> {
self.spawn_item(rest, self.eye_position().unwrap_or([0.0; 3]), [0.0; 3], 40);
}
} else {
self.inventory[self.selected] = Some(filled);
*self.held_mut() = Some(filled);
}
self.swing_arm();
return Ok(true);
}
let creative = self.creative;
let selected = self.selected;
// The hand's slot (Player.interactOn passes the hand).
let selected = self.held_slot();
if self.interact_zombie_villager(index, &item) {
return Ok(true);
}
@@ -2451,10 +2452,10 @@ impl Game<'_> {
// PiglinAi.mobInteract: hand over something gold to admire.
if self.mobs[index].alive() && self.give_piglin(index, &item) {
self.swing_arm();
if !creative && let Some(stack) = self.inventory[selected].as_mut() {
if !creative && let Some(stack) = self.stack_in_mut(selected).as_mut() {
stack.count -= 1;
if stack.count == 0 {
self.inventory[selected] = None;
*self.stack_in_mut(selected) = None;
}
}
return Ok(true);
@@ -2765,11 +2766,11 @@ impl Game<'_> {
}
if consume
&& !creative
&& let Some(stack) = self.inventory[selected].as_mut()
&& let Some(stack) = self.stack_in_mut(selected).as_mut()
{
stack.count -= 1;
if stack.count == 0 {
self.inventory[selected] = None;
*self.stack_in_mut(selected) = None;
}
}
if let Some(g) = give {
@@ -2779,8 +2780,8 @@ impl Game<'_> {
damage: 0,
extra: None,
};
if self.inventory[selected].is_none() {
self.inventory[selected] = Some(stack);
if self.stack_in(selected).is_none() {
*self.stack_in_mut(selected) = Some(stack);
} else if let Some(rest) = self.give(stack) {
let at = self.eye_position().unwrap_or(center);
self.spawn_item(rest, at, [0.0, 0.0, 0.0], 40);
@@ -2812,13 +2813,17 @@ impl Game<'_> {
if self.creative {
return;
}
if let Some(stack) = self.inventory[self.selected].as_mut()
&& let Some(info) = self.res.items().get(&stack.item)
&& info.max_damage > 0
let slot = self.held_slot();
let max_damage = self
.held()
.and_then(|s| self.res.items().get(&s.item))
.map_or(0, |i| i.max_damage);
if max_damage > 0
&& let Some(stack) = self.stack_in_mut(slot).as_mut()
{
stack.damage += 1;
if stack.damage >= info.max_damage {
self.inventory[self.selected] = None;
if stack.damage >= max_damage {
*self.stack_in_mut(slot) = None;
}
}
}
@@ -974,9 +974,14 @@ impl Game<'_> {
.look_at_entity(host, gi, p.id, Some(p.location))
.is_some()
{
// handleInteract: the entity and that hand only; a click the
// entity passes comes back as UseItem.
let hand = if p.hand == 1 { Hand::Off } else { Hand::Main };
let swung = self.as_guest(host, conn, |g| {
g.guest_use = Some(GuestUse::Entity(hand));
let swings = g.swings_started;
let _ = g.use_item();
g.guest_use = None;
g.swings_started != swings
});
self.lan_swing_back(host, conn, swung);
+2 -2
View File
@@ -202,10 +202,10 @@ impl Game<'_> {
self.mobs[index].extra.leash = Some(Holder::Player);
self.lead_sound("tied", at);
// The lead is always used up.
if let Some(stack) = self.inventory[self.selected].as_mut() {
if let Some(stack) = self.held_mut().as_mut() {
stack.count -= 1;
if stack.count == 0 {
self.inventory[self.selected] = None;
*self.held_mut() = None;
}
}
return true;
+1 -1
View File
@@ -104,7 +104,7 @@ impl Game<'_> {
}
/// `AbstractHorse.mobInteract`. Returns whether the click was used.
pub(super) fn interact_mount(&mut self, index: usize, item: &str) -> bool {
let held = self.inventory[self.selected].clone();
let held = self.held().cloned();
let m = &self.mobs[index];
let species = m.species();
// A server player rides it already (`isVehicle`: no seat, no feeding).
+21 -8
View File
@@ -3739,7 +3739,10 @@ impl Game<'_> {
horizon_client::interaction_input::use_on_target(&mut RemoteUseWorld(self), entity);
}
fn remote_use_entity(&mut self) -> bool {
/// `Minecraft.startUseItem`'s entity turn for `hand`: Interact goes to
/// the server, and whether the click ends there is what this client's
/// copy of the entity answers (`MultiPlayerGameMode.interact`).
fn remote_use_entity(&mut self, hand: Hand) -> bool {
let sneaking = self.keys.contains(&KeyCode::ShiftLeft);
// A boat, minecart, item frame, armor stand or painting nearer than
// any mob: the server uses it (AbstractBoat.interact boards it).
@@ -3747,6 +3750,10 @@ impl Game<'_> {
&& self.mob_hit().is_none_or(|(_, d)| hit.distance < d)
&& self.before_block(hit.distance)
{
// The main hand's turn takes it; there is no off hand turn.
if hand == Hand::Off {
return false;
}
self.send(S::Interact(play::Interact {
id: hit.id,
hand: 0,
@@ -3762,18 +3769,18 @@ impl Game<'_> {
let id = self.mobs[i].id as i32;
self.send(S::Interact(play::Interact {
id,
hand: 0,
hand: hand_id(hand),
location: [0.0, 1.0, 0.0],
secondary: sneaking,
}));
// Player.interactOn: another player passes, and the held item is
// used instead (Minecraft.startUseItem).
// Player.interactOn: another player passes, and so does a mob
// the held item means nothing to: the item is used instead.
let player = self
.remote
.as_ref()
.is_some_and(|r| r.level_players.contains_key(&id));
if !player {
self.swing_arm();
if !player && self.remote_mob_takes(i, hand) {
self.with_hand(hand, Game::swing_arm);
return true;
}
}
@@ -4030,7 +4037,13 @@ impl Game<'_> {
if self.remote_use_item(Hand::Main) {
return;
}
if !self.entity_targeted() && self.remote_use_block(Hand::Off) {
// The off hand's turn: the entity again, or the block.
let off_target = if self.entity_targeted() {
self.remote_use_entity(Hand::Off)
} else {
self.remote_use_block(Hand::Off)
};
if off_target {
return;
}
self.remote_use_item(Hand::Off);
@@ -5135,7 +5148,7 @@ struct RemoteUseWorld<'g, 'a>(&'g mut Game<'a>);
impl horizon_client::interaction_input::TargetUseWorld for RemoteUseWorld<'_, '_> {
fn use_entity(&mut self) -> bool {
self.0.remote_use_entity()
self.0.remote_use_entity(Hand::Main)
}
fn use_block(&mut self) -> bool {
self.0.remote_use_block(Hand::Main)
@@ -0,0 +1,91 @@
//! A server's client right-clicking a mob: vanilla's client runs
//! `Player.interactOn` on its own level (`MultiPlayerGameMode.interact`) to
//! learn whether the click ends on the mob or goes on to the held item
//! (`Minecraft.startUseItem` then sends UseItem). The server does the real
//! interaction; this is what the client's copy of the mob answers.
use super::Game;
use horizon_client::{
first_person::Hand,
mobs::{Species, equine, llama},
};
impl Game<'_> {
/// Whether mob `i` takes a right click with `hand`'s item on this
/// client (`Mob.interact`'s result consumes the action).
pub(super) fn remote_mob_takes(&self, i: usize, hand: Hand) -> bool {
let Some(m) = self.mobs.get(i).filter(|m| m.alive()) else {
return false;
};
let stack = match hand {
Hand::Main => self.stack_in(self.selected),
Hand::Off => self.offhand.as_ref(),
};
let item = stack.map_or("", |s| &*s.item);
let baby = m.is_baby();
// Mob.checkAndHandleImportantInteractions: a lead, a named name tag,
// a spawn egg of the mob's own kind.
if item == "minecraft:lead"
|| item == "minecraft:name_tag" && stack.is_some_and(|s| s.custom_name().is_some())
|| item.strip_suffix("_spawn_egg") == Some(m.kind.id.as_str())
{
return true;
}
// Animal.mobInteract: its food is CONSUME on a client.
if m.kind.flags.animal && m.kind.food.iter().any(|f| f.as_str() == item) {
return true;
}
let sneaking = self.keys.contains(&winit::keyboard::KeyCode::ShiftLeft);
match m.species() {
// Villager / WanderingTrader.mobInteract: trading, or a baby's head shake.
Species::Villager | Species::WanderingTrader => item != "minecraft:villager_spawn_egg",
// AbstractHorse, Camel: ridden (or their inventory opened); a baby only eats.
s if equine::is_equine(s)
|| llama::is_llama(s)
|| matches!(s, Species::Camel | Species::CamelHusk) =>
{
!baby
}
// Pig / Strider.mobInteract: a saddled one is ridden.
Species::Pig | Species::Strider => m.extra.saddled && !sneaking,
// HappyGhast.mobInteract: a harnessed adult is ridden.
Species::HappyGhast => !baby && m.extra.head.is_some(),
// TamableAnimal (wolf, cat, parrot) on a client: CONSUME once tame.
Species::Wolf | Species::Cat | Species::Parrot => m.extra.tame,
// Allay.mobInteract: gives an item or takes it back.
Species::Allay => !item.is_empty() || m.extra.main_hand.is_some(),
Species::Cow | Species::Goat => item == "minecraft:bucket" && !baby,
Species::Mooshroom => {
matches!(
item,
"minecraft:bucket" | "minecraft:bowl" | "minecraft:shears"
) && !baby
}
Species::Sheep => item == "minecraft:shears" || item.ends_with("_dye"),
// Bucketable.bucketMobPickup.
Species::Cod
| Species::Salmon
| Species::TropicalFish
| Species::Pufferfish
| Species::Axolotl
| Species::Tadpole => item == "minecraft:water_bucket",
Species::Armadillo => item == "minecraft:brush",
// ZombieVillager.mobInteract: CONSUME for a golden apple.
Species::ZombieVillager => item == "minecraft:golden_apple",
// Creeper.mobInteract: CREEPER_IGNITERS.
Species::Creeper => {
matches!(item, "minecraft:flint_and_steel" | "minecraft:fire_charge")
}
// PiglinAi.mobInteract: an adult admires what it loves.
Species::Piglin => {
!baby
&& self
.res
.items()
.item_tags
.get("minecraft:piglin_loved")
.is_some_and(|tag| tag.contains(item))
}
_ => false,
}
}
}
+38 -1
View File
@@ -489,12 +489,16 @@ impl ServerEntity {
// A value back at its default is left out of the state, but the
// clients still hold the old one: Entity's shared flags and pose go
// back to 0 (a player standing up again, a mob no longer on fire),
// a sleeper's bed to none.
// a sleeper's bed to none, an item frame's item to empty and its
// rotation to 0 (ItemFrame.DATA_ITEM and DATA_ROTATION).
let frame = e.kind.ends_with("item_frame");
for (index, kind, _) in &self.meta {
let default = match (*index, *kind) {
(0, serializer::BYTE) => Value::Byte(0),
(6, serializer::POSE) => Value::Id(0),
(14, serializer::OPTIONAL_BLOCK_POS) => Value::OptPos(None),
(9, serializer::ITEM_STACK) if frame => Value::Item(None),
(10, serializer::INT) if frame => Value::Int(0),
_ => match e.defaults.iter().find(|d| (d.0, d.1) == (*index, *kind)) {
Some(d) => d.2.clone(),
None => continue,
@@ -1106,6 +1110,39 @@ mod tests {
assert!(changes(&mut s, &e).is_empty());
}
#[test]
fn an_emptied_item_frame_sends_its_empty_item_and_rotation() {
let mut e = state(0.0);
e.kind = "minecraft:item_frame".into();
let mut s = added(&e);
changes(&mut s, &e);
e.meta = vec![
(
9,
serializer::ITEM_STACK,
Value::Item(Some(Stack::new(5, 1))),
),
(10, serializer::INT, Value::Int(3)),
];
changes(&mut s, &e);
// Punched out (ItemFrame.removeFramedMap, setItem(EMPTY)): both back to the default.
e.meta.clear();
let data = changes(&mut s, &e)
.into_iter()
.find_map(|p| match p {
play::Clientbound::SetEntityData(d) => Some(d),
_ => None,
})
.expect("the empty frame is sent");
assert_eq!(
data.values,
vec![
(9, serializer::ITEM_STACK, Value::Item(None)),
(10, serializer::INT, Value::Int(0)),
]
);
}
#[test]
fn a_value_back_at_a_listed_default_is_sent() {
let mut e = state(0.0);