smartshop={user={},tmp={},add_storage={},max_wifi_distance=30, mesecon=minetest.get_modpath("mesecons")~=nil, 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={ {{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}}, {{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}}, {{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}}, {{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}}} } minetest.register_craft({ output = "smartshop:shop", recipe = { {"default:chest_locked", "default:chest_locked", "default:chest_locked"}, {"default:sign_wall_wood", "default:chest_locked", "default:sign_wall_wood"}, {"default:sign_wall_wood", "default:torch", "default:sign_wall_wood"}, } }) minetest.register_craft({ output = "smartshop:wifistorage", recipe = { {"default:mese_crystal_fragment", "default:chest_locked", "default:mese_crystal_fragment"}, {"default:mese_crystal_fragment", "default:chest_locked", "default:mese_crystal_fragment"}, {"default:steel_ingot", "default:copper_ingot", "default:steel_ingot"}, } }) smartshop.strpos=function(str,spl) if str==nil then return "" end if spl then local c="," if string.find(str," ") then c=" " end local s=str.split(str,c) if s[3]==nil then return nil else local p={x=tonumber(s[1]),y=tonumber(s[2]),z=tonumber(s[3])} if not (p and p.x and p.y and p.z) then return nil end return p end else if str and str.x and str.y and str.z then return str.x .."," .. str.y .."," .. str.z else return nil end end end smartshop.send_mesecon=function(pos) if smartshop.mesecon then mesecon.receptor_on(pos) minetest.get_node_timer(pos):start(1) end end smartshop.use_offer=function(pos,player,n) local pressed={} pressed["buy" .. n]=true smartshop.user[player:get_player_name()]=pos smartshop.receive_fields(player,pressed) smartshop.user[player:get_player_name()]=nil smartshop.update(pos) end smartshop.get_offer=function(pos) if not pos or not minetest.get_node(pos) then return end if minetest.get_node(pos).name~="smartshop:shop" then return end local meta=minetest.get_meta(pos) local inv=meta:get_inventory() local offer={} for i=1,4,1 do offer[i]={ give=inv:get_stack("give" .. i,1):get_name(), give_count=inv:get_stack("give" .. i,1):get_count(), pay=inv:get_stack("pay" .. i,1):get_name(), pay_count=inv:get_stack("pay" .. i,1):get_count(), } end return offer end smartshop.receive_fields=function(player,pressed) local pname=player:get_player_name() local pos=smartshop.user[pname] if not pos then return elseif pressed.tsend then smartshop.add_storage[pname]={send=true,pos=pos} minetest.after(30, function(pname) if smartshop.add_storage[pname] then minetest.chat_send_player(pname, "Time expired (30s)") smartshop.add_storage[pname]=nil end end, pname) minetest.chat_send_player(pname, "Open a storage owned by you") return elseif pressed.trefill then smartshop.add_storage[pname]={refill=true,pos=pos} minetest.after(30, function(pname) if smartshop.add_storage[pname] then minetest.chat_send_player(pname, "Time expired (30s)") smartshop.add_storage[pname]=nil end end, pname) minetest.chat_send_player(pname, "Open a storage owned by you") return elseif pressed.customer then return smartshop.showform(pos,player,true) elseif pressed.sellall then local meta=minetest.get_meta(pos) local pname=player:get_player_name() if meta:get_int("sellall")==0 then meta:set_int("sellall",1) minetest.chat_send_player(pname, "Sell your stock and give line") else meta:set_int("sellall",0) minetest.chat_send_player(pname, "Sell your stock only") end elseif pressed.toogleee then local meta=minetest.get_meta(pos) local pname=player:get_player_name() if meta:get_int("type")==0 then meta:set_int("type",1) minetest.chat_send_player(pname, "Your stock is limited") else meta:set_int("type",0) minetest.chat_send_player(pname, "Your stock is unlimited") end elseif not pressed.quit then local n=1 for i=1,4,1 do n=i if pressed["buy" .. i] then break end end local meta=minetest.get_meta(pos) local type=meta:get_int("type") local sellall=meta:get_int("sellall") local inv=meta:get_inventory() local pinv=player:get_inventory() local pname=player:get_player_name() local check_storage if pressed["buy" .. n] then local name=inv:get_stack("give" .. n,1):get_name() local stack=name .." ".. inv:get_stack("give" .. n,1):get_count() local pay=inv:get_stack("pay" .. n,1):get_name() .." ".. inv:get_stack("pay" .. n,1):get_count() local stack_to_use="main" if name~="" then --fast checks if not pinv:room_for_item("main", stack) then minetest.chat_send_player(pname, "Error: Your inventory is full, exchange aborted.") return elseif 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 elseif 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.") else if inv:contains_item("main", stack) then elseif sellall==1 and inv:contains_item("give" .. n, stack) then stack_to_use="give" .. n else minetest.chat_send_player(pname, "Error: The owners stock is end.") check_storage=1 end if not check_storage then for i=0,32,1 do if pinv:get_stack("main", i):get_name()==inv:get_stack("pay" .. n,1):get_name() and pinv:get_stack("main",i):get_wear()>0 then minetest.chat_send_player(pname, "Error: your item is used") return end end local rastack=inv:remove_item(stack_to_use, stack) pinv:remove_item("main", pay) pinv:add_item("main",rastack) if type==1 then inv:add_item("main",pay) end if type==0 then inv:add_item("main", rastack) end end end -- send to / refill from wifi storage if type==1 then local tsend=smartshop.strpos(meta:get_string("item_send"),1) local trefill=smartshop.strpos(meta:get_string("item_refill"),1) if tsend then local m=minetest.get_meta(tsend) local inv2=m:get_inventory() local mes=m:get_int("mesein") for i=1,10,1 do if inv2:room_for_item("main", pay) and inv:contains_item("main", pay) then inv2:add_item("main",pay) inv:remove_item("main", pay) if mes==1 or mes==3 then smartshop.send_mesecon(tsend) end else break end end end if trefill then local m=minetest.get_meta(trefill) local inv2=m:get_inventory() local mes=m:get_int("mesein") local space=0 --check if its room for other items, else the shop will stuck for i=1,32,1 do if inv:get_stack("main",i):get_count()==0 then space=space+1 end end for i=1,space,1 do if ismartshop.max_wifi_distance then minetest.chat_send_player(uname, "Too far, max distance " .. smartshop.max_wifi_distance) end local meta=minetest.get_meta(a.pos) local p=smartshop.strpos(pos) if a.send and p then meta:set_string("item_send",p) elseif a.refill and p then meta:set_string("item_refill",p) end minetest.chat_send_player(uname, "smartshop connected") smartshop.add_storage[uname]=nil end end minetest.register_node("smartshop:wifistorage", { description = "Wifi storage", tiles = {"default_chest_top.png^[colorize:#ffffff77^default_obsidian_glass.png"}, groups = {choppy = 2, oddly_breakable_by_hand = 1,tubedevice = 1, tubedevice_receiver = 1,mesecon=2}, paramtype = "light", sunlight_propagates = true, light_source = 10, on_timer = function (pos, elapsed) if smartshop.mesecon then mesecon.receptor_off(pos) end return false end, tube = {insert_object = function(pos, node, stack, direction) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() local added = inv:add_item("main", stack) return added end, can_insert = function(pos, node, stack, direction) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() return inv:room_for_item("main", stack) end, input_inventory = "main", connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}}, after_place_node = function(pos, placer) local meta=minetest.get_meta(pos) local name=placer:get_player_name() meta:set_string("owner",name) meta:set_string("infotext", "Wifi storage by: " .. name) end, on_construct = function(pos) local meta=minetest.get_meta(pos) meta:get_inventory():set_size("main", 60) meta:set_int("mesein",0) meta:set_string("title","wifi" .. math.random(1,999)) end, on_rightclick = function(pos, node, player, itemstack, pointed_thing) smartshop.showform2(pos,player) end, allow_metadata_inventory_put = function(pos, listname, index, stack, player) if stack:get_wear()==0 and (minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true})) then return stack:get_count() end return 0 end, allow_metadata_inventory_take = function(pos, listname, index, stack, player) 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 return stack:get_count() end return 0 end, can_dig = function(pos, player) local meta=minetest.get_meta(pos) local inv=meta:get_inventory() local p=player:get_player_name() if (meta:get_string("owner")==p or minetest.check_player_privs(p, {protection_bypass=true})) and inv:is_empty("main") or meta:get_string("owner")=="" then return true end end, })