|
| 1 | +package com.lambda.interaction.construction |
| 2 | + |
| 3 | +import com.lambda.Lambda.mc |
| 4 | +import com.lambda.util.Communication.logError |
| 5 | +import com.lambda.util.FolderRegister |
| 6 | +import com.lambda.util.extension.readNbtOrException |
| 7 | +import net.minecraft.datafixer.DataFixTypes |
| 8 | +import net.minecraft.nbt.NbtCompound |
| 9 | +import net.minecraft.nbt.NbtHelper |
| 10 | +import net.minecraft.nbt.NbtIo |
| 11 | +import net.minecraft.nbt.NbtSizeTracker |
| 12 | +import net.minecraft.registry.Registries |
| 13 | +import net.minecraft.structure.StructureTemplate |
| 14 | +import net.minecraft.util.Identifier |
| 15 | +import net.minecraft.util.PathUtil |
| 16 | +import net.minecraft.util.WorldSavePath |
| 17 | +import java.nio.file.Files |
| 18 | +import java.nio.file.LinkOption |
| 19 | +import java.nio.file.StandardOpenOption |
| 20 | +import java.util.concurrent.ConcurrentHashMap |
| 21 | +import kotlin.io.path.inputStream |
| 22 | +import kotlin.io.path.notExists |
| 23 | + |
| 24 | +@Suppress("JavaIoSerializableObjectMustHaveReadResolve") |
| 25 | +object StructureRegistry : ConcurrentHashMap<Identifier, StructureTemplate?>() { |
| 26 | + private val levelSession = mc.levelStorage.createSession(FolderRegister.structure.path) |
| 27 | + private val generatedPath = levelSession.getDirectory(WorldSavePath.ROOT).normalize() |
| 28 | + |
| 29 | + /** |
| 30 | + * Loads a structure from disk based on the provided [id]. |
| 31 | + * |
| 32 | + * @param id The identifier of the structure to load. |
| 33 | + * @return The loaded [StructureTemplate], or null if the structure is not found. |
| 34 | + */ |
| 35 | + fun loadStructure(id: Identifier): StructureTemplate? { |
| 36 | + val path = PathUtil.getResourcePath(generatedPath, id.path, ".nbt") |
| 37 | + |
| 38 | + return if (!Files.isDirectory(generatedPath, LinkOption.NOFOLLOW_LINKS) || path.notExists()) null |
| 39 | + else computeIfAbsent(id) { |
| 40 | + val compound = path.inputStream(StandardOpenOption.READ) |
| 41 | + .use { template -> |
| 42 | + NbtIo.readCompressed( |
| 43 | + template, |
| 44 | + NbtSizeTracker.ofUnlimitedBytes() |
| 45 | + ).also { template.close() } |
| 46 | + } |
| 47 | + |
| 48 | + createStructure(nbt = compound) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a [StructureTemplate] from the provided NBT data. |
| 54 | + * |
| 55 | + * @param nbt The [NbtCompound] containing the structure's data. |
| 56 | + * @return The created [StructureTemplate], or null if there was an error. |
| 57 | + */ |
| 58 | + private fun createStructure(nbt: NbtCompound): StructureTemplate? { |
| 59 | + val template = StructureTemplate() |
| 60 | + val version = NbtHelper.getDataVersion(nbt, 500) |
| 61 | + |
| 62 | + template.readNbtOrException( |
| 63 | + Registries.BLOCK.readOnlyWrapper, |
| 64 | + DataFixTypes.STRUCTURE.update(mc.dataFixer, nbt, version), |
| 65 | + )?.let { err -> |
| 66 | + this@StructureRegistry.logError("Could not create structure from file", err.message ?: "") |
| 67 | + return null |
| 68 | + } |
| 69 | + |
| 70 | + return template |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Saves the provided [structure] to disk under the specified [name]. |
| 75 | + * |
| 76 | + * @param name The name of the structure file (without the ".nbt" extension). |
| 77 | + * @param structure The [StructureTemplate] to save. |
| 78 | + */ |
| 79 | + fun saveStructure(name: String, structure: StructureTemplate) { |
| 80 | + val path = PathUtil.getResourcePath(generatedPath, name, ".nbt") |
| 81 | + val compound = structure.writeNbt(NbtCompound()) |
| 82 | + |
| 83 | + NbtIo.writeCompressed(compound, path) |
| 84 | + } |
| 85 | +} |
0 commit comments