From a647f216ca08aa6b7071203d23de9b7ec4eb8f1e Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 1 Feb 2023 22:40:35 +0100 Subject: [PATCH] Add rp_checkitem mod --- mods/rp_checkitem/API.md | 19 ++++++++++++++ mods/rp_checkitem/init.lua | 54 ++++++++++++++++++++++++++++++++++++++ mods/rp_checkitem/mod.conf | 2 ++ 3 files changed, 75 insertions(+) create mode 100644 mods/rp_checkitem/API.md create mode 100644 mods/rp_checkitem/init.lua create mode 100644 mods/rp_checkitem/mod.conf diff --git a/mods/rp_checkitem/API.md b/mods/rp_checkitem/API.md new file mode 100644 index 00000000..c9c4a077 --- /dev/null +++ b/mods/rp_checkitem/API.md @@ -0,0 +1,19 @@ +This tiny mod lets you know when a player got a new item. +This mod is currently not recommended to be used outside of Repixture yet. + +Its only function is: + +## `rp_checkitem.register_on_got_item(item, callback)` + +Registers the function `callback(player)` as an callback for when a player got +or has an item. `callback` might be called repeatedly. + +* `item`: Raw item name of item to check for (no `ItemStack` or item count) +* `callback(player)`: Function that is called if `player` has the item + +NOTE: `callback` is currently very limited. It might not be called instantly, +depending on how the item got into the player inventory, and it might not +always recognize a gotten item if it was too briefly in the inventory. +Only when the player held on to the item for at least 10 seconds, `callback` is +guaranteed to be called. This behavior might be improved in later versions. + diff --git a/mods/rp_checkitem/init.lua b/mods/rp_checkitem/init.lua new file mode 100644 index 00000000..9217d15e --- /dev/null +++ b/mods/rp_checkitem/init.lua @@ -0,0 +1,54 @@ +rp_checkitem = {} + +-- Time in seconds to check inventory for items +local ITEM_CHECK_TIME = 10 + +local items_to_watch = {} + +function rp_checkitem.register_on_got_item(item, callback) + table.insert(items_to_watch, {item=item, callback=callback}) +end + +minetest.register_on_joinplayer(function(player) + local inv = player:get_inventory() + for i=1, #items_to_watch do + local entry = items_to_watch[i] + if inv:contains_item("main", entry.item) then + entry.callback(player) + end + end +end) + +minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info) + if action == "put" then + local itemname = inventory_info.stack:get_name() + for i=1, #items_to_watch do + local entry = items_to_watch[i] + if inventory_info.stack:get_name() == entry.item then + entry.callback(player) + end + end + end +end) + +local timer = 0 +minetest.register_globalstep(function(dtime) + timer = timer + dtime + if timer < ITEM_CHECK_TIME then + return + end + timer = 0 + + local players = minetest.get_connected_players() + for p=1, #players do + local player = players[p] + local inv = player:get_inventory() + for i=1, #items_to_watch do + local entry = items_to_watch[i] + if inv:contains_item("main", entry.item) then + entry.callback(player) + end + end + end +end) + diff --git a/mods/rp_checkitem/mod.conf b/mods/rp_checkitem/mod.conf new file mode 100644 index 00000000..8f3d82f0 --- /dev/null +++ b/mods/rp_checkitem/mod.conf @@ -0,0 +1,2 @@ +name = rp_checkitem +description = Helper mod, provides callback to be called when player got an item