]> git.lizzy.rs Git - Crafter.git/blob - mods/book/init.lua
Add sounds to books
[Crafter.git] / mods / book / init.lua
1 --this is the gui for un-inked books
2 local open_book_gui = function(itemstack, user)
3         minetest.sound_play("book_open", {to_player=user:get_player_name()})
4         local meta = itemstack:get_meta()
5         local book_text = meta:get_string("book.book_text")
6         if book_text == "" then
7                 book_text = "Text here"
8         end
9         local book_title = meta:get_string("book.book_title")
10         if book_title == "" then
11                 book_title = "Title here"
12         end
13         
14         book_writing_formspec = "size[9,8.75]"..
15                 "background[-0.19,-0.25;9.41,9.49;gui_hb_bg.png]"..
16                 "style[book.book_text,book.book_title;textcolor=black;border=false;noclip=false]"..
17                 "textarea[0.3,0;9,0.5;book.book_title;;"..book_title.."]"..
18                 "textarea[0.3,0.3;9,9;book.book_text;;"..book_text.."]"..
19                 "button[-0.2,8.3;1,1;book.book_write;write]"..
20                 "button[8.25,8.3;1,1;book.book_ink;ink  ]"
21         minetest.show_formspec(user:get_player_name(), "book.book_gui", book_writing_formspec)
22 end
23
24
25 --this is the gui for permenantly written books
26 local open_book_inked_gui = function(itemstack, user)
27         minetest.sound_play("book_open", {to_player=user:get_player_name()})
28         local meta = itemstack:get_meta()
29         local book_text = meta:get_string("book.book_text")
30         
31         local book_title = meta:get_string("book.book_title")
32         
33         book_writing_formspec = "size[9,8.75]"..
34                 "background[-0.19,-0.25;9.41,9.49;gui_hb_bg.png]"..
35                 "style_type[textarea;textcolor=black;border=false;noclip=false]"..
36                 "textarea[0.3,0;9,0.5;;;"..book_title.."]"..
37                 "textarea[0.3,0.3;9,9;;;"..book_text.."]"..
38                 "button_exit[4,8.3;1,1;book.book_close;close]"
39         minetest.show_formspec(user:get_player_name(), "book.book_gui", book_writing_formspec)
40 end
41
42
43 --handle the book gui
44 minetest.register_on_player_receive_fields(function(player, formname, fields)
45         if not formname == "book.book_gui" then return end
46         
47         if fields["book.book_write"] and fields["book.book_text"] and fields["book.book_text"] then
48                 local itemstack = ItemStack("book:book")
49                 local meta = itemstack:get_meta()
50                 meta:set_string("book.book_text", fields["book.book_text"])
51                 meta:set_string("book.book_title", fields["book.book_title"])   
52                 meta:set_string("description", fields["book.book_title"])
53                 
54                 player:set_wielded_item(itemstack)
55                 minetest.close_formspec(player:get_player_name(), "book.book_gui")
56                 minetest.sound_play("book_write", {to_player=player:get_player_name()})
57         elseif fields["book.book_ink"] and fields["book.book_text"] and fields["book.book_text"] then
58                 local itemstack = ItemStack("book:book_written")
59                 local meta = itemstack:get_meta()
60                 meta:set_string("book.book_text", fields["book.book_text"])
61                 meta:set_string("book.book_title", fields["book.book_title"])   
62                 meta:set_string("description", fields["book.book_title"])
63                 player:set_wielded_item(itemstack)
64                 minetest.close_formspec(player:get_player_name(), "book.book_gui")
65                 minetest.sound_play("book_close", {to_player=player:get_player_name()})
66         elseif fields["book.book_close"] then
67                 minetest.sound_play("book_close", {to_player=player:get_player_name()})
68         end
69 end)
70
71
72 --this is the book item
73 minetest.register_craftitem("book:book",{
74         description = "Book",
75         groups = {book = 1, written = 0},
76         stack_max = 1,
77         inventory_image = "book.png",
78         
79         on_place = function(itemstack, user, pointed_thing)
80                 print("make books placable on the ground")
81                 open_book_gui(itemstack, user)
82         end,
83
84         on_secondary_use = function(itemstack, user, pointed_thing)
85                 open_book_gui(itemstack, user)
86         end,
87 })
88
89 --permenantly written books
90 minetest.register_craftitem("book:book_written",{
91         description = "Book",
92         groups = {book = 1, written = 1},
93         stack_max = 1,
94         inventory_image = "book_written.png",
95         
96         on_place = function(itemstack, user, pointed_thing)
97                 print("make books placable on the ground")
98                 open_book_inked_gui(itemstack, user)
99         end,
100
101         on_secondary_use = function(itemstack, user, pointed_thing)
102                 open_book_inked_gui(itemstack, user)
103         end,
104 })
105
106 --change this to paper
107 minetest.register_craft({
108         output = "book:book",
109         recipe = {
110                 {"main:wood","weather:snowball","main:wood"},
111                 {"main:wood","weather:snowball","main:wood"},
112                 {"main:wood","weather:snowball","main:wood"},
113         }
114 })
115 --book book book