reward_inv/reward_inv.lua
2023-12-27 08:38:48 +01:00

140 lines
5.1 KiB
Lua

-- functions for handling the detached reward inventory of players
-- There are two modi: In edit mode, with
-- reward_inv.player_is_editing[pname]
-- beeing true, the detached inventory is used to set up new reward inventories.
-- If the above value is false, the inventory is used to hand out one copy
-- each of the stored reward inventory to each player.
--
-- The reward inv the player is currently accessing is stored in:
-- reward_inv.player_using[pname]
-- Called when a player wants to put something into the inventory.
-- Return value: number of items allowed to put.
-- Return value -1: Allow and don't modify item count in inventory.
reward_inv.reward_inv_allow_put = function(inv, listname, index, stack, player)
if(not(player)) then
return 0
end
local pname = player:get_player_name()
if(pname and reward_inv.player_is_editing[pname]) then
-- in edit mode: allow to add items but don't substract them from the
-- player's inventory -> doesn't work; so just allow it
return stack:get_count()
end
-- if not in edit mode: no adding of things allowed
return 0
end
-- Called when a player wants to take something out of the inventory.
-- Return value: number of items allowed to take.
-- Return value -1: Allow and don't modify item count in inventory.
reward_inv.reward_inv_allow_take = function(inv, listname, index, stack, player)
if(not(player)) then
return
end
local pname = player:get_player_name()
if(pname and reward_inv.player_is_editing[pname]) then
-- in edit mode: allow to take items but don't add to the player's
-- inventory (players in edit mode have 'give' priv anyway)
-- -> doesn't work; do it manually
return stack:get_count()
end
-- no need to check if this reward inv has been unlocked by that player
-- - because the reward inv gets filled with what ought to be available;
-- unless something went wrong and no reward has been selected
if(not(reward_inv.player_using[pname])) then
return 0
end
-- taking is allowed - after all this is what the reward inv is for
return stack:get_count()
end
reward_inv.reward_inv_on_take = function(inv, listname, index, stack, player)
if(not(player)) then
return
end
local pname = player:get_player_name()
if(pname and reward_inv.player_is_editing[pname]) then
-- taking something in edit mode is just editing - we store that later
return
end
-- store that the player took that reward
reward_inv.store_reward_taken(inv, listname, index, stack, player)
end
-- create a detached reward inventory for the player
-- usually, such functions are called in minetest.register_on_joinplayer;
-- but since a player may not encounter a reward inventory during most
-- of his/her playtime, it's more efficient to create it just when needed;
-- returns the reward inv
reward_inv.reward_inv_create = function(player)
if(not(player)) then
return
end
local pname = player:get_player_name()
local inv_name = reward_inv.reward_inv_get_name(pname)
-- has the inventory already been created? Then don't create it anew
local inv = minetest.get_inventory({type="detached", name=inv_name})
if(inv) then
-- return the existing one
return inv
end
local inv = minetest.create_detached_inventory(inv_name, {
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
-- moving items around inside the reward inventory is not allowed
-- (not even when setting it up)
return 0
end,
allow_put = function(inv, listname, index, stack, player)
-- generally forbidden (unless in edit mode)
return reward_inv.reward_inv_allow_put(inv, listname, index, stack, player)
end,
-- Called when a player wants to take something out of the inventory.
-- Return value: number of items allowed to take.
-- Return value -1: Allow and don't modify item count in inventory.
allow_take = function(inv, listname, index, stack, player)
-- generally allowed (that's what this is for after all)
return reward_inv.reward_inv_allow_take(inv, listname, index, stack, player)
end,
-- on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
-- -- no special action - moving is forbidden
-- end,
-- on_put = function(inv, listname, index, stack, player)
-- -- no special action - adding items only happens in edit mode, and
-- -- that only gets stored when clicking on the 'Store' button
-- end,
on_take = function(inv, listname, index, stack, player)
-- store that the player took that reward
return reward_inv.reward_inv_on_take(inv, listname, index, stack, player)
end,
-- make inv only available to this player
}, pname)
-- prepare the actual inventoriy and give it a size like a normal chest
inv:set_size("main", 8*4)
return inv
end
-- remove the detached inventory again on logout
minetest.register_on_leaveplayer(function(player, timed_out)
if(not(player)) then
return
end
local pname = player:get_player_name()
if(pname) then
minetest.remove_detached_inventory(reward_inv.reward_inv_get_name(pname))
-- the player is no longer accessing any reward inv
reward_inv.player_using[pname] = nil
-- and the player is no longer in edit mode
reward_inv.player_is_editing[pname] = nil
end
end)