Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Support migrating old items by custom model data
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed May 14, 2022
1 parent dbb09d1 commit e6d96e6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/main/kotlin/com/mineinabyss/looty/LootyFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ 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
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.*
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/mineinabyss/looty/config/LootyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.Serializable
object LootyConfig : IdofrontConfig<LootyConfig.Data>(looty, Data.serializer()) {
@Serializable
class Data(
val debug: Boolean = false
val debug: Boolean = false,
val migrateByCustomModelData: Boolean = false
)
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,8 +11,6 @@ import com.mineinabyss.looty.ecs.components.LootyType

object LootyTypeQuery : GearyQuery() {
val TargetScope.key by get<PrefabKey>()
val TargetScope.isLooty by family {
has<LootyType>()
has<Prefab>()
}
val TargetScope.type by get<LootyType>()
val TargetScope.isPrefab by family { has<Prefab>() }
}
Original file line number Diff line number Diff line change
@@ -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<CustomItem, PrefabKey>()

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]
}

0 comments on commit e6d96e6

Please sign in to comment.