Skip to content

Commit c95f31d

Browse files
committed
Synchronized user manager
1 parent d7e5ac4 commit c95f31d

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/user/BukkitUserManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object BukkitUserManager: UserManager<CommandSender, PlayerUser>() {
1414
override fun get(native: CommandSender): PlayerUser {
1515
val player = native.asPlayer
1616
val uuid = player.uniqueId
17-
return byUuid[uuid]?.also { it.playerCache = player } ?: PlayerUser(player).also { byUuid[uuid] = it }
17+
return getCache(uuid)?.also { it.playerCache = player } ?: PlayerUser(player).also { set(uuid, it) }
1818
}
1919

2020
override fun create(uuid: UUID): PlayerUser = PlayerUser(uuid)

core/src/main/kotlin/io/github/rothes/esu/core/user/UserManager.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@ package io.github.rothes.esu.core.user
22

33
import io.github.rothes.esu.core.util.InitOnce
44
import java.util.*
5+
import java.util.concurrent.locks.ReentrantReadWriteLock
6+
import kotlin.concurrent.read
7+
import kotlin.concurrent.write
58

69
abstract class UserManager<T, R: User> {
710

8-
protected val byUuid = hashMapOf<UUID, R>()
11+
private val byUuid = hashMapOf<UUID, R>()
12+
private val lock = ReentrantReadWriteLock()
13+
14+
protected fun set(uuid: UUID, value: R) = lock.write { byUuid.put(uuid, value) }
915

1016
abstract operator fun get(native: T): R
11-
operator fun get(uuid: UUID): R = byUuid[uuid] ?: create(uuid).also { byUuid[uuid] = it }
17+
operator fun get(uuid: UUID): R = lock.read { byUuid[uuid] } ?: lock.write { create(uuid).also { byUuid[uuid] = it } }
1218

13-
fun getCache(uuid: UUID): R? = byUuid[uuid]
14-
fun getWithoutCaching(uuid: UUID): R = byUuid[uuid] ?: create(uuid)
19+
fun getCache(uuid: UUID): R? = lock.read { byUuid[uuid] }
20+
fun getWithoutCaching(uuid: UUID): R = getCache(uuid) ?: create(uuid)
1521

1622
abstract fun create(uuid: UUID): R
1723

1824
abstract fun unload(native: T): R?
19-
fun unload(uuid: UUID): R? = byUuid.remove(uuid)
25+
fun unload(uuid: UUID): R? = lock.write { byUuid.remove(uuid) }
2026
fun unload(user: R): R? = unload(user.uuid)
2127

2228

velocity/src/main/kotlin/io/github/rothes/esu/velocity/user/VelocityUserManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object VelocityUserManager: UserManager<CommandSource, PlayerUser>() {
1414
override fun get(native: CommandSource): PlayerUser {
1515
val player = native.asPlayer
1616
val uuid = player.uniqueId
17-
return byUuid[uuid]?.also { it.playerCache = player } ?: PlayerUser(player).also { byUuid[uuid] = it }
17+
return getCache(uuid)?.also { it.playerCache = player } ?: PlayerUser(player).also { set(uuid, it) }
1818
}
1919

2020
override fun create(uuid: UUID): PlayerUser = PlayerUser(uuid)

0 commit comments

Comments
 (0)