Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement get_item_defs() client-side call #10455

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions clientmods/preview/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ core.register_chatcommand("text", {
end,
})

core.register_chatcommand("count_items", {
func = function()
local count = 0
for _ in pairs(minetest.get_item_defs()) do
count = count + 1
end
return true, count .. " items are currently registered"
end
})

core.register_on_mods_loaded(function()
core.log("Yeah preview mod is loaded with other CSM mods.")
Expand Down
2 changes: 2 additions & 0 deletions doc/client_lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,8 @@ It can be created via `Raycast(pos1, pos2, objects, liquids)` or
* Returns [node definition](#node-definition) table of `nodename`
* `minetest.get_item_def(itemstring)`
* Returns item definition table of `itemstring`
* `minetest.get_item_defs()`
* Returns table of item definitions indexed by `itemstring`

#### Node Definition

Expand Down
26 changes: 26 additions & 0 deletions src/script/lua_api/l_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,31 @@ int ModApiClient::l_get_item_def(lua_State *L)
return 1;
}

// get_item_defs()
int ModApiClient::l_get_item_defs(lua_State *L)
{
if (checkCSMRestrictionFlag(CSM_RF_READ_ITEMDEFS))
return 0;

IGameDef *gdef = getGameDef(L);
assert(gdef);

IItemDefManager *idef = gdef->idef();
assert(idef);

std::set<std::string> itemdefs;
idef->getAll(itemdefs);
lua_createtable(L, 0, itemdefs.size());
for (const std::string &item_name : itemdefs) {
const ItemDefinition &def = idef->get(item_name);
lua_pushstring(L, item_name.c_str());
push_item_definition_full(L, def);
lua_settable(L, -3);
}

return 1;
}

// get_node_def(nodename)
int ModApiClient::l_get_node_def(lua_State *L)
{
Expand Down Expand Up @@ -436,6 +461,7 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(sound_fade);
API_FCT(get_server_info);
API_FCT(get_item_def);
API_FCT(get_item_defs);
API_FCT(get_node_def);
API_FCT(get_privilege_list);
API_FCT(get_builtin_path);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class ModApiClient : public ModApiBase
// get_item_def(itemstring)
static int l_get_item_def(lua_State *L);

// get_item_defs()
static int l_get_item_defs(lua_State *L);

// get_node_def(nodename)
static int l_get_node_def(lua_State *L);

Expand Down