aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/ambientaddons/utils/Extensions.kt
diff options
context:
space:
mode:
authorAppability <appable@icloud.com>2022-10-11 23:59:37 -0700
committerAppability <appable@icloud.com>2022-10-11 23:59:37 -0700
commit2c0e73deb53f54d78bd5594786313ac82151be1a (patch)
tree97fc2f389cc99d8f9e078a1cd7b0c6a5859a3f34 /src/main/kotlin/com/ambientaddons/utils/Extensions.kt
parent363b2426f8d9e45e52c472750c798dcaceb05a88 (diff)
downloadAmbientAddons-2c0e73deb53f54d78bd5594786313ac82151be1a.tar.gz
AmbientAddons-2c0e73deb53f54d78bd5594786313ac82151be1a.tar.bz2
AmbientAddons-2c0e73deb53f54d78bd5594786313ac82151be1a.zip
added chest qol features (complete, i think)
Diffstat (limited to 'src/main/kotlin/com/ambientaddons/utils/Extensions.kt')
-rw-r--r--src/main/kotlin/com/ambientaddons/utils/Extensions.kt73
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) }
+ }
+
+}