Midas Touch Spell

Planning
Step 1
In this lesson we are going add a spell called "Midas Touch" that can turn anything into gold!

This lesson will make use of what you learned in these lessons:

Challenge
Step 2
CHALLENGE: Begin by adding a new wand: Midas Touch
Temp1499400622

You do not need a crafting recipe for this item.

Give yourself the wand by adding it to your setSpell() function and then calling it in the game.

Planning
Step 3
We can break this spell down into three parts

You will be challenged to make this in the next step

Temp1499401707

Important questions:

  • How do you check if an entity has been damaged by an entity?
  • How do you make an entity disappear?
  • How do you make a block appear at a location?
  • How will you reference the location of the entity?
Challenge
Step 4
CHALLENGE: Add code to your onEntityDamageByEntity function that allows you to transform entities into gold blocks
  1. Find your EntityDamageByEntity event handler.
  2. Add a new if-statement that checks if the damager is holding a midasWand
  3. Remove the entity
  4. Spawn a gold block at the correct location.
  5. Display a message saying the entity has turned to gold!
Temp1499400920
Check Your Work
Step 5
The result

If you have completed this challenge successfully you should be able to cast this spell on entities while holding a midasWand

In the next part of the lesson we will learn how to make it work on blocks also!

Planning
Step 6
Next we are going to use the PlayerInteract event handler to make the "Midas Touch" spell work on blocks!

We can't use EntityDamageByEntity because blocks aren't entities!

Step 7
Targeting the right block

Remember, only blocks that are in range of the player can be clicked on directly.

For this we can use event.clickedBlock:

Temp1499403298

Anything out of range we will have to use player.getTargetBlock():

Temp1499403306
Step 8
Just like for the lightningWand, we need to check if the player is holding the midasWand

in the playerInteract event handler:

Temp1499403605
Step 9
Now we need to create a reference variable named "target"
Temp1499403838

If the player left-clicks a block: we can use event.targetBlock.

Temp1499403819
Step 10
If the player clicks the air, we know they meant to click the out-of-range block they are looking at
Temp1499405460

So here, we will set target using .getTargetBlock():

Temp1499405403
Challenge
Step 11
Now turn the target block into a gold block!

You should be able to do this on your own now.

When you are done you should be able to turn blocks into gold from both touch range:

and from out of touch range

Midas Touch Spell 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