aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/ItemUtil.kt
blob: 38c2b50783ea47f6412ac3923af0bc7acb69db8f (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
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? {
	val extraAttributes = getExtraAttributes()
	var id = extraAttributes.getString("id")
	id = id.takeIf { it.isNotBlank() }
	if (id == "PET") {
		id = getPetId() ?: id
	}
	return id?.let(::ItemId)
}

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")
}