Cleaned up code, made raising prices easier
[smartshop.git] / init.lua
index dd71375..da01e2b 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -55,17 +55,6 @@ end
 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
@@ -86,7 +75,6 @@ smartshop.receive_fields=function(player,pressed)
                        local pos=smartshop.user[player:get_player_name()]\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
@@ -96,10 +84,6 @@ 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
-                                       -- end\r
                                        if meta:get_int("ghost") ~=1 then\r
                                           -- transition shops to ghost inventory.\r
                                           for i=1,4 do\r
@@ -158,8 +142,6 @@ smartshop.update_info=function(pos)
        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 +152,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]=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
@@ -295,7 +277,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 +352,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
@@ -398,9 +377,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 +414,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