Add `get_wielded_item`
authorred-001 <red-001@outlook.ie>
Tue, 31 Jan 2017 13:18:52 +0000 (13:18 +0000)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Mon, 13 Mar 2017 22:56:05 +0000 (23:56 +0100)
15 files changed:
clientmods/preview/init.lua
doc/client_lua_api.txt
src/gamedef.h
src/script/clientscripting.cpp
src/script/common/c_content.cpp
src/script/common/c_content.h
src/script/cpp_api/s_item.cpp
src/script/lua_api/l_base.cpp
src/script/lua_api/l_base.h
src/script/lua_api/l_client.cpp
src/script/lua_api/l_client.h
src/script/lua_api/l_env.cpp
src/script/lua_api/l_inventory.cpp
src/script/lua_api/l_item.cpp
src/script/lua_api/l_object.cpp

index db4f283509f9257dd4ad83c5ea7a9ab0a8610665..2ca4594d3a76e105d80cdcfc049a2f922dd9dc16 100644 (file)
@@ -60,6 +60,28 @@ end)
 
 core.register_on_punchnode(function(pos, node)
        print("The local player punched a node!")
+       local itemstack = core.get_wielded_item()
+       --[[
+       -- getters
+       print(dump(itemstack:is_empty()))
+       print(dump(itemstack:get_name()))
+       print(dump(itemstack:get_count()))
+       print(dump(itemstack:get_wear()))
+       print(dump(itemstack:get_meta()))
+       print(dump(itemstack:get_metadata()))
+       print(dump(itemstack:is_known()))
+       --print(dump(itemstack:get_definition()))
+       print(dump(itemstack:get_tool_capabilities()))
+       print(dump(itemstack:to_string()))
+       print(dump(itemstack:to_table()))
+       -- setters
+       print(dump(itemstack:set_name("default:dirt")))
+       print(dump(itemstack:set_count("95")))
+       print(dump(itemstack:set_wear(934)))
+       print(dump(itemstack:get_meta()))
+       print(dump(itemstack:get_metadata()))
+       --]]
+       print(dump(itemstack:to_table()))
        print("pos:" .. dump(pos))
        print("node:" .. dump(node))
        return false
index 5ffffe938a2bcfa3d480d461e761bfcd34f6fcd5..7440c4014a8dbe9fd794161bcdd627e9601fe906 100644 (file)
@@ -721,6 +721,7 @@ Call these functions only at load time!
 * `minetest.after(time, func, ...)`
     * Call the function `func` after `time` seconds, may be fractional
     * Optional: Variable number of arguments that are passed to `func`
+
 ### Map
 * `minetest.get_node(pos)`
     * Returns the node at the given position as table in the format
@@ -728,6 +729,11 @@ Call these functions only at load time!
       for unloaded areas.
 * `minetest.get_node_or_nil(pos)`
     * Same as `get_node` but returns `nil` for unloaded areas.
+
+### Player
+* `minetest.get_wielded_item()`
+    * Returns the itemstack the local player is holding 
+
 ### Misc.
 * `minetest.parse_json(string[, nullvalue])`: returns something
     * Convert a string containing JSON data into the Lua equivalent
index 16b53e24f7b5a7e173c83b059da5b03e5df7d8ca..593d27e3045e4c8c10212567483ce1f612a5aeb0 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #define GAMEDEF_HEADER
 
 #include <string>
+#include <vector>
 #include "irrlichttypes.h"
 
 class IItemDefManager;
index aaed2865dd0a03ff231dea891feafd6c7aa7abc4..c1e308012130ff9dfb4157d85cdbf93a021a5f20 100644 (file)
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "lua_api/l_storage.h"
 #include "lua_api/l_sound.h"
 #include "lua_api/l_util.h"
+#include "lua_api/l_item.h"
 
 ClientScripting::ClientScripting(Client *client):
        ScriptApiBase()
@@ -55,4 +56,6 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
        ModApiClient::Initialize(L, top);
        ModApiSound::Initialize(L, top);
        ModApiStorage::Initialize(L, top);
+
+       LuaItemStack::Register(L);
 }
index a963856b7d61446d20ee4bac22988db3252d3749..99e12cd829bbd8b1044eaa94642969f0c288bf37 100644 (file)
@@ -20,7 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "common/c_converter.h"
 #include "common/c_types.h"
 #include "nodedef.h"
-#include "itemdef.h"
 #include "object_properties.h"
 #include "cpp_api/s_node.h"
 #include "lua_api/l_object.h"
@@ -784,7 +783,7 @@ bool string_to_enum(const EnumString *spec, int &result,
 }
 
 /******************************************************************************/
-ItemStack read_item(lua_State* L, int index,Server* srv)
+ItemStack read_item(lua_State* L, int index, IItemDefManager *idef)
 {
        if(index < 0)
                index = lua_gettop(L) + 1 + index;
@@ -803,7 +802,6 @@ ItemStack read_item(lua_State* L, int index,Server* srv)
        {
                // Convert from itemstring
                std::string itemstring = lua_tostring(L, index);
-               IItemDefManager *idef = srv->idef();
                try
                {
                        ItemStack item;
@@ -820,7 +818,6 @@ ItemStack read_item(lua_State* L, int index,Server* srv)
        else if(lua_istable(L, index))
        {
                // Convert from table
-               IItemDefManager *idef = srv->idef();
                std::string name = getstringfield_default(L, index, "name", "");
                int count = getintfield_default(L, index, "count", 1);
                int wear = getintfield_default(L, index, "wear", 0);
@@ -1187,7 +1184,7 @@ std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
                if (items.size() < (u32) key) {
                        items.resize(key);
                }
-               items[key - 1] = read_item(L, -1, srv);
+               items[key - 1] = read_item(L, -1, srv->idef());
                lua_pop(L, 1);
        }
        return items;
index 9641f5c9eec8a534cebd6e62a9b61dbbd854fedf..10cccbb0173c83c9e07c42ef0d5d642f07499737 100644 (file)
@@ -38,6 +38,7 @@ extern "C" {
 #include "irrlichttypes_bloated.h"
 #include "util/string.h"
 #include "itemgroup.h"
+#include "itemdef.h"
 
 namespace Json { class Value; }
 
@@ -77,7 +78,7 @@ void               push_dig_params           (lua_State *L,
 void               push_hit_params           (lua_State *L,
                                               const HitParams &params);
 
-ItemStack          read_item                 (lua_State *L, int index, Server *srv);
+ItemStack          read_item                 (lua_State *L, int index, IItemDefManager *idef);
 
 struct TileAnimationParams read_animation_definition(lua_State *L, int index);
 
index 3c84fb8cf60763249e3532200294acbdab36a8c5..cbb83380735095033d89f11660aa374585422f20 100644 (file)
@@ -47,7 +47,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
        PCALL_RES(lua_pcall(L, 3, 1, error_handler));
        if (!lua_isnil(L, -1)) {
                try {
-                       item = read_item(L,-1, getServer());
+                       item = read_item(L, -1, getServer()->idef());
                } catch (LuaError &e) {
                        throw LuaError(std::string(e.what()) + ". item=" + item.name);
                }
@@ -74,7 +74,7 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
        PCALL_RES(lua_pcall(L, 3, 1, error_handler));
        if (!lua_isnil(L, -1)) {
                try {
-                       item = read_item(L,-1, getServer());
+                       item = read_item(L, -1, getServer()->idef());
                } catch (LuaError &e) {
                        throw LuaError(std::string(e.what()) + ". item=" + item.name);
                }
@@ -101,7 +101,7 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
        PCALL_RES(lua_pcall(L, 3, 1, error_handler));
        if(!lua_isnil(L, -1)) {
                try {
-                       item = read_item(L,-1, getServer());
+                       item = read_item(L, -1, getServer()->idef());
                } catch (LuaError &e) {
                        throw LuaError(std::string(e.what()) + ". item=" + item.name);
                }
@@ -127,7 +127,7 @@ bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *use
        PCALL_RES(lua_pcall(L, 3, 1, error_handler));
        if (!lua_isnil(L, -1)) {
                try {
-                       item = read_item(L, -1, getServer());
+                       item = read_item(L, -1, getServer()->idef());
                } catch (LuaError &e) {
                        throw LuaError(std::string(e.what()) + ". item=" + item.name);
                }
@@ -159,7 +159,7 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
        PCALL_RES(lua_pcall(L, 4, 1, error_handler));
        if (!lua_isnil(L, -1)) {
                try {
-                       item = read_item(L,-1, getServer());
+                       item = read_item(L, -1, getServer()->idef());
                } catch (LuaError &e) {
                        throw LuaError(std::string(e.what()) + ". item=" + item.name);
                }
@@ -191,7 +191,7 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
        PCALL_RES(lua_pcall(L, 4, 1, error_handler));
        if (!lua_isnil(L, -1)) {
                try {
-                       item = read_item(L,-1, getServer());
+                       item = read_item(L, -1, getServer()->idef());
                } catch (LuaError &e) {
                        throw LuaError(std::string(e.what()) + ". item=" + item.name);
                }
index f2703718a09fea3bbef804361bebff743aeaebfa..dfe743b72cbb1510f40ff14dff3f3eef46bfcf44 100644 (file)
@@ -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();
index dc1b1b2260c83991558970d7218d62abe2f3bee3..cd382629dcebf54de2804cd1c88c9fe9e7d124fa 100644 (file)
@@ -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 <lua.h>
@@ -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 "."
index 41e33889cc45cc370547ddca1eb4add0a1486bd4..1673a62ce5828ac1da25e451c1b31bff5901e3ec 100644 (file)
@@ -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);
 }
index 207a5bca08701fb7975ecf46ef1bc16ccd05a3af..def9b48a39ca6e68c4f975da444fc37819e8737e 100644 (file)
@@ -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);
index 442c4b99a1b45c29a7a16185f6aaa4213f403ce2..14df558d311a16da9107d2a339ae19646fbb2f0a 100644 (file)
@@ -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;
 
index 38eade6094b9dffb03f139213ad1e8fb7e23a268..9a4aa845df1afcf197848563b2e3c631ff5d02f1 100644 (file)
@@ -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);
index 9638740e8068e66dfb5681c82688eb722ca862ca..7e6f457e161029dc68da10179476cc6c98f522d4 100644 (file)
@@ -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);
index be454ad4513f18f60c0df9bd39f0c18ff4f56a43..d5681b809576f0fe4ab1f485e4e05228ffd54f45 100644 (file)
@@ -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));