Fixes around ServerActiveObject on_punch handling
authorsfan5 <sfan5@live.de>
Wed, 4 Mar 2020 18:13:26 +0000 (19:13 +0100)
committersfan5 <sfan5@live.de>
Thu, 5 Mar 2020 21:04:23 +0000 (22:04 +0100)
doc/lua_api.txt
src/content_sao.cpp
src/network/serverpackethandler.cpp

index acf2f77c15c1b91bbf84516f7c7b040c7d3e7f81..a603a5a37e7db23e99fbfc6cde661a0d218e1ee1 100644 (file)
@@ -3900,6 +3900,7 @@ Callbacks:
     * `dir`: unit vector of direction of punch. Always defined. Points from the
       puncher to the punched.
     * `damage`: damage that will be done to entity.
+    * Can return `true` to prevent the default damage mechanism.
 * `on_death(self, killer)`
     * Called when the object dies.
     * `killer`: an `ObjectRef` (can be `nil`)
index 5119223a7c43493addcdfd1b02c74eaf27f4362b..75c3eaf37c87f86c48e81418d99c7325ea4ba6aa 100644 (file)
@@ -653,7 +653,7 @@ u16 LuaEntitySAO::punch(v3f dir,
        if (!damage_handled) {
                if (result.did_punch) {
                        setHP((s32)getHP() - result.damage,
-                               PlayerHPChangeReason(PlayerHPChangeReason::SET_HP));
+                               PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
 
                        std::string str = gob_cmd_punched(getHP());
                        // create message and add to list
@@ -663,10 +663,10 @@ u16 LuaEntitySAO::punch(v3f dir,
        }
 
        if (getHP() == 0 && !isGone()) {
-               m_pending_removal = true;
                clearParentAttachment();
                clearChildAttachments();
                m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
+               m_pending_removal = true;
        }
 
        actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
@@ -675,6 +675,7 @@ u16 LuaEntitySAO::punch(v3f dir,
                        "), damage=" << (old_hp - (s32)getHP()) <<
                        (damage_handled ? " (handled by Lua)" : "") << std::endl;
 
+       // TODO: give Lua control over wear
        return result.wear;
 }
 
index 7a42ce5ef43a352b7c2b40d0578c3f13b36e8017..23bcc867f7d93588a17908b27119ee3307e87564 100644 (file)
@@ -1166,6 +1166,8 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
                        u16 wear = pointed_object->punch(dir, &toolcap, playersao,
                                        time_from_last_punch);
 
+                       // Callback may have changed item, so get it again
+                       playersao->getWieldedItem(&selected_item);
                        bool changed = selected_item.addWear(wear, m_itemdef);
                        if (changed)
                                playersao->setWieldedItem(selected_item);