Skip to content

Commit 1445c38

Browse files
committed
Merge branch 'master' into refactor/ui
2 parents a30f575 + 485da54 commit 1445c38

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

common/src/main/kotlin/com/lambda/command/commands/BuildCommand.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.lambda.command.commands
1919

20-
import com.lambda.brigadier.CommandResult
2120
import com.lambda.brigadier.CommandResult.Companion.failure
2221
import com.lambda.brigadier.CommandResult.Companion.success
2322
import com.lambda.brigadier.argument.greedyString
@@ -60,7 +59,7 @@ object BuildCommand : LambdaCommand(
6059
try {
6160
StructureRegistry
6261
.loadStructureByRelativePath(Path.of(pathString))
63-
?.let { template ->
62+
.let { template ->
6463
info("Building structure $pathString with dimensions ${template.size.toShortString()} created by ${template.author}")
6564
lastBuildTask = template.toStructure()
6665
.move(player.blockPos)

common/src/main/kotlin/com/lambda/config/AbstractSetting.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.config
1919

2020
import com.google.gson.JsonElement
21+
import com.lambda.Lambda.LOG
2122
import com.lambda.Lambda.gson
2223
import com.lambda.context.SafeContext
2324
import com.lambda.threading.runSafe
@@ -97,7 +98,12 @@ abstract class AbstractSetting<T : Any>(
9798
gson.toJsonTree(value, type)
9899

99100
override fun loadFromJson(serialized: JsonElement) {
100-
value = gson.fromJson(serialized, type)
101+
runCatching {
102+
value = gson.fromJson(serialized, type)
103+
}.onFailure {
104+
LOG.warn("Failed to load setting ${this.name} with value $serialized. Resetting to default value $defaultValue", it)
105+
value = defaultValue
106+
}
101107
}
102108

103109
/**

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ abstract class Configurable(
7878

7979
override fun loadFromJson(serialized: JsonElement) {
8080
serialized.asJsonObject.entrySet().forEach { (name, value) ->
81-
settings.find {
82-
it.name == name
83-
}?.loadFromJson(value)
81+
settings.find { it.name == name }?.loadFromJson(value)
8482
?: LOG.warn("No saved setting found for $name with $value in ${this::class.simpleName}")
8583
}
8684
}

common/src/main/kotlin/com/lambda/interaction/construction/StructureRegistry.kt

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818
package com.lambda.interaction.construction
1919

20+
import com.lambda.Lambda.LOG
2021
import com.lambda.core.Loadable
21-
import com.lambda.util.Communication.logError
2222
import com.lambda.util.FolderRegister
23+
import com.lambda.util.FolderRegister.structure
2324
import com.lambda.util.extension.readLitematicaOrException
2425
import com.lambda.util.extension.readNbtOrException
2526
import com.lambda.util.extension.readSchematicOrException
@@ -45,10 +46,10 @@ import kotlin.io.path.*
4546
*/
4647
@OptIn(ExperimentalPathApi::class)
4748
@Suppress("JavaIoSerializableObjectMustHaveReadResolve")
48-
object StructureRegistry : ConcurrentHashMap<String, StructureTemplate?>(), Loadable {
49+
object StructureRegistry : ConcurrentHashMap<String, StructureTemplate>(), Loadable {
4950
private val pathWatcher by lazy {
5051
FileSystems.getDefault().newWatchService()
51-
.apply { FolderRegister.structure.register(this, ENTRY_CREATE, ENTRY_DELETE) }
52+
.apply { structure.register(this, ENTRY_CREATE, ENTRY_DELETE) }
5253
}
5354

5455
/**
@@ -69,12 +70,13 @@ object StructureRegistry : ConcurrentHashMap<String, StructureTemplate?>(), Load
6970
*
7071
* @param relativePath The name of the structure to load (without extension).
7172
* @param convert Whether to replace the file after converting it.
72-
* @return The loaded [StructureTemplate], or null if the structure is not found.
73+
*
74+
* @throws IllegalStateException if there was an error while parsing the data
7375
*/
7476
fun loadStructureByRelativePath(
7577
relativePath: Path,
7678
convert: Boolean = true,
77-
): StructureTemplate? {
79+
): StructureTemplate {
7880
updateFileWatcher()
7981

8082
return computeIfAbsent(relativePath.pathString.lowercase()) {
@@ -115,52 +117,49 @@ object StructureRegistry : ConcurrentHashMap<String, StructureTemplate?>(), Load
115117
* Loads the structure file and creates a [StructureTemplate].
116118
*
117119
* @param convert Whether to replace the file after converting it.
120+
* @throws IllegalStateException if the parsed data is corrupted
121+
*
118122
* @return The created [StructureTemplate], or null if the structure is not found or invalid.
119123
*/
120124
private fun loadFileAndCreate(path: Path, convert: Boolean) =
121-
FolderRegister.structure.resolve(path).inputStream().use { templateStream ->
125+
structure.resolve(path).inputStream().use { templateStream ->
122126
val compound = NbtIo.readCompressed(templateStream, NbtSizeTracker.ofUnlimitedBytes())
123127
val extension = path.extension
124128
val template = createStructure(compound, extension)
125129

126130
if (convert && extension != "nbt") {
127-
template?.let { saveStructure(path.pathString, it) }
131+
saveStructure(path.nameWithoutExtension, template)
128132
}
129133

130134
// Verify the structure integrity after it had been
131135
// converted to a regular structure template
132-
if (compound.isValidStructureTemplate()) {
133-
template
134-
} else {
135-
logError("Corrupted structure file: ${path.pathString}")
136-
null
137-
}
136+
if (compound.isValidStructureTemplate()) template
137+
else throw IllegalStateException("Corrupted structure file: ${path.pathString}")
138138
}
139139

140140
/**
141141
* Creates a [StructureTemplate] from the provided NBT data.
142142
*
143143
* @param nbt The [NbtCompound] containing the structure's data.
144-
* @return The created [StructureTemplate], or null if there was an error.
144+
* @throws IllegalStateException if there was an error while parsing the data
145145
*/
146-
private fun createStructure(nbt: NbtCompound, suffix: String): StructureTemplate? =
146+
private fun createStructure(nbt: NbtCompound, suffix: String): StructureTemplate =
147147
StructureTemplate().apply {
148148
serializers[suffix]
149149
?.invoke(this, Registries.BLOCK.readOnlyWrapper, nbt)
150150
?.let { error ->
151-
logError("Could not create structure from file: ${error.message}")
152-
return null
151+
throw IllegalStateException("Could not create structure: ${error.message}")
153152
}
154153
}
155154

156155
/**
157156
* Saves the provided [structure] to disk under the specified [name].
158157
*
159-
* @param relativePath The relative path of the structure to save.
158+
* @param relativePath The relative path of the structure to save without the extension.
160159
* @param structure The [StructureTemplate] to save.
161160
*/
162161
private fun saveStructure(relativePath: String, structure: StructureTemplate) {
163-
val path = FolderRegister.structure.resolve(relativePath)
162+
val path = FolderRegister.structure.resolve("$relativePath.nbt")
164163
val compound = structure.writeNbt(NbtCompound())
165164

166165
Files.createDirectories(path.parent)
@@ -179,9 +178,13 @@ object StructureRegistry : ConcurrentHashMap<String, StructureTemplate?>(), Load
179178
contains("DataVersion") && contains("blocks") && contains("palette") && contains("size")
180179

181180
override fun load(): String {
182-
FolderRegister.structure.walk()
183-
.filter { it.extension in serializers.keys }
184-
.forEach { loadStructureByRelativePath(FolderRegister.structure.relativize(it)) }
181+
runCatching {
182+
structure.walk()
183+
.filter { it.extension in serializers.keys }
184+
.sortedBy { it.extension.length } // Don’t walk lexicographically -Constructor
185+
.distinctBy { it.nameWithoutExtension }
186+
.forEach { loadStructureByRelativePath(structure.relativize(it)) }
187+
}.onFailure { LOG.warn(it.message) }
185188

186189
return "Loaded $size structure templates"
187190
}

common/src/main/kotlin/com/lambda/util/FolderRegister.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ object FolderRegister : Loadable {
9696
val path = resolve(
9797
hostName.sanitizeForFilename()
9898
).resolve(
99-
mc.world?.dimensionKey?.value?.path?.sanitizeForFilename() ?: "unknown"
99+
mc.world?.dimensionKey?.value?.path?.sanitizeForFilename() ?: "unknown" // TODO: Change with utils when merged to master
100100
)
101-
path.createIfNotExists()
101+
path.mkdirs()
102102
return path
103103
}
104104
}

0 commit comments

Comments
 (0)