From e6d96e69e08dc945e2447eb0e070326e6b2aaf65 Mon Sep 17 00:00:00 2001 From: 0ffz Date: Sat, 14 May 2022 15:41:36 -0400 Subject: [PATCH] Support migrating old items by custom model data --- .../com/mineinabyss/looty/LootyFactory.kt | 10 ++++++- .../mineinabyss/looty/config/LootyConfig.kt | 3 +- .../looty/ecs/queries/LootyTypeQuery.kt | 8 ++---- .../CustomModelDataToPrefabMap.kt | 28 +++++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/com/mineinabyss/looty/migration/custommodeldata/CustomModelDataToPrefabMap.kt diff --git a/src/main/kotlin/com/mineinabyss/looty/LootyFactory.kt b/src/main/kotlin/com/mineinabyss/looty/LootyFactory.kt index 0719ce9..90e13d2 100644 --- a/src/main/kotlin/com/mineinabyss/looty/LootyFactory.kt +++ b/src/main/kotlin/com/mineinabyss/looty/LootyFactory.kt @@ -14,6 +14,7 @@ import com.mineinabyss.geary.papermc.store.decodeComponentsFrom import com.mineinabyss.geary.papermc.store.encodeComponentsTo import com.mineinabyss.geary.papermc.store.encodePrefabs import com.mineinabyss.geary.prefabs.PrefabKey +import com.mineinabyss.looty.config.LootyConfig import com.mineinabyss.looty.ecs.components.LootyType import com.mineinabyss.looty.ecs.components.PlayerInstancedItem import com.mineinabyss.looty.ecs.components.PlayerInstancedItems @@ -21,6 +22,8 @@ import com.mineinabyss.looty.ecs.components.inventory.SlotType import com.mineinabyss.looty.ecs.components.itemcontexts.PlayerInventorySlotContext import com.mineinabyss.looty.ecs.components.itemcontexts.PlayerSingletonContext import com.mineinabyss.looty.ecs.components.itemcontexts.ProcessingItemContext +import com.mineinabyss.looty.migration.custommodeldata.CustomItem +import com.mineinabyss.looty.migration.custommodeldata.CustomModelDataToPrefabMap import org.bukkit.Material import org.bukkit.inventory.ItemStack import java.util.* @@ -63,7 +66,12 @@ object LootyFactory { /** Gets or creates a [GearyEntity] based on a given item and the context it is in. */ fun PlayerInventorySlotContext.loadItem(context: ProcessingItemContext): GearyEntity? = with(context) { - if (!hasComponentsEncoded) return null + if (!hasComponentsEncoded) { + if (!LootyConfig.data.migrateByCustomModelData) return null + val item = updateMeta() + val prefab = CustomModelDataToPrefabMap[CustomItem(item.type, item.itemMeta.customModelData)] ?: return null + meta.persistentDataContainer.encodePrefabs(listOf(prefab)) + } val gearyPlayer = holder.toGeary() val decoded = meta.persistentDataContainer.decodeComponents() diff --git a/src/main/kotlin/com/mineinabyss/looty/config/LootyConfig.kt b/src/main/kotlin/com/mineinabyss/looty/config/LootyConfig.kt index ec78df7..ca88b6c 100644 --- a/src/main/kotlin/com/mineinabyss/looty/config/LootyConfig.kt +++ b/src/main/kotlin/com/mineinabyss/looty/config/LootyConfig.kt @@ -8,6 +8,7 @@ import kotlinx.serialization.Serializable object LootyConfig : IdofrontConfig(looty, Data.serializer()) { @Serializable class Data( - val debug: Boolean = false + val debug: Boolean = false, + val migrateByCustomModelData: Boolean = false ) } diff --git a/src/main/kotlin/com/mineinabyss/looty/ecs/queries/LootyTypeQuery.kt b/src/main/kotlin/com/mineinabyss/looty/ecs/queries/LootyTypeQuery.kt index f920bae..998bb1d 100644 --- a/src/main/kotlin/com/mineinabyss/looty/ecs/queries/LootyTypeQuery.kt +++ b/src/main/kotlin/com/mineinabyss/looty/ecs/queries/LootyTypeQuery.kt @@ -1,7 +1,7 @@ package com.mineinabyss.looty.ecs.queries -import com.mineinabyss.geary.datatypes.family.family import com.mineinabyss.geary.datatypes.family.MutableFamilyOperations.Companion.has +import com.mineinabyss.geary.datatypes.family.family import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.geary.prefabs.configuration.components.Prefab import com.mineinabyss.geary.systems.accessors.TargetScope @@ -11,8 +11,6 @@ import com.mineinabyss.looty.ecs.components.LootyType object LootyTypeQuery : GearyQuery() { val TargetScope.key by get() - val TargetScope.isLooty by family { - has() - has() - } + val TargetScope.type by get() + val TargetScope.isPrefab by family { has() } } diff --git a/src/main/kotlin/com/mineinabyss/looty/migration/custommodeldata/CustomModelDataToPrefabMap.kt b/src/main/kotlin/com/mineinabyss/looty/migration/custommodeldata/CustomModelDataToPrefabMap.kt new file mode 100644 index 0000000..d1cf0c4 --- /dev/null +++ b/src/main/kotlin/com/mineinabyss/looty/migration/custommodeldata/CustomModelDataToPrefabMap.kt @@ -0,0 +1,28 @@ +package com.mineinabyss.looty.migration.custommodeldata + +import com.mineinabyss.geary.prefabs.PrefabKey +import com.mineinabyss.looty.ecs.queries.LootyTypeQuery +import org.bukkit.Material + +data class CustomItem( + val material: Material, + val customModelData: Int +) + +/** + * Assists in migrating old items which only have custom model data to define their item type. + */ +object CustomModelDataToPrefabMap { + private val map = mutableMapOf() + + init { + LootyTypeQuery.run { + forEach { + val item = it.type.item + map[CustomItem(item.type ?: return@forEach, item.customModelData ?: return@forEach)] = it.key + } + } + } + + operator fun get(item: CustomItem) = map[item] +}