3
3
-- support for MT game translation.
4
4
local S = default .get_translator
5
5
6
+ local esc = minetest .formspec_escape
7
+ local formspec_size = " size[8,8]"
8
+
9
+ local function formspec_core (tab )
10
+ if tab == nil then tab = 1 else tab = tostring (tab ) end
11
+ return " tabheader[0,0;book_header;" ..
12
+ esc (S (" Write" )) .. " ," ..
13
+ esc (S (" Read" )) .. " ;" ..
14
+ tab .. " ;false;false]"
15
+ end
16
+
17
+ local function formspec_write (title , text )
18
+ return " field[0.5,1;7.5,0;title;" .. esc (S (" Title:" )) .. " ;" ..
19
+ esc (title ) .. " ]" ..
20
+ " textarea[0.5,1.5;7.5,7;text;" .. esc (S (" Contents:" )) .. " ;" ..
21
+ esc (text ) .. " ]" ..
22
+ " button_exit[2.5,7.5;3,1;save;" .. esc (S (" Save" )) .. " ]"
23
+ end
24
+
25
+ local function formspec_read (owner , title , string , text , page , page_max )
26
+ return " label[0.5,0.5;" .. esc (S (" by @1" , owner )) .. " ]" ..
27
+ " tablecolumns[color;text]" ..
28
+ " tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
29
+ " table[0.4,0;7,0.5;title;#FFFF00," .. esc (title ) .. " ]" ..
30
+ " textarea[0.5,1.5;7.5,7;;" ..
31
+ esc (string ~= " " and string or text ) .. " ;]" ..
32
+ " button[2.4,7.6;0.8,0.8;book_prev;<]" ..
33
+ " label[3.2,7.7;" .. esc (S (" Page @1 of @2" , page , page_max )) .. " ]" ..
34
+ " button[4.9,7.6;0.8,0.8;book_next;>]"
35
+ end
36
+
37
+ local function formspec_string (lpp , page , lines , string )
38
+ for i = ((lpp * page ) - lpp ) + 1 , lpp * page do
39
+ if not lines [i ] then break end
40
+ string = string .. lines [i ] .. " \n "
41
+ end
42
+ return string
43
+ end
44
+
45
+ local tab_number
6
46
local lpp = 14 -- Lines per book's page
7
47
local function book_on_use (itemstack , user )
8
48
local player_name = user :get_player_name ()
@@ -19,8 +59,8 @@ local function book_on_use(itemstack, user)
19
59
local data = meta :to_table ().fields
20
60
21
61
if data .owner then
22
- title = data .title
23
- text = data .text
62
+ title = data .title or " "
63
+ text = data .text or " "
24
64
owner = data .owner
25
65
26
66
for str in (text .. " \n " ):gmatch (" ([^\n ]*)[\n ]" ) do
@@ -30,37 +70,26 @@ local function book_on_use(itemstack, user)
30
70
if data .page then
31
71
page = data .page
32
72
page_max = data .page_max
33
-
34
- for i = ((lpp * page ) - lpp ) + 1 , lpp * page do
35
- if not lines [i ] then break end
36
- string = string .. lines [i ] .. " \n "
37
- end
73
+ string = formspec_string (lpp , page , lines , string )
38
74
end
39
75
end
40
76
41
77
local formspec
42
- local esc = minetest .formspec_escape
43
- if owner == player_name then
44
- formspec = " size[8,8]" ..
45
- " field[0.5,1;7.5,0;title;" .. esc (S (" Title:" )) .. " ;" ..
46
- esc (title ) .. " ]" ..
47
- " textarea[0.5,1.5;7.5,7;text;" .. esc (S (" Contents:" )) .. " ;" ..
48
- esc (text ) .. " ]" ..
49
- " button_exit[2.5,7.5;3,1;save;" .. esc (S (" Save" )) .. " ]"
78
+ if title == " " and text == " " then
79
+ formspec = formspec_write (title , text )
80
+ elseif owner == player_name then
81
+ local tab = tab_number or 1
82
+ if tab == 2 then
83
+ formspec = formspec_core (tab ) ..
84
+ formspec_read (owner , title , string , text , page , page_max )
85
+ else
86
+ formspec = formspec_core (tab ) .. formspec_write (title , text )
87
+ end
50
88
else
51
- formspec = " size[8,8]" ..
52
- " label[0.5,0.5;" .. esc (S (" by @1" , owner )) .. " ]" ..
53
- " tablecolumns[color;text]" ..
54
- " tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
55
- " table[0.4,0;7,0.5;title;#FFFF00," .. esc (title ) .. " ]" ..
56
- " textarea[0.5,1.5;7.5,7;;" ..
57
- minetest .formspec_escape (string ~= " " and string or text ) .. " ;]" ..
58
- " button[2.4,7.6;0.8,0.8;book_prev;<]" ..
59
- " label[3.2,7.7;" .. esc (S (" Page @1 of @2" , page , page_max )) .. " ]" ..
60
- " button[4.9,7.6;0.8,0.8;book_next;>]"
89
+ formspec = formspec_read (owner , title , string , text , page , page_max )
61
90
end
62
91
63
- minetest .show_formspec (player_name , " default:book" , formspec )
92
+ minetest .show_formspec (player_name , " default:book" , formspec_size .. formspec )
64
93
return itemstack
65
94
end
66
95
@@ -69,12 +98,37 @@ local max_title_size = 80
69
98
local short_title_size = 35
70
99
minetest .register_on_player_receive_fields (function (player , formname , fields )
71
100
if formname ~= " default:book" then return end
101
+ local player_name = player :get_player_name ()
72
102
local inv = player :get_inventory ()
73
103
local stack = player :get_wielded_item ()
104
+ local data = stack :get_meta ():to_table ().fields
105
+
106
+ local title = data .title or " "
107
+ local text = data .text or " "
108
+
109
+ if fields .book_header ~= nil and data .owner == player_name then
110
+ local contents
111
+ local tab = tonumber (fields .book_header )
112
+ if tab == 1 then
113
+ contents = formspec_core (tab ) ..
114
+ formspec_write (title , text )
115
+ elseif tab == 2 then
116
+ local lines , string = {}, " "
117
+ for str in (text .. " \n " ):gmatch (" ([^\n ]*)[\n ]" ) do
118
+ lines [# lines + 1 ] = str
119
+ end
120
+ string = formspec_string (lpp , data .page , lines , string )
121
+ contents = formspec_read (player_name , title , string ,
122
+ text , data .page , data .page_max )
123
+ end
124
+ tab_number = tab
125
+ local formspec = formspec_size .. formspec_core (tab ) .. contents
126
+ minetest .show_formspec (player_name , " default:book" , formspec )
127
+ return
128
+ end
74
129
75
- if fields .save and fields .title and fields .text
76
- and fields .title ~= " " and fields .text ~= " " then
77
- local new_stack , data
130
+ if fields .save and fields .title and fields .text then
131
+ local new_stack
78
132
if stack :get_name () ~= " default:book_written" then
79
133
local count = stack :get_count ()
80
134
if count == 1 then
@@ -83,11 +137,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
83
137
stack :set_count (count - 1 )
84
138
new_stack = ItemStack (" default:book_written" )
85
139
end
86
- else
87
- data = stack :get_meta ():to_table ().fields
88
140
end
89
141
90
- if data and data . owner and data . owner ~= player : get_player_name () then
142
+ if data . owner ~= player_name and title ~= " " and text ~= " " then
91
143
return
92
144
end
93
145
@@ -117,8 +169,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
117
169
end
118
170
119
171
elseif fields .book_next or fields .book_prev then
120
- local data = stack :get_meta ():to_table ().fields
121
- if not data or not data .page then
172
+ if not data .page then
122
173
return
123
174
end
124
175
0 commit comments