Skip to content

Commit

Permalink
Log every inventory actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Emojigit committed Apr 19, 2024
1 parent ad2c11e commit 1691b60
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 17 deletions.
10 changes: 10 additions & 0 deletions technic/machines/HV/nuclear_reactor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ local digiline_def = function(pos, _, channel, msg)
end
end

local on_metadata_inventory_move = technic.machine_on_inventory_move("technic:hv_nuclear_reactor_core")
local on_metadata_inventory_put = technic.machine_on_inventory_put("technic:hv_nuclear_reactor_core")
local on_metadata_inventory_take = technic.machine_on_inventory_take("technic:hv_nuclear_reactor_core")

minetest.register_node("technic:hv_nuclear_reactor_core", {
description = reactor_desc,
tiles = {
Expand Down Expand Up @@ -480,6 +484,9 @@ minetest.register_node("technic:hv_nuclear_reactor_core", {
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
technic_run = run,
})

Expand Down Expand Up @@ -521,6 +528,9 @@ minetest.register_node("technic:hv_nuclear_reactor_core_active", {
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
technic_run = run,
technic_on_disable = function(pos, node)
local timer = minetest.get_node_timer(pos)
Expand Down
3 changes: 3 additions & 0 deletions technic/machines/HV/quarry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ minetest.register_node("technic:quarry", {
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
return player_allowed(pos, player:get_player_name()) and stack:get_count() or 0
end,
on_metadata_inventory_move = technic.machine_on_inventory_move("technic:quarry"),
on_metadata_inventory_put = technic.machine_on_inventory_put("technic:quarry"),
on_metadata_inventory_take = technic.machine_on_inventory_take("technic:quarry"),
mesecons = {
effector = {
action_on = function(pos)
Expand Down
3 changes: 3 additions & 0 deletions technic/machines/MV/tool_workshop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ minetest.register_node("technic:tool_workshop", {
can_dig = technic.machine_can_dig,
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
on_metadata_inventory_move = technic.machine_on_inventory_move("technic:tool_workshop"),
on_metadata_inventory_put = technic.machine_on_inventory_put("technic:tool_workshop"),
on_metadata_inventory_take = technic.machine_on_inventory_take("technic:tool_workshop"),
tube = {
can_insert = function (pos, node, stack, direction)
return minetest.get_meta(pos):get_inventory():room_for_item("src", stack)
Expand Down
10 changes: 10 additions & 0 deletions technic/machines/other/coal_alloy_furnace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ formspec = formspec..
"listring[context;fuel]"..
"listring[current_player;main]"

local on_metadata_inventory_move = technic.machine_on_inventory_move("technic:coal_alloy_furnace")
local on_metadata_inventory_put = technic.machine_on_inventory_put("technic:coal_alloy_furnace")
local on_metadata_inventory_take = technic.machine_on_inventory_take("technic:coal_alloy_furnace")

minetest.register_node("technic:coal_alloy_furnace", {
description = machine_name,
tiles = {"technic_coal_alloy_furnace_top.png", "technic_coal_alloy_furnace_bottom.png",
Expand All @@ -72,6 +76,9 @@ minetest.register_node("technic:coal_alloy_furnace", {
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
})

minetest.register_node("technic:coal_alloy_furnace_active", {
Expand All @@ -92,6 +99,9 @@ minetest.register_node("technic:coal_alloy_furnace_active", {
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
})

minetest.register_abm({
Expand Down
3 changes: 3 additions & 0 deletions technic/machines/other/constructor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ local function make_constructor(mark, length)
allow_metadata_inventory_put = allow_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = technic.machine_on_inventory_move("technic:constructor_mk"..mark),
on_metadata_inventory_put = technic.machine_on_inventory_put("technic:constructor_mk"..mark),
on_metadata_inventory_take = technic.machine_on_inventory_take("technic:constructor_mk"..mark),
on_rotate = function(pos, node, user, mode, new_param2)
if mode ~= 1 then
return false
Expand Down
3 changes: 3 additions & 0 deletions technic/machines/other/injector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ minetest.register_node("technic:injector", {
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = technic.machine_on_inventory_move(nodename),
on_metadata_inventory_put = technic.machine_on_inventory_put(nodename),
on_metadata_inventory_take = technic.machine_on_inventory_take(nodename),
after_place_node = pipeworks.after_place,
after_dig_node = pipeworks.after_dig
})
Expand Down
8 changes: 8 additions & 0 deletions technic/machines/register/battery_box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ function technic.register_battery_box(nodename, data)
return true
end

-- This nodename is not an actual nodename - it is the base nodename.
local on_metadata_inventory_move = technic.machine_on_inventory_move(nodename)
local on_metadata_inventory_put = technic.machine_on_inventory_put(nodename)
local on_metadata_inventory_take = technic.machine_on_inventory_take(nodename)

for i = 0, 8 do
local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
technic_machine=1, ["technic_"..ltier]=1, axey=2, handy=1}
Expand Down Expand Up @@ -389,6 +394,9 @@ function technic.register_battery_box(nodename, data)
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
technic_run = run,
on_timer = on_timer,
on_rightclick = function(pos) update_node(pos, true) end,
Expand Down
39 changes: 39 additions & 0 deletions technic/machines/register/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,42 @@ function technic.machine_inventory_move(pos, from_list, from_index,
local stack = minetest.get_meta(pos):get_inventory():get_stack(from_list, from_index)
return inv_change(pos, player, count, from_list, to_list, stack)
end

function technic.machine_on_inventory_put(nodename)
return function(pos, listname, index, stack, player)
minetest.log("action", string.format(
"%s put %s into %s[%u] of %s at %s",
player:is_player() and player:get_player_name() or "A mod",
stack:to_string(),
listname, index,
nodename,
minetest.pos_to_string(pos)
))
end
end

function technic.machine_on_inventory_take(nodename)
return function(pos, listname, index, stack, player)
minetest.log("action", string.format(
"%s took %s from %s[%u] of %s at %s",
player:is_player() and player:get_player_name() or "A mod",
stack:to_string(),
listname, index,
nodename,
minetest.pos_to_string(pos)
))
end
end

function technic.machine_on_inventory_move(nodename)
return function(pos, from_list, from_index, to_list, to_index, count, player)
minetest.log("action", string.format(
"%s moved item from %s[%u] to %s[%u] of %s at %s",
player:is_player() and player:get_player_name() or "A mod",
from_list, from_index,
to_list, to_index,
nodename,
minetest.pos_to_string(pos)
))
end
end
10 changes: 10 additions & 0 deletions technic/machines/register/machine_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ function technic.register_base_machine(nodename, data)
tentry = ""
end

local on_metadata_inventory_move = technic.machine_on_inventory_move(nodename)
local on_metadata_inventory_put = technic.machine_on_inventory_put(nodename)
local on_metadata_inventory_take = technic.machine_on_inventory_take(nodename)

minetest.register_node(colon..nodename, {
description = def.description,
tiles = {
Expand Down Expand Up @@ -216,6 +220,9 @@ function technic.register_base_machine(nodename, data)
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
technic_run = run,
after_place_node = def.tube and pipeworks.after_place,
after_dig_node = technic.machine_after_dig_node,
Expand Down Expand Up @@ -265,6 +272,9 @@ function technic.register_base_machine(nodename, data)
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
technic_run = run,
technic_disabled_machine_name = nodename,
on_receive_fields = function(pos, formname, fields, sender)
Expand Down
25 changes: 25 additions & 0 deletions technic_chests/inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ function technic.chests.move_inv(from_inv, to_inv, filter)
return moved_items
end

-- DEPRECATED: use technic.machine_on_inventory_* instead.
---@deprecated
---@see technic.machine_on_inventory_put
---@see technic.machine_on_inventory_take
---@see technic.machine_on_inventory_move
function technic.chests.log_inv_change(pos, name, change, items)
local spos = minetest.pos_to_string(pos)
if change == "move" then
Expand All @@ -176,3 +181,23 @@ function technic.chests.log_inv_change(pos, name, change, items)
minetest.log("action", name.." takes "..items.." from chest at "..spos)
end
end

function technic.chests.log_fast_move(pos, name, nodename, change, items)
local spos = minetest.pos_to_string(pos)
local itemlist = ""
for _, stack in ipairs(items) do
itemlist = itemlist .. " " .. stack.name .. " " .. stack.count
end

if change == "put" then
minetest.log("action", string.format(
"%s fast-moved the following item from their inventory into %s at %s:",
name, nodename, spos
) .. itemlist)
elseif change == "take" then
minetest.log("action", string.format(
"%s fast-moved the following item from %s at %s into their inventory:",
name, nodename, spos
) .. itemlist)
end
end
26 changes: 9 additions & 17 deletions technic_chests/register.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ function technic.chests.register_chest(nodename, data)
data.digilines = nil
end

local on_metadata_inventory_move = technic.machine_on_inventory_move(nodename)
local on_metadata_inventory_put = technic.machine_on_inventory_put(nodename)
local on_metadata_inventory_take = technic.machine_on_inventory_take(nodename)

data.formspec = technic.chests.get_formspec(data)
local def = {
description = data.description,
Expand Down Expand Up @@ -144,7 +148,7 @@ function technic.chests.register_chest(nodename, data)
if data.digilines and meta:get_int("send_take") == 1 then
technic.chests.send_digiline_message(pos, "take", player, moved_items)
end
technic.chests.log_inv_change(pos, player:get_player_name(), "take", "stuff")
technic.chests.log_fast_move(pos, player:get_player_name(), nodename, "take", moved_items)
return 0
end
return count
Expand All @@ -160,7 +164,7 @@ function technic.chests.register_chest(nodename, data)
if data.digilines and meta:get_int("send_put") == 1 then
technic.chests.send_digiline_message(pos, "put", player, moved_items)
end
technic.chests.log_inv_change(pos, player:get_player_name(), "put", "stuff")
technic.chests.log_fast_move(pos, player:get_player_name(), nodename, "put", moved_items)
return 0
end
return stack:get_count()
Expand All @@ -171,21 +175,9 @@ function technic.chests.register_chest(nodename, data)
end
return stack:get_count()
end,
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
technic.chests.log_inv_change(pos, player:get_player_name(), "move", "stuff")
end,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
if data.digilines and minetest.get_meta(pos):get_int("send_put") == 1 then
technic.chests.send_digiline_message(pos, "put", player, {stack:to_table()})
end
technic.chests.log_inv_change(pos, player:get_player_name(), "put", stack:get_name())
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
if data.digilines and minetest.get_meta(pos):get_int("send_take") == 1 then
technic.chests.send_digiline_message(pos, "take", player, {stack:to_table()})
end
technic.chests.log_inv_change(pos, player:get_player_name(), "take", stack:get_name())
end,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
on_blast = function(pos)
if data.locked or data.protected then
return
Expand Down
10 changes: 10 additions & 0 deletions technic_cnc/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ function technic_cnc.register_cnc_machine(nodename, def)
groups.tubedevice_receiver = 1
end

local on_metadata_inventory_move = technic.machine_on_inventory_move(nodename)
local on_metadata_inventory_put = technic.machine_on_inventory_put(nodename)
local on_metadata_inventory_take = technic.machine_on_inventory_take(nodename)

-- Inactive state CNC machine
minetest.register_node(":" .. nodename, {
description = def.description,
Expand Down Expand Up @@ -412,6 +416,9 @@ function technic_cnc.register_cnc_machine(nodename, def)
allow_metadata_inventory_put = def.allow_metadata_inventory_put or allow_metadata_inventory_put,
allow_metadata_inventory_take = def.allow_metadata_inventory_take or allow_metadata_inventory_take,
allow_metadata_inventory_move = def.allow_metadata_inventory_move or allow_metadata_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
on_receive_fields = on_receive_fields,
technic_run = def.technic_run or technic_run,
})
Expand All @@ -436,6 +443,9 @@ function technic_cnc.register_cnc_machine(nodename, def)
allow_metadata_inventory_put = def.allow_metadata_inventory_put or allow_metadata_inventory_put,
allow_metadata_inventory_take = def.allow_metadata_inventory_take or allow_metadata_inventory_take,
allow_metadata_inventory_move = def.allow_metadata_inventory_move or allow_metadata_inventory_move,
on_metadata_inventory_move = on_metadata_inventory_move,
on_metadata_inventory_put = on_metadata_inventory_put,
on_metadata_inventory_take = on_metadata_inventory_take,
on_receive_fields = on_receive_fields,
technic_run = def.technic_run or technic_run,
technic_disabled_machine_name = nodename,
Expand Down

0 comments on commit 1691b60

Please sign in to comment.