From 20b0bdeb6b5e5fde188f1db833ee98bf3c9f4783 Mon Sep 17 00:00:00 2001 From: James David Clarke Date: Sun, 14 Jan 2024 23:48:51 +0000 Subject: [PATCH] Can now change the balance of players while they are offline, and emeraldbank.add_emeralds also takes a string of the playername to select the player. --- commands.lua | 24 +++++++++---------- functions.lua | 66 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/commands.lua b/commands.lua index 1a4e36b..2685641 100644 --- a/commands.lua +++ b/commands.lua @@ -71,26 +71,26 @@ core.register_chatcommand("emeralds", { privs = {server=true}, func = function(name, param) local playername, stringnum = param:match("([^ ]+) (.+)") - local player local num = tonumber(stringnum) if playername and num then - player = core.get_player_by_name(playername) - end - if player and num then - emeraldbank.add_emeralds(player, num) - atm.read_account(playername) - minetest.chat_send_player(name, S("@1 has now @2 emeralds in bank account", - playername, atm.balance[playername])) - return true + local success = emeraldbank.add_emeralds(playername, num) + if success then + atm.read_account(playername) + minetest.chat_send_player(name, S("@1 has now @2 emeralds in bank account", + playername, atm.balance[playername])) + return true + else + minetest.chat_send_player(name, S("Player @1 not found or not online.", playername)) + return false + end else - -- Notify the command issuer that the player is not found or not online - minetest.chat_send_player(name, S("Player @1 not found or not online.", playername)) + -- Notify the command issuer that the input is invalid + minetest.chat_send_player(name, S("Invalid input. Please specify a player and a number.")) return false end end }) - -- experimental upgrade command core.register_chatcommand("upgrade", { description = S("Admin Command! Upgrade a shop"), diff --git a/functions.lua b/functions.lua index 15dccdb..997c84f 100644 --- a/functions.lua +++ b/functions.lua @@ -26,22 +26,36 @@ function emeraldbank.get_emeralds(name) return atm.balance[name] end +-- Accepts a player object or player name as input now function emeraldbank.add_emeralds(player, num) - if not player then return false end - local meta = player:get_meta() - local name = player:get_player_name() - atm.read_account(name) - if num then - if atm.balance[name] then - atm.balance[name] = math.floor(atm.balance[name] + num) - else - atm.balance[name] = num - end - mcl_title.set(player, "actionbar", {text=S("Emeralds in Bank: @1", atm.balance[name]), color="yellow"}) - atm.save_account(name) - return true - end - return false + if not player then return false end + + local name + if type(player) == "string" then + name = player + else + name = player:get_player_name() + end + + atm.read_account(name) + + -- Check if atm.balance[name] exists + if atm.balance[name] == nil then + return false + end + + if num then + atm.balance[name] = math.floor(atm.balance[name] + num) + + -- Update actionbar only if 'player' is a player object + if type(player) ~= "string" then + mcl_title.set(player, "actionbar", {text=S("Emeralds in Bank: @1", atm.balance[name]), color="yellow"}) + end + + atm.save_account(name) + return true + end + return false end function emeraldbank.update_accounts() @@ -139,39 +153,39 @@ function emeraldbank.upgrade_shop(pos) local old_inv = old_meta:get_inventory() local old_list = old_inv:get_list("stock") local old_stack = old_inv:get_stack("stock", 1) - + -- set the new shop node core.swap_node(pos, {name = "fancy_vend:player_vendor"}) - + -- setup the new shop node -- Set variables for access later (for various checks, etc.) - + pos.y = pos.y + 1 -- local above_node = minetest.get_node(pos).name - + -- -- If node above is air or the display node, and it is not protected, attempt to place the vendor. If vendor sucessfully places, place display node above, otherwise alert the user -- if (minetest.registered_nodes[above_node].buildable_to or above_node == "fancy_vend:display_node") and not minetest.is_protected(pos, owner) then -- if above_node ~= "fancy_vend:display_node" then -- minetest.set_node(pos, minetest.registered_nodes["fancy_vend:display_node"]) -- end - + -- Set owner local owner = old_meta:get_string("owner") or "" local meta = minetest.get_meta(pos) meta:set_string("owner", owner) - + -- Set default meta meta:set_string("log", minetest.serialize({"Vendor placed by "..owner,})) emeraldbank.reset_vendor_settings(pos) emeraldbank.refresh_vendor(pos) - + if minetest.get_modpath("pipeworks") then pipeworks.after_place(pos) end - + -- copy old metadata in new node core.get_meta(pos):from_table(old_meta_table) - + -- new node local node = core.get_node(pos) local meta = core.get_meta(pos) @@ -192,8 +206,8 @@ function emeraldbank.upgrade_shop(pos) -- inv:set_size("wanted_item", 1*1) -- inv:set_size("given_item", 1*1) -- inv:set_stack("main", i, old_stack) - - + + emeraldbank.set_vendor_settings(pos, settings) emeraldbank.refresh_vendor(pos) end