Skip to content

Commit e2d542e

Browse files
fancyholograms-v2: Use player loaded event to spawn hologram once the player loaded the world
1 parent 4da2da4 commit e2d542e

2 files changed

Lines changed: 24 additions & 28 deletions

File tree

  • plugins/fancyholograms-v2

plugins/fancyholograms-v2/api/src/main/java/de/oliver/fancyholograms/api/hologram/Hologram.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public String getName() {
5656
/**
5757
* Returns the entity id of this hologram
5858
* This id is for packet use only as the entity is not registered to the server
59+
*
5960
* @return entity id
6061
*/
6162
public abstract int getEntityId();

plugins/fancyholograms-v2/src/main/java/de/oliver/fancyholograms/listeners/PlayerListener.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22

33
import de.oliver.fancyholograms.FancyHolograms;
44
import de.oliver.fancyholograms.api.hologram.Hologram;
5+
import io.papermc.paper.event.player.PlayerClientLoadedWorldEvent;
56
import org.bukkit.event.EventHandler;
67
import org.bukkit.event.EventPriority;
78
import org.bukkit.event.Listener;
8-
import org.bukkit.event.player.*;
9+
import org.bukkit.event.player.PlayerChangedWorldEvent;
10+
import org.bukkit.event.player.PlayerQuitEvent;
11+
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
912
import org.bukkit.event.player.PlayerResourcePackStatusEvent.Status;
10-
11-
import java.util.ArrayList;
12-
import java.util.HashMap;
13-
import java.util.List;
14-
import java.util.Map;
15-
import java.util.UUID;
16-
13+
import org.bukkit.event.player.PlayerTeleportEvent;
1714
import org.jetbrains.annotations.NotNull;
1815

16+
import java.util.*;
17+
1918
public final class PlayerListener implements Listener {
2019

2120
private final @NotNull FancyHolograms plugin;
@@ -27,14 +26,21 @@ public PlayerListener(@NotNull final FancyHolograms plugin) {
2726
this.loadingResourcePacks = new HashMap<>();
2827
}
2928

30-
@EventHandler(priority = EventPriority.MONITOR)
31-
public void onJoin(@NotNull final PlayerJoinEvent event) {
32-
for (final var hologram : this.plugin.getHologramsManager().getHolograms()) {
33-
hologram.updateShownStateFor(event.getPlayer());
29+
// For 1.20.2 and higher this method returns actual pack identifier, while for older versions, the identifier is a dummy UUID full of zeroes.
30+
// Versions prior 1.20.2 supports sending and receiving only one resource-pack and a dummy, constant identifier can be used as a key.
31+
private static @NotNull UUID getResourcePackID(final @NotNull PlayerResourcePackStatusEvent event) {
32+
try {
33+
event.getClass().getMethod("getID");
34+
return event.getID();
35+
} catch (final @NotNull NoSuchMethodException e) {
36+
return new UUID(0, 0);
3437
}
38+
}
3539

36-
if (!this.plugin.getHologramConfiguration().areVersionNotificationsEnabled() && event.getPlayer().hasPermission("fancyholograms.admin")) {
37-
FancyHolograms.get().getHologramThread().submit(() -> FancyHolograms.get().getVersionConfig().checkVersionAndDisplay(event.getPlayer(), true));
40+
@EventHandler(priority = EventPriority.MONITOR)
41+
public void onPlayerLoaded(@NotNull final PlayerClientLoadedWorldEvent event) {
42+
for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) {
43+
hologram.forceUpdateShownStateFor(event.getPlayer());
3844
}
3945
}
4046

@@ -50,14 +56,14 @@ public void onQuit(@NotNull final PlayerQuitEvent event) {
5056
@EventHandler(priority = EventPriority.MONITOR)
5157
public void onTeleport(@NotNull final PlayerTeleportEvent event) {
5258
for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) {
53-
hologram.updateShownStateFor(event.getPlayer());
59+
hologram.forceUpdateShownStateFor(event.getPlayer());
5460
}
5561
}
5662

5763
@EventHandler(priority = EventPriority.MONITOR)
5864
public void onWorldChange(@NotNull final PlayerChangedWorldEvent event) {
5965
for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) {
60-
hologram.updateShownStateFor(event.getPlayer());
66+
hologram.forceUpdateShownStateFor(event.getPlayer());
6167
}
6268
}
6369

@@ -72,7 +78,7 @@ public void onResourcePackStatus(@NotNull final PlayerResourcePackStatusEvent ev
7278
// Adding accepted resource-pack to the list of currently loading resource-packs for that player.
7379
if (event.getStatus() == Status.ACCEPTED)
7480
loadingResourcePacks.computeIfAbsent(playerUniqueId, (___) -> new ArrayList<>()).add(packUniqueId);
75-
// Once successfully loaded (or failed to download), removing resource-pack from the map.
81+
// Once successfully loaded (or failed to download), removing resource-pack from the map.
7682
else if (event.getStatus() == Status.SUCCESSFULLY_LOADED || event.getStatus() == Status.FAILED_DOWNLOAD) {
7783
loadingResourcePacks.computeIfAbsent(playerUniqueId, (___) -> new ArrayList<>()).removeIf(uuid -> uuid.equals(packUniqueId));
7884
// Refreshing holograms once (possibly) all resource-packs are loaded.
@@ -87,15 +93,4 @@ else if (event.getStatus() == Status.SUCCESSFULLY_LOADED || event.getStatus() ==
8793
}
8894
}
8995

90-
// For 1.20.2 and higher this method returns actual pack identifier, while for older versions, the identifier is a dummy UUID full of zeroes.
91-
// Versions prior 1.20.2 supports sending and receiving only one resource-pack and a dummy, constant identifier can be used as a key.
92-
private static @NotNull UUID getResourcePackID(final @NotNull PlayerResourcePackStatusEvent event) {
93-
try {
94-
event.getClass().getMethod("getID");
95-
return event.getID();
96-
} catch (final @NotNull NoSuchMethodException e) {
97-
return new UUID(0,0);
98-
}
99-
}
100-
10196
}

0 commit comments

Comments
 (0)