diff options
Diffstat (limited to 'src/main/kotlin/com/ambientaddons/utils/Extensions.kt')
-rw-r--r-- | src/main/kotlin/com/ambientaddons/utils/Extensions.kt | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/main/kotlin/com/ambientaddons/utils/Extensions.kt b/src/main/kotlin/com/ambientaddons/utils/Extensions.kt new file mode 100644 index 0000000..3da5767 --- /dev/null +++ b/src/main/kotlin/com/ambientaddons/utils/Extensions.kt @@ -0,0 +1,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) } + } + +} |