Lightning Strike Spell

Planning
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()

Planning
Step 2
We will build this spell in three broad steps
Temp1498725951
  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
Temp1498725433

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
Temp1498726474

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
Information
Step 5
We used to have to make a recipe at this point so that we could get the wand in game
Temp1498726293

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
Temp1498726574
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")

Temp1498726669
Planning
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?

Temp1498762061

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

Planning
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?

Temp1498764275

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

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

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
Temp1498767981

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

Temp1498768014

It's just easier to read and type:
playerItem
rather than having to keep typing:
event.player.inventory.itemInMainHand
a bunch of times

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

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

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

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"
Temp1498767868
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

Temp1498768261
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

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

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

Temp1498770544
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.

Temp1498770909
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()

Temp1498769761

This function requires us to supply two input parameters:

Temp1498769767

The bukkit documentation describes here how to use these parameters:

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

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

Temp1498771748

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

Lightning Strike Spell Info

Account

MVCode Clubs

Created By

Newprofile jamie

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