From 0af5559015fef211857865889e40958975e401c7 Mon Sep 17 00:00:00 2001 From: Juraj Vajda Date: Fri, 2 Nov 2018 19:04:00 -0400 Subject: [PATCH] added /mp buyinv command --- README.md | 1 + chatcommands.lua | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/README.md b/README.md index 57e237f..2424780 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,6 @@ Commands: - `/mp infohand`, show more information about the item(s) you are currently holding in hand from the store - `/mp buy []`, buy `` of `` from store, if `` is not provided then amount is 1 - `/mp sellinv `, sell all items `` from the 'main' inventory list +- `/mp buyinv `, buy full inventory of items ``, empty slots in the 'main' inventory are required - `/mp top`, show top 5 richest players currently online - `/mp help`, print out this help diff --git a/chatcommands.lua b/chatcommands.lua index d8adaf9..3562c9b 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -175,6 +175,58 @@ minetest.register_chatcommand("mp", { return false, "You cannot sell this item. Search in store for items you can sell, example: /mp find stone. See some suggestion from the store: "..x_marketplace.store_get_random() end + -- + -- buy inventory + -- + elseif params[1] == "buyinv" then + -- item name is missing from param[2] + if not params[2] then + return false, "You need to write the item name you want to buy. example: /mp buyinv default:stone, or check out other items in the store: "..x_marketplace.store_get_random() + end + + local player = minetest.get_player_by_name(caller) + local inv = player:get_inventory("main") + local store_item = x_marketplace.store_list[params[2]] + local amount = 0 + local itemstack = ItemStack({ + name = params[2] + }) + + itemstack:set_count(itemstack:get_stack_max()) + + if store_item then + + for k, v in ipairs(inv:get_list("main")) do + if v:get_name() == "" and v:get_count() == 0 then + amount = amount + itemstack:get_count() + end + end + + if amount == 0 then + return false, "You don't have empty space in your inventory. Transaction cancelled." + end + + local buy_price = amount * store_item.buy + local new_balance = x_marketplace.set_player_balance(caller, buy_price * -1) + + -- not enough money + if not new_balance then + return false, "You don't have enought BitGold. Price for "..amount.." item(s) of "..params[2].." is "..buy_price.." BitGold, but your current balance is: "..x_marketplace.get_player_balance(caller).." BitGold." + end + + for k, v in ipairs(inv:get_list("main")) do + if v:get_name() == "" and v:get_count() == 0 then + inv:add_item("main", itemstack) + end + end + + return true, "You bought "..amount.." item(s) of "..params[2].." for "..buy_price.." BitGold. Your new balance is: "..new_balance.." BitGold" + + else + -- item not in store + return false, "This item is not in store, check out other items from the store: "..x_marketplace.store_get_random() + end + -- -- buy -- @@ -290,6 +342,7 @@ minetest.register_chatcommand("mp", { minetest.colorize("#00FFFF", "/mp infohand")..", show more information about the item(s) you are currently holding in hand from the store\n".. minetest.colorize("#00FFFF", "/mp buy").." [], buy of from store, if is not provided then amount is 1\n".. minetest.colorize("#00FFFF", "/mp sellinv").." , sell all items from the 'main' inventory list\n".. + minetest.colorize("#00FFFF", "/mp buyinv").." , buy full inventory of items , empty slots in the 'main' inventory are required\n".. minetest.colorize("#00FFFF", "/mp top")..", show top 5 richest players currently online\n".. minetest.colorize("#00FFFF", "/mp help")..", print out this help\n"