Minecraft Java Edition Commands – How to Make an Item in the Off-Hand Give a Buff

minecraft-commandsminecraft-java-edition

I'm wondering how to make an item give a potion buff or generic boost when held ONLY in the off-hand.

Thanks!

Best Answer

Easiest way to accomplish a "generic boost" is through attributes, which can be set to only apply on a specific slot. This is a generator that allows adding attributes to items for specific slots. Here's an example command that gives you 10 extra health (5 hearts) when held only in the offhand:

/give @p minecraft:diamond_sword 1 0 {AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Slot:"offhand",Amount:10,Operation:0,UUIDMost:91328,UUIDLeast:135212}]}

There's this wiki page if you want to learn more about all the things attributes can do, and what do different operations are for.

Though there's a few things attributes do better than potions effects, they're a bit limited and may not be able to do what you want.


Alternatively, you could do this by detecting when the player is holding the item in their offhand slot (-106)

/scoreboard players tag @a[tag=OffhandHoldingItem] remove OffhandHoldingItem
/scoreboard players tag @a add OffhandHoldingItem {Inventory:[{Slot:-106b,id:"minecraft:diamond_sword",tag:{display:{Name:"Example Sword"}}}]}

You can then do anything you want to the player, such as:

/effect @a[tag=OffhandHoldingItem] jump_boost 1