From cdce542f166c7b6b5ec7a496f57952d2469c4ee6 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Tue, 9 Apr 2024 23:09:10 +0000 Subject: [PATCH] Fix crashes --- mods/CORE/mcl_util/init.lua | 16 ++++++++++------ mods/ENTITIES/mcl_minecarts/carts.lua | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 99383694d..0335e2037 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -1216,11 +1216,7 @@ end function mcl_util.get_luaentity_from_uuid(uuid) return minetest.luaentities[ mcl_util.get_active_object_id_from_uuid(uuid) ] end -function mcl_util.get_uuid(obj) - local le = obj:get_luaentity() - - if le._uuid then return le._uuid end - +function mcl_util.gen_uuid() -- Generate a random 128-bit ID that can be assumed to be unique -- To have a 1% chance of a collision, there would have to be 1.6x10^76 IDs generated -- https://en.wikipedia.org/wiki/Birthday_problem#Probability_table @@ -1228,7 +1224,15 @@ function mcl_util.get_uuid(obj) for i = 1,16 do u[#u + 1] = string.format("%02X",math.random(1,255)) end - le._uuid = table.concat(u) + return table.concat(u) +end +function mcl_util.assign_uuid(obj) + assert(obj) + + local le = obj:get_luaentity() + if le._uuid then return le._uuid end + + le._uuid = mcl_util.gen_uuid() -- Update the cache with this new id aoid = mcl_util.get_active_object_id(obj) diff --git a/mods/ENTITIES/mcl_minecarts/carts.lua b/mods/ENTITIES/mcl_minecarts/carts.lua index e66504598..78cc552f7 100644 --- a/mods/ENTITIES/mcl_minecarts/carts.lua +++ b/mods/ENTITIES/mcl_minecarts/carts.lua @@ -107,7 +107,7 @@ function DEFAULT_CART_DEF:on_activate(staticdata, dtime_s) -- Transfer older data local data = minetest.deserialize(staticdata) or {} if not data.uuid then - data.uuid = mcl_util.get_uuid(self.object) + data.uuid = mcl_util.assign_uuid(self.object) end self._seq = data.seq or 1 @@ -202,7 +202,7 @@ function DEFAULT_CART_DEF:on_step(dtime) -- Regen local hp = self.object:get_hp() local time_now = minetest.get_gametime() - if hp < MINECART_MAX_HP and staticdata.last_regen <= time_now - 1 then + if hp < MINECART_MAX_HP and (staticdata.last_regen or 0) <= time_now - 1 then staticdata.last_regen = time_now hp = hp + 1 self.object:set_hp(hp) @@ -308,7 +308,7 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer) local entity_id = entity_mapping[itemstack:get_name()] -- Setup cart data - local uuid = mcl_util.get_uuid(cart) + local uuid = mcl_util.gen_uuid() data = make_staticdata( nil, railpos, cart_dir ) data.uuid = uuid data.cart_type = entity_id