Skip to content

Commit

Permalink
Fixes around ServerActiveObject on_punch handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 4, 2020
1 parent 0c08f94 commit 4bd1df9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -3895,6 +3895,8 @@ 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.
* Should return a boolean indicating whether damage should be applied by
the engine.
* `on_death(self, killer)`
* Called when the object dies.
* `killer`: an `ObjectRef` (can be `nil`)
Expand Down
5 changes: 3 additions & 2 deletions src/content_sao.cpp
Expand Up @@ -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
Expand All @@ -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() <<
Expand All @@ -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;
}

Expand Down
6 changes: 4 additions & 2 deletions src/network/serverpackethandler.cpp
Expand Up @@ -1150,8 +1150,8 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
if (pointed_object->isGone())
return;

ItemStack selected_item, hand_item;
ItemStack tool_item = playersao->getWieldedItem(&selected_item, &hand_item);
ItemStack selected_item;
ItemStack tool_item = playersao->getWieldedItem(&selected_item, nullptr);
ToolCapabilities toolcap =
tool_item.getToolCapabilities(m_itemdef);
v3f dir = (pointed_object->getBasePosition() -
Expand All @@ -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);
Expand Down

0 comments on commit 4bd1df9

Please sign in to comment.