From f8965e583c20c26ec232b58d43a95eb09f10b145 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 20 Sep 2022 22:45:59 +0700 Subject: [PATCH] Make android research builder remember old feature levels --- .../mc/otm/android/AndroidResearchBuilder.kt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchBuilder.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchBuilder.kt index f6812b9d9..f4872f0d5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchBuilder.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchBuilder.kt @@ -3,13 +3,18 @@ package ru.dbotthepony.mc.otm.android import com.google.common.collect.ImmutableList import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap import net.minecraft.ChatFormatting +import net.minecraft.nbt.CompoundTag +import net.minecraft.nbt.ListTag import net.minecraft.network.chat.Component import net.minecraft.resources.ResourceLocation import net.minecraft.world.item.ItemStack import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.client.render.SkinElement import ru.dbotthepony.mc.otm.container.iterator +import ru.dbotthepony.mc.otm.core.getCompoundList +import ru.dbotthepony.mc.otm.core.map import ru.dbotthepony.mc.otm.core.nonEmpty +import ru.dbotthepony.mc.otm.core.set import ru.dbotthepony.mc.otm.registry.MRegistry import kotlin.collections.ArrayList @@ -239,6 +244,42 @@ class AndroidResearchBuilder( } } + override fun serializeNBT(): CompoundTag { + return super.serializeNBT().also { + it["oldResearchLevel"] = ListTag().also { + for ((k, v) in oldResearchLevel) { + it.add(CompoundTag().also { + it["key"] = k.registryName!!.toString() + it["value"] = CompoundTag().also { + it["isPresent"] = v.level != null + + if (v.level != null) { + it["value"] = v.level + } + } + }) + } + } + } + } + + override fun deserializeNBT(nbt: CompoundTag) { + super.deserializeNBT(nbt) + + oldResearchLevel.clear() + + for (tag in nbt.getCompoundList("oldResearchLevel")) { + val key = tag.getString("key") + val type = MRegistry.ANDROID_FEATURES.getValue(ResourceLocation(key)) ?: continue + val value = tag.getCompound("value") + + val isPresent = value.getBoolean("isPresent") + val int = value.getInt("value") + + oldResearchLevel[type] = RememberResearchLevel(if (isPresent) int else null) + } + } + override fun consumeResearchCost(simulate: Boolean): Boolean { if (capability.ply.abilities.instabuild) { // creative