Add “player” argument to the cancel callback

This commit is contained in:
Wuzzy 2014-07-17 16:07:44 +02:00
parent 86ed00c7c8
commit 54b18385c8
3 changed files with 11 additions and 16 deletions

View File

@ -98,8 +98,8 @@ Adds a new effect type for the Player Effects mods, so it can be later be applie
* `description` is the text which is exposed to the HUD and visible to the player. * `description` is the text which is exposed to the HUD and visible to the player.
* `icon`: This is optional an can be `nil`. It can be the file name of a texture. Should have a size of 16px×16px. In this case, this is the icon for the HUD. Basically this is just eye-candy. If this is `nil`, no icon is shown. The icon will be exposed to the HUD, iff `hidden` is `false`. * `icon`: This is optional an can be `nil`. It can be the file name of a texture. Should have a size of 16px×16px. In this case, this is the icon for the HUD. Basically this is just eye-candy. If this is `nil`, no icon is shown. The icon will be exposed to the HUD, iff `hidden` is `false`.
* `groups` is a table of strings to which the effect type is assigned to. * `groups` is a table of strings to which the effect type is assigned to.
* `apply` is a function which takes a player object. It is the player object to which the effect is applied to. This function isused by the framework to start the effect; it should only contain the gameplay-relevant stuff, the framework does the rest for you. * `apply`: See below.
* `cancel` is a function which takes an effect table. It is the effect which is to be cancelled. This function is called by the framework when the effect expires or is explicitly cancelled by other means.. * `cancel`: See below.
* `hidden` is an optional boolean value. Iff `true`, the effect description and icon will not be exposed to the player HUD. Otherwise, the effect is exposed. Default: `false` * `hidden` is an optional boolean value. Iff `true`, the effect description and icon will not be exposed to the player HUD. Otherwise, the effect is exposed. Default: `false`
* `cancel_on_death` is an optional boolean value. Iff true, the effect will be cancelled automatically when the player dies. Default: `true`. * `cancel_on_death` is an optional boolean value. Iff true, the effect will be cancelled automatically when the player dies. Default: `true`.
@ -118,7 +118,7 @@ The function may also return just `nil` on a normal success without metadata.
###### `cancel` function ###### `cancel` function
The `cancel` function is called by Player Effects when the effect is to be cancelled. Here the modder can do all the code which is needed to revert the changes an earlier `apply` call made. The `cancel` function is called by Player Effects when the effect is to be cancelled. Here the modder can do all the code which is needed to revert the changes an earlier `apply` call made.
`cancel` takes an `effect` as its only argument. Remember, this `effect` may also contain a field called `metadata`, which may have been added by an earlier `apply` call. `cancel` takes an `effect` as its first argument and a player object as its second argument. Remember, this `effect` may also contain a field called `metadata`, which may have been added by an earlier `apply` call. `player` is the player to which the effect is/was applied. This argument is just there for convenience reasons.
Player Effects does not care about the return value of this function. Player Effects does not care about the return value of this function.

View File

@ -34,8 +34,7 @@ playereffects.register_effect_type("blind", "Blind", nil, {},
return false return false
end end
end, end,
function(effect) function(effect, player)
local player = minetest.get_player_by_name(effect.playername)
player:hud_remove(effect.metadata.hudid) player:hud_remove(effect.metadata.hudid)
end end
) )
@ -46,8 +45,7 @@ playereffects.register_effect_type("high_speed", "High speed", nil, {"speed"},
player:set_physics_override(4,nil,nil) player:set_physics_override(4,nil,nil)
end, end,
function(effect) function(effect, player)
local player = minetest.get_player_by_name(effect.playername)
player:set_physics_override(1,nil,nil) player:set_physics_override(1,nil,nil)
end end
) )
@ -58,8 +56,7 @@ playereffects.register_effect_type("high_speed_hidden", "High speed", nil, {"spe
player:set_physics_override(4,nil,nil) player:set_physics_override(4,nil,nil)
end, end,
function(effect) function(effect, player)
local player = minetest.get_player_by_name(effect.playername)
player:set_physics_override(1,nil,nil) player:set_physics_override(1,nil,nil)
end, end,
true true
@ -73,8 +70,7 @@ playereffects.register_effect_type("low_speed", "Low speed", nil, {"speed"},
player:set_physics_override(0.25,nil,nil) player:set_physics_override(0.25,nil,nil)
end, end,
function(effect) function(effect, player)
local player = minetest.get_player_by_name(effect.playername)
player:set_physics_override(1,nil,nil) player:set_physics_override(1,nil,nil)
end end
) )
@ -84,8 +80,7 @@ playereffects.register_effect_type("highjump", "Greater jump height", "playereff
function(player) function(player)
player:set_physics_override(nil,2,nil) player:set_physics_override(nil,2,nil)
end, end,
function(effect) function(effect, player)
local player = minetest.get_player_by_name(effect.playername)
player:set_physics_override(nil,1,nil) player:set_physics_override(nil,1,nil)
end end
) )
@ -98,7 +93,7 @@ playereffects.register_effect_type("fly", "Fly mode available", "playereffects_e
privs.fly = true privs.fly = true
minetest.set_player_privs(playername, privs) minetest.set_player_privs(playername, privs)
end, end,
function(effect) function(effect, player)
local privs = minetest.get_player_privs(effect.playername) local privs = minetest.get_player_privs(effect.playername)
privs.fly = nil privs.fly = nil
minetest.set_player_privs(effect.playername, privs) minetest.set_player_privs(effect.playername, privs)
@ -111,7 +106,7 @@ playereffects.register_effect_type("fly", "Fly mode available", "playereffects_e
playereffects.register_effect_type("stress", "Stress Test Effect", nil, {}, playereffects.register_effect_type("stress", "Stress Test Effect", nil, {},
function(player) function(player)
end, end,
function(effect) function(effect, player)
end end
) )

View File

@ -192,7 +192,7 @@ function playereffects.cancel_effect(effect_id)
if(effect.hudids.icon_id~=nil) then if(effect.hudids.icon_id~=nil) then
player:hud_remove(effect.hudids.icon_id) player:hud_remove(effect.hudids.icon_id)
end end
playereffects.effect_types[effect.effect_type_id].cancel(effect) playereffects.effect_types[effect.effect_type_id].cancel(effect, player)
playereffects.effects[effect_id] = nil playereffects.effects[effect_id] = nil
minetest.log("action", "[playereffects] Effect type "..effect.effect_type_id.." cancelled from player "..effect.playername.."!") minetest.log("action", "[playereffects] Effect type "..effect.effect_type_id.." cancelled from player "..effect.playername.."!")
end end