]> git.lizzy.rs Git - Crafter.git/blob - mods/book/init.lua
Added sugarcane, paper, sugar. Sugar is food. Books are now made out of paper. Sugarc...
[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                 if not pointed_thing.type == "node" then
81                         return
82                 end
83                 local sneak = user:get_player_control().sneak
84                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
85                 if not sneak and noddef.on_rightclick then
86                         minetest.item_place(itemstack, user, pointed_thing)
87                         return
88                 end
89                 --print("make books placable on the ground")
90                 open_book_gui(itemstack, user)
91         end,
92
93         on_secondary_use = function(itemstack, user, pointed_thing)
94                 open_book_gui(itemstack, user)
95         end,
96 })
97
98 --permenantly written books
99 minetest.register_craftitem("book:book_written",{
100         description = "Book",
101         groups = {book = 1, written = 1},
102         stack_max = 1,
103         inventory_image = "book_written.png",
104         
105         on_place = function(itemstack, user, pointed_thing)
106                 if not pointed_thing.type == "node" then
107                         return
108                 end
109                 local sneak = user:get_player_control().sneak
110                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
111                 if not sneak and noddef.on_rightclick then
112                         minetest.item_place(itemstack, user, pointed_thing)
113                         return
114                 end
115                 --print("make books placable on the ground")
116                 open_book_inked_gui(itemstack, user)
117         end,
118
119         on_secondary_use = function(itemstack, user, pointed_thing)
120                 open_book_inked_gui(itemstack, user)
121         end,
122 })
123
124 --change this to paper
125 minetest.register_craft({
126         output = "book:book",
127         recipe = {
128                 {"main:wood","main:wood","main:wood"},
129                 {"main:paper","main:paper","main:paper"},
130                 {"main:wood","main:wood","main:wood"},
131         }
132 })
133 --book book book