Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 28f5fb2

Browse files
authored
Merge pull request #124 from SLNE-Development/copilot/sub-pr-113-another-one
Fix race condition in PersistentPlayerDataContainerView.snapshot()
2 parents b347ddc + 96c2918 commit 28f5fb2

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/player/CommonCloudPlayerImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ abstract class CommonCloudPlayerImpl(uuid: UUID, override val name: String) :
2222
protected val persistentDataView = object : PersistentPlayerDataContainerViewImpl() {
2323
override fun toTagCompound() = ppdcReentrantLock.read { ppdc.tag }
2424
override fun getTag(key: String) = ppdcReentrantLock.read { ppdc.getTag(key) }
25+
override fun snapshotTag() = ppdcReentrantLock.read { super.snapshotTag() }
2526
}
2627

2728
override val persistentData get() = persistentDataView

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/player/ppdc/PersistentPlayerDataContainerImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ open class PersistentPlayerDataContainerImpl(
144144
}
145145

146146
override fun snapshot(): PersistentPlayerDataContainerImpl {
147-
return PersistentPlayerDataContainerImpl(tag.fast())
147+
return PersistentPlayerDataContainerImpl(snapshotTag().fast())
148148
}
149149

150150
fun fromTagCompound(tag: CompoundBinaryTag) {

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/player/ppdc/PersistentPlayerDataContainerViewImpl.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,27 @@ abstract class PersistentPlayerDataContainerViewImpl : PersistentPlayerDataConta
114114
}
115115

116116
override fun snapshot(): PersistentPlayerDataContainerViewImpl {
117-
val tag = deepCopy(toTagCompound())
118-
119-
val tagCopy = CompoundBinaryTag.builder()
120-
.put(tag)
121-
.build()
117+
val tagCopy = snapshotTag()
122118

123119
return object : PersistentPlayerDataContainerViewImpl() {
124120
override fun toTagCompound() = tagCopy
125121
override fun getTag(key: String) = tagCopy.get(key)
122+
override fun snapshotTag() = tagCopy
126123
}
127124
}
128125

126+
/**
127+
* Creates a snapshot of the tag compound.
128+
* Subclasses should override this method to ensure the snapshot is created
129+
* while holding appropriate locks to prevent concurrent modifications.
130+
*/
131+
protected open fun snapshotTag(): CompoundBinaryTag {
132+
val tag = deepCopy(toTagCompound())
133+
return CompoundBinaryTag.builder()
134+
.put(tag)
135+
.build()
136+
}
137+
129138

130139
/**
131140
* Creates a deep copy of the provided `CompoundBinaryTag` without using recursion.

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/player/ppdc/TrackingPlayerPersistentDataContainerImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ open class TrackingPlayerPersistentDataContainerImpl(
7777
}
7878

7979
override fun snapshot(): TrackingPlayerPersistentDataContainerImpl {
80-
return TrackingPlayerPersistentDataContainerImpl(tag.fast())
80+
return TrackingPlayerPersistentDataContainerImpl(snapshotTag().fast())
8181
}
8282
}

0 commit comments

Comments
 (0)