From 86cd5711ca462d30306b528cac1c2f4a5448d794 Mon Sep 17 00:00:00 2001 From: epCode Date: Sun, 4 Jun 2023 13:20:29 -0700 Subject: [PATCH] Rearange on replace code to be more general purpous --- mods/ENTITIES/mcl_mobs/init.lua | 3 +- mods/ENTITIES/mcl_mobs/movement.lua | 21 ++++--------- mods/ENTITIES/mobs_mc/sheep.lua | 48 +++++++++++++++++++---------- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 6bd39b2cf..62100c627 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -217,6 +217,7 @@ function mcl_mobs.register_mob(name, def) replace_with = def.replace_with, replace_offset = def.replace_offset or 0, on_replace = def.on_replace, + replace_delay = def.replace_delay or 0, timer = 0, env_damage_timer = 0, tamed = false, @@ -490,7 +491,7 @@ function mcl_mobs.register_egg(mob, desc, background_color, overlay_color, addeg on_place = function(itemstack, placer, pointed_thing) local pos = pointed_thing.above - + -- am I clicking on something with existing on_rightclick function? local under = minetest.get_node(pointed_thing.under) local def = minetest.registered_nodes[under.name] diff --git a/mods/ENTITIES/mcl_mobs/movement.lua b/mods/ENTITIES/mcl_mobs/movement.lua index c165bdf89..7a89e010c 100644 --- a/mods/ENTITIES/mcl_mobs/movement.lua +++ b/mods/ENTITIES/mcl_mobs/movement.lua @@ -513,27 +513,18 @@ function mob_class:replace_node(pos) local oldnode = {name = what, 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 mobs_griefing then - self.state = "eat" - self:set_animation("eat") - 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.after(self.replace_delay, function() + if self and self.object and self.object:get_velocity() and self.health > 0 then 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 diff --git a/mods/ENTITIES/mobs_mc/sheep.lua b/mods/ENTITIES/mobs_mc/sheep.lua index f7d6e87f1..fbebb2655 100644 --- a/mods/ENTITIES/mobs_mc/sheep.lua +++ b/mods/ENTITIES/mobs_mc/sheep.lua @@ -113,29 +113,43 @@ mcl_mobs.register_mob("mobs_mc:sheep", { view_range = 12, -- Eat grass - replace_rate = 40, + replace_rate = 1, + replace_delay = 1.3, replace_what = { { "mcl_core:dirt_with_grass", "mcl_core:dirt", -1 }, { "mcl_flowers:tallgrass", "air", 0 }, }, -- Properly regrow wool after eating grass on_replace = function(self, pos, oldnode, newnode) - if not self.color or not colors[self.color] then - self.color = "unicolor_white" - end - self.gotten = false - self.base_texture = sheep_texture(self.color) - self.object:set_properties({ textures = self.base_texture }) - self.drops = { - {name = "mcl_mobitems:mutton", - chance = 1, - min = 1, - max = 2,}, - {name = colors[self.color][1], - chance = 1, - min = 1, - max = 1,}, - } + 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 + self.color = "unicolor_white" + end + self.gotten = false + self.base_texture = sheep_texture(self.color) + self.object:set_properties({ textures = self.base_texture }) + self.drops = { + {name = "mcl_mobitems:mutton", + chance = 1, + min = 1, + max = 2,}, + {name = colors[self.color][1], + chance = 1, + min = 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, -- Set random color on spawn