-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildbook.lua
175 lines (153 loc) · 3.54 KB
/
buildbook.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
local bbook = {}
local playerpage = {}
local playerlastsearch = {}
local thebook = "build_battle:book"
local function drill_image(item)
if type(item) == "string" then
return item
else
if item.name then
return item.name
end
end
return nil
end
local function generate_form(player,searchterm)
local playername = player:get_player_name()
if not searchterm then
searchterm = ""
playerpage[playername] = 1
else
if searchterm ~= playerlastsearch[playername] then
playerpage[playername] = 1
end
end
playerlastsearch[playername] = searchterm
-- results display size
local width = 12
local height = 7
local pagesize = width*height
formspeccer:clear(thebook)
formspeccer:newform(thebook,"12,10")
formspeccer:add_field(thebook,{
xy = "3,1",
wh = "5,1",
name = "searchterm",
label = "Search:",
value = searchterm,
})
formspeccer:add_button(thebook,{
xy = "8,0.65",
wh = "2,1",
name = "label",
label = "Search"
},
true)
formspeccer:add_button(thebook,{
xy = "5,9",
wh = "2,1",
name = "exit",
value = "OK",
label = "Quit",
},
true) -- is exit button
local searchresult = {}
if searchterm ~= "" then
searchresult = bbattle:searchitem(searchterm)
end
if #searchresult > 0 then -- TODO - make multi-page results
local skip = ( playerpage[playername] - 1 ) * pagesize
local reachedend = false
for i=0+skip,skip+pagesize-1 do -- build out buttons
idx = i+1
if searchresult[idx] then
local x = i % width
local y = math.floor((i - i % width) / width ) % height +2
formspeccer:add_item_button(thebook,{
xy = x..","..y,
wh = "1,1",
name = searchresult[idx],
label = "",
item_name = searchresult[idx],
})
else
reachedend = true
end
end
formspeccer:add_label(thebook,{
xy = "7,9",
wh = "2,1",
name = "label",
value = "Page "..tostring(playerpage[playername]),
label = "Page "..tostring(playerpage[playername]),
})
if playerpage[playername] > 1 then
formspeccer:add_button(thebook,{
xy = "1,9",
wh = "2,1",
name = "prev",
value = "<",
label = " << ",
},true)
end
if not reachedend then
formspeccer:add_button(thebook,{
xy = "3,9",
wh = "2,1",
name = "next",
value = ">",
label = " >> ",
},true)
end
else
formspeccer:add_label(thebook,{
xy = "1,3",
wh = "7,1",
name = "label",
value = "Nothing to display",
label = "Nothing to display",
})
end
minetest.after(0,function()
formspeccer:show(player,thebook)
end)
end
minetest.register_on_player_receive_fields(function(player,formname,fields)
local playername = player:get_player_name()
if formname ~= thebook then
return
end
if fields.quit and fields.searchterm and not fields.exit then
if fields.prev then
if playerpage[playername] > 1 then
playerpage[playername] = playerpage[playername] - 1
end
end
if fields.next then
playerpage[playername] = playerpage[playername] + 1
end
generate_form(player, fields.searchterm)
else
local giveitem = nil
for key,value in pairs(fields) do
if key:find("build_battle:") then
giveitem = key
break
end
end
if giveitem then
minetest.debug("Giving "..giveitem)
minetest.chat_send_player(playername, "Giving "..giveitem)
bbattle:giveplayer(playername, {name=giveitem, count = 99})
end
end
return true
end)
minetest.register_craftitem("build_battle:book",{
on_use = function(itemstack, player, pointed_thing)
generate_form(player)
end,
description = "Build Battle Book",
inventory_image = "build_battle_book.png",
stack_max = 1,
})