Player and Entity Inventory Handlers
(This is a suggestion for a mod API feature that would greatly enhance things like armor, custom inventories, custom crafting, etc.)
We have interfaces for handling changes to node inventories and detached inventories. What is missing is handling changes to entity inventories, including player inventories. This would be invaluable for things like custom games, armor, and custom crafting mods. It CAN be done now by creating a separate detached inventory for each player, mangling the name using the player's name, and being careful with permissions "just in case", so it isn't shared between players. However, this defeats the purpose of a "detached" inventory and REALLY isn't pretty (it gets even uglier for Lua entity instances...).
The design I would suggest is to add methods to the InvRef object so that handlers could be set for ANY inventory in the game (underneath they might have to be polymorphic on the type of object the inventory belongs to). This could look like:
As an example of how this could be used, here is a bit of mod code that uses a Chain of Responsibility pattern to do something special with an "armor" inventory list in the player's inventory:
We have interfaces for handling changes to node inventories and detached inventories. What is missing is handling changes to entity inventories, including player inventories. This would be invaluable for things like custom games, armor, and custom crafting mods. It CAN be done now by creating a separate detached inventory for each player, mangling the name using the player's name, and being careful with permissions "just in case", so it isn't shared between players. However, this defeats the purpose of a "detached" inventory and REALLY isn't pretty (it gets even uglier for Lua entity instances...).
The design I would suggest is to add methods to the InvRef object so that handlers could be set for ANY inventory in the game (underneath they might have to be polymorphic on the type of object the inventory belongs to). This could look like:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
inv = minetest.get_inventory(location)
-- event is one of: "allow_move", "allow_put", "allow_take", "on_move", "on_put", "on_take"
-- Later it might even be extensible to things like "on_save" and "on_load"....
oldHandlerFunc = inv:get_handler(event)
inv:set_handler(event, newHandlerFunc)
As an example of how this could be used, here is a bit of mod code that uses a Chain of Responsibility pattern to do something special with an "armor" inventory list in the player's inventory:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
local playerInv = minetest.get_inventory({ type = "player", name = playername })
local oldInvPutHandler = playerInv:get_handler("on_put")
playerInv:set_handler(
"on_put",
function(inv, listname, index, stack, player)
if listname ~= "armor" then
return oldInvPutHandler(inv, listname, index, stack, player)
end
-- Type of armor and size of stack already checked in separate "allow_put" handler.
minetest.log(
"info",
"Player "..player:get_player_name().." equipped armor "..stack:get_name().." to slot "..index)
-- Handle behavior of equipped armor
end)