Skip to content

Commit

Permalink
allow transferring ownership of petz
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxionary committed Aug 2, 2022
1 parent 58fbd0b commit c29f710
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -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
```
15 changes: 15 additions & 0 deletions api/purchase_mechanics.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
2 changes: 2 additions & 0 deletions compat/currency.lua
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions compat/init.lua
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions 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)
1 change: 1 addition & 0 deletions init.lua
Expand Up @@ -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"),
},
Expand Down
2 changes: 1 addition & 1 deletion 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

0 comments on commit c29f710

Please sign in to comment.