4

Is there a way so that I make make it when I left click/right click with a certain item it applies an effect? I know that you can apply attributes to wearing armor and things but I want it so when you use the item it applies an effect on your player.

For example, let's say I want something so that when I right click with a certain item (let's say an iron ingot that was given to me) I'll then have a jump boost for 10 seconds. So kinda like a potion in a way.

Is this possible with command blocks/commands/any mods?

  • I don't fully understand what you are trying to achieve sorry. Do you mean like in mods where when you right click like a sword in your hand then it shoots a fireball or something. – Skye Jul 4 '15 at 14:23
  • @Skye I'll change my question to give an example – Chickenator Jul 4 '15 at 14:23
  • With mods/plugins it's certainly possible, but in vanilla Minecraft you don't get an event called if the item doesn't do anything on its own. – GiantTree Jul 4 '15 at 14:25
  • @GiantTree I've seen it around in servers and thought it would be fun to do. I'm sure it exists, just not sure how to make it happen – Chickenator Jul 4 '15 at 14:27
  • 1
    Those servers usually use plugins. Your client always sends an event to the server whenever you punch with them or use them. That's how it works. – GiantTree Jul 4 '15 at 14:30
2

There is a way to do this in vanilla using commands. I got the general idea from betathedata on reddit, but the commands below are my own implementation of the technique.

This method relies on the fact, that a villager without trade offers has no trade interface, but right-clicking it will still count as "talking" to him. Therefore, having an invisible villager at your location while holding a specific item will do the trick.

Scoreboard objectives

For this to work, we need three objectives set up.

scoreboard objectives add holdingItem dummy
scoreboard objectives add holdTime stat.playOneMinute
scoreboard objectives add rightClick stat.talkedToVillager

The first two are for tracking when a player has just selected a specific item, the third one is what we want to unobtrusively set to 1.

Clock commands

First, set up a fill clock large enough to run at least 8 commands. Start off with

scoreboard players set @a holdingItem 0
scoreboard players set @a holdingItem 1 {SelectedItem:{id:minecraft:iron_ingot}}
scoreboard players reset @a[score_holdingItem=0] holdTime

Replace the SelectedItem tag with whatever you like. You can add multiple of this second command if you want it to work for multiple items.

execute @a[score_holdTime=1] ~ ~ ~ summon Villager ~ ~ ~ {CustomName:"RightClicky",Offers:{Recipes:[]},NoAI:1,Silent:1,ActiveEffects:[{Id:14,Duration:9999,ShowParticles:0b},{Id:11,Amplifier:4,Duration:9999,ShowParticles:0b}]}
execute @a[score_holdTime_min=2] ~ ~ ~ tp @e[type=Villager,name=RightClicky,r=2,c=1] @a[c=1]
execute @a[score_holdingItem=0] ~ ~ ~ tp @e[type=Villager,name=RightClicky,r=2,c=1] ~ -100 ~

The first will summon a villager called RightClicky, who does not move, does not speak and has Invisibility and Resistance V (=Invulnerability), when you swap to the item(s) you specified above.

The second and third commands will either teleport that Villager to you, or into the void (killing him without particle effects showing), depending on whether you are still holding your item or not. This teleporting causes the villager to lag behind you a little, so it does not always work when you are running.

After putting in these commands, your RightClick score will go up every time you right click while holding your item. At this point, place any commands that are supposed to be triggered by the right-click. Use @a[score_RightClick_min=1] for targetting. For example:

effect @a[score_RightClick_min=1] minecraft:jump_boost 10 5

At the very end of your clock, run

scoreboard players set @a RightClick 0

to reset the system to detect another right-click.


Detecting a Left-Click should also possible. After summoning the villager, you can detect if a player hits it, as detailed in this question. Putting the two together is left as an exercise to the reader.

  • I decided to move my answer from this question over to here, because I like this one more, and because I highly doubt the other OP will ever see the answer. The other question should be closed as duplicate as soon as possible (i.e. when this answer here has an upvote). – MrLemon Aug 14 '15 at 8:04
  • I can't check if this works right now but I will when I get back from holiday. I'll assume that it's right and if it's not I'll remove it as a correct answer. Thanks for the full answer! I hope to make some cool things with this! – Chickenator Aug 14 '15 at 18:02
  • For the summon villager command I get the error [11:05:19] Failed to execute 'tp @e[type=Villager,name=RightClicky,r=2,c=1] @p' as Chickenator__ and the other execute commands say that its an unknown command – Chickenator Aug 16 '15 at 10:06
0

There is a scoreboard criteria that tests if a player right clicks and a /testfor data tag for holding an item. If you attach an AND gate to test if a player is holding an iron ingot and right clicking, it should be possible.

  • Can you tell me the testfor and scoreboard command? I can't seem to find it. – Chickenator Jul 31 '15 at 21:02
-3

I'm not sure to understand but if you're on a server, you can use the plugin called "Skript" it is really useful and you can do a lot of stuff with it!

  • U NO ADVERTISE :D – SamsungNote7Plus Jul 5 '15 at 0:52
  • Great answer, if only that were a viable option for singleplayer maps or maybe gave some description to "a lot of stuff" :P – caucow Jul 5 '15 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.