aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/ambientaddons/utils/Extensions.kt
blob: 21584ce3f6cf103c61b67b5289ecac5f6c15c707 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.ambientaddons.utils

import AmbientAddons.Companion.mc
import com.ambientaddons.utils.Extensions.enchants
import com.ambientaddons.utils.Extensions.extraAttributes
import com.ambientaddons.utils.Extensions.skyblockID
import net.minecraft.client.gui.GuiScreen
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.inventory.ContainerChest
import net.minecraft.inventory.IInventory
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.StringUtils

object Extensions {
    fun String.substringBetween(start: String, end: String): String {
        return this
            .substringAfter("(", "")
            .substringBefore(")", "")
    }

    fun String.stripControlCodes(): String {
        return StringUtils.stripControlCodes(this)
    }

    fun String.cleanSB(): String {
        return StringUtils.stripControlCodes(this)
            ?.toCharArray()
            ?.filter { it.code in 21..126 }
            ?.joinToString("")
            ?: ""
    }

    fun String.withModPrefix(): String = "§b§lAmbient §7» §r${this}"

    fun String.renderWidth(): Int = mc.fontRendererObj.getStringWidth(this)

    val GuiScreen.chest: ContainerChest?
        get() = (this as? GuiChest)?.inventorySlots as? ContainerChest

    val IInventory.items: List<ItemStack?>
        get() = (0 until this.sizeInventory).map { this.getStackInSlot(it) }


    private val ItemStack.extraAttributes: NBTTagCompound?
        get() {
            if (!this.hasTagCompound()) return null
            return this.getSubCompound("ExtraAttributes", false) ?: return null
        }

    val ItemStack.skyblockID: String?
        get() = this.extraAttributes?.let {
            if (!it.hasKey("id", 8)) return null
            return it.getString("id")
        }

    val ItemStack.itemQuality: Int?
        get() = this.extraAttributes?.let {
            if (!it.hasKey("baseStatBoostPercentage", 3)) return null
            return it.getInteger("baseStatBoostPercentage")
        }

    val ItemStack.stars: Int?
        get() = this.extraAttributes?.let {
            return when {
                it.hasKey("dungeon_item_level", 3) -> it.getInteger("dungeon_item_level")
                it.hasKey("upgrade_level", 3) -> it.getInteger("upgrade_level")
                else -> null
            }
        }

    val ItemStack.enchants: Map<String, Int>?
        get() = this.extraAttributes?.let { extraAttributes ->
            if (!extraAttributes.hasKey("enchantments", 10)) return null
            val enchantments = extraAttributes.getCompoundTag("enchantments")
            enchantments.keySet.associateWith { enchantments.getInteger(it) }
        }

    val ItemStack.lore: List<String>?
        get() {
            if (!this.hasTagCompound()) return null
            val displayTag = this.getSubCompound("display", false) ?: return null
            if (!displayTag.hasKey("Lore", 9)) return null
            val loreList = displayTag.getTagList("Lore", 8)
            return (0 until loreList.tagCount()).map { loreList.getStringTagAt(it) }
        }

}