From c29f71095c5f8a0073e7302a3e6610d236f01557 Mon Sep 17 00:00:00 2001 From: flux <25628292+fluxionary@users.noreply.github.com> Date: Tue, 2 Aug 2022 08:11:08 -0700 Subject: [PATCH] allow transferring ownership of petz --- README.md | 8 ++++++++ api/purchase_mechanics.lua | 15 +++++++++++++++ compat/currency.lua | 2 ++ compat/init.lua | 4 ++++ compat/petz.lua | 35 +++++++++++++++++++++++++++++++++++ init.lua | 1 + mod.conf | 2 +- 7 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 compat/petz.lua diff --git a/README.md b/README.md index 2b775da..3b231a4 100644 --- a/README.md +++ b/README.md @@ -139,3 +139,11 @@ callback = function(player, shop, i) -- called when the shop sells out of something end ``` +* `smartshop.api.register_transaction_transform(callback)` +```lua +callback = function(player, shop, i, shop_removed, player_removed) + -- sometimes, it is necessary to alter the items in an exchange + -- e.g. changing the owner of a petz "pet" + return shop_removed, player_removed +end +``` diff --git a/api/purchase_mechanics.lua b/api/purchase_mechanics.lua index 76dfeb0..95b3d91 100644 --- a/api/purchase_mechanics.lua +++ b/api/purchase_mechanics.lua @@ -10,6 +10,7 @@ api.registered_purchase_mechanics = {} api.registered_on_purchases = {} api.registered_on_shop_fulls = {} api.registered_on_shop_emptys = {} +api.registered_transaction_transforms = {} --[[ TODO: mechanic definition isn't set in stone currently, see below @@ -49,6 +50,17 @@ function api.on_shop_empty(player, shop, i) end end +function api.register_transaction_transform(callback) + table.insert(api.registered_transaction_transforms, callback) +end + +function api.do_transaction_transforms(player, shop, i, shop_removed, player_removed) + for _, callback in ipairs(api.registered_transaction_transforms) do + shop_removed, player_removed = callback(player, shop, i, shop_removed, player_removed) + end + return shop_removed, player_removed +end + function api.try_purchase(player, shop, i) for _, def in ipairs(api.registered_purchase_mechanics) do if def.allow_purchase(player, shop, i) then @@ -126,6 +138,9 @@ api.register_purchase_mechanic({ local shop_removed = shop:remove_item(give_stack, "give") local player_removed = player_inv:remove_item(pay_stack) + + shop_removed, player_removed = api.do_transaction_transforms(player, shop, i, shop_removed, player_removed) + local player_remaining = player_inv:add_item(shop_removed) local shop_remaining = shop:add_item(player_removed, "pay") diff --git a/compat/currency.lua b/compat/currency.lua index 1f7d47d..c3991a7 100644 --- a/compat/currency.lua +++ b/compat/currency.lua @@ -341,6 +341,8 @@ api.register_purchase_mechanic({ shop_removed = shop:remove_item(give_stack, "give") end + shop_removed, player_removed = api.do_transaction_transforms(shop_removed, player_removed) + if is_currency(pay_stack) then shop_remaining = currency.add_item(shop, player_removed, "pay") else diff --git a/compat/init.lua b/compat/init.lua index 44baa9f..81eb7c0 100644 --- a/compat/init.lua +++ b/compat/init.lua @@ -14,6 +14,10 @@ if smartshop.has.mesecons_mvps then smartshop.dofile("compat", "mesecons_mvps") end +if smartshop.has.petz then + smartshop.dofile("compat", "petz") +end + if smartshop.has.pipeworks then smartshop.dofile("compat", "pipeworks") end diff --git a/compat/petz.lua b/compat/petz.lua new file mode 100644 index 0000000..54565d9 --- /dev/null +++ b/compat/petz.lua @@ -0,0 +1,35 @@ +-- petz has its own mechanism for "selling" petz; this allows transferring ownership of animals put in shops + +local function set_owner(petstack, new_owner_name) + local meta = petstack:get_meta() + local serialized_data = meta:get("staticdata") + if not serialized_data then + return + end + + local data = minetest.deserialize(serialized_data) + + if not (data and data.memory) then + return + end + + if (data.memory.owner or "") ~= "" then + data.memory.owner = new_owner_name + meta:set_string("staticdata", minetest.serialize(data)) + end +end + +local function is_petz(itemstack) + local name = itemstack:get_name() + return name:match("^petz:.*_set$") +end + +smartshop.api.register_transaction_transform(function(player, shop, i, shop_removed, player_removed) + if is_petz(shop_removed) then + set_owner(shop_removed, player:get_player_name()) + end + if is_petz(player_removed) then + set_owner(player_removed, shop:get_owner()) + end + return shop_removed, player_removed +end) diff --git a/init.lua b/init.lua index 1b491ca..8ea83b5 100644 --- a/init.lua +++ b/init.lua @@ -33,6 +33,7 @@ smartshop = { default = minetest.get_modpath("default"), mesecons = minetest.get_modpath("mesecons"), mesecons_mvps = minetest.get_modpath("mesecons_mvps"), + petz = minetest.get_modpath("petz"), pipeworks = minetest.get_modpath("pipeworks"), tubelib = minetest.get_modpath("tubelib"), }, diff --git a/mod.conf b/mod.conf index c1f7055..a242dd6 100644 --- a/mod.conf +++ b/mod.conf @@ -1,3 +1,3 @@ name = smartshop description = A smarter and easier shop -optional_depends = currency, default, mesecons, mesecons_mvps, pipeworks, tubelib +optional_depends = currency, default, mesecons, mesecons_mvps, petz, pipeworks, tubelib