Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Commit

Permalink
Workaround async entity loading
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Aug 31, 2021
1 parent 79f73a9 commit f17c766
Showing 1 changed file with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.studiocode.miniatureblocks.miniature.armorstand

import de.studiocode.inventoryaccess.util.VersionUtils
import de.studiocode.miniatureblocks.MiniatureBlocks
import de.studiocode.miniatureblocks.miniature.Miniature
import de.studiocode.miniatureblocks.miniature.armorstand.impl.AnimatedMiniatureArmorStand
Expand All @@ -11,6 +12,7 @@ import de.studiocode.miniatureblocks.miniature.item.impl.AnimatedMiniatureItem
import de.studiocode.miniatureblocks.miniature.item.impl.NormalMiniatureItem
import de.studiocode.miniatureblocks.resourcepack.file.ModelFile.CustomModel
import de.studiocode.miniatureblocks.util.getTargetMiniature
import de.studiocode.miniatureblocks.util.runTaskLater
import de.studiocode.miniatureblocks.util.runTaskTimer
import de.studiocode.miniatureblocks.util.sendPrefixedMessage
import org.bukkit.*
Expand Down Expand Up @@ -150,22 +152,31 @@ class MiniatureManager(plugin: MiniatureBlocks) : Listener {
fun handleChunkLoad(event: ChunkLoadEvent) = handleChunkLoad(event.chunk)

private fun handleChunkLoad(chunk: Chunk) {
chunk.entities
.filterValidCoordinates()
.filterIsInstance<ArmorStand>()
.filter { it.hasMiniatureData() }
.forEach { armorStand ->
val miniature = MiniatureType.newInstance(armorStand)!!

if (miniature.isValid()) {
// put it into map
loadedMiniatures[armorStand] = miniature
armorStand.isMarker = true // set version < 0.10 armor stands to marker
} else {
// remove armor stand if this miniature model does no longer exist
armorStand.remove()
if (VersionUtils.isServerHigherOrEqual("1.17")) {
// Workaround async entity loading (https://hub.spigotmc.org/jira/browse/SPIGOT-6547)
runTaskLater(20 * 15) { loadArmorStandsInChunk(chunk) }
} else loadArmorStandsInChunk(chunk)
}

private fun loadArmorStandsInChunk(chunk: Chunk) {
if (chunk.isLoaded) {
chunk.entities
.filterValidCoordinates()
.filterIsInstance<ArmorStand>()
.filter { it.hasMiniatureData() }
.forEach { armorStand ->
val miniature = MiniatureType.newInstance(armorStand)!!

if (miniature.isValid()) {
// put it into map
loadedMiniatures[armorStand] = miniature
armorStand.isMarker = true // set version < 0.10 armor stands to marker
} else {
// remove armor stand if this miniature model does no longer exist
armorStand.remove()
}
}
}
}
}

@EventHandler
Expand Down

0 comments on commit f17c766

Please sign in to comment.