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