The Wand

Planning
Step 1
In this lesson we are going to create a magic wand item that we can use to cast spells!
Temp1496083399
Step 2
We're going to start by creating a new item, called magic wand

First create a variable, this will need to contain all the item data about our wand

Temp1496083780
Step 3
Items need a lot of data in order to work, so it's easiest to duplicate and edit an already existing item
Temp1496084307

Here we start our wand out as a single stick.

Information
Step 4
We've taken object data from an existing item in the game, and copied it into a new object "magicWand"
Temp1498242084
Planning
Step 5
Our magicWand item currently has the exact same properties as a regular stick

The properties are stored in the itemMeta

Temp1496088506
Step 6
Create a new variable and set it equal to the magic wand's itemMeta

This will allow us to edit the itemMeta

Temp1496171989
Step 7
First let's change the display name
Temp1498243083

Here we are using . (dot notation) to access object properties.

You can think of this sequence as saying:
magicWand's itemMeta's displayName

Temp1496172073
Step 8
Set the display name to "Magic Wand"
Temp1496172138

The displayName property...
of the itemMeta property...
of the magicWand object

Step 9
Just for fun, let's make the display name text blue!

You can add one of these colors to any string.

The .aqua() method willturn our string aqua

Temp1496089558
Step 10
We can use the lore property to give our item a description

Lore is stored as an array where each element of the array is a new line of text.

Start by setting lore to an empty array:

Temp1496172206
Step 11
The first line of text will show the user the current wand spell
Temp1496172289

We'll have "Current Spell:" in blue and "None" in yellow

Step 12
Add a second element to the array to make a new line of text

Use a comma to separate the two elements

Temp1496172386
Step 13
Add the customized ItemMeta to the wand

Now that we've modified the magicWandMeta we need to set our magicWands itemMeta to magicWandMeta.

Temp1496182418
Check Your Work
Step 15
We've now set the displayName and lore of our magic wand

We aren't ready to deploy this plugin yet, so you won't be able to see this just yet.

Take a moment to see what our code does for us so far:

Temp1498244250
Planning
Step 16
Next we need a way to get a magic wand in game. For this we will create a new recipe

In the next few steps we will make a recipe for a magic wand consisting of diamond, obsidian, and a stick

Temp1496175416
Planning
Step 17
Before we make a recipe, let's make some variables to hole our ingredients
Temp1496175803

Technically this is not necessary, but it will make our code easier to read and to write

Step 18
We're going to use the "add" method of the Recipes module
Temp1496176433

We type the keyword recipes to pull up the Recipes module.
Then we type .add() to call the add method

Step 19
Recipes are stored as a special type of data called an "Object"

Add a pair of curly braces to begin writing an Object

Temp1496176756

Press enter to add a newline inside the object:

This is for readability.

Planning
Step 20
Recipe Objects have three properties: ingredients, shape, and a result

This is a visual model of what our data needs to define:

Temp1496179064

In the next step we'll write these one at a time.

Step 21
First add the ingredients property

Here we are assigning a symbol for each ingredient:

D -> diamond
O -> obsidian
S -> stick

Temp1496179471

The ingredients list is also an object

Step 22
Add a comma and start the next property "shape" on a newline
Temp1496179582
Step 23
The shape is an array and it looks like this
Temp1498244467

We can use the letter keys from the ingredients object in the shape array

Information
Step 24
How the shape array works

Do not type this in, this is an illustration for you to know when you start making your own recipes:

Temp1496182977

Each element of the array corresponds to a row in the crafting table

Information
Step 25
You may want to write you array like this if it helps you

OPTIONAL

Temp1498244777

this way it looks more like the crafting table:

Temp1498244887
Step 26
The last property of the recipe is the result

We want our recipe to result in a magicWand

Add a property named result whose value is magicWand.

Temp1496182214
Check Your Work
Step 27
Deploy your plugin and make sure the recipe you added can be used to craft the Magic Wand
Temp1477346862
Planning
Step 28
Let's make the wand look a bit more interesting
Temp1496183149

Right now the magic want just looks like a regular stick. Let's make it a bit more magical by adding a hidden enchantment that will make it glow!

Step 29
Since you normally cannot enchant sticks in Minecraft, any enchantment you add will be considered an "unsafeEnchantment"

This code must go after setting the magicWand's item meta

Temp1496184922

We'll use the .addUnsafeEnchantment() method to add an enchantment to the stick.

Step 30
As part of the itemMeta we add to our `magicWand`, we'll add an itemFlag which makes enchantments invisible
Temp1496185039

We'll use .addItemFlags() to add the itemFlag.hideEnchants flag to our magic wand's itemMeta.

Check Your Work
Step 31
Make sure everything is working properly

You should be able to craft the magic wand complete with custom display name and lore.

The wand should appear enchanted, but without showing any enchantment text on the item (e.g. Unbreaking II)

Try one of these lessons next:

The Wand Info

Account

MVCode Clubs

Created By

Mvcc logo new aaron

Course:

Wand World

Access Level

premium

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