test_moveaction.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. -- Table to keep track of callback executions
  2. -- [i + 0] = count of expected patterns of index (i + 1)
  3. -- [i + 1] = pattern to check
  4. local PATTERN_NORMAL = { 4, "allow_%w", 2, "on_take", 1, "on_put", 1 }
  5. local PATTERN_SWAP = { 8, "allow_%w", 4, "on_take", 2, "on_put", 2 }
  6. local exec_listing = {} -- List of logged callbacks (e.g. "on_take", "allow_put")
  7. -- Checks whether the logged callbacks equal the expected pattern
  8. core.__helper_check_callbacks = function(expect_swap)
  9. local exec_pattern = expect_swap and PATTERN_SWAP or PATTERN_NORMAL
  10. local ok = #exec_listing == exec_pattern[1]
  11. if ok then
  12. local list_index = 1
  13. for i = 2, #exec_pattern, 2 do
  14. for n = 1, exec_pattern[i + 1] do
  15. -- Match the list for "n" occurrences of the wanted callback name pattern
  16. ok = exec_listing[list_index]:find(exec_pattern[i])
  17. list_index = list_index + 1
  18. if not ok then break end
  19. end
  20. if not ok then break end
  21. end
  22. end
  23. if not ok then
  24. print("Execution order mismatch!")
  25. print("Expected patterns: ", dump(exec_pattern))
  26. print("Got list: ", dump(exec_listing))
  27. end
  28. exec_listing = {}
  29. return ok
  30. end
  31. -- Uncomment the other line for easier callback debugging
  32. local log = function(...) end
  33. --local log = print
  34. core.register_allow_player_inventory_action(function(_, action, inv, info)
  35. log("\tallow " .. action, info.count or info.stack:to_string())
  36. if action == "move" then
  37. -- testMoveFillStack
  38. return info.count
  39. end
  40. if action == "take" or action == "put" then
  41. assert(not info.stack:is_empty(), "Stack empty in: " .. action)
  42. -- testMoveUnallowed
  43. -- testSwapFromUnallowed
  44. -- testSwapToUnallowed
  45. if info.stack:get_name() == "default:takeput_deny" then
  46. return 0
  47. end
  48. -- testMovePartial
  49. if info.stack:get_name() == "default:takeput_max_5" then
  50. return 5
  51. end
  52. -- testCallbacks
  53. if info.stack:get_name():find("default:takeput_cb_%d") then
  54. -- Log callback as executed
  55. table.insert(exec_listing, "allow_" .. action)
  56. return -- Unlimited
  57. end
  58. end
  59. return -- Unlimited
  60. end)
  61. core.register_on_player_inventory_action(function(_, action, inv, info)
  62. log("\ton " .. action, info.count or info.stack:to_string())
  63. if action == "take" or action == "put" then
  64. assert(not info.stack:is_empty(), action)
  65. if info.stack:get_name():find("default:takeput_cb_%d") then
  66. -- Log callback as executed
  67. table.insert(exec_listing, "on_" .. action)
  68. return
  69. end
  70. end
  71. end)