1
0
Fork 0
VoxeLibre/mods/ITEMS/mcl_totems/init.lua

108 lines
3.5 KiB
Lua
Raw Normal View History

2022-05-25 23:25:15 +02:00
local S = minetest.get_translator(minetest.get_current_modname())
2021-04-23 13:40:51 +02:00
local hud_totem = {}
2021-04-23 13:40:51 +02:00
minetest.register_on_leaveplayer(function(player)
hud_totem[player] = nil
end)
2022-05-25 23:25:15 +02:00
minetest.register_craftitem("mcl_totems:totem", {
description = S("Totem of Undying"),
_tt_help = minetest.colorize(mcl_colors.GREEN, S("Protects you from death while wielding it")),
_doc_items_longdesc = S("A totem of undying is a rare artifact which may safe you from certain death."),
_doc_items_usagehelp = S("The totem only works while you hold it in your hand. If you receive fatal damage, you are saved from death and you get a second chance with 1 HP. The totem is destroyed in the process, however."),
inventory_image = "mcl_totems_totem.png",
wield_image = "mcl_totems_totem.png",
stack_max = 1,
groups = {combat_item = 1, offhand_item = 1},
})
minetest.register_alias("mobs_mc:totem", "mcl_totems:totem")
2021-10-24 21:31:51 +02:00
local particle_colors = {"98BF22", "C49E09", "337D0B", "B0B021", "1E9200"} -- TODO: real MC colors
2021-05-29 21:24:16 +02:00
2021-04-23 13:40:51 +02:00
-- Save the player from death when holding totem of undying in hand
mcl_damage.register_modifier(function(obj, damage, reason)
2022-05-25 23:25:15 +02:00
if obj:is_player() and not reason.bypasses_totem then
2021-04-23 13:40:51 +02:00
local hp = obj:get_hp()
if hp - damage <= 0 then
local wield = obj:get_wielded_item()
2022-03-15 15:58:27 +01:00
local in_offhand = false
2022-05-25 23:25:15 +02:00
if not (wield:get_name() == "mcl_totems:totem") then
2022-03-15 15:58:27 +01:00
local inv = obj:get_inventory()
if inv then
wield = obj:get_inventory():get_stack("offhand", 1)
in_offhand = true
end
end
2022-05-25 23:25:15 +02:00
if wield:get_name() == "mcl_totems:totem" then
2021-04-23 13:40:51 +02:00
local ppos = obj:get_pos()
local pnname = minetest.get_node(ppos).name
2022-05-25 23:25:15 +02:00
2021-04-23 13:40:51 +02:00
if obj:get_breath() < 11 then
obj:set_breath(10)
end
if not minetest.is_creative_enabled(obj:get_player_name()) then
wield:take_item()
2022-03-15 15:58:27 +01:00
if in_offhand then
obj:get_inventory():set_stack("offhand", 1, wield)
mcl_inventory.update_inventory_formspec(obj)
else
obj:set_wielded_item(wield)
end
2021-04-23 13:40:51 +02:00
end
2022-06-11 20:41:59 +02:00
awards.unlock(obj:get_player_name(), "mcl:postMortal")
2021-04-23 13:40:51 +02:00
-- Effects
2021-10-24 21:31:51 +02:00
minetest.sound_play({name = "mcl_totems_totem", gain = 1}, {pos=ppos, max_hear_distance = 16}, true)
2022-05-26 07:29:28 +02:00
for i = 1, 4 do
for c = 1, #particle_colors do
minetest.add_particlespawner({
2021-10-25 22:25:34 +02:00
amount = math.floor(100 / (4 * #particle_colors)),
time = 1,
minpos = vector.offset(ppos, 0, -1, 0),
maxpos = vector.offset(ppos, 0, 1, 0),
minvel = vector.new(-1.5, 0, -1.5),
maxvel = vector.new(1.5, 1.5, 1.5),
minacc = vector.new(0, -0.1, 0),
maxacc = vector.new(0, -1, 0),
minexptime = 1,
maxexptime = 3,
minsize = 1,
maxsize = 2,
collisiondetection = true,
collision_removal = true,
object_collision = false,
vertical = false,
texture = "mcl_particles_totem" .. i .. ".png^[colorize:#" .. particle_colors[c],
glow = 10,
})
end
2021-10-24 21:31:51 +02:00
end
2021-05-29 21:24:16 +02:00
2021-08-26 12:17:15 +02:00
-- Big totem overlay
2021-04-23 13:40:51 +02:00
if not hud_totem[obj] then
hud_totem[obj] = obj:hud_add({
hud_elem_type = "image",
text = "mcl_totems_totem.png",
2021-10-24 21:31:51 +02:00
position = {x = 0.5, y = 1},
scale = {x = 17, y = 17},
offset = {x = 0, y = -178},
2021-04-23 13:40:51 +02:00
z_index = 100,
})
minetest.after(3, function()
if obj:is_player() then
obj:hud_remove(hud_totem[obj])
hud_totem[obj] = nil
end
end)
end
-- Set HP to exactly 1
return hp - 1
end
end
end
2021-07-12 20:05:52 +02:00
end, 1000)