From c400ac1ed9fd6d5dfa9f498a093270de03e01c9e Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 15 Jul 2014 00:38:01 +0200 Subject: [PATCH] Add cancel_on_death parameter for effect types. This optional parameter controls wheather the effect shall be cancelled when the player dies. By default, this is true. --- README.md | 9 ++++++--- examples.lua | 10 ++++++---- init.lua | 12 ++++++++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 087f9ba..3f29bc1 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ An effect type is a description of what is later to be concretely applied as an * `cancel`: Function to be called when effect is cancelled. See `playereffects.register_effect_type`. * `icon`: This is optional. It can be the file name of a texture. Should have a size of 16px×16px. Will be exposed to the HUD, iff `hidden` is `false`. * `hidden`: Iff this is false, it will not be exposed to the HUD when this effect is active. +* `cancel_on_death`: Iff this is true, the effect will be cancelled automatically when the player dies. Normally you don’t need to read or edit fields of this table. Use `playereffects.register_effect_type` to add a new effect type to Player Effects. @@ -85,7 +86,7 @@ You should normally not need to care about these internally used fields. ### Functions -#### `playereffects.register_effect_type(effect_type_id, description, icon, groups, apply, cancel, hidden)` +#### `playereffects.register_effect_type(effect_type_id, description, icon, groups, apply, cancel, hidden, cancel_on_death)` Adds a new effect type for the Player Effects mods, so it can be later be applied to players. ##### Parameters @@ -95,7 +96,9 @@ Adds a new effect type for the Player Effects mods, so it can be later be applie * `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.. -* `hidden` is an optional boolean value. Idf true, the effect description and icon will not be exposed to the player HUD. Otherwise, the effect is exposed. +* `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`. + ###### `apply` function The `apply` function is a callback function which is called by Player Effects. Here the modder can put all the gameplay-relevant code. @@ -187,7 +190,7 @@ These commands apply (or try to) apply an effect to you. You will get a response * `slow`: Makes you slower for 120s. * `hfast`: Makes you faster for 10s. This is a hidden effect and is not exposed to the HUD. * `highjump`: Increases your jump height for 20s. -* `fly`: Gives you the `fly` privilege for a short time. Better don’t mess around with this privilege manually when you use this. +* `fly`: Gives you the `fly` privilege for a minute. You keep the privilege even when you die. Better don’t mess around with this privilege manually when you use this. * `blind`: Tints the whole screen black for 5 seconds. This is highly experimental and will be drawn over many existing HUD elements. In other words, prepare your HUD to be messed up. * `null`: Tries to apply an effect which always fails. This demonstrates the failure of effects. diff --git a/examples.lua b/examples.lua index e8fca66..d1bc082 100644 --- a/examples.lua +++ b/examples.lua @@ -90,7 +90,7 @@ playereffects.register_effect_type("highjump", "Greater jump height", "playereff end ) --- Adds the “fly” privilege +-- Adds the “fly” privilege. Keep the privilege even if the player dies playereffects.register_effect_type("fly", "Fly mode available", "playereffects_example_fly.png", {"fly"}, function(player) local playername = player:get_player_name() @@ -102,7 +102,9 @@ playereffects.register_effect_type("fly", "Fly mode available", "playereffects_e local privs = minetest.get_player_privs(effect.playername) privs.fly = nil minetest.set_player_privs(effect.playername, privs) - end + end, + false, -- not hidden + false -- do NOT cancel the effect on death ) -- Dummy effect for the stree test @@ -174,10 +176,10 @@ minetest.register_chatcommand("highjump", { minetest.register_chatcommand("fly", { params = "", - description = "Grants you the fly privilege for a short time.", + description = "Grants you the fly privilege for a minute. You keep the effect when you die.", privs = {}, func = function(name, param) - local ret = playereffects.apply_effect_type("fly", 20, minetest.get_player_by_name(name)) + local ret = playereffects.apply_effect_type("fly", 60, minetest.get_player_by_name(name)) notify(name, ret) end, }) diff --git a/init.lua b/init.lua index fe7eb32..90fb347 100644 --- a/init.lua +++ b/init.lua @@ -47,7 +47,7 @@ function playereffects.next_effect_id() end --[=[ API functions ]=] -function playereffects.register_effect_type(effect_type_id, description, icon, groups, apply, cancel, hidden) +function playereffects.register_effect_type(effect_type_id, description, icon, groups, apply, cancel, hidden, cancel_on_death) effect_type = {} effect_type.description = description effect_type.apply = apply @@ -63,6 +63,12 @@ function playereffects.register_effect_type(effect_type_id, description, icon, g else effect_type.hidden = false end + if cancel_on_death ~= nil then + effect_type.cancel_on_death = cancel_on_death + else + effect_type.cancel_on_death = true + end + playereffects.effect_types[effect_type_id] = effect_type minetest.log("action", "[playereffects] Effect type "..effect_type_id.." registered!") end @@ -257,7 +263,9 @@ end minetest.register_on_dieplayer(function(player) local effects = playereffects.get_player_effects(player:get_player_name()) for e=1,#effects do - playereffects.cancel_effect(effects[e].effect_id) + if(playereffects.effect_types[effects[e].effect_type_id].cancel_on_death == true) then + playereffects.cancel_effect(effects[e].effect_id) + end end end)