Dungeon Items: Grappling Hook and Mirror Shield

Challenge
Step 1
Make a custom recipe for the Grappling Hook

The first thing we need to make our grappling hook is a custom recipe. You should probably know how to do this by now, but you can always review your Explode Bow and Nuclear Bow recipes to remember how it's done.

Make your recipe match the ingredients, display name, and lore in this image.

Temp1527619813
Information
Step 2
The Projectile Hit Event

To make the grappling action, we are going to used the Projectile Hit Event, just like we did with the Nuclear Bow. Read what it says in the Bukkit API:

Temp1527798649

Remember that whenever you see a method that starts with get, you can access it like a property in JavaScript.

So here, you can use the .getEntity() method with just .entity

Step 3
Use the entity method
Temp1527798826 Temp1527798761

We can use .entity to find out what kind of projectile was involved in the event. Use a bukkit.broadcastMessage() to get Minecraft to tell you the entity whenever a projectile hits.

Information
Step 4
The Projectile Class

You may have noticed that the Projectile Hit event does not have a method to tell us the person who shot it.

The Projectile Class does, however! You can read more about it here

Temp1528395698

Remember that we can use the .getShooter() method as just .shooter in ScriptCraft.

Whenever you see a method with set in the name, you can also change it like a normal JavaScript object property. So instead of .setShooter(), you could just use .shooter =

Step 5
Broadcast the shooter

Now we know how to find the projectile of the Projectile Hit event, and how to find the shooter of a projectile.

Use a broadcastMessage() to get the program to tell you what got shot and who shot it.

Temp1528395957 Temp1528395886
Step 6
Test if the player is holding the grappling hook

Now that we have tested that ScriptCraft is giving us the hook and the player doing the shooting, we can assign that data to variables.

Then, make an if statement that broadcasts a message if, during theprojectileHit event, the player-shooter was holding the grapplingHook.

Temp1528483514 Temp1528483329
Step 7

We don't want the grappling hook to always teleport you. Instead, we will make it only work if it hits a particular kind of block.

Broadcast the block that is being hit so we can learn more about it.

Temp1528483879 Temp1528396175
Step 8
Put some chiseled stone blocks in your dungeon

We're going to need to put some chiseled stone blocks around the dungeon to test our grappling hook.

Since the player will teleport on top of them, I added some windows and put the blocks in front of them. You can do this any way you like, as long as there is room above for the player to stand.

Temp1528482946
Check Your Work
Step 9
Target the different stone blocks
Temp1528482710

You should notice two things:

  1. All of the stone blocks have the same type: SMOOTH_BRICK
  2. Each of the different materials has a different data property, a number between 0 and 3

Now we know that just specifying a type will not be enough to limit the grappling hook.

Step 10
Figure out the data value for chiseled stone bricks

To make it easier to read, add an "if" statement to only broadcast if the material type ( target.type is "SMOOTH_BRICK" and make it tell us what that blocks data is, with target.data.

Temp1528487715 Temp1528487638
Step 11
Add the block data value to your "if" condition
Temp1528487886

Now that we know the data value on chiseled stone is 3, add this limitation into your "if" statement and change the broadcast message to reflect it.

Then, save, deploy, and reload to test out that you only get the message when you target chiseled stone blocks.

Step 12
Teleport yourself!

To teleport the player to where the fishing rod hit, we need to first make a variable for the block's location, called targetLoc. Then, use the teleport function to teleport the player there.

Save, deploy, reload and test out your grappling hook!

Temp1528488986
Step 13
Change the location

Right now, we have the problem that the player is teleporting into the wall. Instead, since we are 2 blocks tall, we actually want to go 2 blocks higher than the block we target, to end up on top of it.

Add 2 to the Y position of your location and test it again.

Temp1528489464
Step 14
Save some time
Temp1528489950

Not everything that uses a projectile is a player, like Snow Golems and Witches. Change your program so that it only assigns player to the shooter if they have the entity type player.

Using return tells the program that if it isn't a player doing the projectile shooting, we're done here.

Step 15
Make the grappling hook work when it takes damage

One problem with using .equals is that if an item takes damage, it will no longer work. To fix this, we're going to change the code just to check if the item metas match.

Temp1536261911
Challenge
Step 16
Keep the player from taking damage

Your challenge is to keep the player from getting hurt while using the grappling hook. We did this with the flying broomstick that you made after the Explode Bow and Nuclear Bow.

We're going to use the entityDamage event for this one as well, but with a different damageCause and a different player.itemInHand.

Temp1529183720

Check out this list of all the different damage causes in Minecraft. See if you can figure out which one to use here!

Temp1529183730
Planning
Step 17
The Mirror Shield

The next item we will be building is a Mirror Shield, that can reflect projectiles back when you block with it. We're going to do this by removing the projectile and shooting one of the same type.

So, to test this out, we're going to need a turret.

Challenge
Step 18
Create a custom crafting recipe for a Mirror Shield

Here you can see the ingredients, shape, display name, and lore for the Mirror Shield. Make this yourself.

Temp1535075507
Step 19
Build a redstone turret

Here is a schematic for building a redstone turret. It involves building a rapid repeater, and then adding a dispenser full of arrows.

You will need:

  • 2 blocks (any material)
  • a switch
  • 2 redstone torches
  • a redstone repeater
  • a dispenser
  • and a TON of arrows
Temp1534989284 Temp1534990419
Information
Step 20
Two different scenarios
Temp1536348063 Temp1536348467
Step 21
Change player to be the hit player, too
Temp1536348989
Challenge
Step 22
Check if the player is holding the mirror shield
Temp1536349188

If you want this to work when the player has the shield in the shield item slot, try using .itemInOffHand instead of .itemInHand

Step 23
Velocity Variables

Make variables for the projectile, it's velocity (projectileV) and the player's velocity(playerV), which in this case just means the direction that the player is looking.

Temp1536360745
Step 24
Vectors

We have used lots of different locations so far in this project, which we know require an X, Y, and Z coordinate. We can also get a direction represented by something called a vector.

You can see the methods that use vectors on the Bukkit API

We're going to add this code so that we can see the vector for the velocity of the arrows when they hit you.

Temp1536362137

Save, deploy, reload on your server, turn on the turret, and see what velocity the arrows have. Try getting closer and further away from the turret to see how it changes.

Step 25
Now, find the player's velocity

Make a variable for the player's direction, and broadcast it as well.

Temp1536362498
Step 26
The dot product

The dot product is some fancy physics math that tells us how close to perpendicular two vectors are.

Lucky for us, ScriptCraft can calculate the dot product for us! Add this code, and see how the dot product changes when you are facing different directions toward and away from the turret.

Temp1536776654
Challenge
Step 27
Find the dot product!

As you get hit by arrows, notice the different numbers that get broadcast

  • What kind of numbers do you get when you are directly facing the turret?
  • What kind of numbers do you get when your are at a 90 degree angle from the turret?
  • What kind of numbers do you get when your back is to the turret?
Temp1536363101

Figure out what number you need to add to this code block so that this message is only sent if you are facing the projectile source enough to block with the shield.

Step 28
Find the class of projectile

To make the mirroring effect, we will need to know the type of projectile that hits the shield. Add this code to save that information as a variable.

Temp1536778985
Check Your Work
Step 29
Test out the class

Save, deploy, and reload on your server. Turn on your turret, and see what it broadcasts when the arrows hit you. If this works, try it out with different types of projectiles.

Planning
Step 30
Remember Custom Projectiles?
Temp1536778803

Take a look at this code from the Custom Projectiles lesson.

To make the mirror shield reflect projectiles, we're going to remove the projectile that hits it, and launch a new one in the direction that the player is facing.

Challenge
Step 31
Launch that projectile!

Now that we know if the player is facing the projectile, if they are holding the mirror shield, and the class of projectile that hits, we need to:

  • remove the projectile
  • create a variable for the new projectile and use the launchProjectile() method
  • give it a velocity that is the same direction as the player

We do NOT need to:

  • make it incendiary or give it a yield property
Dungeon Items: Grappling Hook and Mirror Shield Info

Account

MVCode Clubs

Created By

Mandala2 slinkous

Course:

Temple Dungeon

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