aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/ambientaddons/utils/Extensions.kt
blob: 3da576789c57509dd05f22b7baad705473b2ab5f (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
package com.ambientaddons.utils

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 {
        return "§9§lAmbient §7» §r${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.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) }
        }

}