2 minetest
.register_craftitem("mcl_books:book", {
4 _doc_items_longdesc
= "Books are used to make bookshelves and book and quills.",
5 inventory_image
= "default_book.png",
10 if minetest
.get_modpath("mcl_core") and minetest
.get_modpath("mcl_mobitems") then
11 minetest
.register_craft({
13 output
= 'mcl_books:book',
14 recipe
= { 'mcl_core:paper', 'mcl_core:paper', 'mcl_core:paper', 'mcl_mobitems:leather', }
18 -- Get the included text out of the book item
19 -- itemstack: Book item
20 -- meta: Meta of book (optional)
21 local get_text
= function(itemstack
)
23 local meta
= itemstack
:get_meta()
24 local text
= meta
:get_string("text")
26 -- Backwards-compability with MCL2 0.21.0
27 -- Remember that get_metadata is deprecated since MT 0.4.16!
28 if text
== nil or text
== "" then
29 local meta_legacy
= itemstack
:get_metadata()
30 if itemstack
:get_name() == "mcl_books:written_book" then
31 local des
= minetest
.deserialize(meta_legacy
)
47 local make_description
= function(title
, author
, generation
)
49 if generation
== 0 then
50 desc
= string.format("“%s”", title
)
51 elseif generation
== 1 then
52 desc
= string.format("Copy of “%s”", title
)
53 elseif generation
== 2 then
54 desc
= string.format("Copy of Copy of “%s”", title
)
56 desc
= "Tattered Book"
58 desc
= desc
.. "\n" .. core
.colorize("#AAAAAA", string.format("by %s", author
))
62 local write = function(itemstack
, user
, pointed_thing
)
63 -- Call on_rightclick if the pointed node defines it
64 if pointed_thing
.type == "node" then
65 local node
= minetest
.get_node(pointed_thing
.under
)
66 if user
and not user
:get_player_control().sneak
then
67 if minetest
.registered_nodes
[node
.name
] and minetest
.registered_nodes
[node
.name
].on_rightclick
then
68 return minetest
.registered_nodes
[node
.name
].on_rightclick(pointed_thing
.under
, node
, user
, itemstack
) or itemstack
73 local text
= get_text(itemstack
)
74 local formspec
= "size[8,9]"..
75 "background[-0.5,-0.5;9,10;mcl_books_book_bg.png]"..
76 "textarea[0.75,0.1;7.25,9;text;;"..minetest
.formspec_escape(text
).."]"..
77 "button[0.75,7.95;3,1;sign;Sign]"..
78 "button_exit[4.25,7.95;3,1;ok;Done]"
79 minetest
.show_formspec(user
:get_player_name(), "mcl_books:writable_book", formspec
)
82 local read = function(itemstack
, user
, pointed_thing
)
83 -- Call on_rightclick if the pointed node defines it
84 if pointed_thing
.type == "node" then
85 local node
= minetest
.get_node(pointed_thing
.under
)
86 if user
and not user
:get_player_control().sneak
then
87 if minetest
.registered_nodes
[node
.name
] and minetest
.registered_nodes
[node
.name
].on_rightclick
then
88 return minetest
.registered_nodes
[node
.name
].on_rightclick(pointed_thing
.under
, node
, user
, itemstack
) or itemstack
93 local text
= get_text(itemstack
)
94 local formspec
= "size[8,9]"..
95 "background[-0.5,-0.5;9,10;mcl_books_book_bg.png]"..
96 "textarea[0.75,0.1;7.25,9;;"..core
.colorize("#000000", minetest
.formspec_escape(text
))..";]"..
97 "button_exit[2.25,7.95;3,1;ok;Done]"
98 minetest
.show_formspec(user
:get_player_name(), "mcl_books:written_book", formspec
)
102 minetest
.register_craftitem("mcl_books:writable_book", {
103 description
= "Book and Quill",
104 _doc_items_longdesc
= "This item can be used to write down some notes.",
105 _doc_items_usagehelp
= "Hold it in the hand, then rightclick to read the current notes and edit then. You can edit the text as often as you like. You can also sign the book which turns it into a written book which you can stack, but it can't be edited anymore.",
106 inventory_image
= "mcl_books_book_writable.png",
110 on_secondary_use
= write,
113 minetest
.register_on_player_receive_fields(function ( player
, formname
, fields
)
114 if ((formname
== "mcl_books:writable_book") and fields
and fields
.text
) then
115 local stack
= player
:get_wielded_item()
116 if (stack
:get_name() and (stack
:get_name() == "mcl_books:writable_book")) then
117 local meta
= stack
:get_meta()
119 meta
:set_string("text", fields
.text
)
120 player
:set_wielded_item(stack
)
121 elseif fields
.sign
then
122 meta
:set_string("text", fields
.text
)
123 player
:set_wielded_item(stack
)
125 local name
= player
:get_player_name()
126 local formspec
= "size[8,9]"..
127 "background[-0.5,-0.5;9,10;mcl_books_book_bg.png]"..
128 "field[0.75,1;7.25,1;title;"..core
.colorize("#000000", "Enter book title:")..";]"..
129 "label[0.75,1.5;"..core
.colorize("#404040", minetest
.formspec_escape("by " .. name
)).."]"..
130 "label[0.75,6.95;"..core
.colorize("#000000", "Note: The book will no longer") .. "\n" .. core
.colorize("#000000", "be editable after signing.").."]"..
131 "button_exit[0.75,7.95;3,1;sign;Sign and Close]"..
132 "button[4.25,7.95;3,1;cancel;Cancel]"
133 minetest
.show_formspec(player
:get_player_name(), "mcl_books:signing", formspec
)
136 elseif ((formname
== "mcl_books:signing") and fields
and fields
.sign
and fields
.title
) then
137 local newbook
= ItemStack("mcl_books:written_book")
138 local book
= player
:get_wielded_item()
139 local name
= player
:get_player_name()
140 if book
:get_name() == "mcl_books:writable_book" then
141 if fields
.title
== "" then
142 fields
.title
= "Nameless Book"
144 local meta
= newbook
:get_meta()
145 local text
= get_text(book
)
146 meta
:set_string("title", fields
.title
)
147 meta
:set_string("author", name
)
148 meta
:set_string("text", text
)
149 meta
:set_string("description", make_description(fields
.title
, name
, 0))
151 -- The book copy counter. 0 = original, 1 = copy of original, 2 = copy of copy of original, …
152 meta
:set_int("generation", 0)
154 player
:set_wielded_item(newbook
)
156 minetest
.log("error", "[mcl_books] "..name
.." failed to sign a book!")
158 elseif ((formname
== "mcl_books:signing") and fields
and fields
.cancel
) then
159 local book
= player
:get_wielded_item()
160 if book
:get_name() == "mcl_books:writable_book" then
161 write(book
, player
, { type = "nothing" })
166 if minetest
.get_modpath("mcl_dye") and minetest
.get_modpath("mcl_mobitems") then
167 minetest
.register_craft({
169 output
= "mcl_books:writable_book",
170 recipe
= { "mcl_books:book", "mcl_dye:black", "mcl_mobitems:feather" },
175 minetest
.register_craftitem("mcl_books:written_book", {
176 description
= "Written Book",
177 _doc_items_longdesc
= "Written books contain some text written by someone. They can be read and copied, but not edited.",
178 _doc_items_usagehelp
= [[Hold it in your hand, then rightclick to read the book.
180 To copy the text of the written book, place it into the crafting grid together with a book and quill (or multiple of those) and craft. The written book will not be consumed. Copies of copies can not be copied.]],
181 inventory_image
= "mcl_books_book_written.png",
182 groups
= { not_in_creative_inventory
=1, book
=1, no_rename
=1 },
185 on_secondary_use
= read
190 -- This adds 8 recipes
191 local baq
= "mcl_books:writable_book"
192 local wb
= "mcl_books:written_book"
197 {baq
, baq
, baq
, baq
, wb
},
198 {baq
, baq
, baq
, baq
, wb
, baq
},
199 {baq
, baq
, baq
, baq
, wb
, baq
, baq
},
200 {baq
, baq
, baq
, baq
, wb
, baq
, baq
, baq
},
201 {baq
, baq
, baq
, baq
, wb
, baq
, baq
, baq
, baq
},
203 for r
=#recipes
, 1, -1 do
204 minetest
.register_craft({
206 output
= "mcl_books:written_book "..r
,
211 minetest
.register_craft_predict(function(itemstack
, player
, old_craft_grid
, craft_inv
)
212 if itemstack
:get_name() ~= "mcl_books:written_book" then
218 for i
= 1, player
:get_inventory():get_size("craft") do
219 if old_craft_grid
[i
]:get_name() == "mcl_books:written_book" then
220 original
= old_craft_grid
[i
]
228 local ometa
= original
:get_meta()
229 local generation
= ometa
:get_int("generation")
231 -- Check generation, don't allow crafting with copy of copy of book
232 if generation
>= 2 then
235 -- Valid copy. Let's update the description field of the result item
236 -- so it is properly displayed in the crafting grid.
237 local imeta
= itemstack
:get_meta()
238 local title
= ometa
:get_string("title")
239 local author
= ometa
:get_string("author")
241 -- Increase book generation and update description
242 generation
= generation
+ 1
243 if generation
< 1 then
247 local desc
= make_description(title
, author
, generation
)
248 imeta
:set_string("description", desc
)
253 minetest
.register_on_craft(function(itemstack
, player
, old_craft_grid
, craft_inv
)
254 if itemstack
:get_name() ~= "mcl_books:written_book" then
260 for i
= 1, player
:get_inventory():get_size("craft") do
261 if old_craft_grid
[i
]:get_name() == "mcl_books:written_book" then
262 original
= old_craft_grid
[i
]
271 local text
= get_text(original
)
272 if not text
or text
== "" then
273 local copymeta
= original
:get_metadata()
274 itemstack
:set_metadata(copymeta
)
276 local ometa
= original
:get_meta()
277 local generation
= ometa
:get_int("generation")
279 -- No copy of copy of copy of book allowed
280 if generation
>= 2 then
285 local imeta
= itemstack
:get_meta()
286 local title
= ometa
:get_string("title")
287 local author
= ometa
:get_string("author")
288 imeta
:set_string("title", title
)
289 imeta
:set_string("author", author
)
290 imeta
:set_string("text", text
)
292 -- Increase book generation and update description
293 generation
= generation
+ 1
294 if generation
< 1 then
298 local desc
= make_description(title
, author
, generation
)
300 imeta
:set_string("description", desc
)
301 imeta
:set_int("generation", generation
)
303 -- put the book with metadata back in the craft grid
304 craft_inv
:set_stack("craft", index
, original
)
308 if minetest
.get_modpath("mcl_sounds") then
309 wood_sound
= mcl_sounds
.node_sound_wood_defaults()
313 minetest
.register_node("mcl_books:bookshelf", {
314 description
= "Bookshelf",
315 _doc_items_longdesc
= "Bookshelves are used for decoration.",
316 tiles
= {"mcl_books_bookshelf_top.png", "mcl_books_bookshelf_top.png", "default_bookshelf.png"},
318 is_ground_content
= false,
319 groups
= {handy
=1,axey
=1, flammable
=3,building_block
=1, material_wood
=1},
320 drop
= "mcl_books:book 3",
322 _mcl_blast_resistance
= 7.5,
326 minetest
.register_craft({
327 output
= 'mcl_books:bookshelf',
329 {'group:wood', 'group:wood', 'group:wood'},
330 {'mcl_books:book', 'mcl_books:book', 'mcl_books:book'},
331 {'group:wood', 'group:wood', 'group:wood'},
335 minetest
.register_craft({
337 recipe
= "mcl_books:bookshelf",