Capture player punch() event

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

Capture player punch() event

by stu » Fri Feb 20, 2015 20:33

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
 

Sokomine
Member
 
Posts: 2980
Joined: Sun Sep 09, 2012 17:31

Re: Capture player punch() event

by Sokomine » Sat Mar 07, 2015 16:41

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.
A list of my mods can be found here.
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

Re: Capture player punch() event

by prestidigitator » Wed Apr 01, 2015 21:48

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.).
 

User avatar
Minetestforfun
Member
 
Posts: 936
Joined: Tue Aug 05, 2014 14:09
GitHub: Darcidride
IRC: Darcidride + MinetestForFun
In-game: Darcidride + MinetestForFun

Re: Capture player punch() event

by Minetestforfun » Thu Apr 02, 2015 01:11

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...
 

Sokomine
Member
 
Posts: 2980
Joined: Sun Sep 09, 2012 17:31

Re: Capture player punch() event

by Sokomine » Mon Apr 06, 2015 03:29

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?
A list of my mods can be found here.
 

User avatar
BrandonReese
Member
 
Posts: 836
Joined: Wed Sep 12, 2012 00:44
GitHub: bremaweb
IRC: BrandonReese
In-game: BrandonReese

Re: Capture player punch() event

by BrandonReese » Thu Apr 09, 2015 18:35

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
Last edited by BrandonReese on Fri Apr 10, 2015 18:25, edited 1 time in total.
 

User avatar
Minetestforfun
Member
 
Posts: 936
Joined: Tue Aug 05, 2014 14:09
GitHub: Darcidride
IRC: Darcidride + MinetestForFun
In-game: Darcidride + MinetestForFun

Re: Capture player punch() event

by Minetestforfun » Thu Apr 09, 2015 19:10

Since 1 year and this is not merged... We need this feature
 

TeTpaAka
Member
 
Posts: 131
Joined: Sat Dec 28, 2013 21:54

Re: Capture player punch() event

by TeTpaAka » Fri May 15, 2015 21:25

I rebased it and it got merged today. So current master has this feature.
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

Re: Capture player punch() event

by stu » Fri May 15, 2015 21:47

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/
 

User avatar
Minetestforfun
Member
 
Posts: 936
Joined: Tue Aug 05, 2014 14:09
GitHub: Darcidride
IRC: Darcidride + MinetestForFun
In-game: Darcidride + MinetestForFun

Re: Capture player punch() event

by Minetestforfun » Sat May 16, 2015 21:36

TeTpaAka wrote:I rebased it and it got merged today. So current master has this feature.


Thank you very much for this function :)
 

TeTpaAka
Member
 
Posts: 131
Joined: Sat Dec 28, 2013 21:54

Re: Capture player punch() event

by TeTpaAka » Sat May 16, 2015 21:57

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.
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 8 guests

cron