diff --git a/api.lua b/api.lua index fc08fb1..2208d28 100644 --- a/api.lua +++ b/api.lua @@ -61,7 +61,11 @@ function x_marketplace.set_player_balance(name, amount) local new_balance = balance + amount if new_balance < 0 then - return false + return false, "below" + end + + if new_balance > x_marketplace.max_balance then + return false, "above" end player:set_attribute("balance", new_balance) diff --git a/chatcommands.lua b/chatcommands.lua index 47ace23..30d8193 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -104,7 +104,11 @@ minetest.register_chatcommand("mp", { player:set_wielded_item(ItemStack("")) local sell_price = store_item.sell * item_count - local new_balance = x_marketplace.set_player_balance(caller, sell_price) + local new_balance, msg = x_marketplace.set_player_balance(caller, sell_price) + + if msg == "above" then + return false, minetest.colorize(colors.yellow, "MARKET PLACE: You will go above the maximum balance if you sell this item. Transaction cancelled.") + end return true, minetest.colorize(colors.green, "MARKET PLACE: You sold "..item_count.." item(s) of "..item_name.." for "..sell_price.." BitGold. Your new balance is: "..new_balance.." BitGold") else @@ -215,23 +219,33 @@ minetest.register_chatcommand("mp", { if store_item then local inv = player:get_inventory("main") local itemstack = ItemStack(params[2]) + local balance = x_marketplace.get_player_balance(caller) + local new_balance = 0 local amount = 0 for k, v in ipairs(inv:get_list("main")) do if v:get_name() == params[2] then - local amount_removed = v:get_count() - inv:remove_item("main", v) + local amount_to_remove = v:get_count() - if amount_removed > itemstack:get_stack_max() then - amount_removed = itemstack:get_stack_max() + if amount_to_remove > itemstack:get_stack_max() then + amount_to_remove = itemstack:get_stack_max() end - amount = amount + amount_removed + amount = amount + amount_to_remove + + if balance + amount * store_item.sell > x_marketplace.max_balance then + amount = amount - amount_to_remove + minetest.chat_send_player(caller, minetest.colorize(colors.yellow, "MARKET PLACE: We couldn't buy all your items without going above your maximum balance.")) + break + else + new_balance = balance + amount * store_item.sell + inv:remove_item("main", v) + end end end local sell_price = amount * store_item.sell - local new_balance = x_marketplace.set_player_balance(caller, sell_price) + new_balance, msg = x_marketplace.set_player_balance(caller, sell_price) return true, minetest.colorize(colors.green, "MARKET PLACE: You sold "..amount.." item(s) of "..params[2].." for "..sell_price.." BitGold. Your new balance is: "..new_balance.." BitGold") else diff --git a/init.lua b/init.lua index 98b77db..28816b6 100644 --- a/init.lua +++ b/init.lua @@ -2,6 +2,7 @@ -- @module x_marketplace -- @author SaKeL x_marketplace = {} +x_marketplace.max_balance = 1000000 -- one million local path = minetest.get_modpath("x_marketplace") diff --git a/store_list.lua b/store_list.lua index a80c3cb..87427e9 100644 --- a/store_list.lua +++ b/store_list.lua @@ -144,10 +144,10 @@ x_marketplace.store_list = { -- buy = 0.00, -- sell = 0.00 -- }, - ["default:diamondblock"] = { - buy = 16200.00, - sell = 1.00 - }, + -- ["default:diamondblock"] = { + -- buy = 16200.00, + -- sell = 1.00 + -- }, ["default:dirt"] = { buy = 1.00, sell = 0.25 @@ -264,10 +264,10 @@ x_marketplace.store_list = { buy = 4.00, sell = 1.00 }, - ["default:mese"] = { - buy = 16200.00, - sell = 1.00 - }, + -- ["default:mese"] = { + -- buy = 16200.00, + -- sell = 1.00 + -- }, ["default:mese_post_light"] = { buy = 128.00, sell = 0.25 @@ -380,10 +380,10 @@ x_marketplace.store_list = { buy = 0.80, sell = 0.20 }, - ["default:steelblock"] = { - buy = 2430.00, - sell = 1 - }, + -- ["default:steelblock"] = { + -- buy = 2430.00, + -- sell = 1 + -- }, ["default:stone"] = { buy = 2.00, sell = 0.50