Smartshop now uses ghost items for stuff being sold
[smartshop.git] / init.lua
index 8eb1348..9f518e8 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -96,9 +96,17 @@ smartshop.receive_fields=function(player,pressed)
                                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
+                                       -- 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 meta:get_int("ghost") ~=1 then\r
+                                          -- transition shops to ghost inventory.\r
+                                          if inv:room_for_item("main", pay) and inv:room_for_item("main", stack) then\r
+                                             meta:set_int("ghost", 1)\r
+                                             inv:add_item("main", pay)\r
+                                             inv:add_item("main", stack)\r
+                                          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
@@ -115,6 +123,10 @@ smartshop.receive_fields=function(player,pressed)
                                        if type==1 then \r
                                                inv:remove_item("main", stack)\r
                                                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
+                                               end\r
                                        end\r
                                end\r
                        end\r
@@ -376,28 +388,55 @@ 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", 1)\r
        end,\r
 on_rightclick = function(pos, node, player, itemstack, pointed_thing)\r
                smartshop.showform(pos,player)\r
        end,\r
 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
-               return stack:get_count()\r
-               end\r
-               return 0\r
-       end,\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
+        local inv = minetest.get_inventory({type="node", pos=pos})\r
+        inv:set_stack(listname, index, stack)\r
+        return 0\r
+      end \r
+      return stack:get_count()\r
+   end\r
+   return 0\r
+end,\r
 allow_metadata_inventory_take = 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
-               return stack:get_count()\r
-               end\r
-               return 0\r
-       end,\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") == 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, ItemStack(""))\r
+        return 0\r
+      end       \r
+      return stack:get_count()\r
+   end\r
+   return 0\r
+end,\r
 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, 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
-               return count\r
-               end\r
-               return 0\r
-       end,\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
+      local inv = minetest.get_inventory({type="node", pos=pos})\r
+      if meta:get_int("ghost") ~= 1 then\r
+        return count\r
+      end\r
+      if (string.find(from_list, "pay") or string.find(from_list, "give")) and to_list == "main" then\r
+        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
+        return 0\r
+      else\r
+        return count\r
+      end\r
+   end\r
+   return 0\r
+end,\r
 can_dig = function(pos, player)\r
                local meta=minetest.get_meta(pos)\r
                local inv=meta:get_inventory()\r