From 88df9fb5b6c78df9485e8bf3750e2608bd78e14c Mon Sep 17 00:00:00 2001 From: red-001 Date: Tue, 31 Jan 2017 13:18:52 +0000 Subject: Add `get_wielded_item` --- src/script/lua_api/l_base.cpp | 6 ++++++ src/script/lua_api/l_base.h | 3 +++ src/script/lua_api/l_client.cpp | 19 +++++++++++++++++++ src/script/lua_api/l_client.h | 3 ++- src/script/lua_api/l_env.cpp | 2 +- src/script/lua_api/l_inventory.cpp | 10 +++++----- src/script/lua_api/l_item.cpp | 26 +++++++++++++------------- src/script/lua_api/l_object.cpp | 2 +- 8 files changed, 50 insertions(+), 21 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/script/lua_api/l_base.cpp b/src/script/lua_api/l_base.cpp index f2703718a..dfe743b72 100644 --- a/src/script/lua_api/l_base.cpp +++ b/src/script/lua_api/l_base.cpp @@ -43,6 +43,12 @@ Client *ModApiBase::getClient(lua_State *L) return getScriptApiBase(L)->getClient(); } #endif + +IGameDef *ModApiBase::getGameDef(lua_State *L) +{ + return getScriptApiBase(L)->getGameDef(); +} + Environment *ModApiBase::getEnv(lua_State *L) { return getScriptApiBase(L)->getEnv(); diff --git a/src/script/lua_api/l_base.h b/src/script/lua_api/l_base.h index dc1b1b226..cd382629d 100644 --- a/src/script/lua_api/l_base.h +++ b/src/script/lua_api/l_base.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_types.h" #include "common/c_internal.h" +#include "gamedef.h" extern "C" { #include @@ -46,6 +47,8 @@ public: static Client* getClient(lua_State *L); #endif // !SERVER + static IGameDef* getGameDef(lua_State *L); + static Environment* getEnv(lua_State *L); static GUIEngine* getGuiEngine(lua_State *L); // When we are not loading the mod, this function returns "." diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 41e33889c..1673a62ce 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "gettext.h" #include "common/c_converter.h" #include "common/c_content.h" +#include "lua_api/l_item.h" int ModApiClient::l_get_current_modname(lua_State *L) { @@ -132,6 +133,23 @@ int ModApiClient::l_get_node_or_nil(lua_State *L) return 1; } +int ModApiClient::l_get_wielded_item(lua_State *L) +{ + Client *client = getClient(L); + + 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()); + } + return 1; +} + void ModApiClient::Initialize(lua_State *L, int top) { API_FCT(get_current_modname); @@ -143,4 +161,5 @@ void ModApiClient::Initialize(lua_State *L, int top) API_FCT(gettext); API_FCT(get_node); API_FCT(get_node_or_nil); + API_FCT(get_wielded_item); } diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h index 207a5bca0..def9b48a3 100644 --- a/src/script/lua_api/l_client.h +++ b/src/script/lua_api/l_client.h @@ -53,7 +53,8 @@ private: // get_node_or_nil(pos) static int l_get_node_or_nil(lua_State *L); - + // get_wielded_item() + static int l_get_wielded_item(lua_State *L); public: static void Initialize(lua_State *L, int top); diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 442c4b99a..14df558d3 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -472,7 +472,7 @@ int ModApiEnvMod::l_add_item(lua_State *L) // pos //v3f pos = checkFloatPos(L, 1); // item - ItemStack item = read_item(L, 2,getServer(L)); + ItemStack item = read_item(L, 2,getServer(L)->idef()); if(item.empty() || !item.isKnown(getServer(L)->idef())) return 0; diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index 38eade609..9a4aa845d 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -194,7 +194,7 @@ int InvRef::l_set_stack(lua_State *L) InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); int i = luaL_checknumber(L, 3) - 1; - ItemStack newitem = read_item(L, 4, getServer(L)); + ItemStack newitem = read_item(L, 4, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list != NULL && i >= 0 && i < (int) list->getSize()){ list->changeItem(i, newitem); @@ -295,7 +295,7 @@ int InvRef::l_add_item(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); - ItemStack item = read_item(L, 3, getServer(L)); + ItemStack item = read_item(L, 3, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list){ ItemStack leftover = list->addItem(item); @@ -315,7 +315,7 @@ int InvRef::l_room_for_item(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); - ItemStack item = read_item(L, 3, getServer(L)); + ItemStack item = read_item(L, 3, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list){ lua_pushboolean(L, list->roomForItem(item)); @@ -332,7 +332,7 @@ int InvRef::l_contains_item(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); - ItemStack item = read_item(L, 3, getServer(L)); + ItemStack item = read_item(L, 3, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list){ lua_pushboolean(L, list->containsItem(item)); @@ -349,7 +349,7 @@ int InvRef::l_remove_item(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkobject(L, 1); const char *listname = luaL_checkstring(L, 2); - ItemStack item = read_item(L, 3, getServer(L)); + ItemStack item = read_item(L, 3, getServer(L)->idef()); InventoryList *list = getlist(L, ref, listname); if(list){ ItemStack removed = list->removeItem(item); diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 9638740e8..7e6f457e1 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -190,7 +190,7 @@ int LuaItemStack::l_replace(lua_State *L) { NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); - o->m_stack = read_item(L,2,getServer(L)); + o->m_stack = read_item(L, 2, getGameDef(L)->idef()); lua_pushboolean(L, true); return 1; } @@ -250,7 +250,7 @@ int LuaItemStack::l_get_stack_max(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - lua_pushinteger(L, item.getStackMax(getServer(L)->idef())); + lua_pushinteger(L, item.getStackMax(getGameDef(L)->idef())); return 1; } @@ -260,7 +260,7 @@ int LuaItemStack::l_get_free_space(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - lua_pushinteger(L, item.freeSpace(getServer(L)->idef())); + lua_pushinteger(L, item.freeSpace(getGameDef(L)->idef())); return 1; } @@ -271,7 +271,7 @@ int LuaItemStack::l_is_known(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - bool is_known = item.isKnown(getServer(L)->idef()); + bool is_known = item.isKnown(getGameDef(L)->idef()); lua_pushboolean(L, is_known); return 1; } @@ -307,7 +307,7 @@ int LuaItemStack::l_get_tool_capabilities(lua_State *L) LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; const ToolCapabilities &prop = - item.getToolCapabilities(getServer(L)->idef()); + item.getToolCapabilities(getGameDef(L)->idef()); push_tool_capabilities(L, prop); return 1; } @@ -322,7 +322,7 @@ int LuaItemStack::l_add_wear(lua_State *L) LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; int amount = lua_tointeger(L, 2); - bool result = item.addWear(amount, getServer(L)->idef()); + bool result = item.addWear(amount, getGameDef(L)->idef()); lua_pushboolean(L, result); return 1; } @@ -334,8 +334,8 @@ int LuaItemStack::l_add_item(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - ItemStack newitem = read_item(L,-1, getServer(L)); - ItemStack leftover = item.addItem(newitem, getServer(L)->idef()); + ItemStack newitem = read_item(L, -1, getGameDef(L)->idef()); + ItemStack leftover = item.addItem(newitem, getGameDef(L)->idef()); create(L, leftover); return 1; } @@ -348,9 +348,9 @@ int LuaItemStack::l_item_fits(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaItemStack *o = checkobject(L, 1); ItemStack &item = o->m_stack; - ItemStack newitem = read_item(L, 2, getServer(L)); + ItemStack newitem = read_item(L, 2, getGameDef(L)->idef()); ItemStack restitem; - bool fits = item.itemFits(newitem, &restitem, getServer(L)->idef()); + bool fits = item.itemFits(newitem, &restitem, getGameDef(L)->idef()); lua_pushboolean(L, fits); // first return value create(L, restitem); // second return value return 2; @@ -407,7 +407,7 @@ ItemStack& LuaItemStack::getItem() int LuaItemStack::create_object(lua_State *L) { NO_MAP_LOCK_REQUIRED; - ItemStack item = read_item(L, 1, getServer(L)); + ItemStack item = read_item(L, 1, getGameDef(L)->idef()); LuaItemStack *o = new LuaItemStack(item); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); @@ -596,7 +596,7 @@ int ModApiItemMod::l_get_content_id(lua_State *L) NO_MAP_LOCK_REQUIRED; std::string name = luaL_checkstring(L, 1); - INodeDefManager *ndef = getServer(L)->getNodeDefManager(); + INodeDefManager *ndef = getGameDef(L)->getNodeDefManager(); content_t c = ndef->getId(name); lua_pushinteger(L, c); @@ -609,7 +609,7 @@ int ModApiItemMod::l_get_name_from_content_id(lua_State *L) NO_MAP_LOCK_REQUIRED; content_t c = luaL_checkint(L, 1); - INodeDefManager *ndef = getServer(L)->getNodeDefManager(); + INodeDefManager *ndef = getGameDef(L)->getNodeDefManager(); const char *name = ndef->get(c).name.c_str(); lua_pushstring(L, name); diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index be454ad45..d5681b809 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -364,7 +364,7 @@ int ObjectRef::l_set_wielded_item(lua_State *L) ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it - ItemStack item = read_item(L, 2, getServer(L)); + ItemStack item = read_item(L, 2, getServer(L)->idef()); bool success = co->setWieldedItem(item); if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) { getServer(L)->SendInventory(((PlayerSAO*)co)); -- cgit v1.2.3