]> git.lizzy.rs Git - skycraft.git/blob - src/onload/shop.lua
New file Structure
[skycraft.git] / src / onload / shop.lua
1 function skycraft.sell(player, item, money)
2         local inv = player:get_inventory()
3         if not inv:contains_item("main", item) then return minetest.chat_send_player(player:get_player_name(), "You don't have enough items!") end
4         inv:remove_item("main", item)
5         skycraft.give_money(player, money)
6 end
7
8 function skycraft.buy(player, item, money)
9         local inv = player:get_inventory()
10         if not inv:room_for_item("main", item) then return minetest.chat_send_player(player:get_player_name(), "You don't have enough space in your inventory!") end
11         if not skycraft.take_money(player, money) then return end
12         inv:add_item("main", item)
13 end
14
15 for nodename, nodedef in pairs(minetest.registered_nodes) do
16         if nodename:find("mcl_signs:") then
17                 minetest.override_item(nodename, {
18                         on_rightclick = function(pos, node, player, itemstack, pointed_thing)
19                                 if pos.y < 5000 then return end
20                                 local text = minetest.get_meta(pos):get_string("text") or ""
21                                 local lines = text:split("\n")
22                                 local action, amount, price = lines[1], lines[2], lines[3]
23                                 print(action, amount, price)
24                                 if not (action and amount and price) then return end
25                                 price = string.gsub(price, "%$", "")
26                                 price = tonumber(price)
27                                 amount = string.gsub(amount, "x", "")
28                                 amount = tonumber(amount)
29                                 print(action, amount, price)
30                                 if not (amount and price) then return end
31                                 local func, frameoffset
32                                 if action == "Buy" then
33                                         func, frameoffset = skycraft.buy, -1
34                                 elseif action == "Sell" then
35                                         func, frameoffset = skycraft.sell, 1
36                                 else
37                                         return
38                                 end
39                                 local framepos = vector.add(pos, {x = 0, y = frameoffset, z = 0})
40                                 if minetest.get_node(framepos).name ~= "mcl_itemframes:item_frame" then return end
41                                 local inv = minetest.get_meta(framepos):get_inventory()
42                                 if inv:is_empty("main") then return end
43                                 local itemstack = inv:get_stack("main", 1)
44                                 func(player, itemstack:get_name() .. " " .. tostring(amount), price)
45                         end,
46                 })
47         end
48 end
49