items.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. --
  2. -- Item callbacks
  3. --
  4. local function print_to_everything(msg)
  5. core.log("action", "[callbacks] " .. msg)
  6. core.chat_send_all(msg)
  7. end
  8. core.register_craftitem("callbacks:callback_item_1", {
  9. description = "Callback Test Item 1".."\n"..
  10. "Tests callbacks: on_secondary_use, on_drop, on_pickup, on_use, after_use".."\n"..
  11. "Punch/Drop + Sneak: Switch to Callback Test Item 2".."\n"..
  12. "Aux1 + pickup item: Print additional on_pickup arguments",
  13. inventory_image = "callbacks_callback_item_1.png",
  14. wield_image = "callbacks_callback_item_1.png",
  15. groups = { callback_test = 1 },
  16. on_secondary_use = function(itemstack, user, pointed_thing)
  17. print_to_everything("[callbacks:callback_item_1 on_secondary_use] " .. itemstack:get_name())
  18. local ctrl = user and user:get_player_control() or {}
  19. if ctrl.sneak then
  20. itemstack = ItemStack(itemstack)
  21. itemstack:set_name("callbacks:callback_item_2")
  22. return itemstack
  23. end
  24. end,
  25. on_drop = function(itemstack, dropper, pos)
  26. print_to_everything("[callbacks:callback_item_1 on_drop] " .. itemstack:get_name())
  27. local ctrl = dropper and dropper:get_player_control() or {}
  28. if ctrl.sneak then
  29. itemstack = ItemStack(itemstack)
  30. itemstack:set_name("callbacks:callback_item_2")
  31. end
  32. return core.item_drop(itemstack, dropper, pos)
  33. end,
  34. on_pickup = function(itemstack, picker, pointed_thing, ...)
  35. print_to_everything("[callbacks:callback_item_1 on_pickup]")
  36. assert(pointed_thing.ref:get_luaentity().name == "__builtin:item")
  37. local ctrl = picker and picker:get_player_control() or {}
  38. if ctrl.aux1 then
  39. -- Debug message
  40. print_to_everything("on_pickup dump:")
  41. print_to_everything(dump({...}))
  42. end
  43. if ctrl.sneak then
  44. -- Pick up one item of the other kind at once
  45. local taken = itemstack:take_item()
  46. taken:set_name("callbacks:callback_item_2")
  47. local leftover = core.item_pickup(taken, picker, pointed_thing, ...)
  48. leftover:set_name("callbacks:callback_item_1")
  49. itemstack:add_item(leftover)
  50. return itemstack
  51. elseif ctrl.up then
  52. -- Don't pick up
  53. return
  54. elseif ctrl.left then
  55. -- Eat it
  56. return core.do_item_eat(2, nil, itemstack, picker, pointed_thing)
  57. else
  58. -- Normal: pick up everything
  59. return core.item_pickup(itemstack, picker, pointed_thing, ...)
  60. end
  61. end,
  62. on_use = function(itemstack, user, pointed_thing)
  63. print_to_everything("[callbacks:callback_item_1 on_use] " .. itemstack:get_name())
  64. local ctrl = user and user:get_player_control() or {}
  65. if ctrl.sneak then
  66. itemstack = ItemStack(itemstack)
  67. itemstack:set_name("callbacks:callback_item_2")
  68. return itemstack
  69. end
  70. end,
  71. after_use = function(itemstack, user, node, digparams) -- never called
  72. print_to_everything("[callbacks:callback_item_1 after_use]")
  73. local ctrl = user and user:get_player_control() or {}
  74. if ctrl.up then
  75. itemstack = ItemStack(itemstack)
  76. itemstack:set_name("callbacks:callback_item_2")
  77. return itemstack
  78. end
  79. end,
  80. })
  81. core.register_craftitem("callbacks:callback_item_2", {
  82. description = "Callback Test Item 2".."\n"..
  83. "Punch to switch to Callback Test Item 1",
  84. inventory_image = "callbacks_callback_item_2.png",
  85. wield_image = "callbacks_callback_item_2.png",
  86. groups = { callback_test = 1 },
  87. on_use = function(itemstack, user, pointed_thing)
  88. print_to_everything("[callbacks:callback_item_2 on_use]")
  89. itemstack = ItemStack(itemstack)
  90. itemstack:set_name("callbacks:callback_item_1")
  91. return itemstack
  92. end,
  93. })
  94. core.register_on_item_pickup(function(itemstack, picker, pointed_thing, time_from_last_punch, ...)
  95. assert(not pointed_thing or pointed_thing.ref:get_luaentity().name == "__builtin:item")
  96. local item_name = itemstack:get_name()
  97. if item_name ~= "callbacks:callback_item_1" and item_name ~= "callbacks:callback_item_2" then
  98. return
  99. end
  100. print_to_everything("["..item_name.." register_on_item_pickup]")
  101. local ctrl = picker and picker:get_player_control() or {}
  102. if item_name == "callbacks:callback_item_2" and not ctrl.sneak then
  103. -- Same here. Pick up the other item type.
  104. itemstack:set_name("callbacks:callback_item_1")
  105. return picker:get_inventory():add_item("main", itemstack)
  106. end
  107. end)