]> git.lizzy.rs Git - skycraft.git/blob - money.lua
2 Bugfixes
[skycraft.git] / money.lua
1 function skycraft.get_money(player)
2         return player:get_meta():get_int("skycraft:money")
3 end
4
5 function skycraft.set_money(player, value)
6         player:get_meta():set_int("skycraft:money", value)
7 end
8
9 function skycraft.take_money(player, amount)
10         local name = player:get_player_name()
11         local money = skycraft.get_money(player)
12         if amount > money then 
13                 return false, minetest.chat_send_player(name, "You don't have enough money!")
14         end
15         skycraft.set_money(player, money - amount)
16         minetest.chat_send_player(name, minetest.colorize("#009EFF", "$" .. tostring(amount)) .. " taken from your account.")
17         return true
18 end
19
20 function skycraft.give_money(player, amount)
21         skycraft.set_money(player, skycraft.get_money(player) + amount)
22         minetest.chat_send_player(player:get_player_name(), minetest.colorize("#009EFF", "$" .. tostring(amount)) .. " added to your account.")
23 end
24
25 function skycraft.sell(player, item, money)
26         local inv = player:get_inventory()
27         if not inv:contains_item("main", item) then return minetest.chat_send_player(player:get_player_name(), "You don't have enough items!") end
28         inv:remove_item("main", item)
29         skycraft.give_money(player, money)
30 end
31
32 function skycraft.buy(player, item, money)
33         local inv = player:get_inventory()
34         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
35         if not skycraft.take_money(player, money) then return end
36         inv:add_item("main", item)
37 end
38
39 local money_chatcommand_def = {
40         description = "Show your balance",
41         func = function(name)
42                 local player = minetest.get_player_by_name(name)
43                 if not player then return false, "You need to be online to use this command" end
44                 return true, "You have " .. minetest.colorize("#009EFF", "$" .. tostring(skycraft.get_money(player))) .. "."
45         end
46 }
47
48 minetest.register_chatcommand("money", money_chatcommand_def)
49
50 minetest.register_chatcommand("balance", money_chatcommand_def)
51
52 minetest.register_on_newplayer(function(player)
53         skycraft.give_money(player, 200)
54 end)
55
56 minetest.register_on_mods_loaded(function()
57         for nodename, nodedef in pairs(minetest.registered_nodes) do
58                 if nodename:find("mcl_signs:") then
59                         minetest.override_item(nodename, {
60                                 on_rightclick = function(pos, node, player, itemstack, pointed_thing)
61                                         if pos.y < 5000 then return end
62                                         local text = minetest.get_meta(pos):get_string("text") or ""
63                                         local lines = text:split("\n")
64                                         local action, amount, price = lines[1], lines[2], lines[3]
65                                         print(action, amount, price)
66                                         if not (action and amount and price) then return end
67                                         price = string.gsub(price, "%$", "")
68                                         price = tonumber(price)
69                                         amount = string.gsub(amount, "x", "")
70                                         amount = tonumber(amount)
71                                         print(action, amount, price)
72                                         if not (amount and price) then return end
73                                         local func, frameoffset
74                                         if action == "Buy" then
75                                                 func, frameoffset = skycraft.buy, -1
76                                         elseif action == "Sell" then
77                                                 func, frameoffset = skycraft.sell, 1
78                                         else
79                                                 return
80                                         end
81                                         local framepos = vector.add(pos, {x = 0, y = frameoffset, z = 0})
82                                         if minetest.get_node(framepos).name ~= "mcl_itemframes:item_frame" then return end
83                                         local inv = minetest.get_meta(framepos):get_inventory()
84                                         if inv:is_empty("main") then return end
85                                         local itemstack = inv:get_stack("main", 1)
86                                         func(player, itemstack:get_name() .. " " .. tostring(amount), price)
87                                 end,
88                         })
89                 end
90         end
91 end)