Cuberite Forum
How to get attacked user/mob - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: How to get attacked user/mob (/thread-1689.html)



How to get attacked user/mob - Nathanchunkie - 12-21-2014

Hi,

Can't find in the API how to register the user or mob that is attacked by a cPlayer. Do I miss anything or is it impossible to log?

Code:
function OnTakeDamage(Receiver, TDI)
    
    if ((TDI.Attacker ~= nil) and TDI.Attacker:IsA("cPlayer")) then
        TDI.FinalDamage = 999;
        LOG("Player attacked a")
        LOG(Receiver);
    end

end



RE: How to get attacked user/mob - tonibm19 - 12-21-2014

Try:
ReceiverPlayer = tolua.cast(Receiver, "cPlayer")
Or
ReceiverMob = tolua.cast(Receiver, " cMonster")


RE: How to get attacked user/mob - xoft - 12-22-2014

The TDI.Attacker represents the attacker, it can be either a cPlayer object or a cMonster descendant. So you should first test it with the IsA() function and if it is a cPlayer, then cast it:
function OnTakeDamage(a_Receiver, a_TDI)
    if ((a_TDI.Attacker == nil) or not(a_TDI.Attacker:IsA("cPlayer"))) then
        -- The attacker is not a player
        return
    end

    -- Get the true cPlayer object out of a_TDI.Attacker:
    local Player = tolua.cast(a_TDI.Attacker, "cPlayer")

    -- Just show off what we can do with it:
    LOG("Player " .. Player:GetName() .. " attacked a " .. a_Receiver:GetClass())

    -- If the player attacked a cow, kick them
    if (a_Receiver:IsA("cCow")) then
        Player:Kick("The cow is a sacred animal")
    end
end