Fixed crash when smartshop position unknown
[smartshop.git] / init.lua
index 5415438..ce6cce0 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -13,6 +13,13 @@ minetest.register_craft({
                {"default:sign_wall_wood", "default:torch", "default:sign_wall_wood"},\r
        }\r
 })\r
+smartshop.get_human_name = function(item)\r
+   if core.registered_items[item] then\r
+      return core.registered_items[item].description\r
+   else\r
+      return "Unknown Item"\r
+   end\r
+end\r
 \r
 smartshop.use_offer=function(pos,player,n)\r
        local pressed={}\r
@@ -46,26 +53,14 @@ smartshop.send_mail=function(owner, pos, item)
    if not minetest.get_modpath( "mail" ) then\r
       return\r
    end\r
-   item = ItemStack(item)\r
    local spos = "("..pos.x..", "..pos.y..", "..pos.z..")"\r
-   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")\r
+   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")\r
 end\r
 \r
 \r
 smartshop.receive_fields=function(player,pressed)\r
                if pressed.customer then\r
                        return smartshop.showform(smartshop.user[player:get_player_name()],player,true)\r
-               elseif pressed.sellall then\r
-                       local pos=smartshop.user[player:get_player_name()]\r
-                       local meta=minetest.get_meta(pos)\r
-                       local pname=player:get_player_name()\r
-                       if meta:get_int("sellall")==0 then\r
-                               meta:set_int("sellall",1)\r
-                               minetest.chat_send_player(pname, "Sell your stock and give line")\r
-                       else\r
-                               meta:set_int("sellall",0)\r
-                               minetest.chat_send_player(pname, "Sell your stock only")\r
-                       end\r
                elseif pressed.tooglelime then\r
                        local pos=smartshop.user[player:get_player_name()]\r
                        local meta=minetest.get_meta(pos)\r
@@ -84,9 +79,11 @@ smartshop.receive_fields=function(player,pressed)
                                if pressed["buy" .. i] then break end\r
                        end\r
                        local pos=smartshop.user[player:get_player_name()]\r
+                       if not pos then\r
+                          return\r
+                       end\r
                        local meta=minetest.get_meta(pos)\r
                        local type=meta:get_int("type")\r
-                       local sellall=meta:get_int("sellall")\r
                        local inv=meta:get_inventory()\r
                        local pinv=player:get_inventory()\r
                        local pname=player:get_player_name()\r
@@ -95,11 +92,7 @@ smartshop.receive_fields=function(player,pressed)
                                local stack=name .." ".. inv:get_stack("give" .. n,1):get_count()\r
                                local pay=inv:get_stack("pay" .. n,1):get_name() .." ".. inv:get_stack("pay" .. n,1):get_count()\r
                                if name~="" then\r
-                                       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\r
-                                       -- if type==1 and sellall==1 and inv:contains_item("main", stack)==false and inv:contains_item("give" .. n, stack)==true then\r
-                                       --      inv:add_item("main", stack)\r
-                                       --      inv:remove_item("give" .. n, stack)\r
-                                       -- end\r
+                                       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\r
                                        if meta:get_int("ghost") ~=1 then\r
                                           -- transition shops to ghost inventory.\r
                                           for i=1,4 do\r
@@ -111,14 +104,14 @@ smartshop.receive_fields=function(player,pressed)
                                           end\r
                                        end\r
                                        if type==1 and inv:contains_item("main", stack)==false then\r
-                                          minetest.chat_send_player(pname, "Error: The owners stock is end.")\r
+                                          minetest.chat_send_player(pname, "Error: "..smartshop.get_human_name(name).." is sold out.")\r
                                           if not meta:get_int("alerted") or meta:get_int("alerted") == 0 then\r
                                              meta:set_int("alerted",1) -- Do not alert twice\r
-                                             smartshop.send_mail(meta:get_string("owner"), pos, stack)\r
+                                             smartshop.send_mail(meta:get_string("owner"), pos, name)\r
                                           end\r
                                           return\r
                                        end\r
-                                       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\r
+                                       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\r
                                        if not pinv:room_for_item("main", stack) then minetest.chat_send_player(pname, "Error: Your inventory is full, exchange aborted.") return end\r
                                        pinv:remove_item("main", pay)\r
                                        pinv:add_item("main", stack)\r
@@ -127,7 +120,7 @@ smartshop.receive_fields=function(player,pressed)
                                                inv:add_item("main", pay)\r
                                                if not inv:contains_item("main", stack)  and (not meta:get_int("alerted") or meta:get_int("alerted") == 0) then\r
                                                   meta:set_int("alerted",1) -- Do not alert twice\r
-                                                  smartshop.send_mail(meta:get_string("owner"), pos, stack)\r
+                                                  smartshop.send_mail(meta:get_string("owner"), pos, name)\r
                                                end\r
                                        end\r
                                end\r
@@ -155,11 +148,12 @@ end)
 \r
 \r
 smartshop.update_info=function(pos)\r
+        if not pos then\r
+          return\r
+        end\r
        local meta=minetest.get_meta(pos)\r
        local inv = meta:get_inventory()\r
        local owner=meta:get_string("owner")\r
-       local gve=0\r
---     if meta:get_int("sellall")==1 then gve=1 end\r
        if meta:get_int("type")==0 then\r
                meta:set_string("infotext","(Smartshop by " .. owner ..") Stock is unlimited")\r
                return false\r
@@ -170,7 +164,7 @@ smartshop.update_info=function(pos)
        for i=1,4,1 do\r
                stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()\r
                stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()\r
-               stuff["stock" ..i]=gve*stuff["count" ..i]\r
+               stuff["stock" ..i]=0 -- stuff["count" ..i]\r
                stuff["buy" ..i]=0\r
                for ii=1,32,1 do\r
                        name=inv:get_stack("main",ii):get_name()\r
@@ -187,14 +181,8 @@ smartshop.update_info=function(pos)
                        stuff["buy" ..i]=""\r
                        stuff["name" ..i]=""\r
                else\r
-                       --if string.find(stuff["name" ..i],":")~=nil then\r
-                       --      stuff["name" ..i]=stuff["name" ..i].split(stuff["name" ..i],":")[2]\r
-                  --end\r
-                  if core.registered_items[stuff["name"..i]] then\r
-                     stuff["name"..i] = core.registered_items[stuff["name"..i]].description\r
-                  else\r
-                     stuff["name"..i] = "Unknown Object"\r
-                  end\r
+\r
+                  stuff["name"..i] = smartshop.get_human_name(stuff["name"..i])\r
                   stuff["buy" ..i]="(" ..stuff["buy" ..i] ..") "\r
                   stuff["name" ..i]=stuff["name" ..i] .."\n"\r
                end\r
@@ -295,7 +283,6 @@ smartshop.showform=function(pos,player,re)
                gui=""\r
                .."size[8,10]"\r
                .."button_exit[6,0;1.5,1;customer;Customer]"\r
-               .."button[7.2,0;1,1;sellall;All]"\r
                .."label[0,0.2;Item:]"\r
                .."label[0,1.2;Price:]"\r
                .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"\r
@@ -371,11 +358,9 @@ after_place_node = function(pos, placer)
                meta:set_string("owner",placer:get_player_name())\r
                meta:set_string("infotext", "Shop by: " .. placer:get_player_name())\r
                meta:set_int("type",1)\r
-               meta:set_int("sellall",1)\r
                if minetest.check_player_privs(placer:get_player_name(), {creative=true}) or minetest.check_player_privs(placer:get_player_name(), {give=true}) then\r
                        meta:set_int("creative",1)\r
                        meta:set_int("type",0)\r
-                       meta:set_int("sellall",0)\r
                end\r
        end,\r
 on_construct = function(pos)\r
@@ -390,7 +375,7 @@ on_construct = function(pos)
                meta:get_inventory():set_size("pay3", 1)\r
                meta:get_inventory():set_size("give4", 1)\r
                meta:get_inventory():set_size("pay4", 1)\r
-               meta:set_int("ghost", 0)\r
+               meta:set_int("ghost", 1)\r
        end,\r
 on_rightclick = function(pos, node, player, itemstack, pointed_thing)\r
                smartshop.showform(pos,player)\r
@@ -398,9 +383,14 @@ on_rightclick = function(pos, node, player, itemstack, pointed_thing)
 allow_metadata_inventory_put = function(pos, listname, index, stack, player)\r
    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\r
       local meta = minetest.get_meta(pos)\r
-      if meta:get_int("ghost") and (string.find(listname, "pay") or string.find(listname, "give")) then\r
+      if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then\r
         local inv = minetest.get_inventory({type="node", pos=pos})\r
-        inv:set_stack(listname, index, stack)\r
+--      minetest.chat_send_all( inv:get_stack(listname, index):get_name()..stack:get_name())\r
+        if inv:get_stack(listname, index):get_name() == stack:get_name() then\r
+           inv:add_item(listname, stack)\r
+        else\r
+           inv:set_stack(listname, index, stack)\r
+        end\r
         return 0\r
       end \r
       return stack:get_count()\r
@@ -430,8 +420,12 @@ allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to
         inv:set_stack(from_list, from_index, ItemStack(""))\r
         return 0        \r
       elseif (string.find(to_list, "pay") or string.find(to_list, "give")) and from_list == "main" then\r
-        inv:set_stack(to_list, to_index, inv:get_stack(from_list, from_index))\r
-        inv:set_stack(from_list, from_index, inv:get_stack(from_list, from_index))\r
+        if inv:get_stack(to_list, to_index):get_name() == inv:get_stack(from_list, from_index):get_name() then\r
+           inv:add_item(to_list, inv:get_stack(from_list, from_index))\r
+        else\r
+           inv:set_stack(to_list, to_index, inv:get_stack(from_list, from_index))\r
+           inv:set_stack(from_list, from_index, inv:get_stack(from_list, from_index))      \r
+        end\r
         return 0\r
       else\r
         return count\r