From e462a9a5ef46776d974203bb44e0b89e8c980b29 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Wed, 7 Aug 2019 19:16:31 +0200 Subject: Unify wield item handling (#8677) This moves the wield item functions to Player and the tool utils for range calculation Also 'local_inventory' was removed due to redundancy in Client --- src/script/lua_api/l_client.cpp | 16 ++++++---------- src/script/lua_api/l_object.cpp | 14 ++++++++------ 2 files changed, 14 insertions(+), 16 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 6d9d832b7..6345fc75f 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -210,17 +210,13 @@ int ModApiClient::l_get_language(lua_State *L) int ModApiClient::l_get_wielded_item(lua_State *L) { Client *client = getClient(L); + LocalPlayer *player = client->getEnv().getLocalPlayer(); + if (!player) + return 0; - Inventory local_inventory(client->idef()); - client->getLocalInventory(local_inventory); - - InventoryList *mlist = local_inventory.getList("main"); - - if (mlist && client->getPlayerItem() < mlist->getSize()) { - LuaItemStack::create(L, mlist->getItem(client->getPlayerItem())); - } else { - LuaItemStack::create(L, ItemStack()); - } + ItemStack selected_item; + player->getWieldedItem(&selected_item, nullptr); + LuaItemStack::create(L, selected_item); return 1; } diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index ad8554834..5dba63159 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -308,8 +308,9 @@ int ObjectRef::l_get_wield_list(lua_State *L) NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); - if (co == NULL) return 0; - // Do it + if (!co) + return 0; + lua_pushstring(L, co->getWieldList().c_str()); return 1; } @@ -320,8 +321,9 @@ int ObjectRef::l_get_wield_index(lua_State *L) NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); - if (co == NULL) return 0; - // Do it + if (!co) + return 0; + lua_pushinteger(L, co->getWieldIndex() + 1); return 1; } @@ -332,12 +334,12 @@ int ObjectRef::l_get_wielded_item(lua_State *L) NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); - if (co == NULL) { + if (!co) { // Empty ItemStack LuaItemStack::create(L, ItemStack()); return 1; } - // Do it + LuaItemStack::create(L, co->getWieldedItem()); return 1; } -- cgit v1.2.3