Implement method to despawn sneekers that aren't near any players

This commit is contained in:
Jordan Irwin 2021-05-08 18:15:22 -07:00
parent d741261156
commit cc73168dd4
4 changed files with 52 additions and 3 deletions

@ -1,3 +1,2 @@
TODO: TODO:
- add option to remove mobs that aren't near any players

@ -114,8 +114,11 @@ def.on_activate = function(self, staticdata)
self.knockback = false self.knockback = false
self.state = math.random(1, 2) self.state = math.random(1, 2)
self.old_y = self.object:get_pos().y self.old_y = self.object:get_pos().y
-- despawning
self.lifetime = sneeker.lifetime self.lifetime = sneeker.lifetime
self.lifetimer = 0 self.lifetimer = 0
self.check_despawn_player_distance = true
local data = core.deserialize(staticdata) local data = core.deserialize(staticdata)
if data and type(data) == "table" then if data and type(data) == "table" then
@ -154,6 +157,33 @@ def.on_step = function(self, dtime)
local ANIM_WALK = 2 local ANIM_WALK = 2
local pos = self.object:get_pos() local pos = self.object:get_pos()
if sneeker.despawn_player_far then
-- run check about once per 60 seconds
local interval_reached = math.floor(self.lifetimer % 60) == 0
if not interval_reached and not self.check_despawn_player_distance then
self.check_despawn_player_distance = true
end
if interval_reached and self.check_despawn_player_distance then
local player_nearby = false
for _, entity in ipairs(core.get_objects_inside_radius(pos, sneeker.despawn_player_radius)) do
if entity:is_player() then
player_nearby = true
break
end
end
if not player_nearby then
self.object:remove()
return true
end
-- set flag to not check again until next interval
self.check_despawn_player_distance = false
end
end
local yaw = self.object:get_yaw() local yaw = self.object:get_yaw()
local inside = core.get_objects_inside_radius(pos, 10) local inside = core.get_objects_inside_radius(pos, 10)
local walk_speed = self.walk_speed local walk_speed = self.walk_speed

@ -27,6 +27,18 @@ sneeker.spawn_require_player_nearby = core.settings:get_bool("sneeker.spawn_requ
-- @setting sneeker.spawn_player_radius -- @setting sneeker.spawn_player_radius
sneeker.spawn_player_radius = tonumber(core.settings:get("sneeker.spawn_player_radius") or 100) sneeker.spawn_player_radius = tonumber(core.settings:get("sneeker.spawn_player_radius") or 100)
--- If enabled, mobs not near any players will despawn.
--
-- @setting sneeker.despawn_player_far
sneeker.despawn_player_far = core.settings:get_bool("sneeker.despawn_player_far", true)
--- Distance determining if a player is near enough to prevent despawn.
--
-- Only used if `sneeker.despawn_player_far` enabled.
--
-- @setting sneeker.despawn_player_radius
sneeker.despawn_player_radius = tonumber(core.settings:get("sneeker.despawn_player_radius") or 500)
--- Sets possibility for spawn. --- Sets possibility for spawn.
-- --
-- Inverted value (e.g. 10000 = 1/10000). -- Inverted value (e.g. 10000 = 1/10000).

@ -13,12 +13,20 @@ sneeker.lifetime (Sneeker lifetime) int 900
sneeker.boom_gain (Boom volume) float 1.5 sneeker.boom_gain (Boom volume) float 1.5
# Determines whether or not a player must be close for spawn to occur. # Determines whether or not a player must be close for spawn to occur.
sneeker.spawn_require_player_nearby (Require player nearby) bool true sneeker.spawn_require_player_nearby (Require player nearby for spawn) bool true
# Distance in nodes a player must be for spawn to occur. # Distance in nodes a player must be for spawn to occur.
# #
# Only used if `sneeker.spawn_require_player_nearby` enabled. # Only used if `sneeker.spawn_require_player_nearby` enabled.
sneeker.spawn_player_radius (Player detect radius) int 100 sneeker.spawn_player_radius (Player spawn detect radius) int 100
# If enabled, mobs not near any players will despawn.
sneeker.despawn_player_far (Despawn sneekers not near players) bool true
# Distance determining if a player is near enough to prevent despawn.
#
# Only used if `sneeker.despawn_player_far` enabled.
sneeker.despawn_player_radius (Player despawn detect radius) int 500
# Sets possibility for spawn. # Sets possibility for spawn.
# #