block gives you item when you click it

Post Reply
User avatar
Joseph16
Member
Posts: 310
Joined: Tue Dec 06, 2016 05:35
In-game: smb3

block gives you item when you click it

by Joseph16 » Post

Does anyone know how I do this? It needs to be infinite.
Testin' mines since 1989

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: block gives you item when you click it

by Krock » Post

Unclear instructions whether it should happen on right-click or on punch. However, here's a code example that gives the player 1 gold when punching and 1 steel when right-clicking.

Code: Select all

minetest.register_node("mymod:mynode", {
	-- .. other fields

	on_punch = function(pos, node, puncher, pointed_thing)
		if not (puncher and puncher:is_player()) then
			return -- punched by a non-player
		end
		local inv = puncher:get_inventory()
		inv:add_item("main", ItemStack("default:gold_ingot 1"))

		minetest.log("action", "Ouch! I was punched by "
			.. puncher:get_player_name())
	end,

	on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
		if not (clicker and clicker:is_player()) then
			return -- right-clicked by a non-player
		end
		local inv = clicker:get_inventory()
		inv:add_item("main", ItemStack("default:steel_ingot 1"))

		minetest.log("action", "Huh? I was right-clicked by "
			.. clicker:get_player_name())
	end,
})
Untested, but will work most likely.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Joseph16
Member
Posts: 310
Joined: Tue Dec 06, 2016 05:35
In-game: smb3

Re: block gives you item when you click it

by Joseph16 » Post

Krock wrote:Unclear instructions whether it should happen on right-click or on punch. However, here's a code example that gives the player 1 gold when punching and 1 steel when right-clicking.

Code: Select all

minetest.register_node("mymod:mynode", {
	-- .. other fields

	on_punch = function(pos, node, puncher, pointed_thing)
		if not (puncher and puncher:is_player()) then
			return -- punched by a non-player
		end
		local inv = puncher:get_inventory()
		inv:add_item("main", ItemStack("default:gold_ingot 1"))

		minetest.log("action", "Ouch! I was punched by "
			.. puncher:get_player_name())
	end,

	on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
		if not (clicker and clicker:is_player()) then
			return -- right-clicked by a non-player
		end
		local inv = clicker:get_inventory()
		inv:add_item("main", ItemStack("default:steel_ingot 1"))

		minetest.log("action", "Huh? I was right-clicked by "
			.. clicker:get_player_name())
	end,
})
Untested, but will work most likely.
Thank you! That’s perfect! I needed the right click one.
Testin' mines since 1989

User avatar
Joseph16
Member
Posts: 310
Joined: Tue Dec 06, 2016 05:35
In-game: smb3

Re: block gives you item when you click it

by Joseph16 » Post

Krock wrote:Unclear instructions whether it should happen on right-click or on punch. However, here's a code example that gives the player 1 gold when punching and 1 steel when right-clicking.

Code: Select all

minetest.register_node("mymod:mynode", {
	-- .. other fields

	on_punch = function(pos, node, puncher, pointed_thing)
		if not (puncher and puncher:is_player()) then
			return -- punched by a non-player
		end
		local inv = puncher:get_inventory()
		inv:add_item("main", ItemStack("default:gold_ingot 1"))

		minetest.log("action", "Ouch! I was punched by "
			.. puncher:get_player_name())
	end,

	on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
		if not (clicker and clicker:is_player()) then
			return -- right-clicked by a non-player
		end
		local inv = clicker:get_inventory()
		inv:add_item("main", ItemStack("default:steel_ingot 1"))

		minetest.log("action", "Huh? I was right-clicked by "
			.. clicker:get_player_name())
	end,
})
Untested, but will work most likely.
Thank you! That’s perfect! I needed the right click one.
Testin' mines since 1989

User avatar
Joseph16
Member
Posts: 310
Joined: Tue Dec 06, 2016 05:35
In-game: smb3

Re: block gives you item when you click it

by Joseph16 » Post

Krock wrote:Unclear instructions whether it should happen on right-click or on punch. However, here's a code example that gives the player 1 gold when punching and 1 steel when right-clicking.

Code: Select all

minetest.register_node("mymod:mynode", {
	-- .. other fields

	on_punch = function(pos, node, puncher, pointed_thing)
		if not (puncher and puncher:is_player()) then
			return -- punched by a non-player
		end
		local inv = puncher:get_inventory()
		inv:add_item("main", ItemStack("default:gold_ingot 1"))

		minetest.log("action", "Ouch! I was punched by "
			.. puncher:get_player_name())
	end,

	on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
		if not (clicker and clicker:is_player()) then
			return -- right-clicked by a non-player
		end
		local inv = clicker:get_inventory()
		inv:add_item("main", ItemStack("default:steel_ingot 1"))

		minetest.log("action", "Huh? I was right-clicked by "
			.. clicker:get_player_name())
	end,
})
Untested, but will work most likely.
I get an error: unexpected symbol near ‘}’ on the last line of code. I’ve looked for mistakes but I copied it exactly (except for my stuff) any ideas?
Testin' mines since 1989

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: block gives you item when you click it

by Krock » Post

Joseph13 wrote:I get an error: unexpected symbol near ‘}’ on the last line of code. I’ve looked for mistakes but I copied it exactly (except for my stuff) any ideas?
If removing the tailing comma before '}' (comma on the line above) does not help, paste the code. Otherwise I'll not be able to help you.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Joseph16
Member
Posts: 310
Joined: Tue Dec 06, 2016 05:35
In-game: smb3

Re: block gives you item when you click it

by Joseph16 » Post

Krock wrote:
Joseph13 wrote:I get an error: unexpected symbol near ‘}’ on the last line of code. I’ve looked for mistakes but I copied it exactly (except for my stuff) any ideas?
If removing the tailing comma before '}' (comma on the line above) does not help, paste the code. Otherwise I'll not be able to help you.
Ok thanks!
Testin' mines since 1989

User avatar
Joseph16
Member
Posts: 310
Joined: Tue Dec 06, 2016 05:35
In-game: smb3

Re: block gives you item when you click it

by Joseph16 » Post

Krock wrote:
Joseph13 wrote:I get an error: unexpected symbol near ‘}’ on the last line of code. I’ve looked for mistakes but I copied it exactly (except for my stuff) any ideas?
If removing the tailing comma before '}' (comma on the line above) does not help, paste the code. Otherwise I'll not be able to help you.
I re-copied it and looked it all and realized I had it all wrong. After fixing it I get a new error: ‘end’ expected (to close ‘if’ at line 11) near ‘by’. Can you re wright the code? I’m so lost!
Testin' mines since 1989

User avatar
GamingAssociation39
Member
Posts: 858
Joined: Mon Apr 25, 2016 16:09
GitHub: Gerold55
IRC: Gerold55
In-game: Gerold55
Location: Maryland, USA

Re: block gives you item when you click it

by GamingAssociation39 » Post

Joseph13 wrote:
Krock wrote:
Joseph13 wrote:I get an error: unexpected symbol near ‘}’ on the last line of code. I’ve looked for mistakes but I copied it exactly (except for my stuff) any ideas?
If removing the tailing comma before '}' (comma on the line above) does not help, paste the code. Otherwise I'll not be able to help you.
I re-copied it and looked it all and realized I had it all wrong. After fixing it I get a new error: ‘end’ expected (to close ‘if’ at line 11) near ‘by’. Can you re wright the code? I’m so lost!

Code: Select all

minetest.register_node("mymod:mynode", {
   -- .. other fields

   on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
      if not (clicker and clicker:is_player()) then
         return -- right-clicked by a non-player
      end
      local inv = clicker:get_inventory()
      inv:add_item("main", ItemStack("default:steel_ingot 1"))

      minetest.log("action", "Huh? I was right-clicked by "
         .. clicker:get_player_name())
   end,
})
Jesus Is Lord and Savior!!!

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests