Page 1 of 1

Capture player punch() event

PostPosted: Fri Feb 20, 2015 20:33
by stu
I have asked this before but never did get an an answer, I guess that it is probably not possible.
However, I was recently wondering if there might be some kind of metatable trickery I could perform on the player userdata.

I tried the following code but it does not work as I expected

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local my_player = nil
local player_wrapper = {}
local player_wrapper_mt = {}

function player_wrapper_mt:__index(key)
    local proto = rawget(self, "__proto__")
    local field = proto and proto[key]
    if type(field) ~= "function" then
        return field
    else
        return function (obj, ...)
            if obj == self then
                return field(proto, ...)
            else
                return field(obj, ...)
            end
        end
    end
end

function player_wrapper:new(player)
   return setmetatable({__proto__ = player}, player_wrapper_mt)
end

function player_wrapper:punch(puncher, time_from_last_punch, tool_capabilities, direction)
   print("Player punched!")
end

minetest.register_on_joinplayer(function(player)   
   my_player = player_wrapper:new(player)
end)


Any suggestions? I am pretty green when it comes to the more advanced Lua features

Re: Capture player punch() event

PostPosted: Sat Mar 07, 2015 16:41
by Sokomine
The whole "player looses health due to something" aspect still does not seem to be covered by the api. Mods don't know what killed the player and are unable to print suitable messages in the chat. That's a pity because specific messages would be far more intresting than random messages. And logging who killed whom when and where might help server admins identify troublemakers. To my knowledge, the api still can't provide that. Unless it changed in the last few weeks.

Re: Capture player punch() event

PostPosted: Wed Apr 01, 2015 21:48
by prestidigitator
From the Lua 5.1 reference manual:

You can replace the metatable of tables through the setmetatable function. You cannot change the metatable of other types from Lua (except by using the debug library); you must use the C API for that.


Player objects are userdata objects, not tables. That means they are implemented in the C++ engine code. You cannot add new fields to them from Lua, redefine existing fields, nor change their metatables (except perhaps using a debug hack). But even if you could, your wrapper would intercept calls from mods, but wouldn't necessarily have a chance to do anything when the built-in action of the player punching due to a mouse click is performed (most or all of that is done by the game engine behind the scenes, without participation from Lua). The other option would be to use the on_use callback (on all tools including the hand?), but this unfortunately replaces the default behavior, so you'd have to recreate the whole tool/damage system. And remember that there are other ways of taking damage that have no Lua hooks (touching nodes like fire, falling, etc.).

Re: Capture player punch() event

PostPosted: Thu Apr 02, 2015 01:11
by Minetestforfun
Sokomine wrote:The whole "player looses health due to something" aspect still does not seem to be covered by the api. Mods don't know what killed the player and are unable to print suitable messages in the chat. That's a pity because specific messages would be far more intresting than random messages. And logging who killed whom when and where might help server admins identify troublemakers. To my knowledge, the api still can't provide that. Unless it changed in the last few weeks.


Indeed it's a problem, we need this feature, for logs mainly but also this would allow more features like "hall of fame", for example in PVP server...

Re: Capture player punch() event

PostPosted: Mon Apr 06, 2015 03:29
by Sokomine
There really ought to be a way to at least react to damage a player took on the lua-side. Is there still no way to at least print a message what caused a player to die?

Re: Capture player punch() event

PostPosted: Thu Apr 09, 2015 18:35
by BrandonReese
I wrote this a while back but it needs to be rebased and probably cleaned up a bit. This adds a callback in lua when a player is punched. It's been a while but I believe it works just like entity on_punch

https://github.com/minetest/minetest/pull/1307

Re: Capture player punch() event

PostPosted: Thu Apr 09, 2015 19:10
by Minetestforfun
Since 1 year and this is not merged... We need this feature

Re: Capture player punch() event

PostPosted: Fri May 15, 2015 21:25
by TeTpaAka
I rebased it and it got merged today. So current master has this feature.

Re: Capture player punch() event

PostPosted: Fri May 15, 2015 21:47
by stu
TeTpaAka wrote:I rebased it and it got merged today. So current master has this feature.

This will be very useful, thank you to everyone involved for their efforts to make this finally happen. \o/

Re: Capture player punch() event

PostPosted: Sat May 16, 2015 21:36
by Minetestforfun
TeTpaAka wrote:I rebased it and it got merged today. So current master has this feature.


Thank you very much for this function :)

Re: Capture player punch() event

PostPosted: Sat May 16, 2015 21:57
by TeTpaAka
stu wrote:
TeTpaAka wrote:I rebased it and it got merged today. So current master has this feature.

This will be very useful, thank you to everyone involved for their efforts to make this finally happen. \o/
Minetestforfun wrote:
TeTpaAka wrote:I rebased it and it got merged today. So current master has this feature.


Thank you very much for this function :)

Thank you. Though it was mostly BrandonReese's code.