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

Commit 71297ee

Browse files
committed
Merge remote-tracking branch 'origin/feat/ppdc-view' into copilot/sub-pr-113
2 parents 51a1154 + 6b809c9 commit 71297ee

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/player/CloudPlayer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ interface CloudPlayer : Audience, OfflineCloudPlayer {
6666
*/
6767
fun <R> editPdc(block: PersistentPlayerDataContainer.() -> R): R
6868

69-
@Deprecated("Use renamed non-suspendable method", ReplaceWith("editPdc(block)"))
69+
@Deprecated("Use non-suspending editPdc method instead", ReplaceWith("editPdc(block)"))
7070
suspend fun <R> withPersistentData(block: PersistentPlayerDataContainer.() -> R): R =
7171
editPdc(block)
7272

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: 4 additions & 4 deletions
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) {
@@ -215,20 +215,20 @@ open class PersistentPlayerDataContainerImpl(
215215
) {
216216
if (pathToDeepestParent.isEmpty()) return
217217

218-
val stack = Stack<Pair<FastCompoundBinaryTag, String>>()
218+
val stack = ArrayDeque<Pair<FastCompoundBinaryTag, String>>()
219219
var current: FastCompoundBinaryTag = root
220220

221221
for (segment in pathToDeepestParent) {
222222
val childTag = current.getCompound(segment, null) ?: return
223223
val childFast = childTag as? FastCompoundBinaryTag ?: childTag.fast()
224224

225225
current.put(segment, childFast)
226-
stack.push(current to segment)
226+
stack.addLast(current to segment)
227227
current = childFast
228228
}
229229

230230
while (stack.isNotEmpty()) {
231-
val (parent, key) = stack.pop()
231+
val (parent, key) = stack.removeLast()
232232
val child = parent.getCompound(key, null) ?: continue
233233
if (child.size() == 0) {
234234
parent.remove(key)

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
@@ -124,18 +124,27 @@ abstract class PersistentPlayerDataContainerViewImpl : PersistentPlayerDataConta
124124
}
125125

126126
override fun snapshot(): PersistentPlayerDataContainerViewImpl {
127-
val tag = deepCopy(toTagCompound())
128-
129-
val tagCopy = CompoundBinaryTag.builder()
130-
.put(tag)
131-
.build()
127+
val tagCopy = snapshotTag()
132128

133129
return object : PersistentPlayerDataContainerViewImpl() {
134130
override fun toTagCompound() = tagCopy
135131
override fun getTag(key: String) = tagCopy.get(key)
132+
override fun snapshotTag() = tagCopy
136133
}
137134
}
138135

136+
/**
137+
* Creates a snapshot of the tag compound.
138+
* Subclasses should override this method to ensure the snapshot is created
139+
* while holding appropriate locks to prevent concurrent modifications.
140+
*/
141+
protected open fun snapshotTag(): CompoundBinaryTag {
142+
val tag = deepCopy(toTagCompound())
143+
return CompoundBinaryTag.builder()
144+
.put(tag)
145+
.build()
146+
}
147+
139148

140149
/**
141150
* 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)