diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageBusBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageBusBlockEntity.kt index 7d1d84e17..184917bff 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageBusBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageBusBlockEntity.kt @@ -35,6 +35,7 @@ import ru.dbotthepony.mc.otm.storage.* import java.math.BigInteger import java.util.* import java.util.function.Consumer +import java.util.function.Supplier import java.util.stream.Stream private data class SlotTuple(val slot: Int, val stack: ItemStack) @@ -130,7 +131,7 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter } init { - savetables.codec(::filter, ItemFilter.CODEC, FILTER_KEY) + savetables.codec(::filter, ItemFilter.CODEC, FILTER_KEY, Supplier { ItemFilter(MAX_FILTERS) }) } override fun setLevel(level: Level) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageInterfaces.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageInterfaces.kt index 2eb5f7747..090a594f4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageInterfaces.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/storage/StorageInterfaces.kt @@ -38,6 +38,7 @@ import ru.dbotthepony.mc.otm.storage.ItemStorageStack import ru.dbotthepony.mc.otm.storage.StorageStack import java.math.BigInteger import java.util.* +import java.util.function.Supplier abstract class AbstractStorageImportExport( blockType: BlockEntityType<*>, @@ -111,7 +112,7 @@ abstract class AbstractStorageImportExport( } init { - savetables.codec(::filter, ItemFilter.CODEC, FILTER_KEY) + savetables.codec(::filter, ItemFilter.CODEC, FILTER_KEY, Supplier { ItemFilter(MAX_FILTERS) }) } companion object { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Savetables.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Savetables.kt index 26875fc30..caa118d04 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Savetables.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/Savetables.kt @@ -207,7 +207,7 @@ class Savetables : INBTSerializable { } } - fun codec(prop: Delegate, codec: Codec, name: String, default: T): Stateless { + fun codec(prop: Delegate, codec: Codec, name: String, default: Supplier): Stateless { return Stateless(prop, name, Tag::class.java) .withSerializer { codec.encode(prop.get(), NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).getOrThrow { throw IllegalStateException("Failed to write save data for '$name': $it") } } .withDeserializer { @@ -215,9 +215,9 @@ class Savetables : INBTSerializable { .decode(NbtOps.INSTANCE, it) .resultOrPartial { LOGGER.error("Failed to read save data for '$name'", RuntimeException(it)) } .map { it.first } - .orElse(default) + .orElseGet(default) } - .withDefault { default } + .withDefault { default.get() } } fun codec(prop: KMutableProperty0, codec: Codec, name: String = prop.name): Stateless { @@ -225,10 +225,22 @@ class Savetables : INBTSerializable { } fun codec(prop: KMutableProperty0, codec: Codec, name: String = prop.name, default: T): Stateless { + return codec(Delegate.Of(prop), codec, name, Supplier { default }) + } + + fun codec(prop: KMutableProperty0, codec: Codec, name: String = prop.name, default: Supplier): Stateless { return codec(Delegate.Of(prop), codec, name, default) } + fun codec(prop: KMutableProperty0, codec: Codec, default: Supplier): Stateless { + return codec(Delegate.Of(prop), codec, prop.name, default) + } + fun vector(prop: Delegate, name: String, default: Vector = Vector.ZERO): Stateless { + return codec(prop, Vector.CODEC, name, Supplier { default }) + } + + fun vector(prop: Delegate, name: String, default: Supplier): Stateless { return codec(prop, Vector.CODEC, name, default) }