How to detect a held enchanted item using commands in Minecraft 1.18.2?



  • For context, I don't think the riptide enchantment gives a player enough speed or distance underwater. I mostly wanted to use it for fast water travel without a boat. I've found that giving a player dolphin's grace gives me the experience I'm looking for. Unfortunately, I've found that only giving the effect to players using the item is impossible, so I want to give the effect to any player holding a riptide 3 trident. I've written the following command to execute this:

    execute as @a[nbt={SelectedItem:{id:"minecraft:trident",tag:{Enchantments:[{id:riptide,lvl:3}]}}}] run effect give @s minecraft:dolphins_grace 1
    

    And I've put it in a repeating, unconditional, always active command block, yet I don't get the effect when I hold the trident. Additionally, I also want this to work when other enchantments are used alongside riptide 3, but I'm not sure the command above would work if e.g. mending and riptide 3 are added to a trident.

    Could anyone tell me why my command doesn't work, if this would work for additional enchantments, and if not, how to fix that?


  • QA Engineer

    Turns out one character made the difference:

    execute as @a[nbt={SelectedItem:{tag:{Enchantments:[{id:"minecraft:riptide",lvl:3s}]}}}] run effect give @s dolphins_grace 1
    

    Apparently, enchantment levels need to be indicated with the letter s behind them.

    Also, the command to works fine with additional enchantments, in a singleplayer world. On my PaperMC server, however, additional enchantments make the command fail to register. I'm looking for a way to get around this. Worst case, I have to add all possible combinations of enchantments including riptide.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2