Add more thread-safety checks

This commit is contained in:
filoghost 2022-10-01 16:27:24 +02:00
parent c7e0f64469
commit 777b696f38
4 changed files with 20 additions and 4 deletions

View File

@ -39,7 +39,6 @@ class DefaultHolographicDisplaysAPI implements HolographicDisplaysAPI {
public @NotNull Hologram createHologram(@NotNull Location location) { public @NotNull Hologram createHologram(@NotNull Location location) {
Preconditions.notNull(location, "location"); Preconditions.notNull(location, "location");
Preconditions.notNull(location.getWorld(), "location.getWorld()"); Preconditions.notNull(location.getWorld(), "location.getWorld()");
Preconditions.checkMainThread("async hologram creation");
return apiHologramManager.createHologram(ImmutablePosition.of(location), plugin); return apiHologramManager.createHologram(ImmutablePosition.of(location), plugin);
} }
@ -48,7 +47,6 @@ class DefaultHolographicDisplaysAPI implements HolographicDisplaysAPI {
public @NotNull Hologram createHologram(@NotNull Position position) { public @NotNull Hologram createHologram(@NotNull Position position) {
Preconditions.notNull(position, "position"); Preconditions.notNull(position, "position");
Preconditions.notNull(position.getWorldName(), "position.getWorldName()"); Preconditions.notNull(position.getWorldName(), "position.getWorldName()");
Preconditions.checkMainThread("async hologram creation");
return apiHologramManager.createHologram(ImmutablePosition.of(position), plugin); return apiHologramManager.createHologram(ImmutablePosition.of(position), plugin);
} }

View File

@ -38,7 +38,6 @@ public class V2HologramsAPIProvider extends HologramsAPIProvider {
Preconditions.notNull(plugin, "plugin"); Preconditions.notNull(plugin, "plugin");
Preconditions.notNull(source, "source"); Preconditions.notNull(source, "source");
Preconditions.notNull(source.getWorld(), "source.getWorld()"); Preconditions.notNull(source.getWorld(), "source.getWorld()");
Preconditions.checkMainThread("async hologram creation");
return hologramManager.createHologram(ImmutablePosition.of(source), plugin); return hologramManager.createHologram(ImmutablePosition.of(source), plugin);
} }

View File

@ -5,6 +5,7 @@
*/ */
package me.filoghost.holographicdisplays.core.base; package me.filoghost.holographicdisplays.core.base;
import me.filoghost.fcommons.Preconditions;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.World; import org.bukkit.World;
@ -20,6 +21,8 @@ public abstract class BaseHologramManager<H extends BaseHologram> {
private final List<H> unmodifiableHologramsView = Collections.unmodifiableList(holograms); private final List<H> unmodifiableHologramsView = Collections.unmodifiableList(holograms);
protected void addHologram(H hologram) { protected void addHologram(H hologram) {
Preconditions.checkMainThread("async hologram create");
holograms.add(hologram); holograms.add(hologram);
} }
@ -28,11 +31,15 @@ public abstract class BaseHologramManager<H extends BaseHologram> {
} }
public void deleteHologram(H hologram) { public void deleteHologram(H hologram) {
Preconditions.checkMainThread("async hologram delete");
hologram.setDeleted(); hologram.setDeleted();
holograms.remove(hologram); holograms.remove(hologram);
} }
public void deleteHologramsIf(Predicate<H> condition) { public void deleteHologramsIf(Predicate<H> condition) {
Preconditions.checkMainThread("async hologram delete");
Iterator<H> iterator = holograms.iterator(); Iterator<H> iterator = holograms.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
H hologram = iterator.next(); H hologram = iterator.next();
@ -44,6 +51,8 @@ public abstract class BaseHologramManager<H extends BaseHologram> {
} }
public void deleteHolograms() { public void deleteHolograms() {
Preconditions.checkMainThread("async hologram delete");
Iterator<H> iterator = holograms.iterator(); Iterator<H> iterator = holograms.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
H hologram = iterator.next(); H hologram = iterator.next();

View File

@ -48,6 +48,16 @@ public class ChunkListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) { public void onChunkUnload(ChunkUnloadEvent event) {
Chunk chunk = event.getChunk(); Chunk chunk = event.getChunk();
// Always execute on the main thread
if (Bukkit.isPrimaryThread()) {
onChunkUnload(chunk);
} else {
Bukkit.getScheduler().runTask(plugin, () -> onChunkUnload(chunk));
}
}
private void onChunkUnload(Chunk chunk) {
apiHologramManager.onChunkUnload(chunk); apiHologramManager.onChunkUnload(chunk);
v2HologramManager.onChunkUnload(chunk); v2HologramManager.onChunkUnload(chunk);
} }
@ -61,7 +71,7 @@ public class ChunkListener implements Listener {
return; return;
} }
// In case another plugin loads the chunk asynchronously, always make sure to load the holograms on the main thread // Always execute on the main thread
if (Bukkit.isPrimaryThread()) { if (Bukkit.isPrimaryThread()) {
onChunkLoad(chunk); onChunkLoad(chunk);
} else { } else {