Step 1
In this lesson we are going to make a function which places any wand in our hand

Notice in this video I don't have to craft the wand.

This will make working with spells much cleaner and quicker.

Step 2
We will achieve this with three steps

You have seen how to do everything in this lesson before. This will be good review

  1. Create an export function named setSpell()
  2. Give it a parameter which takes a spell name as a string
  3. Use if-statements to set the player's inventory's itemInMainHand to the correct wand
Step 3
Create an export function named 'setSpell()'

I like to place my export functions at the bottom of my code.

Step 4
Give our function a parameter which takes in a string

Now if we say:


spellName will be equal to "Vanishing"

But if we say:


spellName will be equal to "Chicken".

Step 5
Next we need to set the player's itemInMainHand

How can we get to the itemInMainHand property?

Here is a "map"

  • We can get the player with self
  • Then we can get self's inventory with self.inventory
  • Lastly we can get self's inventory's item in main hand with self.inventory.itemInMainHand

We'll be using this in the next step

Step 6
Inside our setSpell function we can check the value of the parameter "spellName"

If spellName is equal to "None" the player should get a magicWand in their hand

Step 7
Practice by finishing this code for the Vanishing wand
Step 8
Add to your setSpell function so that it also allows you to switch to the ChickenWand

Typing this command in Minecraft should place the chickenWand into your hand

SetSpell Info


MVCode Clubs

Created By

Newprofile jamie

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach