The Haunted Library Part 3: The Secret Room

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.

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.

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, {}.

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 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 method. We will use this function to test several different methods in this lesson.

Check Your Work
Step 5
Test the method

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

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.


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

Check Your Work
Step 8
Close the secret passage.

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

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!

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.

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.

Check Your Work
Step 12
Test out the lever!

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

Step 13
Challenge: Make a method to hide the lever

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

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


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.

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.

Step 16
Add a sound effect

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

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.

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.

Step 19
Make the lever open the secret passage

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!

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

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.

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.

Step 24
Make a method to build the empty room

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 method in the buildLibrary exports function, and comment everything else.

Check Your Work
Step 26
Build the secret room

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

Step 27
Laying out the secret room

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.

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,];
rand(randomWool, 10, 10, 10);

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

  • 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[] = 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.

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.

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.

Step 31
And shelves and a chest

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.

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.

Check Your Work
Step 34

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

Step 36
Find the location for the painting

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

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.

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.


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.

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.

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.

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.


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

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.

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.

Step 48
Find a location for the enchanting table
Step 49
Save the location
Step 50
Build the enchanting table

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

Check Your Work
Step 51
Test the enchanting table

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

Step 52
Add a portrait to the library

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

Step 53
Solution to the portrait challenge
Step 54
Call the portrait placing method

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

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

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


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.
Step 61
Call the drawer building method

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

Check Your Work
Step 62
Test the drawer building method!

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

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


MVCode Clubs

Created By

Mandala2 slinkous


The Haunted Library

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