Escape Room

Step 1
Welcome to the Escape Room!

In this project, we'll learn about all the different events that you can use in your Minecraft plugins. We will build a room that locks you in until you complete certain steps.

2018 03 29 13.29.37

Before starting:

  • You should already have done the Drone lesson
  • We will be using code similar to the Capture the Flag mini game, so it helps if you have at least started it
Step 2
Make a room

First, we need a room. We can use one of the ScriptCraft built-in drone functions for this: cottage()

On your server, place a block on the ground and call the cottage() function.

Temp1522356222

You could also use a different drone function, like fort(), or build your own. Check out all the possibilities here.

Step 3
Get the Escape Room coordinates

In your game, press F3 to see the debug info. We need the coordinates for when you are right in front of the room, but not in it. Write them down somewhere so we can add them to our program.

Temp1522357140
Step 4
Create a location

We can save locations in ScriptCraft using the location() function. Add this code with your own coordinates for a place just in front of the Escape Room.

Temp1522357771
Step 5
Make an export function to teleport to the Escape Room

Using exports allows us to call the function in the game. When you use an export function, self will be whoever used it.

Temp1522358367
Check Your Work
Step 6
Test your new function

Move to somewhere away from your Escape Room, and call this new function to test that it teleports you back to the room.

Temp1522358547
Planning
Step 7
Who's in the room?

To figure out if a player is in the room, we're going to need to save two more locations, for opposite corners.

Webp.net gifmaker %283%29

We will test if the player is in the room by checking if their X, Y, and Z coordinates are between the X, Y, and Z coordinates of these two corners.

Step 8
Find the bottom front corner

With your debug info on (F3), look at the floor block of the back corner. Instead of using the XYZ of your position, you can get the location of a block your are highlighting after "Looking at: ".

Temp1522359402
Step 9
Find the coordinates for the back right top corner
Temp1522359625
Step 10
Save the corners as locations

In your code, make an object called escapeRoom, and give it locations as properties for two corners.

Temp1522360209
Planning
Step 11
Is the player in the room?

To detect if the player is actually in the room, we are going to look at the X, Y, and Z coordinates.

Xaxisexample

If the player's X is less than the smaller X of the room, then we know they are not in it. If their X is greater than the larger X, we also know they can't be in it.

Step 12
Create a function, and add this code to check if the player's X is outside the boundaries

Make a new function called inEscapeRoom() and add this code to check if the player's X is less than the minimum X or greater than the maximum X

Temp1522946963
Challenge
Step 13
Repeat this for Y and Z

For Y and Z:

  • make an array of the coordinates for both of the escape room's corners
  • sort the coordinates from smallest to largest
  • make an "if" statement for if the player's coordinate is less than the minimum
  • make an "if" statement for if the player's coordinate is more than the maximum
Step 14
Otherwise, return true

When any of these tests returns false, the program stops. If none of them do that, we can safely say the player is in the room!

Temp1522947532
Step 15
Test the Player Move event

We're going to make a new function, called onPlayerMove(), and have it called whenever the a player moves in Minecraft.

Temp1522556866

You can learn more about the Bukkit PlayerMoveEvent here

Check Your Work
Step 16
Deploy and test this on your server!

Save and deploy your code now, and go on to your server. Once you reload, you should see "A player moved" every single time you move in the game.

Temp1522702362
Step 17
Find the event's player

We can get the player involved in any player event by using event.player (as long as you have event as the input for the function).

Change "A player moved" inside your broadcast message to event.player

Temp1522948291
Check Your Work
Step 18
Figure out who is moving

Save, deploy, and reload on your server and move around again. You should now see a message with your own username.

Temp1522948417

Your name should be different!

Step 19
Test your inEscapeRoom() function

Now that we know we can access the event's player, it's time to test our inEscapeRoom() function. Use an if statement to broadcast a message only when a player is in the Escape Room.

Temp1523034529
Check Your Work
Step 20
Deploy your change and test if you are in the room

Save, deploy, and reload. When you move into the Escape Room, you should see the message saying that you are in the Escape Room.

Run around the outside as well to make sure it only works inside.

Temp1523033803
Step 21
Get the coordinates of the door

To seal the doorway, we're going to need the coordinates for it's location. In the game, turn on your debug info with F3 and look at the bottom block at the door. We'll make this into another location in our escapeRoom object.

Temp1522953368
Step 22
Add the door location to your escapeRoom object

We're going to add the location of the door as a property of the object, and another property that turns from false to true if we have sealed the room.

Temp1522953766
Step 23
Make an export function to seal the door

First, we're going to need to initialize a drone at the location. Add this function using the location you saved for the doorway.

Temp1522954392
Step 24
Make the drone build a box in the door way

Use the box() function to make the drone seal the door. Here we used iron bars for the material, but you can choose a different one if you like.

Temp1522954756
Information
Step 25
How to choose different materials for the drone

Minecraft has data values that are a number assigned to each type of block. You can find a visual list of all of them here.

You can use these numbers instead of the blocks module to build with the drone.

For example:

box(blocks.stone)

and

box(1)

Both produce one stone block.

If you want to find out what a block is called in ScriptCraft, the easiest way is to search the actual code for the blocks module

Check Your Work
Step 26
Test the sealDoor() function

On your server, use /js sealDoor(); to test your new export function.

Temp1522959358
Challenge
Step 27
Make an unsealDoor() function

Now, see if you can make the unsealDoor() export function on your own! It should look very similar to seal doors.

Hint: blocks.air will have the drone place an empty air block

You could also try using the drone .door() command to put the door back there, instead of empty blocks!

Step 28
Add two more properties to the escapeRoom object

We're going to add two more boolean (true/false) properties to the escapeRoom object so we know when the puzzle has been started and when the player has finished it.

Temp1523037755
Step 29
Get this thing started!

Now, we're going to seal the doors as soon as the player enters the room. First, we check that we haven't already started it with !escapeRoom.started (which is the same as escapeRoom.started === false). Then we seal the doors and tell the player what's going on. Lastly, we change our started property, so we know that the game is afoot.

Temp1524677312

This broadcast message is pretty boring. You should make a different one that makes sense for you

Step 30
Make it possible to escape

We'll go ahead and add the code to release us once we've completed the room, even though we can't do that yet.

Temp1533408918
Step 31
Fix the unsealDoor() function

Right now, if we finished the Escape Room, our code would unseal the door over, and over, and over again. Every single time we moved.

To fix this, edit the unsealDoor() function so that it only unseals if the room is currently sealed.

Temp1523122202

ERROR ALERT: This should say escapeRoom.sealed = false

Challenge
Step 32
Fix the sealDoor() function

Now, make the sealDoor() function only seal if the room is not currently sealed.

Remember that you can use the ! operator to mean "not" just like you did with !escapeRoom.started

Check Your Work
Step 33
Test out the trap

Reload your server and go into the Escape Room. The door should seal behind you.

Remember that you can still use /js unsealDoor() to get back out!

Planning
Step 34
Check out some events!

In your Minecraft server, try typing the following and pressing tab:

  • /js events.
  • /js events.player
  • /js events.playerBedLeave

There are 160 possible events for us to use!

Step 35
Test the playerBedLeave event
Screen shot 2018 04 12 at 9.52.20 am
Information
Step 36
Investigating Events

Whenever we want to use an event, we will follow these steps

  • Write a function to be called when the event happens
  • Put a broadcast message in that function so we know the function was called
  • Call that function when the event happens
Step 37
Escape the room

We're going to make our puzzle super easy at first. All you have to do is sleep in the bed to escape!

Temp1523558567
Step 38
Make it more complicated

Instead of just one simple step, we're going to have three, now. Add a steps property to your escape room, with these three steps inside of it.

Temp1524164612
Step 39
Make the "sleep" step completed

Instead of just finishing the room, mark the "sleep" step as true.

Temp1524165106
Step 40
Add a chest of clues

Place a chest in your Escape Room, and add the following items:

  • Book and Quill (We'll put our clues in this book)
  • Wooden Shovel
  • Potato
Temp1524165435
Step 41
Keep the player from digging their way out!

Now that we have introduced a shovel, we need to make sure that no one tries to dig their way out of the Escape Room.

Temp1524165696

This code will cancel any time anyone tries to break a block if the escape room is started.

Planning
Step 42
Burn, baby, burn!

The next event we are going to use in our Escape Room is the Furnace Burn Event.

We're going to need to know what the fuel was in the event, and we can see from the page that there is a method listed called getFuel().

Whenever you see getSomething() as a method name for Bukkit, you can almost always just use .something in JavaScript to return it.

Temp1524166837

This doesn't have to make perfect sense right now, but you can read more about it in the ScriptCraft documentation

Step 43
Make sure the event calls your function

Our first step with every event will be to make sure that it is calling our function by having it do nothing but give us a broadcast message.

Temp1524167076
Check Your Work
Step 44
Test out the Furnace Burn event

Save, deploy, and reload your server and throw some fuel and something to burn in the furnace. You should see a message like this:

Temp1524168916
Step 45
Find the fuel

Next, we will change our broadcast message to tell us what the fuel for the event was

Temp1524169855
Check Your Work
Step 46
Test the fuel burn event again

You should see a message with an item stack of the fuel you put in.

Temp1524170142
Step 47
Find out what to call the shovel

We need to know exactly what to call the wooden shovel in ScriptCraft, so that the step is only complete if the player uses the shovel.

Temp1524170393

Add .type to the message telling us the fuel used in the event.

Step 48
Find out what the type of fuel you want to use is called

Save, deploy, reload and throw the type of fuel you want to use into the furnace. Our broadcast message should tell you what it is called.

Temp1524170974
Step 49
Find the furnace

Great, now we can save this fuel type information as a variable called fuelType.

Next, we need to figure out how to determine if the event is happening in the Escape Room. We'll check if the furnace involved in the event is inside the room, since it is a block. event.furnace just returns undefined, but because this is a block event, we can try to get the furnace with event.block.

Temp1524171810
Check Your Work
Step 50
Test the furnace burn event again

Reload and put some new fuel in the furnace. The broadcast message should give you a block.

Temp1524250277
Step 51

Make a variable for the furnace, and use your two variables to make the conditions of completing the step. Once we know it works, we'll mark the step complete.

Temp1524261763
Step 52
Make an function to check if all the steps have been finished

Before we move on to the last step, we're going to make an export function to check all the steps for us. It will just give us back true or false, like a collision function.

Temp1524506998
Information
Step 53
The "for . . . in" loop

To check if the room is finished, we are going to need to check each of the steps individually, using a loop. Since escapeRoom.steps is an object, we'll use a for . . . in loop.

Webp.net gifmaker

The important thing to know at this point is that you can use .propertyKey or ["propertyKey"] to access an object's property. We have to use the second one.

escapeRoom.steps.sleep
// is the same thing as 
escapeRoom.steps["sleep"]
Step 54
Test out the for. . . in loop

Let's go ahead and test this out on our room. Add this loop to your export function. Then save, deploy, reload, and call /js checkRoomSteps() in your Minecraft server.

Temp1524513072
Check Your Work
Step 55
All the steps

We can see now that stepName got assigned to each of the keys of our steps object. First it was sleep, the next time through the loop it was cook, and finally it was eat.

Temp1524513255
Step 56
Tell us if all the steps are completed

Just like in our inEscapeRoom() function, whenever the function does the return it stops. So it will tell us the name of the first step that is false, and then stop checking.

If none of the steps are false, then it will get to return true, and we will know that all the steps have been completed.

We'll take out the broadcast message later.

Temp1524513569
Step 57
Check if the room is finished whenever the player moves

Now, we just need to call this function during the Player Move Event, and we should be able to free the player when all steps are complete.

Temp1524513849
Challenge
Step 58
Complete the "eat" step on your own!

The last event you need to add is the Player Item Consume Event. See if you can figure the function on your own.

Remember to follow these steps:

  1. Make a function to be called whenever the Player Item Consume Event happens
  2. Check if the player is in the room.
  3. Broadcast the item's type, and eat a baked potato to find out what to call it.
  4. If the player is in the room, and eats a baked potato, make the "eat" step true
Step 59
Add some clues

The last step is to add some clues on how to escape! Open up your book and quill and add some clues about how to get out. You could write exactly this, or something completely different!

Temp1524678172
Escape Room 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