Skip to content

Commit

Permalink
Run HologramsLoadedEvent async in hologram manager
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Aug 3, 2024
1 parent df5f654 commit afa96da
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/main/java/de/oliver/fancyholograms/HologramManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ public void removeHologram(@NotNull final Hologram hologram) {
Optional<Hologram> 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;
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -188,8 +188,8 @@ void initializeTasks() {
});

final var updateTimes = CacheBuilder.newBuilder()
.expireAfterAccess(Duration.ofMinutes(5))
.<String, Long>build();
.expireAfterAccess(Duration.ofMinutes(5))
.<String, Long>build();

hologramThread.scheduleAtFixedRate(() -> {
final var time = System.currentTimeMillis();
Expand Down Expand Up @@ -233,15 +233,19 @@ public void reloadHolograms() {
}

public void unloadHolograms() {
final var online = List.copyOf(Bukkit.getOnlinePlayers());

FancyHolograms.get().getHologramThread().submit(() -> {
List<Hologram> 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)));
Expand All @@ -253,8 +257,8 @@ public void unloadHolograms(String world) {

FancyHolograms.get().getHologramThread().submit(() -> {
List<Hologram> 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);

Expand Down

0 comments on commit afa96da

Please sign in to comment.