forked from minetest/minetest
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DevTest: Move callback items to callbacks mod
- Loading branch information
Showing
6 changed files
with
161 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,2 @@ | ||
local function print_to_everything(msg) | ||
minetest.log("action", msg) | ||
minetest.chat_send_all(msg) | ||
end | ||
|
||
minetest.register_node("callbacks:callback_node", { | ||
description = "Callback Test Node (construct/destruct/timer)".."\n".. | ||
"Tests callbacks: on_construct, after_place_node, on_destruct, after_destruct, after_dig_node, on_timer", | ||
tiles = {"callbacks_callback_node.png"}, | ||
groups = {dig_immediate=3}, | ||
-- This was known to cause a bug in minetest.item_place_node() when used | ||
-- via minetest.place_node(), causing a placer with no position | ||
paramtype2 = "facedir", | ||
drop = "", | ||
|
||
on_construct = function(pos) | ||
print_to_everything("callbacks:callback_node:on_construct("..minetest.pos_to_string(pos)..")") | ||
local meta = minetest.get_meta(pos) | ||
meta:set_string("mine", "test") | ||
local timer = minetest.get_node_timer(pos) | ||
timer:start(4, 3) | ||
end, | ||
|
||
after_place_node = function(pos, placer) | ||
print_to_everything("callbacks:callback_node:after_place_node("..minetest.pos_to_string(pos)..")") | ||
local meta = minetest.get_meta(pos) | ||
if meta:get_string("mine") == "test" then | ||
print_to_everything("correct metadata found") | ||
else | ||
print_to_everything("incorrect metadata found") | ||
end | ||
end, | ||
|
||
on_destruct = function(pos) | ||
print_to_everything("callbacks:callback_node:on_destruct("..minetest.pos_to_string(pos)..")") | ||
end, | ||
|
||
after_destruct = function(pos) | ||
print_to_everything("callbacks:callback_node:after_destruct("..minetest.pos_to_string(pos)..")") | ||
end, | ||
|
||
after_dig_node = function(pos, oldnode, oldmetadata, digger) | ||
print_to_everything("callbacks:callback_node:after_dig_node("..minetest.pos_to_string(pos)..")") | ||
end, | ||
|
||
on_timer = function(pos, elapsed) | ||
print_to_everything("callbacks:callback_node:on_timer(): elapsed="..dump(elapsed)) | ||
return true | ||
end, | ||
}) | ||
|
||
dofile(minetest.get_modpath("callbacks").."/items.lua") | ||
dofile(minetest.get_modpath("callbacks").."/nodes.lua") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
-- | ||
-- Item callbacks | ||
-- | ||
|
||
minetest.register_craftitem("callbacks:callback_item_1", { | ||
description = "Callback test item 1\n(Use/Drop + Sneak to switch to item 2)", | ||
inventory_image = "callbacks_callback_item_1.png", | ||
wield_image = "callbacks_callback_item_1.png", | ||
|
||
on_secondary_use = function(itemstack, user, pointed_thing) | ||
minetest.log("[callbacks:callback_item_1 on_secondary_use] " .. itemstack:get_name()) | ||
local ctrl = user and user:get_player_control() or {} | ||
if ctrl.sneak then | ||
itemstack = ItemStack(itemstack) | ||
itemstack:set_name("callbacks:callback_item_2") | ||
return itemstack | ||
end | ||
end, | ||
|
||
on_drop = function(itemstack, dropper, pos) | ||
minetest.log("[callbacks:callback_item_1 on_drop] " .. itemstack:get_name()) | ||
local ctrl = dropper and dropper:get_player_control() or {} | ||
if ctrl.sneak then | ||
itemstack = ItemStack(itemstack) | ||
itemstack:set_name("callbacks:callback_item_2") | ||
end | ||
|
||
return minetest.item_drop(itemstack, dropper, pos) | ||
end, | ||
|
||
on_pickup = function(itemstack, picker, pointed_thing, ...) | ||
minetest.log("[callbacks:callback_item_1 on_pickup]") | ||
assert(pointed_thing.ref:get_luaentity().name == "__builtin:item") | ||
local ctrl = picker and picker:get_player_control() or {} | ||
if ctrl.aux1 then | ||
-- Debug message | ||
minetest.log(dump({...})) | ||
end | ||
if ctrl.sneak then | ||
-- Pick up one item of the other kind at once | ||
local taken = itemstack:take_item() | ||
taken:set_name("callbacks:callback_item_2") | ||
local leftover = minetest.item_pickup(taken, picker, pointed_thing, ...) | ||
leftover:set_name("callbacks:callback_item_1") | ||
itemstack:add_item(leftover) | ||
return itemstack | ||
elseif ctrl.up then | ||
-- Don't pick up | ||
return | ||
elseif ctrl.left then | ||
-- Eat it | ||
return minetest.do_item_eat(2, nil, itemstack, picker, pointed_thing) | ||
else | ||
-- Normal: pick up everything | ||
return minetest.item_pickup(itemstack, picker, pointed_thing, ...) | ||
end | ||
end, | ||
|
||
on_use = function(itemstack, user, pointed_thing) | ||
minetest.log("[callbacks:callback_item_1 on_use] " .. itemstack:get_name()) | ||
local ctrl = user and user:get_player_control() or {} | ||
if ctrl.sneak then | ||
itemstack = ItemStack(itemstack) | ||
itemstack:set_name("callbacks:callback_item_2") | ||
return itemstack | ||
end | ||
end, | ||
|
||
after_use = function(itemstack, user, node, digparams) -- never called | ||
minetest.log("[callbacks:callback_item_1 after_use]") | ||
local ctrl = user and user:get_player_control() or {} | ||
if ctrl.up then | ||
itemstack = ItemStack(itemstack) | ||
itemstack:set_name("callbacks:callback_item_2") | ||
return itemstack | ||
end | ||
end, | ||
}) | ||
|
||
minetest.register_craftitem("callbacks:callback_item_2", { | ||
description = "Callback test item 2\n(Use to switch to item 1)", | ||
inventory_image = "callbacks_callback_item_2.png", | ||
wield_image = "callbacks_callback_item_2.png", | ||
|
||
on_use = function(itemstack, user, pointed_thing) | ||
minetest.log("[callbacks:callback_item_2 on_use]") | ||
itemstack = ItemStack(itemstack) | ||
itemstack:set_name("callbacks:callback_item_1") | ||
return itemstack | ||
end, | ||
}) | ||
|
||
minetest.register_on_item_pickup(function(itemstack, picker, pointed_thing, time_from_last_punch, ...) | ||
assert(not pointed_thing or pointed_thing.ref:get_luaentity().name == "__builtin:item") | ||
|
||
local item_name = itemstack:get_name() | ||
if item_name ~= "callbacks:callback_item_1" and item_name ~= "callbacks:callback_item_2" then | ||
return | ||
end | ||
minetest.log("["..item_name.." register_on_item_pickup]") | ||
|
||
local ctrl = picker and picker:get_player_control() or {} | ||
if item_name == "callbacks:callback_item_2" and not ctrl.sneak then | ||
-- Same here. Pick up the other item type. | ||
itemstack:set_name("callbacks:callback_item_1") | ||
return picker:get_inventory():add_item("main", itemstack) | ||
end | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
local function print_to_everything(msg) | ||
minetest.log("action", msg) | ||
minetest.chat_send_all(msg) | ||
end | ||
|
||
minetest.register_node("callbacks:callback_node", { | ||
description = "Callback Test Node (construct/destruct/timer)".."\n".. | ||
"Tests callbacks: on_construct, after_place_node, on_destruct, after_destruct, after_dig_node, on_timer", | ||
tiles = {"callbacks_callback_node.png"}, | ||
groups = {dig_immediate=3}, | ||
-- This was known to cause a bug in minetest.item_place_node() when used | ||
-- via minetest.place_node(), causing a placer with no position | ||
paramtype2 = "facedir", | ||
drop = "", | ||
|
||
on_construct = function(pos) | ||
print_to_everything("callbacks:callback_node:on_construct("..minetest.pos_to_string(pos)..")") | ||
local meta = minetest.get_meta(pos) | ||
meta:set_string("mine", "test") | ||
local timer = minetest.get_node_timer(pos) | ||
timer:start(4, 3) | ||
end, | ||
|
||
after_place_node = function(pos, placer) | ||
print_to_everything("callbacks:callback_node:after_place_node("..minetest.pos_to_string(pos)..")") | ||
local meta = minetest.get_meta(pos) | ||
if meta:get_string("mine") == "test" then | ||
print_to_everything("correct metadata found") | ||
else | ||
print_to_everything("incorrect metadata found") | ||
end | ||
end, | ||
|
||
on_destruct = function(pos) | ||
print_to_everything("callbacks:callback_node:on_destruct("..minetest.pos_to_string(pos)..")") | ||
end, | ||
|
||
after_destruct = function(pos) | ||
print_to_everything("callbacks:callback_node:after_destruct("..minetest.pos_to_string(pos)..")") | ||
end, | ||
|
||
after_dig_node = function(pos, oldnode, oldmetadata, digger) | ||
print_to_everything("callbacks:callback_node:after_dig_node("..minetest.pos_to_string(pos)..")") | ||
end, | ||
|
||
on_timer = function(pos, elapsed) | ||
print_to_everything("callbacks:callback_node:on_timer(): elapsed="..dump(elapsed)) | ||
return true | ||
end, | ||
}) | ||
|
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters