diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/TypeMap.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/TypeMap.kt index 39d4dc81f..e9542b857 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/TypeMap.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/TypeMap.kt @@ -3,28 +3,25 @@ package com.mineinabyss.geary.datatypes.maps import com.mineinabyss.geary.datatypes.GearyEntity import com.mineinabyss.geary.datatypes.Record import com.soywiz.kds.* -import kotlinx.atomicfu.locks.SynchronizedObject -import kotlinx.atomicfu.locks.synchronized public class TypeMap { - private val lock = SynchronizedObject() +// private val lock = SynchronizedObject() private val map: FastIntMap = FastIntMap() // We don't return nullable record to avoid boxing. // Accessing an entity that doesn't exist is indicative of a problem elsewhere and should be made obvious. @PublishedApi - internal fun get(entity: GearyEntity): Record = synchronized(lock) { map[entity.id.toInt()] } + internal fun get(entity: GearyEntity): Record = map[entity.id.toInt()] ?: error("Tried to access components on an entity that no longer exists (${entity.id})") @PublishedApi - internal fun set(entity: GearyEntity, record: Record): Unit = synchronized(lock) { + internal fun set(entity: GearyEntity, record: Record) { if (contains(entity)) error("Tried setting the record of an entity that already exists.") map[entity.id.toInt()] = record } - internal fun remove(entity: GearyEntity) = synchronized(lock) { + internal fun remove(entity: GearyEntity) = map.remove(entity.id.toInt()) - } public operator fun contains(entity: GearyEntity): Boolean = map.contains(entity.id.toInt()) diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/GearyEngine.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/GearyEngine.kt index a109f20fe..85df03196 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/GearyEngine.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/GearyEngine.kt @@ -222,7 +222,7 @@ public open class GearyEngine(override val tickDuration: Duration) : TickingEngi entity.apply { children.forEach { // Remove self from the child's parents or remove the child if it no longer has parents - if (parents == setOf(this)) it.removeEntity(event) + if (it.parents == setOf(this)) it.removeEntity(event) else it.removeParent(this) } } diff --git a/geary-prefabs/src/main/kotlin/com/mineinabyss/geary/prefabs/PrefabManager.kt b/geary-prefabs/src/main/kotlin/com/mineinabyss/geary/prefabs/PrefabManager.kt index 97e68c1e3..85297b6d0 100644 --- a/geary-prefabs/src/main/kotlin/com/mineinabyss/geary/prefabs/PrefabManager.kt +++ b/geary-prefabs/src/main/kotlin/com/mineinabyss/geary/prefabs/PrefabManager.kt @@ -10,6 +10,7 @@ import com.mineinabyss.geary.prefabs.configuration.components.Prefab import com.mineinabyss.geary.prefabs.helpers.inheritPrefabs import com.mineinabyss.geary.serialization.GearyEntitySerializer import com.mineinabyss.idofront.messaging.logError +import com.mineinabyss.idofront.messaging.logWarn import okio.Path.Companion.toOkioPath import java.io.File import java.util.* @@ -68,8 +69,8 @@ class PrefabManager : GearyContext by GearyContextKoin() { registerPrefab(key, entity) entity }.onFailure { - logError("Error deserializing prefab: $name from ${file.path}") - it.printStackTrace() + logError("Can't read prefab $name from ${file.path}:") + logWarn(it.toString()) }.getOrNull() } } diff --git a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/access/BukkitEntity2Geary.kt b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/access/BukkitEntity2Geary.kt index 441b6dba2..e662d8a24 100644 --- a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/access/BukkitEntity2Geary.kt +++ b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/access/BukkitEntity2Geary.kt @@ -105,7 +105,7 @@ class BukkitEntity2Geary : Listener, GearyMCContext by GearyMCContextKoin() { entity.toGearyOrNull()?.removeEntity() } - @EventHandler(priority = EventPriority.LOWEST) + @EventHandler(priority = EventPriority.HIGHEST) fun PlayerQuitEvent.onPlayerLogout() { player.toGearyOrNull()?.removeEntity() } diff --git a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/access/BukkitEntityConversion.kt b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/access/BukkitEntityConversion.kt index 88b5c7944..0d7dfa694 100644 --- a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/access/BukkitEntityConversion.kt +++ b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/access/BukkitEntityConversion.kt @@ -4,9 +4,12 @@ import com.mineinabyss.geary.datatypes.GearyEntity import com.mineinabyss.geary.helpers.entity import com.mineinabyss.geary.papermc.globalContextMC import com.mineinabyss.idofront.typealiases.BukkitEntity +import org.bukkit.entity.Player -fun BukkitEntity.toGeary(): GearyEntity = - globalContextMC.bukkit2Geary[entityId] ?: entity { set(this@toGeary) } +fun BukkitEntity.toGeary(): GearyEntity { + if(this is Player && !isOnline) error("Tried to access Geary entity for offline player: $name") + return globalContextMC.bukkit2Geary[entityId] ?: entity { set(this@toGeary) } +} //TODO perhaps add a function literal that fires before any entity create events do inline fun BukkitEntity.toGeary( diff --git a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/engine/PaperMCEngine.kt b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/engine/PaperMCEngine.kt index bfcb38b56..a1bba1d02 100644 --- a/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/engine/PaperMCEngine.kt +++ b/platforms/papermc/core/src/main/kotlin/com/mineinabyss/geary/papermc/engine/PaperMCEngine.kt @@ -7,8 +7,8 @@ import com.mineinabyss.geary.systems.GearySystem import com.mineinabyss.geary.systems.TickingSystem import com.mineinabyss.idofront.plugin.registerEvents import com.mineinabyss.idofront.time.ticks -import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlinx.coroutines.yield import org.bukkit.Bukkit import org.bukkit.NamespacedKey import org.bukkit.event.Listener @@ -40,7 +40,7 @@ class PaperMCEngine(private val plugin: Plugin) : GearyEngine(tickDuration = 1.t launch(plugin.minecraftDispatcher) { while (true) { tick(Bukkit.getServer().currentTick.toLong()) - delay(tickDuration) + yield() } } }