Check for correct player in apply_effect_type
[minetest_playereffects.git] / examples.lua
blobba798f7bc8acd955b9c1ef35f0b12bfa175d518b
1 ----- EXAMPLE EFFECT TYPES -----
2 --[[ Null effect. The apply function always returns false, which means applying the
3 effect will never succeed ]]
4 playereffects.register_effect_type("null", "No effect", nil, {},
5 function()
6 return false
7 end
11 -- Makes the player screen black for 5 seconds (very experimental!)
12 playereffects.register_effect_type("blind", "Blind", nil, {},
13 function(player)
14 local hudid = player:hud_add({
15 hud_elem_type = "image",
16 position = { x=0.5, y=0.5 },
17 scale = { x=-100, y=-100 },
18 text = "playereffects_example_black.png",
20 if(hudid ~= nil) then
21 return { hudid = hudid }
22 else
23 minetest.log("error", "[playereffects] [examples] The effect \"Blind\" could not be applied. The call to hud_add(...) failed.")
24 return false
25 end
26 end,
27 function(effect)
28 local player = minetest.get_player_by_name(effect.playername)
29 player:hud_remove(effect.metadata.hudid)
30 end
33 -- Makes the user faster
34 playereffects.register_effect_type("high_speed", "High speed", nil, {"speed"},
35 function(player)
36 player:set_physics_override(4,nil,nil)
37 end,
39 function(effect)
40 local player = minetest.get_player_by_name(effect.playername)
41 player:set_physics_override(1,nil,nil)
42 end
45 -- Makes the user faster (hidden effect)
46 playereffects.register_effect_type("high_speed_hidden", "High speed", nil, {"speed"},
47 function(player)
48 player:set_physics_override(4,nil,nil)
49 end,
51 function(effect)
52 local player = minetest.get_player_by_name(effect.playername)
53 player:set_physics_override(1,nil,nil)
54 end,
55 true
60 -- Slows the user down
61 playereffects.register_effect_type("low_speed", "Low speed", nil, {"speed"},
62 function(player)
63 player:set_physics_override(0.25,nil,nil)
64 end,
66 function(effect)
67 local player = minetest.get_player_by_name(effect.playername)
68 player:set_physics_override(1,nil,nil)
69 end
72 -- Increases the jump height
73 playereffects.register_effect_type("highjump", "Greater jump height", "playereffects_example_highjump.png", {"jump"},
74 function(player)
75 player:set_physics_override(nil,2,nil)
76 end,
77 function(effect)
78 local player = minetest.get_player_by_name(effect.playername)
79 player:set_physics_override(nil,1,nil)
80 end
83 -- Adds the “fly” privilege
84 playereffects.register_effect_type("fly", "Fly mode available", "playereffects_example_fly.png", {"fly"},
85 function(player)
86 local playername = player:get_player_name()
87 local privs = minetest.get_player_privs(playername)
88 privs.fly = true
89 minetest.set_player_privs(playername, privs)
90 end,
91 function(effect)
92 local privs = minetest.get_player_privs(effect.playername)
93 privs.fly = nil
94 minetest.set_player_privs(effect.playername, privs)
95 end
98 -- Dummy effect for the stree test
99 playereffects.register_effect_type("stress", "Stress Test Effect", nil, {},
100 function(player)
101 end,
102 function(effect)
107 ------ Chat commands for the example effects ------
108 -- Null effect (never succeeds)
109 minetest.register_chatcommand("null", {
110 params = "",
111 description = "Does nothing.",
112 privs = {},
113 func = function(name, param)
114 playereffects.apply_effect_type("null", 5, minetest.get_player_by_name(name))
115 end,
118 minetest.register_chatcommand("blind", {
119 params = "",
120 description = "Makes your screen black for a short time.",
121 privs = {},
122 func = function(name, param)
123 playereffects.apply_effect_type("blind", 5, minetest.get_player_by_name(name))
124 end,
126 minetest.register_chatcommand("fast", {
127 params = "",
128 description = "Makes you fast for a short time.",
129 privs = {},
130 func = function(name, param)
131 playereffects.apply_effect_type("high_speed", 10, minetest.get_player_by_name(name))
132 end,
134 minetest.register_chatcommand("hfast", {
135 params = "",
136 description = "Makes you fast for a short time (hidden effect).",
137 privs = {},
138 func = function(name, param)
139 playereffects.apply_effect_type("high_speed_hidden", 10, minetest.get_player_by_name(name))
140 end,
142 minetest.register_chatcommand("slow", {
143 params = "",
144 description = "Makes you slow for a long time.",
145 privs = {},
146 func = function(name, param)
147 playereffects.apply_effect_type("low_speed", 120, minetest.get_player_by_name(name))
148 end,
150 minetest.register_chatcommand("highjump", {
151 params = "",
152 description = "Makes you jump higher for a short time.",
153 privs = {},
154 func = function(name, param)
155 playereffects.apply_effect_type("highjump", 20, minetest.get_player_by_name(name))
156 end,
159 minetest.register_chatcommand("fly", {
160 params = "",
161 description = "Grants you the fly privilege for a short time.",
162 privs = {},
163 func = function(name, param)
164 playereffects.apply_effect_type("fly", 20, minetest.get_player_by_name(name))
165 end,
168 --[[ The stress test applies a shitload of effects at once.
169 This is used to test the performance of this mod at very large effect numbers. ]]
170 minetest.register_chatcommand("stresstest", {
171 params = "[<effects>]",
172 descriptions = "Start the stress test for Player Effects with <effects> effects.",
173 privs = {server=true},
174 func = function(name, param)
175 local player = minetest.get_player_by_name(name)
176 local max = 100
177 if(type(param)=="string") then
178 if(type(tonumber(param)) == "number") then
179 max = tonumber(param)
180 if(max > 1000) then max = 1000 end
183 minetest.debug("[playereffects] Stress test started for "..name.." with "..max.." effects.")
184 for i=1,max do
185 playereffects.apply_effect_type("stress", 10, player)