The Haunted Library Part 3: The Secret Room

Planning
Step 1
There's more to this library than meets the eye!

In this lesson, we will build a hidden basement under the library, and the secret passage to get there.

We will also learn how to use spawnEntity() to add things like mobs, paintings, and item frames.

Temp1557438702
Planning
Step 2
Planning the secret room

The diagram below shows where the secret room will be under the library. The secret passage is built into a bookshelf. The ceiling of the secret room will be three blocks below the floor of the library.

Temp1554480495
Step 3
Put the secret passage in the library object

Add a property called secretPassage to the library object, and assign it to an empty object, {}.

Temp1554480516
Step 4
Make functions to open the secret passage

First, define a method for library.secretPassage called open.

You will need to figure out which bookshelf in your library.bookshelves.data array is the one that you want to use.

In this case, it was the second bookshelf in the list, so we use the index 1.

Then, make an export function that calls your library.secretPassage.open() method. We will use this function to test several different methods in this lesson.

Temp1554480545
Check Your Work
Step 5
Test the library.secretPassage.open() method

Save, deploy, reload, and call /js secretPassage(). You should see an opening appear in the bookshelf.

Temp1554480674
Challenge
Step 6
Challenge: Finish the secret passage

See if you can write the rest of the drone code to open a secret passage down by yourself, using a for loop.

Remember, with each step you need to:
1. Place a box of air blocks, 3 blocks tall.
2. Move the drone forward 1 block.
3. Move the drone down 1 block.

You will need to repeat this for a total of 6 times.

Temp1554480692

Hint: If you get stuck, the answer is in the step below

Step 7
Make a method to close the secret passage

We're going to close the secret passage by rebuilding the bookshelf on top of it.

Once you have made this method, call it in your secretPassage() exports function, and comment the library.secretPassage.open() method.

Temp1554480745
Check Your Work
Step 8
Close the secret passage.

Save, deploy, reload, and call /js secretPassage(). The secret passage should be completely hidden.

Temp1554480762
Step 9
Find a location for the hidden lever

Find a good location for your hidden lever to appear. You will probably want it to be just two blocks above the ground, so that players can reach it, but having to climb up a ladder to get to the lever would be cool!

Temp1554584062
Step 10
Save the lever location

Add a lever property to the library.secretPassage object. Yaw doesn't matter very much here, so you can use 0 for the yaw and the pitch.

Temp1554584506
Step 11
Make a method to show the lever

Make a new method for library.secretPassage.lever called show. It will just place one lever block.

Then, call this method in secretPassage, and comment the others.

Temp1554584785
Check Your Work
Step 12
Test out the lever!
Temp1554584698

Save, deploy, reload, and call /js secretPassage(). A lever should appear in the middle of your bookcase.

Challenge
Step 13
Challenge: Make a method to hide the lever

You should make a called hide for library.secretPassage.lever. You can copy your library.secretPassage.lever.show method almost exactly, but change the block material to a bookshelf.

Then, call this method in the secretPassage export function, and comment the others.

Temp1554584914

Be sure to test your code by saving, deploying, reloading, and calling /js secretPassage()!

Step 14
Add a property to tell if the secret passage is open

Add a property called open to library.secretPassage. It starts out as false, because the secret passage should be closed when we start.

Temp1554586254
Step 15
Mark when the passage opens and closes.

When we open the passage, we want to mark that opened is true, and mark that it is false again when we close the passage. Add these lines to change the variable.

Switching a variable back and forth from true to false is called toggling.

Temp1554586439
Step 16
Add a sound effect

To make sure people will notice that the passage is opening, add a sound effect.

Temp1557434630
Step 17
Detect when a player clicks the lever

The playerInteract event fires any time a player clicks anything (or stands on a pressure plate). Add this event function with a function as an input (function-inception, I know!) to send a message when a player clicks this lever.

Temp1554586741
Check Your Work
Step 18
Test the lever

Save, deploy, reload and click your lever. You should see the broadcast message you wrote in the previous step.

Then, place a lever somewhere else, and try using it, to make sure you do NOT get the message.

Also try clicking some other blocks in the library, to make sure that only this lever gives you the message.

Temp1554586639
Step 19
Make the lever open the secret passage
Temp1554586811

Add this code to make the lever open the secret passage and close it.

Check Your Work
Step 20
Open the secret passage!

Save, deploy, reload, and pull the secret lever.

It should first open the secret passage, and then close it again!

Planning
Step 21
Planning out the secret basement room

We are going to start the corner of the room at the bottom of this passage. To find the location, head down the passage, and turn to your right. Look at the bottom block and record the location.

Yaw is important here to make your room build in the correct direction!

Temp1554480803 Temp1554480815
Step 22
Save the secret room location
Temp1554480857

Add a property called secretRoom to your library object, and make it an object with a property called loc that is assigned to the location we just found.

Planning
Step 23
Planning the room's dimensions

The room will be 15 long and 13 blocks wide (remember that you are start in the back left corner of this diagram, facing to the right), and 3 blocks tall.

Temp1554480896
Step 24
Make a method to build the empty room
Temp1554480912

Make a method for library.secretRoom called build that creates a drone at library.secretRoom.loc and builds the space with air blocks.

Step 25
Call the build method

Call your library.secretRoom.build method in the buildLibrary exports function, and comment everything else.

Temp1554483866
Check Your Work
Step 26
Build the secret room

Save, deploy, reload, and call /js buildLibrary()

Temp1554480954
Planning
Step 27
Laying out the secret room
Temp1554576621

The diagram below shows the elements we will be adding to this secret room. The different densities of cobweb will add a lot of code, so if you only want to add the high-density block that is fine.

Information
Step 28
Random Blocks

With the drone's rand() method, there are 2 different ways to tell it what blocks to randomize.

  • If you want an even distribution of blocks, you can use an array:
var randomWool = [blocks.wool.white, blocks.wool.gray, blocks.wool.black];
rand(randomWool, 10, 10, 10);

This will produce a 10x10x10 box with an even number of white, gray, and black wool blocks.

Temp1556656930
  • If you want more of one kind of block and fewer of another, use an object:
var randomWool = {}
randomWool[blocks.wool.white] = 1;
randomWool[blocks.wool.gray] = 2;
randomWool[blocks.wool.black] = 4;
rand(randomWool, 10, 10, 10);

This will produce a 10x10x10 box with twice as many gray wool blocks as white wool blocks, and four times as many black wool blocks.

Temp1556656834
Step 29
Add some cobwebs!

We are going to use the technique of the last step to make 3 different cobweb spots, with more cobweb in the corner and less around the outside, but you could always just use cobwebHigh for all 4 spots and not worry about it.

Temp1554481032
Check Your Work
Step 30
Build some cobwebs

Save, deploy, reload, and call /js buildLibrary() to add the cobwebs to the secret room. They should be in the back right corner from the entrance.

Temp1554481052
Step 31
And shelves and a chest
Temp1554481076

We're going to move the drone back to the start, to add bookshelves and a chest. The bookshelves will help with the lighting.

Check Your Work
Step 32
Build the shelves

Save, deploy, reload, and call /js buildLibrary(). You should have two bookshelves near the entrance, and a chest tucked in on the far side of one of them.

Temp1554481199
Step 33
Add some beds

Just like with doors, we have a special drone method to make beds called .bed(). We will use it to add 2 here.

Temp1554576953
Check Your Work
Step 34

Save, deploy, reload, and call /js buildLibrary(). You shoudl have 2 beds, 1 block away from the back wall.

Temp1554577058
Planning
Step 36
Find the location for the painting
Temp1554577715

The painting will position itself to fit in the space, so we just need to find a block on the wall behind the bed.

Step 37
Save the painting location
Temp1554578163

Create a new property of library.secretRoom called paintingLoc

Step 38
Make a method to spawn a painting

Make a method called placePainting for library.secretRoom, and then call it in your buildLibrary exports function.

Temp1556658670 Temp1556658774
Check Your Work
Step 39
Temp1556658918 Temp1554578017

What happens if you call buildLibrary() again?

Step 40
Make a variable for Art

We're going to make a variable called Art so that we don't have to type org.bukkit.Art every single time we want to set the artwork of a painting.

Temp1554577396
Step 41
Set the art of the painting

We are going to set which art should be in the painting, by using the setArt method.

Temp1556659141

You can find a list of all the available paintings here

Check Your Work
Step 42
Test your painting

Save, deploy, and reload.

Before you call /js buildLibrary, you need to make sure you destroy the painting that is already there.

Temp1554579331
Planning
Step 43
Find a location for the skeleton

Place a block down and use the debug menu's "Looking at" information to find its location. Spawned entities always face the same direction, so yaw does not matter here.

Temp1556819157
Step 44
Save the skeleton location

Create a property, skeletonLoc for library.secretRoom, and assign it to the location you chose in the previous step.

Temp1555963274
Step 45
Make a function to spawn the skeleton

We are going to spawn a skeleton sitting in a boat. Do you remember the .addPassenger method from the pig stacking plugin?

The .setAI(false) line allows us to make a skeleton that won't move or shoot! Very useful.

Temp1556819309

Then, call the spawnLibrarianSkeleton() function inside your secretPassage() exports function, and comment the other code.

Temp1556819539
Check Your Work
Step 46
Check out that skeleton

Save, deploy, reload, and call /js secretPassage(). You should have a skeleton in a boat.

It may be facing a different direction from this image.

Temp1554583268
Challenge
Step 47
Optional Challenge: Customize the skeleton

You can equip and name your skeleton just like any other custom mob. Give it some equipment and a name.

Temp1554582901
Planning
Step 48
Find a location for the enchanting table
Temp1554756994
Step 49
Save the location
Temp1554757012
Step 50
Build the enchanting table

Use the drone to move to the location you just saved, and build an enchanting table in your library.secretRoom.build method. Comment out the rest of the code for now.

Temp1554757025
Check Your Work
Step 51
Test the enchanting table

Make sure that the exports.buildLibrary function calls library.secretRoom.build() and nothing else. Then save, deploy, reload, and call /js buildLibrary().

Temp1554757075
Challenge
Step 52
Add a portrait to the library
Temp1554943274

Use what you learned from the painting in the basement to add a portrait on the walls of the library.

Temp1554942979
Information
Step 53
Solution to the portrait challenge
Temp1556820680
Step 54
Call the portrait placing method

Call your library.portrait.place method in the buildLibrary function to test it. We will move this somewhere else later.

Temp1555704036
Planning
Step 55
Choose a location for the filing cabinet
Temp1554942373
Step 56
Save the filing cabinet location
Temp1554944125
Step 57
Make a method to build the filing cabinet with dropper blocks
Temp1554945003
Step 58
Call the filing cabinet building method
Temp1554945241
Check Your Work
Step 59
Test the filing cabinet build method

Make sure library.interior.build is uncommented in your buildLibrary function. Then save, deploy, reload, and call /js buildLibrary().

Temp1554945145

Why doesn't this look like a real dropper? I don't know! But it won't matter, as we are going to put some things in front of it.

Step 60
Make a method to add drawers to the filing cabinets

To add the "drawers", we do two steps:

  1. Use .spawnEntity() to spawn an item frame in front of the dropper.
  2. Use .setItem() to make the item frame hold a button.
Temp1554946588
Step 61
Call the drawer building method

Call the library.filingCabinet.buildDrawers method in buildLibrary, and temporarily comment all the other code.

Temp1555967610
Check Your Work
Step 62
Test the drawer building method!

Save, deploy, reload and call /js buildLibrary() to see the new drawers.

Temp1554946628
Planning
Step 63
You finished building the library!

Congratulations, you have finished building all the structures we will need for the library! In the next lesson, you will learn how to add items to inventories (like the filing cabinet), and make custom inventories, books, and even villager trades!

The Haunted Library Part 3: The Secret Room Info

Account

MVCode Clubs

Created By

Mandala2 slinkous

Course:

The Haunted Library

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