Fixed crash when smartshop position unknown
[smartshop.git] / init.lua
1 smartshop={user={},tmp={},dir={{x=0,y=0,z=-1},{x=-1,y=0,z=0},{x=0,y=0,z=1},{x=1,y=0,z=0}},dpos={
2 {{x=0.2,y=0.2,z=0},{x=-0.2,y=0.2,z=0},{x=0.2,y=-0.2,z=0},{x=-0.2,y=-0.2,z=0}},
3 {{x=0,y=0.2,z=0.2},{x=0,y=0.2,z=-0.2},{x=0,y=-0.2,z=0.2},{x=0,y=-0.2,z=-0.2}},
4 {{x=-0.2,y=0.2,z=0},{x=0.2,y=0.2,z=0},{x=-0.2,y=-0.2,z=0},{x=0.2,y=-0.2,z=0}},
5 {{x=0,y=0.2,z=-0.2},{x=0,y=0.2,z=0.2},{x=0,y=-0.2,z=-0.2},{x=0,y=-0.2,z=0.2}}}
6 }
7
8 minetest.register_craft({
9 output = "smartshop:shop",
10 recipe = {
11 {"default:chest_locked", "default:chest_locked", "default:chest_locked"},
12 {"default:sign_wall_wood", "default:chest_locked", "default:sign_wall_wood"},
13 {"default:sign_wall_wood", "default:torch", "default:sign_wall_wood"},
14 }
15 })
16 smartshop.get_human_name = function(item)
17 if core.registered_items[item] then
18 return core.registered_items[item].description
19 else
20 return "Unknown Item"
21 end
22 end
23
24 smartshop.use_offer=function(pos,player,n)
25 local pressed={}
26 pressed["buy" .. n]=true
27 smartshop.user[player:get_player_name()]=pos
28 smartshop.receive_fields(player,pressed)
29 smartshop.user[player:get_player_name()]=nil
30 smartshop.update(pos)
31
32
33 end
34
35 smartshop.get_offer=function(pos)
36 if not pos or not minetest.get_node(pos) then return end
37 if minetest.get_node(pos).name~="smartshop:shop" then return end
38 local meta=minetest.get_meta(pos)
39 local inv=meta:get_inventory()
40 local offer={}
41 for i=1,4,1 do
42 offer[i]={
43 give=inv:get_stack("give" .. i,1):get_name(),
44 give_count=inv:get_stack("give" .. i,1):get_count(),
45 pay=inv:get_stack("pay" .. i,1):get_name(),
46 pay_count=inv:get_stack("pay" .. i,1):get_count(),
47 }
48 end
49 return offer
50 end
51
52 smartshop.send_mail=function(owner, pos, item)
53 if not minetest.get_modpath( "mail" ) then
54 return
55 end
56 local spos = "("..pos.x..", "..pos.y..", "..pos.z..")"
57 mail.send("DO NOT REPLY", owner, "Out of "..smartshop.get_human_name(item).." at "..spos, "Your smartshop at "..spos.." is out of "..smartshop.get_human_name(item)..". Please restock")
58 end
59
60
61 smartshop.receive_fields=function(player,pressed)
62 if pressed.customer then
63 return smartshop.showform(smartshop.user[player:get_player_name()],player,true)
64 elseif pressed.tooglelime then
65 local pos=smartshop.user[player:get_player_name()]
66 local meta=minetest.get_meta(pos)
67 local pname=player:get_player_name()
68 if meta:get_int("type")==0 then
69 meta:set_int("type",1)
70 minetest.chat_send_player(pname, "Your stock is limited")
71 else
72 meta:set_int("type",0)
73 minetest.chat_send_player(pname, "Your stock is unlimited")
74 end
75 elseif not pressed.quit then
76 local n=1
77 for i=1,4,1 do
78 n=i
79 if pressed["buy" .. i] then break end
80 end
81 local pos=smartshop.user[player:get_player_name()]
82 if not pos then
83 return
84 end
85 local meta=minetest.get_meta(pos)
86 local type=meta:get_int("type")
87 local inv=meta:get_inventory()
88 local pinv=player:get_inventory()
89 local pname=player:get_player_name()
90 if pressed["buy" .. n] then
91 local name=inv:get_stack("give" .. n,1):get_name()
92 local stack=name .." ".. inv:get_stack("give" .. n,1):get_count()
93 local pay=inv:get_stack("pay" .. n,1):get_name() .." ".. inv:get_stack("pay" .. n,1):get_count()
94 if name~="" then
95 if type==1 and inv:room_for_item("main", pay)==false then minetest.chat_send_player(pname, "Error: The owner's stock is full, can't receive, exchange aborted.") return end
96 if meta:get_int("ghost") ~=1 then
97 -- transition shops to ghost inventory.
98 for i=1,4 do
99 if inv:room_for_item("main", "pay"..i) and inv:room_for_item("main", "give"..i) then
100 meta:set_int("ghost", 1)
101 inv:add_item("main", inv:get_stack("pay"..i,1))
102 inv:add_item("main", inv:get_stack("give"..i,1))
103 end
104 end
105 end
106 if type==1 and inv:contains_item("main", stack)==false then
107 minetest.chat_send_player(pname, "Error: "..smartshop.get_human_name(name).." is sold out.")
108 if not meta:get_int("alerted") or meta:get_int("alerted") == 0 then
109 meta:set_int("alerted",1) -- Do not alert twice
110 smartshop.send_mail(meta:get_string("owner"), pos, name)
111 end
112 return
113 end
114 if not pinv:contains_item("main", pay) then minetest.chat_send_player(pname, "Error: You don't have enough in your inventory to buy this, exchange aborted.") return end
115 if not pinv:room_for_item("main", stack) then minetest.chat_send_player(pname, "Error: Your inventory is full, exchange aborted.") return end
116 pinv:remove_item("main", pay)
117 pinv:add_item("main", stack)
118 if type==1 then
119 inv:remove_item("main", stack)
120 inv:add_item("main", pay)
121 if not inv:contains_item("main", stack) and (not meta:get_int("alerted") or meta:get_int("alerted") == 0) then
122 meta:set_int("alerted",1) -- Do not alert twice
123 smartshop.send_mail(meta:get_string("owner"), pos, name)
124 end
125 end
126 end
127 end
128 else
129 local pos=smartshop.user[player:get_player_name()]
130 smartshop.update_info(pos)
131 if smartshop.user[player:get_player_name()] or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
132 local meta=minetest.get_meta(smartshop.user[player:get_player_name()])
133 if meta:get_string("owner")==player:get_player_name() then
134 smartshop.update(smartshop.user[player:get_player_name()],"update")
135 end
136 end
137 smartshop.user[player:get_player_name()]=nil
138 end
139 end
140
141 minetest.register_on_player_receive_fields(function(player, form, pressed)
142 if form=="smartshop.showform" then
143 smartshop.receive_fields(player,pressed)
144 end
145 end)
146
147
148
149
150 smartshop.update_info=function(pos)
151 if not pos then
152 return
153 end
154 local meta=minetest.get_meta(pos)
155 local inv = meta:get_inventory()
156 local owner=meta:get_string("owner")
157 if meta:get_int("type")==0 then
158 meta:set_string("infotext","(Smartshop by " .. owner ..") Stock is unlimited")
159 return false
160 end
161 local name=""
162 local count=0
163 local stuff={}
164 for i=1,4,1 do
165 stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()
166 stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()
167 stuff["stock" ..i]=0 -- stuff["count" ..i]
168 stuff["buy" ..i]=0
169 for ii=1,32,1 do
170 name=inv:get_stack("main",ii):get_name()
171 count=inv:get_stack("main",ii):get_count()
172 if name==stuff["name" ..i] then
173 stuff["stock" ..i]=stuff["stock" ..i]+count
174 end
175 end
176 local nstr=(stuff["stock" ..i]/stuff["count" ..i]) ..""
177 nstr=nstr.split(nstr, ".")
178 stuff["buy" ..i]=tonumber(nstr[1])
179
180 if stuff["name" ..i]=="" or stuff["buy" ..i]==0 then
181 stuff["buy" ..i]=""
182 stuff["name" ..i]=""
183 else
184
185 stuff["name"..i] = smartshop.get_human_name(stuff["name"..i])
186 stuff["buy" ..i]="(" ..stuff["buy" ..i] ..") "
187 stuff["name" ..i]=stuff["name" ..i] .."\n"
188 end
189 end
190 meta:set_string("infotext",
191 "(Smartshop by " .. owner ..") Purchases left:\n"
192 .. stuff.buy1 .. stuff.name1
193 .. stuff.buy2 .. stuff.name2
194 .. stuff.buy3 .. stuff.name3
195 .. stuff.buy4 .. stuff.name4
196 )
197 end
198
199
200
201
202 smartshop.update=function(pos,stat)
203 --clear
204 local spos=minetest.pos_to_string(pos)
205 for _, ob in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do
206 if ob and ob:get_luaentity() and ob:get_luaentity().smartshop and ob:get_luaentity().pos==spos then
207 ob:remove()
208 end
209 end
210 if stat=="clear" then return end
211 --update
212 local meta=minetest.get_meta(pos)
213 local inv = meta:get_inventory()
214 local node=minetest.get_node(pos)
215 local dp = smartshop.dir[node.param2+1]
216 if not dp then return end
217 pos.x = pos.x + dp.x*0.01
218 pos.y = pos.y + dp.y*6.5/16
219 pos.z = pos.z + dp.z*0.01
220 for i=1,4,1 do
221 local item=inv:get_stack("give" .. i,1):get_name()
222 local pos2=smartshop.dpos[node.param2+1][i]
223 if item~="" then
224 smartshop.tmp.item=item
225 smartshop.tmp.pos=spos
226 local e = minetest.env:add_entity({x=pos.x+pos2.x,y=pos.y+pos2.y,z=pos.z+pos2.z},"smartshop:item")
227 e:setyaw(math.pi*2 - node.param2 * math.pi/2)
228 end
229 end
230 end
231
232
233 minetest.register_entity("smartshop:item",{
234 hp_max = 1,
235 visual="wielditem",
236 visual_size={x=.20,y=.20},
237 collisionbox = {0,0,0,0,0,0},
238 physical=false,
239 textures={"air"},
240 smartshop=true,
241 type="",
242 on_activate = function(self, staticdata)
243 if smartshop.tmp.item ~= nil then
244 self.item=smartshop.tmp.item
245 self.pos=smartshop.tmp.pos
246 smartshop.tmp={}
247 else
248 if staticdata ~= nil and staticdata ~= "" then
249 local data = staticdata:split(';')
250 if data and data[1] and data[2] then
251 self.item = data[1]
252 self.pos = data[2]
253 end
254 end
255 end
256 if self.item ~= nil then
257 self.object:set_properties({textures={self.item}})
258 else
259 self.object:remove()
260 end
261 end,
262 get_staticdata = function(self)
263 if self.item ~= nil and self.pos ~= nil then
264 return self.item .. ';' .. self.pos
265 end
266 return ""
267 end,
268 })
269
270
271 smartshop.showform=function(pos,player,re)
272 local meta=minetest.get_meta(pos)
273 local creative=meta:get_int("creative")
274 local inv = meta:get_inventory()
275 local gui=""
276 local spos=pos.x .. "," .. pos.y .. "," .. pos.z
277 local owner=meta:get_string("owner")==player:get_player_name()
278 if minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then owner=true end
279 if re then owner=false end
280 smartshop.user[player:get_player_name()]=pos
281 if owner then
282 meta:set_int("alerted",0) -- Player has been there to refill
283 gui=""
284 .."size[8,10]"
285 .."button_exit[6,0;1.5,1;customer;Customer]"
286 .."label[0,0.2;Item:]"
287 .."label[0,1.2;Price:]"
288 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
289 .."list[nodemeta:" .. spos .. ";pay1;2,1;1,1;]"
290 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
291 .."list[nodemeta:" .. spos .. ";pay2;3,1;1,1;]"
292 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
293 .."list[nodemeta:" .. spos .. ";pay3;4,1;1,1;]"
294 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
295 .."list[nodemeta:" .. spos .. ";pay4;5,1;1,1;]"
296 if creative==1 then
297 gui=gui .."label[0.5,-0.4;Your stock is unlimited becaouse you have creative or give]"
298 .."button[6,1;2.2,1;tooglelime;Toggle limit]"
299 end
300 gui=gui
301 .."list[nodemeta:" .. spos .. ";main;0,2;8,4;]"
302 .."list[current_player;main;0,6.2;8,4;]"
303 .."listring[nodemeta:" .. spos .. ";main]"
304 .."listring[current_player;main]"
305 else
306 gui=""
307 .."size[8,6]"
308 .."list[current_player;main;0,2.2;8,4;]"
309 .."label[0,0.2;Item:]"
310 .."label[0,1.2;Price:]"
311 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
312 .."item_image_button[2,1;1,1;".. inv:get_stack("pay1",1):get_name() ..";buy1;\n\n\b\b\b\b\b" .. inv:get_stack("pay1",1):get_count() .."]"
313 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
314 .."item_image_button[3,1;1,1;".. inv:get_stack("pay2",1):get_name() ..";buy2;\n\n\b\b\b\b\b" .. inv:get_stack("pay2",1):get_count() .."]"
315 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
316 .."item_image_button[4,1;1,1;".. inv:get_stack("pay3",1):get_name() ..";buy3;\n\n\b\b\b\b\b" .. inv:get_stack("pay3",1):get_count() .."]"
317 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
318 .."item_image_button[5,1;1,1;".. inv:get_stack("pay4",1):get_name() ..";buy4;\n\n\b\b\b\b\b" .. inv:get_stack("pay4",1):get_count() .."]"
319 end
320 minetest.after((0.1), function(gui)
321 return minetest.show_formspec(player:get_player_name(), "smartshop.showform",gui)
322 end, gui)
323 end
324
325 minetest.register_node("smartshop:shop", {
326 description = "Smartshop",
327 tiles = {"default_chest_top.png^[colorize:#ffffff77^default_obsidian_glass.png"},
328 groups = {choppy = 2, oddly_breakable_by_hand = 1,tubedevice = 1, tubedevice_receiver = 1},
329 drawtype="nodebox",
330 node_box = {type="fixed",fixed={-0.5,-0.5,-0.0,0.5,0.5,0.5}},
331 paramtype2="facedir",
332 paramtype = "light",
333 sunlight_propagates = true,
334 light_source = 10,
335 tube = {insert_object = function(pos, node, stack, direction)
336 local meta = minetest.get_meta(pos)
337 local inv = meta:get_inventory()
338 local added = inv:add_item("main", stack)
339 return added
340 end,
341 can_insert = function(pos, node, stack, direction)
342 local meta = minetest.get_meta(pos)
343 local inv = meta:get_inventory()
344 for i=1,4 do
345 local sellitem = inv:get_stack("give"..i,1):get_name()
346 if sellitem == stack:get_name() then
347 return inv:room_for_item("main", stack)
348 end
349 -- minetest.chat_send_all(sellitem)
350 end
351 --
352 return false
353 end,
354 input_inventory = "main",
355 connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
356 after_place_node = function(pos, placer)
357 local meta=minetest.get_meta(pos)
358 meta:set_string("owner",placer:get_player_name())
359 meta:set_string("infotext", "Shop by: " .. placer:get_player_name())
360 meta:set_int("type",1)
361 if minetest.check_player_privs(placer:get_player_name(), {creative=true}) or minetest.check_player_privs(placer:get_player_name(), {give=true}) then
362 meta:set_int("creative",1)
363 meta:set_int("type",0)
364 end
365 end,
366 on_construct = function(pos)
367 local meta=minetest.get_meta(pos)
368 meta:set_int("state", 0)
369 meta:get_inventory():set_size("main", 32)
370 meta:get_inventory():set_size("give1", 1)
371 meta:get_inventory():set_size("pay1", 1)
372 meta:get_inventory():set_size("give2", 1)
373 meta:get_inventory():set_size("pay2", 1)
374 meta:get_inventory():set_size("give3", 1)
375 meta:get_inventory():set_size("pay3", 1)
376 meta:get_inventory():set_size("give4", 1)
377 meta:get_inventory():set_size("pay4", 1)
378 meta:set_int("ghost", 1)
379 end,
380 on_rightclick = function(pos, node, player, itemstack, pointed_thing)
381 smartshop.showform(pos,player)
382 end,
383 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
384 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
385 local meta = minetest.get_meta(pos)
386 if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then
387 local inv = minetest.get_inventory({type="node", pos=pos})
388 -- minetest.chat_send_all( inv:get_stack(listname, index):get_name()..stack:get_name())
389 if inv:get_stack(listname, index):get_name() == stack:get_name() then
390 inv:add_item(listname, stack)
391 else
392 inv:set_stack(listname, index, stack)
393 end
394 return 0
395 end
396 return stack:get_count()
397 end
398 return 0
399 end,
400 allow_metadata_inventory_take = function(pos, listname, index, stack, player)
401 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
402 local meta = minetest.get_meta(pos)
403 if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then
404 local inv = minetest.get_inventory({type="node", pos=pos})
405 inv:set_stack(listname, index, ItemStack(""))
406 return 0
407 end
408 return stack:get_count()
409 end
410 return 0
411 end,
412 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
413 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
414 local meta = minetest.get_meta(pos)
415 local inv = minetest.get_inventory({type="node", pos=pos})
416 if meta:get_int("ghost") ~= 1 then
417 return count
418 end
419 if (string.find(from_list, "pay") or string.find(from_list, "give")) and to_list == "main" then
420 inv:set_stack(from_list, from_index, ItemStack(""))
421 return 0
422 elseif (string.find(to_list, "pay") or string.find(to_list, "give")) and from_list == "main" then
423 if inv:get_stack(to_list, to_index):get_name() == inv:get_stack(from_list, from_index):get_name() then
424 inv:add_item(to_list, inv:get_stack(from_list, from_index))
425 else
426 inv:set_stack(to_list, to_index, inv:get_stack(from_list, from_index))
427 inv:set_stack(from_list, from_index, inv:get_stack(from_list, from_index))
428 end
429 return 0
430 else
431 return count
432 end
433 end
434 return 0
435 end,
436 can_dig = function(pos, player)
437 local meta=minetest.get_meta(pos)
438 local inv=meta:get_inventory()
439 if ((meta:get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true})) and inv:is_empty("main") and inv:is_empty("pay1") and inv:is_empty("pay2") and inv:is_empty("pay3") and inv:is_empty("pay4") and inv:is_empty("give1") and inv:is_empty("give2") and inv:is_empty("give3") and inv:is_empty("give4")) or meta:get_string("owner")=="" then
440 smartshop.update(pos,"clear")
441 return true
442 end
443 end,
444 })