Skip to content

Commit fa42aa9

Browse files
committed
[EntityCulling] Improve synchronized usages
1 parent 83f031c commit fa42aa9

File tree

1 file changed

+24
-29
lines changed
  • bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/networkthrottle/entityculling

1 file changed

+24
-29
lines changed

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/networkthrottle/entityculling/UserCullData.kt

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ class UserCullData(
3232

3333
var shouldCull = true
3434

35+
@Synchronized
3536
fun setCulled(entity: Entity, entityId: Int, culled: Boolean, pend: Boolean = true) {
3637
if (culled) {
37-
synchronized(hiddenEntities) {
38-
if (hiddenEntities.put(entityId, entity) == null && pend)
39-
pendCulledChange(entity, true)
40-
}
38+
if (hiddenEntities.put(entityId, entity) == null && pend)
39+
pendCulledChange(entity, true)
4140
} else {
42-
synchronized(hiddenEntities) {
43-
if (hiddenEntities.remove(entityId) != null && pend)
44-
pendCulledChange(entity, false)
45-
}
41+
if (hiddenEntities.remove(entityId) != null && pend)
42+
pendCulledChange(entity, false)
4643
}
4744
}
4845

@@ -61,7 +58,7 @@ class UserCullData(
6158

6259
fun onEntityRemove(entities: IntArrayList) {
6360
if (entities.isEmpty) return
64-
synchronized(hiddenEntities) {
61+
synchronized(this) {
6562
entities.forEachInt { i ->
6663
hiddenEntities.remove(i)
6764
}
@@ -72,32 +69,30 @@ class UserCullData(
7269
isRemoved = true
7370
}
7471

72+
@Synchronized
7573
private fun reset() {
76-
synchronized(hiddenEntities) {
77-
val values = hiddenEntities.values
78-
hiddenEntities.clear()
79-
for (entity in values) {
80-
pendCulledChange(entity, false)
81-
}
74+
val values = hiddenEntities.values
75+
hiddenEntities.clear()
76+
for (entity in values) {
77+
pendCulledChange(entity, false)
8278
}
8379
}
8480

81+
@Synchronized
8582
private fun checkEntitiesValid() {
8683
try {
87-
synchronized(hiddenEntities) {
88-
val raytraceHandler = raytraceHandler
89-
val iterator = hiddenEntities.int2ReferenceEntrySet().iterator()
90-
val playerLoc = player.location
91-
for (entry in iterator) {
92-
val entity = entry.value
93-
var flag = !raytraceHandler.isValid(entity)
94-
val loc = entity.location
95-
if (loc.world != playerLoc.world || (playerLoc.x - loc.x).square() + (playerLoc.z - loc.z).square() > 1024 * 1024) {
96-
playerEntityVisibilityHandler.forceShowEntity(player, entity)
97-
flag = true
98-
}
99-
if (flag) iterator.remove()
84+
val raytraceHandler = raytraceHandler
85+
val iterator = hiddenEntities.int2ReferenceEntrySet().iterator()
86+
val playerLoc = player.location
87+
for (entry in iterator) {
88+
val entity = entry.value
89+
var flag = !raytraceHandler.isValid(entity)
90+
val loc = entity.location
91+
if (loc.world != playerLoc.world || (playerLoc.x - loc.x).square() + (playerLoc.z - loc.z).square() > 1024 * 1024) {
92+
playerEntityVisibilityHandler.forceShowEntity(player, entity)
93+
flag = true
10094
}
95+
if (flag) iterator.remove()
10196
}
10297
} catch (e: Throwable) {
10398
plugin.err("[EntityCulling] Failed to check entities valid for player ${player.name}", e)
@@ -107,7 +102,7 @@ class UserCullData(
107102
private fun updateChanges() {
108103
if (terminated) return
109104

110-
val changes = synchronized(hiddenEntities) {
105+
val changes = synchronized(this) {
111106
if (pendingChanges.isEmpty()) return
112107
val temp = pendingChanges.toTypedArray()
113108
pendingChanges.clear()

0 commit comments

Comments
 (0)