Call on_secondary_use when object is right-clicked
authorsfan5 <sfan5@live.de>
Sat, 9 Nov 2019 23:36:29 +0000 (00:36 +0100)
committersfan5 <sfan5@live.de>
Sun, 10 Nov 2019 12:12:31 +0000 (13:12 +0100)
doc/lua_api.txt
src/network/serverpackethandler.cpp
src/script/cpp_api/s_item.cpp
src/script/cpp_api/s_item.h

index f7cb4ca7c8530b333fca9e412ff59b26b8e68bc4..9ca44747fc6bffeff9190eaa6658a23300cadd02 100644 (file)
@@ -6432,9 +6432,9 @@ Used by `minetest.register_node`, `minetest.register_craftitem`, and
         -- default: minetest.item_place
 
         on_secondary_use = function(itemstack, user, pointed_thing),
-        -- Same as on_place but called when pointing at nothing.
+        -- Same as on_place but called when not pointing at a node.
         -- The user may be any ObjectRef or nil.
-        -- pointed_thing: always { type = "nothing" }
+        -- default: nil
 
         on_drop = function(itemstack, dropper, pos),
         -- Shall drop item and return the leftover itemstack.
index d8fbeebd55c6a1ef55adfb117156c89b49e6fe2a..7a42ce5ef43a352b7c2b40d0578c3f13b36e8017 100644 (file)
@@ -1316,6 +1316,13 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
                                        << pointed_object->getDescription() << std::endl;
 
                        // Do stuff
+                       if (m_script->item_OnSecondaryUse(
+                                       selected_item, playersao, pointed)) {
+                               if (playersao->setWieldedItem(selected_item)) {
+                                       SendInventory(playersao, true);
+                               }
+                       }
+
                        pointed_object->rightClick(playersao);
                } else if (m_script->item_OnPlace(
                                selected_item, playersao, pointed)) {
@@ -1376,8 +1383,10 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
                actionstream << player->getName() << " activates "
                                << selected_item.name << std::endl;
 
+               pointed.type = POINTEDTHING_NOTHING; // can only ever be NOTHING
+
                if (m_script->item_OnSecondaryUse(
-                               selected_item, playersao)) {
+                               selected_item, playersao, pointed)) {
                        if (playersao->setWieldedItem(selected_item)) {
                                SendInventory(playersao, true);
                        }
index cbdfcf1b1476052fe42b317d7583ed99038d3798..24955cefcb161ed7568480312fa9eb7d5469f9bf 100644 (file)
@@ -115,7 +115,8 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
        return true;
 }
 
-bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *user)
+bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item,
+               ServerActiveObject *user, const PointedThing &pointed)
 {
        SCRIPTAPI_PRECHECKHEADER
 
@@ -126,8 +127,6 @@ bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *use
 
        LuaItemStack::create(L, item);
        objectrefGetOrCreate(L, user);
-       PointedThing pointed;
-       pointed.type = POINTEDTHING_NOTHING;
        pushPointedThing(pointed);
        PCALL_RES(lua_pcall(L, 3, 1, error_handler));
        if (!lua_isnil(L, -1)) {
index 6c7f286a92652e63c63efcf26155461a79197ced..ad229f73e18c9da62f9fec238a4f926796caccd2 100644 (file)
@@ -42,7 +42,7 @@ public:
        bool item_OnUse(ItemStack &item,
                        ServerActiveObject *user, const PointedThing &pointed);
        bool item_OnSecondaryUse(ItemStack &item,
-                       ServerActiveObject *user);
+                       ServerActiveObject *user, const PointedThing &pointed);
        bool item_OnCraft(ItemStack &item, ServerActiveObject *user,
                        const InventoryList *old_craft_grid, const InventoryLocation &craft_inv);
        bool item_CraftPredict(ItemStack &item, ServerActiveObject *user,