aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/ItemUtil.kt
blob: 4bbd4cde72810837866871ab6b70b6867610f2d0 (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
package moe.nea.ledger

import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound


fun ItemStack.getExtraAttributes(): NBTTagCompound {
	val nbt = this.tagCompound ?: return NBTTagCompound()
	return nbt.getCompoundTag("ExtraAttributes")
}

fun ItemStack?.getInternalId(): ItemId? {
	if (this == null) return null
	val extraAttributes = getExtraAttributes()
	var id = extraAttributes.getString("id")
	id = id.takeIf { it.isNotBlank() }
	if (id == "PET") {
		id = getPetId() ?: id
	}
	if (id == "ENCHANTED_BOOK") {
		id = getEnchanments().entries.singleOrNull()?.let {
			"${it.key};${it.value}".uppercase()
		}
	}
	return id?.let(::ItemId)
}

fun ItemStack.getEnchanments(): Map<String, Int> {
	val enchantments = getExtraAttributes().getCompoundTag("enchantments")
	return enchantments.keySet.associateWith { enchantments.getInteger(it) }
}

class PetInfo {
	var type: String? = null
	var tier: String? = null
}

fun ItemStack.getPetId(): String? {
	val petInfoStr = getExtraAttributes().getString("petInfo")
	val petInfo = Ledger.gson.fromJson(petInfoStr, PetInfo::class.java)
	if (petInfo.type == null || petInfo.tier == null) return null
	return petInfo.type + ";" + rarityToIndex(petInfo.tier ?: "")
}

fun rarityToIndex(rarity: String): Int {
	return when (rarity) {
		"COMMON" -> 0
		"UNCOMMON" -> 1
		"RARE" -> 2
		"EPIC" -> 3
		"LEGENDARY" -> 4
		"MYTHIC" -> 5
		else -> -1
	}
}

fun ItemStack.getLore(): List<String> {
	val nbt = this.tagCompound ?: NBTTagCompound()
	val extraAttributes = nbt.getCompoundTag("display")
	val lore = extraAttributes.getTagList("Lore", 8)
	return (0 until lore.tagCount()).map { lore.getStringTagAt(it) }
}


fun ItemStack.getDisplayNameU(): String {
	val nbt = this.tagCompound ?: NBTTagCompound()
	val extraAttributes = nbt.getCompoundTag("display")
	return extraAttributes.getString("Name")
}