Bugfix and code cleanup
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 23 Nov 2017 12:38:00 +0000 (13:38 +0100)
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 23 Nov 2017 12:38:00 +0000 (13:38 +0100)
init.lua

index da01e2b..7db558d 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,9 +53,8 @@ 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
@@ -83,7 +89,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 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
@@ -95,14 +101,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
@@ -111,7 +117,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
@@ -152,7 +158,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]=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
@@ -169,14 +175,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