From c369c110e53f366cf812f2321f05e0f5ec9a1c63 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Tue, 16 Apr 2024 16:29:26 +0200 Subject: [PATCH] possible workaround for folia compatibility --- .../preventstabby/data/PlayerManager.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java index 44bceda..84fe812 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java +++ b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java @@ -6,14 +6,11 @@ import me.youhavetrouble.preventstabby.api.event.PlayerLeaveCombatEvent; import me.youhavetrouble.preventstabby.util.PluginMessages; import me.youhavetrouble.preventstabby.util.PvpState; import org.bukkit.Bukkit; +import org.bukkit.Chunk; import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.entity.Tameable; import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; @@ -32,16 +29,17 @@ public class PlayerManager { playerList.values().removeIf(PlayerData::isCacheExpired); }, 250, 250, TimeUnit.MILLISECONDS); - Bukkit.getScheduler().runTaskTimer(plugin, (task -> { - List entities = new ArrayList<>(); - Bukkit.getWorlds().forEach((world -> entities.addAll(world.getLivingEntities()))); - Bukkit.getAsyncScheduler().runNow(plugin, (asyncTask) -> entities.forEach(livingEntity -> { - if (!(livingEntity instanceof Tameable tameable)) return; - UUID ownerId = tameable.getOwnerUniqueId(); - if (ownerId == null) return; - getPlayerData(ownerId); - })); - }), 5, 20 * 15); + Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, (task) -> Bukkit.getWorlds().forEach((world -> { + for (Chunk chunk : world.getLoadedChunks()) { + if (!chunk.isEntitiesLoaded()) continue; + for (Entity entity : chunk.getEntities()) { + if (!(entity instanceof Tameable tameable)) continue; + UUID ownerId = tameable.getOwnerUniqueId(); + if (ownerId == null) continue; + getPlayerData(ownerId); + } + } + })), 5, 20 * 15); Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, (task) -> { for (PlayerData playerData : playerList.values()) {