diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index ab3154765..fb4119605 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -717,6 +717,8 @@ function mcl_util.gen_uuid() return table.concat(u) end function mcl_util.get_entity_id(entity) + if entity.object then entity = entity.object end + if entity:is_player() then return entity:get_player_name() else diff --git a/mods/ENTITIES/mcl_mobs/init.lua b/mods/ENTITIES/mcl_mobs/init.lua index 74959a738..267d6603f 100644 --- a/mods/ENTITIES/mcl_mobs/init.lua +++ b/mods/ENTITIES/mcl_mobs/init.lua @@ -382,7 +382,9 @@ end function mcl_mobs.register_arrow(name, def) if not name or not def then return end -- errorcheck - local behaviors = {} + local behaviors = { + vl_projectile.has_owner_grace_distance + } if def.hit_node then table.insert(behaviors, vl_projectile.collides_with_solids) end diff --git a/mods/ITEMS/mcl_bows/arrow.lua b/mods/ITEMS/mcl_bows/arrow.lua index 3c2d8d5a1..dc48062d8 100644 --- a/mods/ITEMS/mcl_bows/arrow.lua +++ b/mods/ITEMS/mcl_bows/arrow.lua @@ -46,7 +46,6 @@ local arrow_entity = { "last_pos", "startpos", "damage", "is_critical", "stuck", "stuckin", "stuckin_player", "time_in_air", "vl_projectile", }, - _startpos=nil, _damage=1, -- Damage on impact _is_critical=false, -- Whether this arrow would deal critical damage _stuck=false, -- Whether arrow is stuck @@ -76,12 +75,10 @@ local arrow_entity = { vl_projectile.sticks, vl_projectile.burns, vl_projectile.has_tracer, + vl_projectile.has_owner_grace_distance, -- Custom arrow behaviors function(self, dtime) - local pos = self.object:get_pos() - self._allow_punch = self._allow_punch or not self._owner or not self._startpos or pos and vector.distance(self._startpos, pos) > 1.5 - if self._deflection_cooloff > 0 then self._deflection_cooloff = self._deflection_cooloff - dtime end @@ -91,8 +88,6 @@ local arrow_entity = { vl_projectile.raycast_collides_with_entities, }, allow_punching = function(self, entity_def, projectile_def, object) - if not self._allow_punch then return false end - local lua = object:get_luaentity() if lua and lua.name == "mobs_mc:rover" then return false end diff --git a/mods/ITEMS/mcl_bows/bow.lua b/mods/ITEMS/mcl_bows/bow.lua index 5eeb1a2e1..0505c20b6 100644 --- a/mods/ITEMS/mcl_bows/bow.lua +++ b/mods/ITEMS/mcl_bows/bow.lua @@ -65,7 +65,6 @@ function mcl_bows.shoot_arrow(arrow_item, pos, dir, yaw, shooter, power, damage, le._source_object = shooter le._damage = damage le._is_critical = is_critical - le._startpos = pos le._knockback = knockback le._collectable = collectable le._arrow_item = arrow_item diff --git a/mods/ITEMS/mcl_potions/lingering.lua b/mods/ITEMS/mcl_potions/lingering.lua index f40380a27..a344803fb 100644 --- a/mods/ITEMS/mcl_potions/lingering.lua +++ b/mods/ITEMS/mcl_potions/lingering.lua @@ -208,6 +208,7 @@ function mcl_potions.register_lingering(name, descr, color, def) pointable = false, _vl_projectile = { behaviors = { + vl_projectile.has_owner_grace_distance, vl_projectile.collides_with_entities, vl_projectile.collides_with_solids, }, diff --git a/mods/ITEMS/mcl_potions/splash.lua b/mods/ITEMS/mcl_potions/splash.lua index b0cb7ef3a..b41b2c939 100644 --- a/mods/ITEMS/mcl_potions/splash.lua +++ b/mods/ITEMS/mcl_potions/splash.lua @@ -176,6 +176,7 @@ function mcl_potions.register_splash(name, descr, color, def) collisionbox = {-0.1,-0.1,-0.1,0.1,0.1,0.1}, _vl_projectile = { behaviors = { + vl_projectile.has_owner_grace_distance, vl_projectile.collides_with_entities, vl_projectile.collides_with_solids, }, diff --git a/mods/ITEMS/vl_projectile/init.lua b/mods/ITEMS/vl_projectile/init.lua index 2805860f7..0feff8ce1 100644 --- a/mods/ITEMS/vl_projectile/init.lua +++ b/mods/ITEMS/vl_projectile/init.lua @@ -215,6 +215,13 @@ function mod.burns(self, dtime, entity_def, projectile_def) end end +function mod.has_owner_grace_distance(self, dtime, entity_def, projectile_def) + local pos = self.object:get_pos() + + self._allow_punch = self._allow_punch or + not self._owner or not self._startpos or + pos and vector.distance(self._startpos, pos) > ( projectile_def.grace_distance or 1.5 ) +end function mod.has_tracer(self, dtime, entity_def, projectile_def) local hide_tracer = projectile_def.hide_tracer if hide_tracer and hide_tracer(self) then return end @@ -593,10 +600,11 @@ function mod.create(entity_id, options) local luaentity = obj:get_luaentity() if options.owner_id then luaentity._owner = options.owner_id - else + elseif options.owner then luaentity._owner = mcl_util.get_entity_id(options.owner) end luaentity._starting_velocity = obj:get_velocity() + luaentity._startpos = pos luaentity._vl_projectile = { extra = options.extra, } @@ -606,11 +614,29 @@ function mod.create(entity_id, options) end function mod.register(name, def) - assert(def._vl_projectile, "vl_projectile.register() requires definition to define _vl_projectile") - assert(def._vl_projectile.behaviors, "vl_projectile.register() requires definition to define _vl_projectile.behaviors") - local behaviors = def._vl_projectile.behaviors + def_vl_projectile = def._vl_projectile + assert(def_vl_projectile, "vl_projectile.register() requires definition to define _vl_projectile") + local behaviors = def_vl_projectile.behaviors + + assert(behaviors, "vl_projectile.register() requires definition to define _vl_projectile.behaviors") for i = 1,#behaviors do assert(behaviors[i] and type(behaviors[i]) == "function", "def._vl_projectile.behaviors["..i.." is malformed") + if behaviors[i] == vl_projectile.has_owner_grace_distance then + local old_allow_punching = def_vl_projectile.allow_punching + if old_allow_punching then + def_vl_projectile.allow_punching = function(self, ...) + if not self._allow_punch then return false end + + return old_allow_punching(self, ...) + end + else + def_vl_projectile.allow_punching = function(self, ...) + if not self._allow_punch then return false end + + return true + end + end + end end if not def.on_step then