Skip to content

Commit

Permalink
Made showing and hiding holograms more thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
OakLoaf committed May 21, 2024
1 parent ad332db commit 07b90f1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,20 @@ public final void deleteHologram() {
delete();
}

/**
* Must be called asynchronously
*/
public final void showHologram(Player player) {
show(player);
}

/**
* Must be called asynchronously
*/
public final void showHologram(Collection<? extends Player> players) {
players.forEach(this::showHologram);
}

/**
* Must be called asynchronously
*/
public final void hideHologram(Player player) {
hide(player);
public final void showHologram(Player player) {
viewers.add(player.getUniqueId());
}

/**
* Must be called asynchronously
*/
public final void hideHologram(Collection<? extends Player> players) {
players.forEach(this::hideHologram);
players.forEach(this::hideHologram);
}

public final void hideHologram(Player player) {
viewers.remove(player.getUniqueId());
}

@Deprecated(forRemoval = true)
Expand Down Expand Up @@ -202,7 +190,7 @@ protected boolean shouldShowTo(@NotNull final Player player) {
*
* @param player the player to check and update the shown state for
*/
public void checkShownStateFor(Player player) {
public void updateShownStateFor(Player player) {
boolean isShown = isViewer(player);
boolean shouldBeShown = shouldShowTo(player);

Expand All @@ -213,6 +201,24 @@ public void checkShownStateFor(Player player) {
}
}

/**
* Checks and forcefully updates the shown state for a player.
* If the hologram is shown and should not be, it hides it.
* If the hologram is not shown and should be, it shows it.
*
* @param player the player to check and update the shown state for
*/
public void forceUpdateShownStateFor(Player player) {
boolean isShown = isViewer(player);
boolean shouldBeShown = shouldShowTo(player);

if (isShown && !shouldBeShown) {
show(player);
} else if (!isShown && shouldBeShown) {
hide(player);
}
}

/**
* Gets the text shown in the hologram. If a player is specified, placeholders in the text are replaced
* with their corresponding values for the player.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void initializeTasks() {
hologramThread.scheduleAtFixedRate(() -> {
for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) {
for (final Player player : Bukkit.getOnlinePlayers()) {
hologram.checkShownStateFor(player);
hologram.forceUpdateShownStateFor(player);
}
}
}, 0, 1, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No

Hologram hologram = this.plugin.getHologramsManager().create(textData);
hologram.createHologram();
hologram.checkShownStateFor(p);
hologram.updateShownStateFor(p);
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No

Hologram hologram = this.plugin.getHologramsManager().create(textData);
hologram.createHologram();
hologram.checkShownStateFor(p);
hologram.updateShownStateFor(p);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean run(@NotNull CommandSender sender, @Nullable Hologram hologram, @

copy.createHologram();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
copy.checkShownStateFor(onlinePlayer);
copy.updateShownStateFor(onlinePlayer);
}

FancyHolograms.get().getHologramsManager().addHologram(copy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public boolean run(@NotNull CommandSender sender, @Nullable Hologram hologram, @

holo.createHologram();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
holo.checkShownStateFor(onlinePlayer);
holo.updateShownStateFor(onlinePlayer);
}

FancyHolograms.get().getHologramsManager().addHologram(holo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public PlayerListener(@NotNull final FancyHolograms plugin) {
@EventHandler(priority = EventPriority.MONITOR)
public void onJoin(@NotNull final PlayerJoinEvent event) {
for (final var hologram : this.plugin.getHologramsManager().getHolograms()) {
hologram.checkShownStateFor(event.getPlayer());
hologram.updateShownStateFor(event.getPlayer());
}

if (!this.plugin.getHologramConfiguration().areVersionNotificationsMuted() && event.getPlayer().hasPermission("fancyholograms.admin")) {
Expand All @@ -39,14 +39,14 @@ public void onQuit(@NotNull final PlayerQuitEvent event) {
@EventHandler(priority = EventPriority.MONITOR)
public void onTeleport(@NotNull final PlayerTeleportEvent event) {
for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) {
hologram.checkShownStateFor(event.getPlayer());
hologram.updateShownStateFor(event.getPlayer());
}
}

@EventHandler(priority = EventPriority.MONITOR)
public void onWorldChange(@NotNull final PlayerChangedWorldEvent event) {
for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) {
hologram.checkShownStateFor(event.getPlayer());
hologram.updateShownStateFor(event.getPlayer());
}
}

Expand Down

0 comments on commit 07b90f1

Please sign in to comment.