mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-13 09:26:11 -04:00
Entity: cache id
This workarounds ANOTHER stupid stdlib bug. While entities are ticking SOMETIMES the lock *of the sync* is not releases after acquiring the read lock and no other threads can acquire the lock. I debugged this and it is a race condition in the ReentrantReadWriteLock (or in the synchronizer) class. Pretty ironic.
This commit is contained in:
parent
7d3f8aece1
commit
97e6cd11cf
@ -55,10 +55,20 @@ abstract class Entity(
|
|||||||
) : Initializable, EntityAttachable {
|
) : Initializable, EntityAttachable {
|
||||||
private var flags: Int by data(FLAGS_DATA, 0x00) { it.toInt() }
|
private var flags: Int by data(FLAGS_DATA, 0x00) { it.toInt() }
|
||||||
protected val random = Random()
|
protected val random = Random()
|
||||||
|
var _id: Int? = null
|
||||||
|
var _uuid: UUID? = null
|
||||||
val id: Int?
|
val id: Int?
|
||||||
get() = session.world.entities.getId(this)
|
get() {
|
||||||
|
if (_id != null) return _id
|
||||||
|
_id = session.world.entities.getId(this)
|
||||||
|
return _id
|
||||||
|
}
|
||||||
open val uuid: UUID?
|
open val uuid: UUID?
|
||||||
get() = session.world.entities.getUUID(this)
|
get() {
|
||||||
|
if (_uuid != null) return _uuid
|
||||||
|
_uuid = session.world.entities.getUUID(this)
|
||||||
|
return _uuid
|
||||||
|
}
|
||||||
|
|
||||||
@Deprecated(message = "Use session.version", replaceWith = ReplaceWith("session.version.versionId"))
|
@Deprecated(message = "Use session.version", replaceWith = ReplaceWith("session.version.versionId"))
|
||||||
protected val versionId: Int get() = session.version.versionId
|
protected val versionId: Int get() = session.version.versionId
|
||||||
@ -95,6 +105,7 @@ abstract class Entity(
|
|||||||
|
|
||||||
open val canRaycast: Boolean get() = true
|
open val canRaycast: Boolean get() = true
|
||||||
|
|
||||||
|
|
||||||
var age = 0
|
var age = 0
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
@ -96,6 +96,8 @@ class WorldEntities : Iterable<Entity> {
|
|||||||
|
|
||||||
fun remove(entity: Entity) {
|
fun remove(entity: Entity) {
|
||||||
lock.lock()
|
lock.lock()
|
||||||
|
entity._id = null
|
||||||
|
entity._uuid = null
|
||||||
if (entity !is LocalPlayerEntity && !entities.remove(entity)) {
|
if (entity !is LocalPlayerEntity && !entities.remove(entity)) {
|
||||||
lock.unlock()
|
lock.unlock()
|
||||||
return
|
return
|
||||||
@ -112,6 +114,8 @@ class WorldEntities : Iterable<Entity> {
|
|||||||
lock.unlock()
|
lock.unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
entity._id = null
|
||||||
|
entity._uuid = null
|
||||||
if (entity is LocalPlayerEntity) {
|
if (entity is LocalPlayerEntity) {
|
||||||
idEntityMap.put(entityId, entity)
|
idEntityMap.put(entityId, entity)
|
||||||
lock.unlock()
|
lock.unlock()
|
||||||
@ -192,6 +196,8 @@ class WorldEntities : Iterable<Entity> {
|
|||||||
fun clear(session: PlaySession, local: Boolean = false) {
|
fun clear(session: PlaySession, local: Boolean = false) {
|
||||||
this.lock.lock()
|
this.lock.lock()
|
||||||
for (entity in this.entities) {
|
for (entity in this.entities) {
|
||||||
|
entity._id = null
|
||||||
|
entity._uuid = null
|
||||||
if (!local && entity is LocalPlayerEntity) continue
|
if (!local && entity is LocalPlayerEntity) continue
|
||||||
entityIdMap.remove(entity)?.let { idEntityMap.remove(it) }
|
entityIdMap.remove(entity)?.let { idEntityMap.remove(it) }
|
||||||
entityUUIDMap.remove(entity)?.let { uuidEntityMap.remove(it) }
|
entityUUIDMap.remove(entity)?.let { uuidEntityMap.remove(it) }
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* Minosoft
|
||||||
* Copyright (C) 2020-2024 Moritz Zwerger
|
* Copyright (C) 2020-2025 Moritz Zwerger
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
*
|
*
|
||||||
@ -62,11 +62,11 @@ class EntityObjectSpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
|
|||||||
}
|
}
|
||||||
entity.startInit()
|
entity.startInit()
|
||||||
entity.setObjectData(data)
|
entity.setObjectData(data)
|
||||||
|
velocity?.let { entity.physics.velocity = it }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handle(session: PlaySession) {
|
override fun handle(session: PlaySession) {
|
||||||
session.world.entities.add(entityId, entityUUID, entity)
|
session.world.entities.add(entityId, entityUUID, entity)
|
||||||
velocity?.let { entity.physics.velocity = it }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun log(reducedLog: Boolean) {
|
override fun log(reducedLog: Boolean) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user