How would I go about making a custom minecraft item that triggers events when it's used?

Solution 1:

This method works by handing over the sword to an invisible armor stand to detect the right-click (block). I haven't tested it but it should work. If it doesn't please tell me. This won't work when using the sword to fight as you will hit the armor stand.

First run the following commands once to create the scoreboard objectives used:

  1. /scoreboard objectives add holdSword dummy
  2. /scoreboard objectives add rightClick dummy
  3. /scoreboard objectives add summoned dummy

Create a fill clock that runs the following commands in the correct order (from lowest to highest coordinate):

  1. /scoreboard players add @a summoned 0
  2. /scoreboard players set @a holdSword 0
  3. /scoreboard players set @a holdSword 1 {SelectedItem:{id:"minecraft:iron_sword",tag:{display:{Name:"Longdark Blade",Lore:"[Long ago, it was thought that metals","could control the stars themselves...]"}}}}
  4. /scoreboard players set @e[type=ArmorStand] rightClick 0
  5. /scoreboard players set @e[type=ArmorStand] rightClick 1 {Equipment:[{id:"minecraft:iron_sword",tag:{display:{Name:"Longdark Blade",Lore:"[Long ago, it was thought that metals","could control the stars themselves...]"}}}]}
  6. /execute @e[type=ArmorStand,score_rightClick_min=1] ~ ~ ~ /time add 4800
  7. /execute @e[score_holdSword_min=1,score_summoned=0] ~ ~ ~ /summon ArmorStand ~ ~ ~ {CustomName:rightclick,Invisible:1,Invulnerable:1,NoBasePlate:1,NoGravity:1,ShowArms:1}
  8. /scoreboard players set @a[score_holdSword_min=1,score_summoned=0] summoned 1
  9. /execute @a[score_holdSword_min=1,score_summoned_min=1] ~ ~ ~ /tp @e[type=ArmorStand,name=rightclick,r=1] @p
  10. /execute @a[score_holdSword=0,score_summoned_min=1] ~ ~ ~ /kill @e[type=ArmorStand,name=rightclick,r=1]
  11. /give @a[score_holdSword=0,score_summoned_min=1] iron_sword 1 0 {display:{Name:"Longdark Blade",Lore:"[Long ago, it was thought that metals","could control the stars themselves...]"}}
  12. /scoreboard players set @a[score_holdSword=0,score_summoned_min=1] summoned 0

Explanation

The first command block is just to ensure that the score summoned can be used for all players. The second command will reset all players' scores of holdSword, while the third command will set the score back to 1 when holding the sword in their hand. When the sword is held in the player's hand an armour stand will summon at them (seventh command) which will be used to check for right click. It will check for right click by checking if the armour stand is holding the specific sword (fourth command reset score and fifth command set score if holding item), because when you right click an armour stand with an item, it gives the item to the armour stand. When the right click is detected and the armour stand is holding the item in his hand, the time will be advanced by 4800 (sixth command). The ninth command will teleport the armour stand to the player 20 times a second, which will ensure it will detect the right click. The tenth command will kill the armour stand if the item is not held anymore. The eleventh command will reset the score of the summoned armour stand so that the whole process can happen again.