aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/debug/itemeditor/LegacyItemData.kt
blob: c0f48ca5505515b949d7df73893556be09022340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package moe.nea.firmament.features.debug.itemeditor

import kotlinx.serialization.Serializable
import kotlin.jvm.optionals.getOrNull
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtCompound
import net.minecraft.util.Identifier
import moe.nea.firmament.Firmament
import moe.nea.firmament.repo.ExpensiveItemCacheApi
import moe.nea.firmament.repo.ItemCache
import moe.nea.firmament.util.MC

/**
 * Load data based on [prismarine.js' 1.8 item data](https://github.com/PrismarineJS/minecraft-data/blob/master/data/pc/1.8/items.json)
 */
object LegacyItemData {
	@Serializable
	data class ItemData(
		val id: Int,
		val name: String,
		val displayName: String,
		val stackSize: Int,
		val variations: List<Variation> = listOf()
	) {
		val properId = if (name.contains(":")) name else "minecraft:$name"

		fun allVariants() =
			variations.map { LegacyItemType(properId, it.metadata.toShort()) } + LegacyItemType(properId, 0)
	}

	@Serializable
	data class Variation(
		val metadata: Int, val displayName: String
	)

	data class LegacyItemType(
		val name: String,
		val metadata: Short
	) {
		override fun toString(): String {
			return "$name:$metadata"
		}
	}

	@Serializable
	data class EnchantmentData(
		val id: Int,
		val name: String,
		val displayName: String,
	)

	inline fun <reified T : Any> getLegacyData(name: String) =
		Firmament.tryDecodeJsonFromStream<T>(
			LegacyItemData::class.java.getResourceAsStream("/legacy_data/$name.json")!!
		).getOrThrow()

	val enchantmentData = getLegacyData<List<EnchantmentData>>("enchantments")
	val enchantmentLut = enchantmentData.associateBy { Identifier.ofVanilla(it.name) }

	val itemDat = getLegacyData<List<ItemData>>("items")
	@OptIn(ExpensiveItemCacheApi::class) // This is fine, we get loaded in a thread.
	val itemLut = itemDat.flatMap { item ->
		item.allVariants().map { legacyItemType ->
			val nbt = ItemCache.convert189ToModern(NbtCompound().apply {
				putString("id", legacyItemType.name)
				putByte("Count", 1)
				putShort("Damage", legacyItemType.metadata)
			})!!
			val stack = ItemStack.fromNbt(MC.defaultRegistries, nbt).getOrNull()
				?: error("Could not transform ${legacyItemType}")
			stack.item to legacyItemType
		}
	}.toMap()

}