Skip to content

Commit

Permalink
Bugfixes for child removal and loading offline players
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Aug 2, 2022
1 parent c9b58d2 commit dc0b816
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record> = 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BukkitEntity>(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<BukkitEntity>(this@toGeary) }
}

//TODO perhaps add a function literal that fires before any entity create events do
inline fun BukkitEntity.toGeary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}
Expand Down

0 comments on commit dc0b816

Please sign in to comment.