Lightning Strike Spell

Step 1
In this lesson we will make a Lightning Strike spell

We'll learn to use the playerInteract event- which you will use a lot when making your own plugins

Also we get to see how much quicker it is to make a new wand now that we have setSpell()

Step 2
We will build this spell in three broad steps
  1. Make a lightningWand just as we did with the other wands.
  2. Update setSpell() so it can give us a lightningWand.
  3. Use the playerInteract method to make the wand cause lightning to strike.
    • We'll go into this in better detail when we get to it
Step 3
Create the lightning wand out of a stick and create a reference to its itemMeta

Any time we say lightningWandMeta we could say lightningWand.itemMeta if we wanted.
This is called a reference variable and it helps make your code easier to read and write.

Step 4
Copy and paste the meta information and assignment from one of your other wands and edit it for the lightningWand

Here we're:

  • Changing the spell name in the Lore
  • Giving it a hidden enchantment (just so that it glows purple)
  • Updating the itemMeta of the wand with the changes we made here
Step 5
We used to have to make a recipe at this point so that we could get the wand in game

Not anymore! setSpell() can grant us the item instantly.

Step 6
Update setSpell() so that if you pass it "Lightning" it will equip the player with the lightningWand we just defined
Check Your Work
Step 7
Deploy and make sure you can give yourself a lightningWand using setSpell()

Remember you need to call:
/js setSpell("put the proper input here")

Step 8
Now we're ready to make the spell actually work

Before we can program anything we need to answer:
What exactly do we want this spell to do?


This is very important, so I'm going to draw out the plan in the next few steps...

Step 9
If the player clicks, lightning should strike the block they clicked on

This let's us answer the question: What event should we use?


The playerInteract event is called whenever the player clicks on a block- including air

Step 10
The player can only clicks blocks within a certain range

For this short range we can have lightning strike the Location of the Block that we clicked.

Notice here how we can't click on blocks that are too far away. We need another way to tell lightning to strike far away too:

Step 11
Let's start and we'll solve that problem in a bit. For now create an event handler for the 'playerInteract' event

This function is called an event handler.
It handles what to do when an event is called by the game.

Step 12
Before we do anything I want to make two reference variables

A reference for the player and for the item the player is holding


It's just easier to read and type:
rather than having to keep typing:
a bunch of times

Step 13
Make sure the player is actually holding the lightningWand
Step 14
Players can interact with objects by either left or right-clicking. Let's put a check tomake sure they left-clicked

event.action stores whether it was a left or right-click
action.leftClickBlock is a datatype Minecraft uses

Step 15
If the player left-clicks a block within the interact range, it gets stored in `event.clickedBlock`

We'll make a reference to this block so that we can use it later to tell our lightning where to strike.

Step 16
The block that's been clicked on will be 'referred to' as "target"
Step 17
Use world.strikeLightning() to strike a location

We can call the .location method on our block, "target",to tell the world where to strike lightning

Check Your Work
Step 18
Deploy and you should see you can strike any block that you can click...

Now we need away to strike blocks that are far away

Step 19
If we try to click something outside of our reach, it counts as us clicking on a block of air

Here my mouse is aimed at the far-away flower, but since it's out of range I'd actually be clicking on the block of air between me and the flower

Step 20

We've said how to find the target if the player clicks on a block, so now let's tell our plugin how to find the target if the player clicks air.

Step 21
The getTargetBlock() method finds the block that is lined up with the cursor

So if the player clicks on air, we can use .getTargetBlock()


This function requires us to supply two input parameters:


The bukkit documentation describes here how to use these parameters:

Step 22
If the player clicks air we'll get a target up to 100 units away
Check Your Work
Step 23
Your wand should now cast lightning far away as well as close up
Step 24

Understanding the concepts in this lesson means you can now refer to any block/location the user of your plugins clicks on


This can be very useful in many of your future plugins as well as future lessons!

Lightning Strike Spell 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