From cc73168dd4108bb68c435d8e9a7f7c5057443de9 Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Sat, 8 May 2021 18:15:22 -0700 Subject: [PATCH] Implement method to despawn sneekers that aren't near any players --- TODO.txt | 1 - init.lua | 30 ++++++++++++++++++++++++++++++ settings.lua | 12 ++++++++++++ settingtypes.txt | 12 ++++++++++-- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/TODO.txt b/TODO.txt index 0251b35..0810866 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,3 +1,2 @@ TODO: -- add option to remove mobs that aren't near any players diff --git a/init.lua b/init.lua index a9bafdb..243c323 100644 --- a/init.lua +++ b/init.lua @@ -114,8 +114,11 @@ def.on_activate = function(self, staticdata) self.knockback = false self.state = math.random(1, 2) self.old_y = self.object:get_pos().y + + -- despawning self.lifetime = sneeker.lifetime self.lifetimer = 0 + self.check_despawn_player_distance = true local data = core.deserialize(staticdata) if data and type(data) == "table" then @@ -154,6 +157,33 @@ def.on_step = function(self, dtime) local ANIM_WALK = 2 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 inside = core.get_objects_inside_radius(pos, 10) local walk_speed = self.walk_speed diff --git a/settings.lua b/settings.lua index 8c0a19a..3e21197 100644 --- a/settings.lua +++ b/settings.lua @@ -27,6 +27,18 @@ sneeker.spawn_require_player_nearby = core.settings:get_bool("sneeker.spawn_requ -- @setting sneeker.spawn_player_radius 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. -- -- Inverted value (e.g. 10000 = 1/10000). diff --git a/settingtypes.txt b/settingtypes.txt index 4e73b5d..cdbdcf4 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -13,12 +13,20 @@ sneeker.lifetime (Sneeker lifetime) int 900 sneeker.boom_gain (Boom volume) float 1.5 # 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. # # 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. #