]> git.lizzy.rs Git - skycraft.git/blob - src/onload/shop_signs.lua
Fixed Shop Signs
[skycraft.git] / src / onload / shop_signs.lua
1 for nodename, nodedef in pairs(minetest.registered_nodes) do
2         if nodename:find("mcl_signs:") then
3                 minetest.override_item(nodename, {
4                         on_rightclick = function(pos, node, player, itemstack, pointed_thing)
5                                 if pos.y < 5000 then return end
6                                 local text = minetest.get_meta(pos):get_string("text") or ""
7                                 local lines = text:split("\n")
8                                 local action, amount, price = lines[1], lines[2], lines[3]
9                                 if not (action and amount and price) then return end
10                                 price = string.gsub(price, "%$", "")
11                                 price = tonumber(price)
12                                 amount = string.gsub(amount, "x", "")
13                                 amount = tonumber(amount)
14                                 if not (amount and price) then return end
15                                 local func, frameoffset
16                                 if action == "Buy" then
17                                         func, frameoffset = skycraft.buy, -1
18                                 elseif action == "Sell" then
19                                         func, frameoffset = skycraft.sell, 1
20                                 else
21                                         return
22                                 end
23                                 local framepos = vector.add(pos, {x = 0, y = frameoffset, z = 0})
24                                 if minetest.get_node(framepos).name ~= "mcl_itemframes:item_frame" then return end
25                                 local inv = minetest.get_meta(framepos):get_inventory()
26                                 if inv:is_empty("main") then return end
27                                 local itemstack = inv:get_stack("main", 1)
28                                 func(player, itemstack:get_name() .. " " .. tostring(amount), price)
29                                 return player:get_wielded_item()
30                         end,
31                 })
32         end
33 end
34