Page 1 of 1

Is this a bug or an "undocumented feature"?

Posted: Sun Jul 17, 2016 02:24
by isaiah658
Using add_item with on_rightclick seems to have different behavior than if add_item is used with lets say on_punch. With on_rightclick the player will not receive the item if the slot the item is meant to go into (the first open slot) is the wielded/active slot. So if the first slot in the hotbar is empty and the player has the first slot as their active slot an item will not be added with add_item if used in on_rightclick. It will be added though if it is used with on_punch. Here are two examples:

This will properly if the wielded spot is where the item is meant to go:

Code: Select all

on_punch = function(pos, node, player, pointed_thing)
	local inv = player:get_inventory()
	local left = inv:add_item("main", "default:dirt")
end,
This will not work if the wielded spot is where the item is meant to go:

Code: Select all

on_rightclick = function(pos, node, player, itemstack, pointed_thing)
	local inv = player:get_inventory()
	local left = inv:add_item("main", "default:dirt")
end,
Surprisingly add_item in on_rightclick is still returning true as if it did add the item. Is this an annoying feature that is meant to be this way or a bug? It would seem like a bug to me because it is inconsistent.

Re: Is this a bug or an "undocumented feature"?

Posted: Mon Aug 01, 2016 21:58
by Zedicius
I can confirm this issue.

Code: Select all

minetest.register_node("testmod:bush", {
	description = "Bush",
	tiles = {"bush.png"},
	walkable = true,
	groups = {snappy = 3, flammable = 2},
	sounds = default.node_sound_leaves_defaults(),
  on_rightclick = function(pos, node, clicker, itemstack, object)
    clicker:get_inventory():add_item("main", "testmod:crystal")
    minetest.remove_node(pos)
    minetest.place_node(pos, {name = "testmod:bush_depleted"})
  end
})
Is there a workaround for this at the moment?

Re: Is this a bug or an "undocumented feature"?

Posted: Tue Aug 02, 2016 21:27
by isaiah658
Edit: Better workaround thanks to Void7. If you use:

Code: Select all

minetest.after(0, function() inv:add_item("main", itemdrop) end)
then the item will be added as you would expect. It has to do with how the game is checking things with on_rightclick. So not technically a bug.

The only workaround I know of is to use minetest.add_item(droppos, itemdrop) and drop the item on the ground nearby. That's what I'm doing with the berry bushes of mine. You could also use on_punch (essentially left clicking) which would work with adding the item to the inventory properly. Since I'm not alone wondering about this I'm going to post on the irc and hope I can get a solid response. If anyone has any input please share it.

Re: Is this a bug or an "undocumented feature"?

Posted: Wed Aug 03, 2016 07:11
by Zedicius
Ah! I did not know about the .after method. I should really go over the entire list this weekend.

Thanks!