Skip to content

fix: place villagers from off-hand when the main hand is occupied (#109) - #110

Open
mattlawliet wants to merge 1 commit into
Clickism:masterfrom
mattlawliet:fix/offhand-place-with-occupied-mainhand
Open

mattlawliet wants to merge 1 commit into
Clickism:masterfrom
mattlawliet:fix/offhand-place-with-occupied-mainhand

Conversation

@mattlawliet

Copy link
Copy Markdown

Fixes #109.

The bug

Pick up a villager, move the head to your off-hand, hold anything non-placeable in your main hand (a totem, a sword), right-click the ground: the head is placed as a block and the villager is gone.

Cause

PlaceVillagerListener.getUsedHand() picked the hand by which one is non-empty, not which one holds a villager:

if (!player.getMainHandItem().isEmpty()) return InteractionHand.MAIN_HAND;

With a totem in the main hand that returns MAIN_HAND, so:

  • the OFF_HAND event (holding the head) fails hand.equals(usedHand) and passes;
  • the MAIN_HAND event (holding the totem) fails isVillager and passes.

The mod never runs and vanilla places the head block. Introduced in 5c46fc0, which fixed #87 only for the case where the main hand is empty.

Fix

Drop the hand gate. isVillager(player.getItemInHand(hand)) already selects the correct hand, and vanilla runs the main hand first and only falls through to the off-hand when the main hand consumed nothing — so main-hand behaviour is untouched.

main / off before after
head / — places places
— / head places (#87) places
totem / head head placed as a block places
block / head vanilla places block vanilla places block
flint&steel / head vanilla lights fire vanilla lights fire
head / head one villager one villager

Second change in the same method: the stack was shrunk, then the selected hotbar slot was cleared. Once off-hand placement works, that deletes whatever is in the main hand — the totem in the repro above. Now it clears the hand that was actually used.

Testing

Fabric server, MC 26.2, loader 0.19.5, fabric-api 0.160.0, ClickVillagers 1.6.7. Reproduced with a build of master, then verified against a build of this branch. Manual matrix, survival mode, all passing:

  1. head main hand, off-hand empty -> spawns
  2. head off-hand, main hand empty -> spawns (Getting villager head #87 not regressed)
  3. totem main hand, head off-hand -> spawns, totem survives (this issue)
  4. cobblestone main hand, head off-hand -> block places, no villager, head kept
  5. flint and steel main hand, head off-hand -> fire lights, no villager, head kept
  6. head main hand, right-click a chest -> chest opens, no villager
  7. head main hand, shift-right-click a boat -> villager rides the boat
  8. trade, pick up, place, pick up again -> profession and trades intact

PlaceVillagerInVehicleListener gates MAIN_HAND explicitly and is untouched — placing into a boat from the off-hand is still unsupported, same as before.

Builds clean on every stonecutter target: 1.21.1-fabric, 1.21.1-neoforge, 1.21.11-fabric, 1.21.11-neoforge, 26.1-fabric, 26.2-fabric. NeoForge shares this listener via NeoforgeEventListener, so it gets the fix too. The Paper module is unaffected.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FGCvEm7XDj8rS2YefvQPVT

getUsedHand() picked the hand by "which one is non-empty" rather than
"which one holds a villager". With any non-empty main hand (a totem, a
sword, anything not placeable), the off-hand event was skipped and the
main-hand event failed the isVillager check, so the mod never ran and
vanilla placed the villager head as a block instead. Closes Clickism#109.

The isVillager check on getItemInHand(hand) already selects the correct
hand, so the extra gate is dropped entirely. Vanilla runs the main hand
first and only falls through to the off-hand when the main hand did
nothing, so main-hand behaviour is unchanged.

Also shrink the stack in the hand that was actually used instead of
clearing the selected hotbar slot, which deleted the main-hand item when
placing from the off-hand.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FGCvEm7XDj8rS2YefvQPVT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Placing villagers with the offhand whilst the main hand is occupied breaks the villager Getting villager head

2 participants