From afa96da127cbd9a309c8cbc6bd079bb9a474a5e2 Mon Sep 17 00:00:00 2001 From: OliverSchlueter Date: Sat, 3 Aug 2024 13:33:49 +0200 Subject: [PATCH] Run HologramsLoadedEvent async in hologram manager --- .../fancyholograms/HologramManagerImpl.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/oliver/fancyholograms/HologramManagerImpl.java b/src/main/java/de/oliver/fancyholograms/HologramManagerImpl.java index 1dcaa87..354525e 100644 --- a/src/main/java/de/oliver/fancyholograms/HologramManagerImpl.java +++ b/src/main/java/de/oliver/fancyholograms/HologramManagerImpl.java @@ -106,15 +106,15 @@ public void removeHologram(@NotNull final Hologram hologram) { Optional optionalHologram = Optional.ofNullable(this.holograms.remove(name.toLowerCase(Locale.ROOT))); optionalHologram.ifPresent(hologram -> { - for (UUID viewer : hologram.getViewers()) { - Player player = Bukkit.getPlayer(viewer); - if (player != null) { - FancyHolograms.get().getHologramThread().submit(() -> hologram.forceHideHologram(player)); + for (UUID viewer : hologram.getViewers()) { + Player player = Bukkit.getPlayer(viewer); + if (player != null) { + FancyHolograms.get().getHologramThread().submit(() -> hologram.forceHideHologram(player)); + } } - } - FancyHolograms.get().getHologramThread().submit(() -> plugin.getHologramStorage().delete(hologram)); - } + FancyHolograms.get().getHologramThread().submit(() -> plugin.getHologramStorage().delete(hologram)); + } ); return optionalHologram; @@ -152,7 +152,7 @@ public void loadHolograms() { } isLoaded = true; - Bukkit.getPluginManager().callEvent(new HologramsLoadedEvent(ImmutableList.copyOf(allLoaded))); + FancyHolograms.get().getHologramThread().submit(() -> Bukkit.getPluginManager().callEvent(new HologramsLoadedEvent(ImmutableList.copyOf(allLoaded)))); FancyHolograms.get().getLogger().info(String.format("Loaded %d holograms for all loaded worlds", allLoaded.size())); } @@ -188,8 +188,8 @@ void initializeTasks() { }); final var updateTimes = CacheBuilder.newBuilder() - .expireAfterAccess(Duration.ofMinutes(5)) - .build(); + .expireAfterAccess(Duration.ofMinutes(5)) + .build(); hologramThread.scheduleAtFixedRate(() -> { final var time = System.currentTimeMillis(); @@ -233,15 +233,19 @@ public void reloadHolograms() { } public void unloadHolograms() { - final var online = List.copyOf(Bukkit.getOnlinePlayers()); - FancyHolograms.get().getHologramThread().submit(() -> { List unloaded = new ArrayList<>(); for (final var hologram : this.getPersistentHolograms()) { this.holograms.remove(hologram.getName()); unloaded.add(hologram); - online.forEach(hologram::forceHideHologram); + + for (UUID viewer : hologram.getViewers()) { + Player player = Bukkit.getPlayer(viewer); + if (player != null) { + hologram.forceHideHologram(player); + } + } } Bukkit.getPluginManager().callEvent(new HologramsUnloadedEvent(ImmutableList.copyOf(unloaded))); @@ -253,8 +257,8 @@ public void unloadHolograms(String world) { FancyHolograms.get().getHologramThread().submit(() -> { List h = getPersistentHolograms().stream() - .filter(hologram -> hologram.getData().getLocation().getWorld().getName().equals(world)) - .toList(); + .filter(hologram -> hologram.getData().getLocation().getWorld().getName().equals(world)) + .toList(); FancyHolograms.get().getHologramStorage().saveBatch(h, true);