Skip to content

Commit

Permalink
persistence: Ensure that CompoundSavedData always saves to file
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobkmar committed May 13, 2024
1 parent 1f96e5f commit a8ca3f6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {

allprojects {
group = "net.silkmc"
version = "1.10.5"
version = "1.10.6"
if (this.name.startsWith("silk")) {
description = "Silk is a Minecraft API for Kotlin"
}
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/BuildConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ object BuildConstants {
const val majorMinecraftVersion = "1.20"

// check these values here: https://jakobk.net/mcdev
const val minecraftVersion = "1.20.5"
const val fabricLoaderVersion = "0.15.10"
const val minecraftVersion = "1.20.6"
const val fabricLoaderVersion = "0.15.11"
const val fabricLanguageKotlinVersion = "1.10.19+kotlin.1.9.23"

const val kotestVersion = "5.8.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.silkmc.silk.persistence.mixin.other;

import net.minecraft.core.HolderLookup;
import net.minecraft.world.level.saveddata.SavedData;
import net.silkmc.silk.persistence.internal.CompoundSavedData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.File;

@Mixin(SavedData.class)
public class MixinSavedData {

@SuppressWarnings("UnreachableCode")
@Inject(
method = "save(Ljava/io/File;Lnet/minecraft/core/HolderLookup$Provider;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/nbt/NbtUtils;addCurrentDataVersion(Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/nbt/CompoundTag;",
shift = At.Shift.BEFORE
),
cancellable = true
)
private void onSave(File file, HolderLookup.Provider provider,
CallbackInfo ci) {
if (((SavedData) (Object) this) instanceof CompoundSavedData compoundSavedData) {
if (compoundSavedData.getCompound().isEmpty() && !file.exists()) {
ci.cancel();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ abstract class PersistentCompound {
@PublishedApi
internal val values = HashMap<CompoundKey<*>, Any>()

/**
* Returns whether this compound contains no data.
*/
val isEmpty: Boolean
get() = values.isEmpty() && (data?.isEmpty != false)

/**
* Puts the given value into the persistent storage.
*
Expand Down Expand Up @@ -165,6 +171,8 @@ internal class PersistentCompoundImpl : PersistentCompound() {
}

override fun putInCompound(nbtCompound: CompoundTag, writeRaw: Boolean) {
if (isEmpty) return

val currentData = data!!

for ((untypedKey, value) in values) {
Expand All @@ -174,11 +182,10 @@ internal class PersistentCompoundImpl : PersistentCompound() {
currentData.put(typedKey.name, typedKey.convertValueToNbtElement(value))
}

if (!currentData.isEmpty) {
if (writeRaw)
nbtCompound.merge(currentData)
else
nbtCompound.put(CUSTOM_DATA_KEY, currentData)
if (writeRaw) {
nbtCompound.merge(currentData)
} else {
nbtCompound.put(CUSTOM_DATA_KEY, currentData)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import net.silkmc.silk.core.annotations.InternalSilkApi
import net.silkmc.silk.persistence.PersistentCompound

@InternalSilkApi
class CompoundSavedData(internal val compound: PersistentCompound) : SavedData() {
class CompoundSavedData(val compound: PersistentCompound) : SavedData() {

// a compound does not track whether it is dirty, however custom
// logic is implemented via a mixin to prevent file creation for
// empty compounds
override fun isDirty(): Boolean {
return true
}

override fun save(nbt: CompoundTag, provider: HolderLookup.Provider) =
nbt.also { compound.putInCompound(it, writeRaw = true) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"chunk.ProtoChunkMixin",
"entity.EntityMixin",
"other.MixinDataFixTypes",
"other.MixinSavedData",
"world.ServerWorldMixin"
]
}

0 comments on commit a8ca3f6

Please sign in to comment.