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