Vanishing Spell

Planning
Step 1
In this lesson we will add a vanishing spell that can be cast when holding a specific magic wand

When the player hits an entity with the wand, the entity will disappear:

We can achieve this with three steps:

  1. Create a Wand of Vanishing
  2. Check if the player hits an entity with the wand
  3. Make the entity "vanish".
Challenge
Step 2
CHALLENGE: Create a new wand item that will be used to cast the "Vanishing" spell
  1. Create a new custom item magicWand_Vanishing
  2. Give magicWand_Vanishing a display name, lore, and hidden enchantment.
  3. Add a crafting recipe that can be used to create a magicWand_Vanishing using a magicWand and two other ingredients (shown here using a golden carrot and fermented spider eye)
Temp1477522566

Hints:

  • Here is a list of every item in Minecraft: Spigot Materials

  • You'll see the golden carrot entry written as GOLDEN_CARROT, however we don't use underscores when accessing an item, we use camelCase: items.goldenCarrot(1)

Check Your Work
Step 3
Deploy your plug-in and make sure you can craft the wand of vanishing
Temp1496386927

For this next part we need to first learn about Scriptcraft's Event System...

Information
Step 4
Whenever something important happens in-game, Minecraft records it as an `event`

Every in-game action activates an event.

Example events include:

Temp1496346895

You can see a full list of event names here

Planning
Step 5
Checking if the player hits an entity with the wand
Vanishing wand

We can use the EntityDamageByEntity event to check if:

  • an entity has been damaged by a damager
  • the damager was the player
  • the player is holding the Wand of Vanishing
Step 6
We can use an Event Handler to detect if the player hit an entity with the wand
Temp1496187610

At the bottom of your program, add a call to the 'entityDamageByEntity' event

Step 7
Here we assign an empty function to the event handler

This Event Handler will call this function when it is activated

Temp1496390615
Step 8
For now we'll test our function by having it print a message to the screen.
Temp1496389570
Step 9
Deploy, and reload
Temp1496388015 Temp1496396111
Check Your Work
Step 10
Test your event handling code by hitting an entity with your wand

Make sure you see the message "EntityDamageByEntity Event triggered" whenever you deal damage to another entity.

Planning
Step 11
Within this event, we can check if the damage was caused by the player

To do this we will look at the eventInfo parameter of our function

Temp1496390858

This parameter automatically records information about the event such as:

  • eventInfo.damager : damage dealer
  • eventInfor.entity : damage taker
Step 12
Write an if statement to check if the damager was the player
Temp1496391402
Step 13
Deploy, and reload

Remember to do this every time you want to test your updated code. From now on it will be up to you to do this on your own!

Temp1496396090 Temp1496396145
Check Your Work
Step 14
You should now only see the message printed when you deal damage to an entity yourself.

Make sure a burning skeleton no longer broadcasts the message!

Planning
Step 15
Now we're going to check if the player has a wand of vanishing in their hand

First make a local variable which stores the item in the player's hand

Temp1496392042 Temp1496392343
Step 16
Next check if the player's item is the wand of vanishing

We need to use a special method .equals to check if playerItem is equal to wandOfVanishing

Temp1496393484

You often won't be able to use === to compare really complicated variables like minecraft items!

Check Your Work
Step 17
Make sure the message only appears when you damage an entity while holding the vanishing wand

You should only see the message printed when you hit an entity holding a magicWand_Vanishing in your main hand.

Planning
Step 18
It's finally time to make the entity vanish!

We'll do this in three steps:

  1. Make a variable to store the damagee: event.entity.
  2. Use the .remove() method to make the damagee disappear.
  3. Broadcast back a message that announces the spell worked.
Step 19
Make a variable to store the damagee: event.entity

We'll called the variable enemy and set it to event.damagee.

Temp1496394376
Step 20
Instead of broadcasting a message, make the entity disappear when it is hit by the wand

You can call .remove() on any entity to remove it from the game.

Temp1496394026
Step 21
After we remove the entity, send the `player` a message telling them what they have done

We'll give this message some color and have it tell the player what entity they made vanish

Temp1496394201
"The ".green() + 
enemy.type.toString().yellow() + 
" has vanished!".green()
Check Your Work
Step 22
Test your vanishing spell

Make sure you can make entities vanish from the world with a single click!

Now you may notice that they turn red as they get damaged right before vanishing, we will fix that next.

Step 23
Cancel the damage event when we are using the vanishing spell

We can prevent it from taking damage from the player's wand by cancelling the damage event using event.cancelled = true;

Temp1496394599

Then we are finished with the spell effect, so we should add a return statement to end the function's execution.

Check Your Work
Step 24
Make sure your vanishing spell no longer deals damage

The entity you cast the vanishing spell on should no longer turn red from taking damage before disappearing!

Planning
Step 25
No spell is complete without a particle effect!

Let's make our spell look cooler by adding a particle effect on the location where the entity was when it vanished.

We can use world.spawnParticle() but this function needs 3 parameters we have to define first:

Temp1482527454
Step 26
At the top of your program, create a variable to store the world data

We'll create a variable called world and set it to your server's world

Temp1496394765

We'll make this a global variable and define it at the top of the plugin

Step 27
Back in the entityDamageByEntity event, let's choose a particle effect

Create a variable called vanish particle and set it to one of Minecraft's particle effects

Temp1496395739

I chose the spell particle, a complete list of particles can be found here

Step 28
Create a variable to store the location of the enemy
Temp1496395853

We'll want the particles to spawn at the enemy's location after it vanishes

Step 29
Create a variable and set it to the number of particles you want
Temp1496395906

I think 7 looks nice

Step 30
Now, right after removing the enemy, call the world's `.spawnParticle()` method

The particle is vanishParticle
The location isenemyLoc
The count is particleNumber

Temp1496396059
Check Your Work
Step 31
Test your completed vanishing spell!

If you used the example particle effect you should see an effect like this:

Try one of these lessons next:

Vanishing 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