From 54b18385c83b199a0d15094182c298a9d524ecfa Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Thu, 17 Jul 2014 16:07:44 +0200 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=9Cplayer=E2=80=9D=20argument=20to?= =?UTF-8?q?=20the=20cancel=20callback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- examples.lua | 19 +++++++------------ init.lua | 2 +- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 52e662d..924f601 100644 --- a/README.md +++ b/README.md @@ -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. * `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. -* `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. -* `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.. +* `apply`: See below. +* `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` * `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 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. diff --git a/examples.lua b/examples.lua index d1bc082..80e05a1 100644 --- a/examples.lua +++ b/examples.lua @@ -34,8 +34,7 @@ playereffects.register_effect_type("blind", "Blind", nil, {}, return false end end, - function(effect) - local player = minetest.get_player_by_name(effect.playername) + function(effect, player) player:hud_remove(effect.metadata.hudid) end ) @@ -46,8 +45,7 @@ playereffects.register_effect_type("high_speed", "High speed", nil, {"speed"}, player:set_physics_override(4,nil,nil) end, - function(effect) - local player = minetest.get_player_by_name(effect.playername) + function(effect, player) player:set_physics_override(1,nil,nil) end ) @@ -58,8 +56,7 @@ playereffects.register_effect_type("high_speed_hidden", "High speed", nil, {"spe player:set_physics_override(4,nil,nil) end, - function(effect) - local player = minetest.get_player_by_name(effect.playername) + function(effect, player) player:set_physics_override(1,nil,nil) end, true @@ -73,8 +70,7 @@ playereffects.register_effect_type("low_speed", "Low speed", nil, {"speed"}, player:set_physics_override(0.25,nil,nil) end, - function(effect) - local player = minetest.get_player_by_name(effect.playername) + function(effect, player) player:set_physics_override(1,nil,nil) end ) @@ -84,8 +80,7 @@ playereffects.register_effect_type("highjump", "Greater jump height", "playereff function(player) player:set_physics_override(nil,2,nil) end, - function(effect) - local player = minetest.get_player_by_name(effect.playername) + function(effect, player) player:set_physics_override(nil,1,nil) end ) @@ -98,7 +93,7 @@ playereffects.register_effect_type("fly", "Fly mode available", "playereffects_e privs.fly = true minetest.set_player_privs(playername, privs) end, - function(effect) + function(effect, player) local privs = minetest.get_player_privs(effect.playername) privs.fly = nil 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, {}, function(player) end, - function(effect) + function(effect, player) end ) diff --git a/init.lua b/init.lua index c11b2d2..647f52c 100644 --- a/init.lua +++ b/init.lua @@ -192,7 +192,7 @@ function playereffects.cancel_effect(effect_id) if(effect.hudids.icon_id~=nil) then player:hud_remove(effect.hudids.icon_id) 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 minetest.log("action", "[playereffects] Effect type "..effect.effect_type_id.." cancelled from player "..effect.playername.."!") end