diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2020-08-22 15:45:06 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2020-08-22 15:45:06 +0200 |
commit | 9b1030cac4409b262dca73d2f0741fe78d4998ee (patch) | |
tree | 4815f2122fd6b9778d0cc0f2cedb10389157f580 /src | |
parent | 2321e3da4c7f29ae06400e91e4e031777c6a5d9a (diff) | |
download | dragonfireclient-9b1030cac4409b262dca73d2f0741fe78d4998ee.tar.xz |
Added minetest.get_inventory(location)
Diffstat (limited to 'src')
-rw-r--r-- | src/script/common/c_content.cpp | 14 | ||||
-rw-r--r-- | src/script/common/c_content.h | 3 | ||||
-rw-r--r-- | src/script/cpp_api/s_client.cpp | 10 | ||||
-rw-r--r-- | src/script/lua_api/l_client.cpp | 22 | ||||
-rw-r--r-- | src/script/lua_api/l_client.h | 3 |
5 files changed, 43 insertions, 9 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 44b638ba7..9efa2c57f 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1307,6 +1307,20 @@ void push_tool_capabilities(lua_State *L, } /******************************************************************************/ +void push_inventory(lua_State *L, Inventory *inventory) +{ + std::vector<const InventoryList*> lists = inventory->getLists(); + std::vector<const InventoryList*>::iterator iter = lists.begin(); + lua_createtable(L, 0, lists.size()); + for (; iter != lists.end(); iter++) { + const char* name = (*iter)->getName().c_str(); + lua_pushstring(L, name); + push_inventory_list(L, inventory, name); + lua_rawset(L, -3); + } +} + +/******************************************************************************/ void push_inventory_list(lua_State *L, Inventory *inv, const char *name) { InventoryList *invlist = inv->getList(name); diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 8f32e58eb..5a8bf6700 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -116,6 +116,9 @@ void read_object_properties (lua_State *L, int index, void push_object_properties (lua_State *L, ObjectProperties *prop); +void push_inventory (lua_State *L, + Inventory *inventory); + void push_inventory_list (lua_State *L, Inventory *inv, const char *name); diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index 7f88d2f96..0d4e21876 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -228,15 +228,7 @@ bool ScriptApiClient::on_inventory_open(Inventory *inventory) lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_on_inventory_open"); - std::vector<const InventoryList*> lists = inventory->getLists(); - std::vector<const InventoryList*>::iterator iter = lists.begin(); - lua_createtable(L, 0, lists.size()); - for (; iter != lists.end(); iter++) { - const char* name = (*iter)->getName().c_str(); - lua_pushstring(L, name); - push_inventory_list(L, inventory, name); - lua_rawset(L, -3); - } + push_inventory(L, inventory); runCallbacks(1, RUN_CALLBACKS_MODE_OR); return readParam<bool>(L, -1); diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 2306fa578..5d0ccf2e0 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -456,6 +456,27 @@ int ModApiClient::l_dig_node(lua_State *L) return 0; } +// get_inventory(location) +int ModApiClient::l_get_inventory(lua_State *L) +{ + Client *client = getClient(L); + InventoryLocation inventory_location; + Inventory *inventory; + std::string location; + + location = readParam<std::string>(L, 1); + + try { + inventory_location.deSerialize(location); + inventory = client->getInventory(inventory_location); + push_inventory(L, inventory); + } catch (SerializationError) { + lua_pushnil(L); + } + + return 1; +} + void ModApiClient::Initialize(lua_State *L, int top) { API_FCT(get_current_modname); @@ -486,4 +507,5 @@ void ModApiClient::Initialize(lua_State *L, int top) API_FCT(send_damage); API_FCT(place_node); API_FCT(dig_node); + API_FCT(get_inventory); } diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h index 45c921f0d..21a540f8f 100644 --- a/src/script/lua_api/l_client.h +++ b/src/script/lua_api/l_client.h @@ -113,6 +113,9 @@ private: // dig_node(pos) static int l_dig_node(lua_State *L); + + // get_inventory(location) + static int l_get_inventory(lua_State *L); public: static void Initialize(lua_State *L, int top); }; |