In this lesson, we will be learning how to add items to a block with an inventory, like a chest, as well as how to create a custom inventory and open it.
We will also make custom written books, with a title, author, and pages.
And finally, we will learn how to spawn custom villagers and make custom trades.
Custom books work exactly like custom items, except instead of
displayName, you have
lore on custom items, the
pages property should be an array (
[ ]) of strings (
" "). You can copy the code below, or write your own diary pages. Just make sure that you page that tells the reader that some pages are missing.
\n stands for "new line" and is like pressing "enter" on your keyboard
diaryMeta.pages = [ "My Diary \n\n\nby\n\n\nMelvil the Librarian", "Dear Diary,\n\nToday I found a strange secret passage in the library!\n\nWhen I went down in it, I found a secret room, furnished with everything a librarian like myself could need.", "But strangely, just as I entered this new room, the passage sealed behind me!\n\nOh well, time to do some reading!", "\n\n [ Some pages are missing. . . ]", "Will I ever escape this room? \n\n I have read and alphabetized every book here!" ];
Your challenge is to make another custom book! Here is the info you need:
The chest in the secret room is a decoy, and doesn't contain any real clues for the players. Use the same method that we did above to fill it with some random items.
chestContentsthat is equal to the block at that location's
Then, call your method in the
testInventory export function to see items show up in the chest!
Our Restricted Shelf is going to be a custom inventory, that does not open with a block with an inventory, like a chest or dropper, but instead by clicking a particular shelf.
Here, we use
bukkit.createInventory, with three inputs:
nullmeans that it does not belong to a particular player, so that anyone can access it.
18is the size. It has to be a multiple of 9. We picked 18 because a bookshelf block looks like it has two shelves, and 18 will make a two-row inventory.
"The Restricted Bookshelf"is the name of the inventory.
Create a custom book called "The Secrets of the Library" to add to the Restricted Section.
You can copy the pages below, or write your own!
secretsMeta.pages = [ "The Secrets of the Library\n\n\nby\n\nMelvil the Librarian", "This library holds many secrets, and as the head librarian, I have been able to uncover many of them.", "Some secrets, however, continue to elude me... I have heard tales of a secret passage in one of these bookcases", "But have never seen the evidence with my own eyes. The pursuit of knowledge is my only goal! And not know about this is driving me crazy", "An old colleague of mine suggested, once, that only the authors whose works are hidden away in the Restricted Section have been able to find this elusive passage", "So I have put pen to paper myself to write this very book, and enter it into the Restricted Shelves. Now we will see what this library reveals to me!" ];
Just like with the filing cabinet, us a
for loop to fill each slot in the inventory with items from an array. This time, instead of just going through the list, we will use
Math.floor() to get a random decimal between 0 and 1, multiply it by the number of items in
creepy items, and then round that down to a whole number with no decimals.
Save, deploy, reload, open the Restricted Bookshelf, and try to take anything out besides The Secrets of the Library. You should hear a horrible noise and not be able to pick up the item.
When you create a trade, you will use
new Trade() with two inputs:
keyToTheLibraryis the item that the villager gives you
1is the number of times the villager can do this trade.
addIngredient to decide what a player has to give in the trade.
setRecipes to make this the only trade the ghost has. You could list more trades in there if you wanted to make more of them.
Congratulations, we have made all the elements that we will need for this Escape Room! In the final lesson, we will tie it all together, making these items, entities, and inventories appear as players solve the puzzle!