Use codecs to deserialize pattern state from nbt tags
This commit is contained in:
parent
2be09fe5d2
commit
c8e7cd4c93
@ -67,27 +67,6 @@ private fun <T : IPatternState> codec(builder: RecordCodecBuilder.Instance<T>):
|
||||
)
|
||||
}
|
||||
|
||||
private fun deserializeNBT(nbt: Tag?, mutable: Boolean): IPatternState? {
|
||||
if (nbt !is CompoundTag)
|
||||
return null
|
||||
|
||||
if (!nbt.contains("id", "researchPercent", "item"))
|
||||
return null
|
||||
|
||||
val id = nbt.getUUID("id")
|
||||
val researchPercent = nbt.getDouble("researchPercent")
|
||||
val item = ForgeRegistries.ITEMS.getValue(ResourceLocation(nbt.getString("item"))) ?: return null
|
||||
|
||||
if (item == Items.AIR)
|
||||
return null
|
||||
|
||||
if (mutable) {
|
||||
return MutablePatternState(id, item, researchPercent)
|
||||
} else {
|
||||
return PatternState(id, item, researchPercent)
|
||||
}
|
||||
}
|
||||
|
||||
private fun read(buff: FriendlyByteBuf, mutable: Boolean): IPatternState? {
|
||||
val id = buff.readUUID()
|
||||
val item = buff.readItemType()
|
||||
@ -121,7 +100,8 @@ data class PatternState(
|
||||
|
||||
companion object {
|
||||
fun deserializeNBT(tag: Tag?): PatternState? {
|
||||
return deserializeNBT(tag, false) as PatternState?
|
||||
tag ?: return null
|
||||
return CODEC.decode(NbtOps.INSTANCE, tag).result().map { it.first }.orElse(null)
|
||||
}
|
||||
|
||||
fun read(buff: FriendlyByteBuf): PatternState? {
|
||||
@ -155,7 +135,8 @@ data class MutablePatternState(
|
||||
|
||||
companion object {
|
||||
fun deserializeNBT(tag: Tag?): MutablePatternState? {
|
||||
return deserializeNBT(tag, true) as MutablePatternState?
|
||||
tag ?: return null
|
||||
return CODEC.decode(NbtOps.INSTANCE, tag).result().map { it.first }.orElse(null)
|
||||
}
|
||||
|
||||
fun read(buff: FriendlyByteBuf): MutablePatternState? {
|
||||
@ -169,7 +150,3 @@ data class MutablePatternState(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun FriendlyByteBuf.writePatternState(state: IPatternState) = state.write(this)
|
||||
fun FriendlyByteBuf.readPatternState() = PatternState.read(this)
|
||||
fun FriendlyByteBuf.readMutablePatternState() = MutablePatternState.read(this)
|
||||
|
Loading…
Reference in New Issue
Block a user