Actions

Using carrot on a sticks as triggers

From Zedwiki

Carrot on a sticks are often used as activators for tools in maps. The reason for this is that a scoreboard objective with the type stat.useItem.carrot_on_a_stick will increment whenever the item is right clicked, regardless of any kind of success condition as is common with other items. Blocks for example need to be placed, and food needs to be eaten. But the carrot on a stick becomes a right click detector with this objective set up, making it extremely useful.

This tutorial will cover the construction of a simplified version of the class ability system from Dungeon Dash, which is far too unweildy to use as a useful tutorial. It will also feature the ability to have seperate carrot on a sticks that do different things depending on thier lore, creating vast numbers of possible uses for them in a single map. The dungeon dash class system is unfortunatly outdated, and the 1.12 addition of @s would help simplify some of the commands. Other aspects of it will likely be covered in future tutorials.

Commands

This tutorial uses 5 commands.

  • scoreboard (tagging, creating objectives, setting scores, testing scores)
  • execute
  • say
  • give
  • entitydata

The Tutorial

Creating The Objective

Firstly we want to setup our objective to track players right clicking a carrot on a stick. There should only be one of these on the server, more are completly unnecessary.

/scoreboard objectives add UseStick stat.useItem.minecraft.carrot_on_a_stick


Set this objective to be displayed on the sidebar to see it working when you right click your carrot on a stick, then clear the sidebar.

/scoreboard objectives setdisplay sidebar UseStick to display our new objective.

/scoreboard objectives setdisplay sidebar to clear the sidebar.

Detecting Players Who Used The Stick

For our example system we are going to have two class abilities which depend on tags the players have, and a magic wand which does not. The carrot on a sticks for the class abilities are the same, but the magic wand is different. To do this we need 3 command blocks. The following commands should be in a chain together, with a repeat command block at the front.

/execute @a[score_UseStick_min=1,tag=!AbilityCooldown] ~ ~ ~ /scoreboard players tag @s[tag=MageClass] add MageAbility {SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{display:{Lore:["Right click to use your class ability."]}}}}


/execute @a[score_UseStick_min=1,tag=!AbilityCooldown] ~ ~ ~ /scoreboard players tag @s[tag=TankClass] add TankAbility {SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{display:{Lore:["Right click to use your class ability."]}}}}


/scoreboard players tag @a[score_UseStick_min=1] add MagicWand {SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{display:{Lore:["This is a magic wand!"]}}}}


The first command adds the MageAbility tag to players with the tag MageClass, a score in UseStick of 1 or more, and without the tag AbilityCooldown. The execute command is used so we can test for multiple tags at once, as selectors will not do this on thier own. For example, @e[tag=Apples,tag=Bananas] is equivilent to @e[tag=Bananas] as the later parts override the previos ones rather than combining with them in any useful way. At the end of the first command there is a data tag.


{SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{display:{Lore:["Right click to use your class ability."]}}}}


This is used to check the nbt data of the target of the scoreboard command, so that only players who match with get the tag. This data tag checks that thier selected item (main hand item) is a carrot on a stick with lore "Right click to use your class ability". This enables us to check for different lore on a different item to give another effect.

The second command is the same as the first command with Mage swapped for Tank, the second class in this example.

The third command, which adds the MagicWand tag to players holding a carrot on a stick with the lore "This is a magic wand!", does not depend on the tag AbilityCooldown or the players class tag and so can be used at any time, unlike the other two which will be on a cooldown.

With those 3 commands we can now tag players who right clicked thier carrot on a stick, and detect which kind of stick they used and what class tag they have. Now, with a number of players tagged to identify which thing should be done to them, the effects of the abilities can be made.

Reset the objective

Before we do anything else it is important to set the objective UseStick for all players back to zero, as otherwise thier stick will only work once which would be a bit rubbish. We do this using the following command.


/scoreboard players set @a[score_UseStick_min=1] UseStick 0


Add Ability Cooldown Objective

Create a dummy objective called AbilityCooldown to make a cooldown for the class abilities.

/scoreboard objectives add AbilityCooldown dummy


Mages Ability

The MageAbility tag marks players using thier class ability as a mage. In Dungeon Dash this summons an iron golem, as well as making lots of particles and sound effects. In this example we will just summon an iron golem, as the particles really are not that important. We will also announce use of thier ability in chat using say. This is done by executing on players with the MageAbility tag. If the say command is executed on a player it will use thier name in chat, followed by the message. Once thier ability is activated, a cooldown time is set and the MageAbility tag removed. Make sure to remove the MageAbility tag or you will constantly summon iron golems on anyone who uses the stick in a situation that is both annoying and difficult to fix.

/execute @a[tag=MageAbility] ~ ~ ~ say I summoned a Rusty!


/execute @a[tag=MageAbility] ~ ~ ~ /summon minecraft:villager_golem ~ ~ ~ {CustomName:Rusty}


/scoreboard players set @a[tag=MageAbility] AbilityCooldown 400


/scoreboard players tag @a[tag=MageAbility] remove MageAbility


Tanks Ability

Tanks ability is to damage nearby entities. As with the mages ability this gives lots of particle effects and sounds in Dungeon Dash but that is not helpful for this tutorial.

/execute @a[tag=TankAbility] ~ ~ ~ say Armor Burst Activated!


/execute @a[tag=TankAbility] ~ ~ ~ /effect @e[r=4,type=!player] minecraft:instant_damage 10 1


/scoreboard players set @a[tag=TankAbility] AbilityCooldown 600


/scoreboard players tag @a[tag=TankAbility] remove TankAbility


Magic Wand

Seperate from the class abilities, the magic wand can be used by any class at any time. It will make nearby sheep turn pink and has no cooldown.

/execute @a[tag=MagicWand] ~ ~ ~ say Woooshhh!


/execute @a[tag=MagicWand] ~ ~ ~ /entitydata @e[r=10,type=sheep] {Color:6b}


/scoreboard players tag @a[tag=MagicWand] remove MagicWand


Ability Cooldown

Remember the commands setting the ability cooldown score in the class ability sections? We now need to finish off the chain by managing these so that the cooldown works correctly. First, add the AbilityCooldown tag to players with an AbilityCooldown score of 1 or more.

/scoreboard players tag @a[score_AbilityCooldown_min=1] add AbilityCooldown


Second, reduce the score in AbilityCooldown all players with AbilityCooldown score of 1 or more.

/scoreboard players remove @a[score_AbilityCooldown_min=1] AbilityCooldown 1


Thirdly, remove the AbilityCooldown tag from players with an AbilityCooldown score of 0.

/scoreboard players tag @a[score_AbilityCooldown=0] remove AbilityCooldown


Putting It All Together

Apart from the commands to create the objectives all the above commands should be in one chain, in the order they appear in the page from top to bottom. This can be on constantly so the sticks work all the time. The different stick items can be obtained using the following commands.

/give @p minecraft:carrot_on_a_stick 1 0 {display:{Lore:["Right click to use your class ability."]}}


/give @p minecraft:carrot_on_a_stick 1 0 {display:{Lore:["This is a magic wand!"]}}