Rearange on replace code to be more general purpous

This commit is contained in:
epCode 2023-06-04 13:20:29 -07:00 committed by epCode
parent 908ba9fba6
commit 86cd5711ca
3 changed files with 39 additions and 33 deletions

View File

@ -217,6 +217,7 @@ function mcl_mobs.register_mob(name, def)
replace_with = def.replace_with, replace_with = def.replace_with,
replace_offset = def.replace_offset or 0, replace_offset = def.replace_offset or 0,
on_replace = def.on_replace, on_replace = def.on_replace,
replace_delay = def.replace_delay or 0,
timer = 0, timer = 0,
env_damage_timer = 0, env_damage_timer = 0,
tamed = false, tamed = false,

View File

@ -513,27 +513,18 @@ function mob_class:replace_node(pos)
local oldnode = {name = what, param2 = node.param2} local oldnode = {name = what, param2 = node.param2}
local newnode = {name = with, param2 = node.param2} local newnode = {name = with, param2 = node.param2}
local on_replace_return = true local on_replace_return = false
if self.on_replace then
on_replace_return = self.on_replace(self, pos, oldnode, newnode)
end
if on_replace_return ~= false then if on_replace_return ~= false then
if mobs_griefing then if mobs_griefing then
self.state = "eat" minetest.after(self.replace_delay, function()
self:set_animation("eat") if self and self.object and self.object:get_velocity() and self.health > 0 then
self:set_velocity(0)
minetest.after(1.3, function()
if self and self.object and not self.dead then
self.object:set_velocity(vector.new(0,0,0))
minetest.set_node(pos, newnode) minetest.set_node(pos, newnode)
if self.on_replace then
on_replace_return = self.on_replace(self, pos, oldnode, newnode)
end
end
end)
minetest.after(2.5, function()
if self and self.object and self.state == 'eat' and not self.dead then
self.state = "walk"
end end
end) end)
end end

View File

@ -113,13 +113,20 @@ mcl_mobs.register_mob("mobs_mc:sheep", {
view_range = 12, view_range = 12,
-- Eat grass -- Eat grass
replace_rate = 40, replace_rate = 1,
replace_delay = 1.3,
replace_what = { replace_what = {
{ "mcl_core:dirt_with_grass", "mcl_core:dirt", -1 }, { "mcl_core:dirt_with_grass", "mcl_core:dirt", -1 },
{ "mcl_flowers:tallgrass", "air", 0 }, { "mcl_flowers:tallgrass", "air", 0 },
}, },
-- Properly regrow wool after eating grass -- Properly regrow wool after eating grass
on_replace = function(self, pos, oldnode, newnode) on_replace = function(self, pos, oldnode, newnode)
self.state = "eat"
self:set_animation("eat")
self:set_velocity(0)
minetest.after(self.replace_delay, function()
self.object:set_velocity(vector.zero())
if self and self.object and not self.object:get_velocity() and self.health > 0 then
if not self.color or not colors[self.color] then if not self.color or not colors[self.color] then
self.color = "unicolor_white" self.color = "unicolor_white"
end end
@ -136,6 +143,13 @@ mcl_mobs.register_mob("mobs_mc:sheep", {
min = 1, min = 1,
max = 1,}, max = 1,},
} }
end
end)
minetest.after(2.5, function()
if self and self.object and self.state == 'eat' and self.health > 0 and self.object:get_velocity() then
self.state = "walk"
end
end)
end, end,
-- Set random color on spawn -- Set random color on spawn