Getting a List of Currently Registered Items

Post Reply
User avatar
SegFault22
Member
Posts: 870
Joined: Mon May 21, 2012 03:17
Location: NaN

Getting a List of Currently Registered Items

by SegFault22 » Post

I'm working on a mod, where it would be very helpful to be able to "get" (return, via function output) a list of the names (itemstrings) of all currently registered items. I'm working on a bit of code that can scan the list for items that conform to an existing naming convention, and register their material name in a list to be used when adding stuff made out of materials added by other mods.
For example, if another mod adds a material that doesn't exist in the mod, it would be detected by the sniffer and added to a list of materials, for which to register items such as ingots, plates, dust-piles, wires, rods, bolts, rounds, and such, and recipes using them to create those and other items. However, it would have to conform to an existing naming convention, such as "modname:item_ingot_(material name, such as "nichrome", "steel", etc)" or "modname:ingot_(material name)" or "modname:item_(material name)_ingot" - items detected that do not conform to such conventions would simply not be added to the list, and a text-output would be sent to the console, including the unknown item's name, inserted into a "please report to development for further compatibility"-message.

Is there any way, with the currently-existing api, to make the game return a list of registered items (in stackstring format, or a format that can be changed to or interpreted as stackstring), without making a script that checks all possible mod/item ids for existing things (which would consume far more resources than necessary), or patching the register code to output each registered item's stackstring to a list of items?

User avatar
rubenwardy
Moderator
Posts: 6192
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: United Kingdom
Contact:

Re: Getting a List of Currently Registered Items

by rubenwardy » Post

Code: Select all

keys = {}
for key, val in pairs(minetest.registered_items) do
      -- iterate through items, for example to add keys to a table
      table.insert(keys, key)
end
Also see Lua Patterns and string.split(key, ":").

User avatar
SegFault22
Member
Posts: 870
Joined: Mon May 21, 2012 03:17
Location: NaN

Re: Getting a List of Currently Registered Items

by SegFault22 » Post

rubenwardy wrote:

Code: Select all

keys = {}
for key, val in pairs(minetest.registered_items) do
      -- iterate through items, for example to add keys to a table
      table.insert(keys, key)
end
Also see Lua Patterns and string.split(key, ":").
Thank you, I did not know there was access to minetest.registered_items outside of the "C++ part of the engine". With this I will commence adding more of the significant planned features to my mod, and it is now much closer to being ready for official release

User avatar
Casimir
Member
Posts: 1164
Joined: Fri Aug 03, 2012 16:59
GitHub: CasimirKaPazi

Re: Getting a List of Currently Registered Items

by Casimir » Post

This code writes a list of all items in the world directory. However, they are unsorted.

Code: Select all

local function count_items()
	local number = 0
	local file = minetest.get_worldpath() .. "/items"
	local output = io.open(file, "w")
	for name, item in pairs(minetest.registered_items) do
		number = number + 1
		output:write(name.."\n")
	end
	io.close(output)
	print("There are "..number.." registered items.")
	minetest.chat_send_player("singleplayer", "There are "..number.." registered items.")
end

minetest.after(5, count_items)

User avatar
orwell
Member
Posts: 830
Joined: Wed Jun 24, 2015 18:45
GitHub: orwell96
IRC: orwell96_mt
In-game: orwell
Location: Raxacoricofallapatorius

Re: Getting a List of Currently Registered Items

by orwell » Post

Code: Select all

core.register_chatcommand("ilist", {
	params = "[search]",
	description = "find item names or list all items",
	privs = {},
	func = function(name, param)
		--minetest.chat_send_player(name, "")
		local parami=""
		if param~=nil then
			parami=param
		end
		minetest.chat_send_player(name,"------------------------nodes")
		for n, i in pairs(core.registered_nodes) do
			if string.find(n, parami, 1, true) then
				minetest.chat_send_player(name, n)
			end
		end
		minetest.chat_send_player(name,"------------------------tools")
		for n, i in pairs(core.registered_tools) do
			if string.find(n, parami, 1, true) then
				minetest.chat_send_player(name, n)
			end
		end
		minetest.chat_send_player(name,"------------------------craftitems")
		for n, i in pairs(core.registered_craftitems) do
			if string.find(n, parami, 1, true) then --plain text search
				minetest.chat_send_player(name, n)
			end
		end

	end,

})
A chatcommand I wrote one day to list and search for item IDs. (searches for plain text and case-sensitive and prints to caller)
Lua is great!
List of my mods
I like singing. I like dancing. I like ... niyummm...

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest