Rename mobs mod to mcl_mobs
[MineClone/MineClone2.git] / mods / ITEMS / mcl_books / init.lua
blob29ae2a028fcf2e8ff4f9d2b6ed1aab5b15ed7512
1 -- Book
2 minetest.register_craftitem("mcl_books:book", {
3 description = "Book",
4 _doc_items_longdesc = "Books are used to make bookshelves and book and quills.",
5 inventory_image = "default_book.png",
6 stack_max = 64,
7 groups = { book=1 },
8 })
10 if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_mobitems") then
11 minetest.register_craft({
12 type = 'shapeless',
13 output = 'mcl_books:book',
14 recipe = { 'mcl_core:paper', 'mcl_core:paper', 'mcl_core:paper', 'mcl_mobitems:leather', }
16 end
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)
22 -- Grab the text
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)
32 if des then
33 text = des.text
34 end
35 else
36 text = meta_legacy
37 end
38 end
40 -- Security check
41 if not text then
42 text = ""
43 end
44 return text
45 end
47 local make_description = function(title, author, generation)
48 local desc
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)
55 else
56 desc = "Tattered Book"
57 end
58 desc = desc .. "\n" .. core.colorize("#AAAAAA", string.format("by %s", author))
59 return desc
60 end
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
69 end
70 end
71 end
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)
80 end
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
89 end
90 end
91 end
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)
99 end
101 -- Book and Quill
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",
107 groups = { book=1 },
108 stack_max = 1,
109 on_place = write,
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()
118 if fields.ok then
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)
155 else
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" })
164 end)
166 if minetest.get_modpath("mcl_dye") and minetest.get_modpath("mcl_mobitems") then
167 minetest.register_craft({
168 type = "shapeless",
169 output = "mcl_books:writable_book",
170 recipe = { "mcl_books:book", "mcl_dye:black", "mcl_mobitems:feather" },
174 -- Written Book
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 },
183 stack_max = 16,
184 on_place = read,
185 on_secondary_use = read
188 -- Copy books
190 -- This adds 8 recipes
191 local baq = "mcl_books:writable_book"
192 local wb = "mcl_books:written_book"
193 local recipes = {
194 {wb, baq},
195 {baq, baq, wb},
196 {baq, baq, wb, baq},
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({
205 type = "shapeless",
206 output = "mcl_books:written_book "..r,
207 recipe = recipes[r],
211 minetest.register_craft_predict(function(itemstack, player, old_craft_grid, craft_inv)
212 if itemstack:get_name() ~= "mcl_books:written_book" then
213 return
216 local original
217 local index
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]
221 index = i
224 if not original then
225 return
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
233 return ItemStack("")
234 else
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
244 generation = 1
247 local desc = make_description(title, author, generation)
248 imeta:set_string("description", desc)
249 return itemstack
251 end)
253 minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
254 if itemstack:get_name() ~= "mcl_books:written_book" then
255 return
258 local original
259 local index
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]
263 index = i
266 if not original then
267 return
270 -- copy of the book
271 local text = get_text(original)
272 if not text or text == "" then
273 local copymeta = original:get_metadata()
274 itemstack:set_metadata(copymeta)
275 else
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
281 return ItemStack("")
284 -- Copy metadata
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
295 generation = 1
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)
305 end)
307 local wood_sound
308 if minetest.get_modpath("mcl_sounds") then
309 wood_sound = mcl_sounds.node_sound_wood_defaults()
312 -- Bookshelf
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"},
317 stack_max = 64,
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",
321 sounds = wood_sound,
322 _mcl_blast_resistance = 7.5,
323 _mcl_hardness = 1.5,
326 minetest.register_craft({
327 output = 'mcl_books:bookshelf',
328 recipe = {
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({
336 type = "fuel",
337 recipe = "mcl_books:bookshelf",
338 burntime = 15,