fancyholograms: reload cannot load hologram (#152)

This commit is contained in:
橙汁喵
2025-11-21 22:00:26 +08:00
committed by GitHub
parent 0a6aa9da80
commit 529821ced6

View File

@@ -47,7 +47,7 @@ public final class HologramManagerImpl implements HologramManager {
HologramManagerImpl(@NotNull final FancyHolograms plugin, @NotNull final Function<HologramData, Hologram> adapter) { HologramManagerImpl(@NotNull final FancyHolograms plugin, @NotNull final Function<HologramData, Hologram> adapter) {
this.plugin = plugin; this.plugin = plugin;
this.adapter = adapter; this.adapter = adapter;
hologramLoadLogging = plugin.getHologramConfiguration().isHologramLoadLogging(); this.hologramLoadLogging = plugin.getHologramConfiguration().isHologramLoadLogging();
} }
/** /**
@@ -102,7 +102,7 @@ public final class HologramManagerImpl implements HologramManager {
* @param hologram The hologram to remove. * @param hologram The hologram to remove.
*/ */
public void removeHologram(@NotNull final Hologram hologram) { public void removeHologram(@NotNull final Hologram hologram) {
removeHologram(hologram.getData().getName()); this.removeHologram(hologram.getData().getName());
} }
/** /**
@@ -122,7 +122,7 @@ public final class HologramManagerImpl implements HologramManager {
} }
} }
FancyHolograms.get().getHologramThread().submit(() -> plugin.getHologramStorage().delete(hologram)); FancyHolograms.get().getHologramThread().submit(() -> this.plugin.getHologramStorage().delete(hologram));
} }
); );
@@ -142,44 +142,48 @@ public final class HologramManagerImpl implements HologramManager {
} }
public void saveHolograms() { public void saveHolograms() {
if (!isLoaded) { if (!this.isLoaded) {
return; return;
} }
plugin.getHologramStorage().saveBatch(getPersistentHolograms(), false); this.plugin.getHologramStorage().saveBatch(this.getPersistentHolograms(), false);
} }
@Override @Override
public void loadHolograms() { public void loadHolograms() {
List<Hologram> allLoaded = new ArrayList<>(); FancyHolograms.get().getHologramThread().submit(() -> {
List<Hologram> allLoaded = new ArrayList<>();
for (World world : Bukkit.getWorlds()) { for (World world : Bukkit.getWorlds()) {
Collection<Hologram> loaded = plugin.getHologramStorage().loadAll(world.getName()); Collection<Hologram> loaded = this.plugin.getHologramStorage().loadAll(world.getName());
loaded.forEach(this::addHologram); loaded.forEach(this::addHologram);
allLoaded.addAll(loaded); allLoaded.addAll(loaded);
} }
isLoaded = true; this.isLoaded = true;
FancyHolograms.get().getHologramThread().submit(() -> Bukkit.getPluginManager().callEvent(new HologramsLoadedEvent(ImmutableList.copyOf(allLoaded)))); Bukkit.getPluginManager().callEvent(new HologramsLoadedEvent(ImmutableList.copyOf(allLoaded)));
if (this.hologramLoadLogging) FancyHolograms.get().getFancyLogger().info(String.format("Loaded %d holograms for all loaded worlds", allLoaded.size()));
if (hologramLoadLogging) FancyHolograms.get().getFancyLogger().info(String.format("Loaded %d holograms for all loaded worlds", allLoaded.size())); });
} }
@Override @Override
public boolean isLoaded() { public boolean isLoaded() {
return isLoaded; return this.isLoaded;
} }
public void loadHolograms(String world) { public void loadHolograms(String world) {
ImmutableList<Hologram> loaded = ImmutableList.copyOf(plugin.getHologramStorage().loadAll(world)); FancyHolograms.get().getHologramThread().submit(() -> {
loaded.forEach(this::addHologram); ImmutableList<Hologram> loaded = ImmutableList.copyOf(this.plugin.getHologramStorage().loadAll(world));
loaded.forEach(this::addHologram);
isLoaded = true; this.isLoaded = true;
Bukkit.getPluginManager().callEvent(new HologramsLoadedEvent(ImmutableList.copyOf(loaded))); Bukkit.getPluginManager().callEvent(new HologramsLoadedEvent(ImmutableList.copyOf(loaded)));
if (hologramLoadLogging) FancyHolograms.get().getFancyLogger().info(String.format("Loaded %d holograms for world %s", loaded.size(), world)); if (this.hologramLoadLogging)
FancyHolograms.get().getFancyLogger().info(String.format("Loaded %d holograms for world %s", loaded.size(), world));
});
} }
/** /**
@@ -188,9 +192,9 @@ public final class HologramManagerImpl implements HologramManager {
* This method is intended to be called internally by the plugin. * This method is intended to be called internally by the plugin.
*/ */
void initializeTasks() { void initializeTasks() {
ScheduledExecutorService hologramThread = plugin.getHologramThread(); ScheduledExecutorService hologramThread = this.plugin.getHologramThread();
hologramThread.submit(() -> { hologramThread.submit(() -> {
loadHolograms(); this.loadHolograms();
hologramThread.scheduleAtFixedRate(() -> { hologramThread.scheduleAtFixedRate(() -> {
for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) { for (final Hologram hologram : this.plugin.getHologramsManager().getHolograms()) {
@@ -198,7 +202,7 @@ public final class HologramManagerImpl implements HologramManager {
hologram.forceUpdateShownStateFor(player); hologram.forceUpdateShownStateFor(player);
} }
} }
}, 0, plugin.getHologramConfiguration().getUpdateVisibilityInterval() * 50L, TimeUnit.MILLISECONDS); }, 0, this.plugin.getHologramConfiguration().getUpdateVisibilityInterval() * 50L, TimeUnit.MILLISECONDS);
}); });
final var updateTimes = CacheBuilder.newBuilder() final var updateTimes = CacheBuilder.newBuilder()
@@ -208,7 +212,7 @@ public final class HologramManagerImpl implements HologramManager {
hologramThread.scheduleAtFixedRate(() -> { hologramThread.scheduleAtFixedRate(() -> {
final var time = System.currentTimeMillis(); final var time = System.currentTimeMillis();
for (final var hologram : getHolograms()) { for (final var hologram : this.getHolograms()) {
HologramData data = hologram.getData(); HologramData data = hologram.getData();
if (data.hasChanges()) { if (data.hasChanges()) {
hologram.forceUpdate(); hologram.forceUpdate();
@@ -225,7 +229,7 @@ public final class HologramManagerImpl implements HologramManager {
hologramThread.scheduleWithFixedDelay(() -> { hologramThread.scheduleWithFixedDelay(() -> {
final var time = System.currentTimeMillis(); final var time = System.currentTimeMillis();
for (final var hologram : getHolograms()) { for (final var hologram : this.getHolograms()) {
if (hologram.getData() instanceof TextHologramData textData) { if (hologram.getData() instanceof TextHologramData textData) {
final var interval = textData.getTextUpdateInterval(); final var interval = textData.getTextUpdateInterval();
if (interval < 1) { if (interval < 1) {
@@ -250,8 +254,8 @@ public final class HologramManagerImpl implements HologramManager {
* Reloads holograms by clearing the existing holograms and loading them again from the plugin's configuration. * Reloads holograms by clearing the existing holograms and loading them again from the plugin's configuration.
*/ */
public void reloadHolograms() { public void reloadHolograms() {
unloadHolograms(); this.unloadHolograms();
loadHolograms(); this.loadHolograms();
} }
public void unloadHolograms() { public void unloadHolograms() {
@@ -278,7 +282,7 @@ public final class HologramManagerImpl implements HologramManager {
final var online = List.copyOf(Bukkit.getOnlinePlayers()); final var online = List.copyOf(Bukkit.getOnlinePlayers());
FancyHolograms.get().getHologramThread().submit(() -> { FancyHolograms.get().getHologramThread().submit(() -> {
List<Hologram> h = getPersistentHolograms().stream() List<Hologram> h = this.getPersistentHolograms().stream()
.filter(hologram -> hologram.getData().getLocation().getWorld().getName().equals(world)) .filter(hologram -> hologram.getData().getLocation().getWorld().getName().equals(world))
.toList(); .toList();