How do you clear inventory in code?

Post Reply
ChristienChapman
Member
Posts: 117
Joined: Sat May 28, 2022 00:04

How do you clear inventory in code?

by ChristienChapman » Post

I tried setting item stacks and it did not work, I tried registering clearinv command to a function and it never cleared anything. The noob is at it again. Whoever helps will be credited in my project. God bless.
God bless you.

List of releases:
Minetest Zombies Minigame - viewtopic.php?f=9&t=28442&p=412633#p412633

- > cdb_1d60e1a03f83

User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

Re: How do you clear inventory in code?

by sirrobzeroone » Post

The flowers timer mod I'm working on does something like this. Rubenwardy's book also has some good stuff on inventory management:

https://rubenwardy.com/minetest_modding ... ories.html

Also about 7 posts down on this thread Rubenwardy provides some good guidance on a very similar question:

viewtopic.php?t=8947

Here's my rollback code that skims over a players inventory and replaces any flower stages with default flowers. You could modify this to simply replace everything with air and probably no need to let the player know, hopefully a bit of a starter, may not be the most efficient way to do it if you wish to simply clear the whole inventory not specific items.

Code: Select all

minetest.register_on_joinplayer(function(player)
			local roll_replace = flowers_nt.rollback_replace
			local inv = player:get_inventory()
			
			for i_tar,i_rep in pairs(roll_replace) do		
				if inv:contains_item("main", i_tar) then
					local main_inv = inv:get_list("main")
					
					for i,itemstack in pairs(main_inv) do
						if itemstack:get_name() == i_tar then
							inv:remove_item("main", ItemStack(itemstack:get_name().." "..itemstack:get_count()))
							
							if i_rep ~= "air" then
								inv:set_stack("main", i, ItemStack(i_rep.." "..itemstack:get_count()))
							end
							
							minetest.chat_send_player(player:get_player_name(),"Flowers_NT Rollback: "..itemstack:get_name().." replaced with "..i_rep.." x"..itemstack:get_count())				
						end
					end						
				end	
			end
		end)		

ChristienChapman
Member
Posts: 117
Joined: Sat May 28, 2022 00:04

Re: How do you clear inventory in code?

by ChristienChapman » Post

Well if this clears specific items, then all I have to do is put in the scope the specific items you would have in the game as you cannot have blocks but only weapons like pickaxes and swords. Very much thanks I will look into the code tomorrow and give you credit as you deserve..
God bless you.

List of releases:
Minetest Zombies Minigame - viewtopic.php?f=9&t=28442&p=412633#p412633

- > cdb_1d60e1a03f83

ChristienChapman
Member
Posts: 117
Joined: Sat May 28, 2022 00:04

Re: How do you clear inventory in code?

by ChristienChapman » Post

Hey thanks sir. One of the posts you directed me to had a super easy way to clear inventory. I put it where I needed and it worked! Thanks so much!
God bless you.

List of releases:
Minetest Zombies Minigame - viewtopic.php?f=9&t=28442&p=412633#p412633

- > cdb_1d60e1a03f83

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests