diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-19 22:13:32 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-19 22:13:32 +0100 |
commit | 175379be77dd530bc019e34d3937950df22e4f8a (patch) | |
tree | 26667fda120efefa857655583f622f170856bd38 /src/main/kotlin/moe/nea/ledger/ItemUtil.kt | |
parent | 7c4b2e089fa23622799bc06a08f6932197365368 (diff) | |
download | LocalTransactionLedger-175379be77dd530bc019e34d3937950df22e4f8a.tar.gz LocalTransactionLedger-175379be77dd530bc019e34d3937950df22e4f8a.tar.bz2 LocalTransactionLedger-175379be77dd530bc019e34d3937950df22e4f8a.zip |
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/ItemUtil.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/ItemUtil.kt | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/ItemUtil.kt b/src/main/kotlin/moe/nea/ledger/ItemUtil.kt index 949f58a..a3d8162 100644 --- a/src/main/kotlin/moe/nea/ledger/ItemUtil.kt +++ b/src/main/kotlin/moe/nea/ledger/ItemUtil.kt @@ -1,5 +1,6 @@ package moe.nea.ledger +import net.minecraft.inventory.IInventory import net.minecraft.item.ItemStack import net.minecraft.nbt.NBTTagCompound @@ -37,7 +38,10 @@ class PetInfo { fun ItemStack.getPetId(): String? { val petInfoStr = getExtraAttributes().getString("petInfo") - val petInfo = runCatching { Ledger.gson.fromJson(petInfoStr, PetInfo::class.java) }.getOrNull() // TODO: error reporting to sentry + val petInfo = runCatching { + Ledger.gson.fromJson(petInfoStr, + PetInfo::class.java) + }.getOrNull() // TODO: error reporting to sentry if (petInfo?.type == null || petInfo.tier == null) return null return petInfo.type + ";" + rarityToIndex(petInfo.tier ?: "") } @@ -62,6 +66,22 @@ fun ItemStack.getLore(): List<String> { } +fun IInventory.asIterable(): Iterable<ItemStack?> = object : Iterable<ItemStack?> { + override fun iterator(): Iterator<ItemStack?> { + return object : Iterator<ItemStack?> { + var i = 0 + override fun hasNext(): Boolean { + return i < this@asIterable.sizeInventory + } + + override fun next(): ItemStack? { + if (!hasNext()) throw NoSuchElementException("$i is out of range for inventory ${this@asIterable}") + return this@asIterable.getStackInSlot(i++) + } + } + } +} + fun ItemStack.getDisplayNameU(): String { val nbt = this.tagCompound ?: NBTTagCompound() val extraAttributes = nbt.getCompoundTag("display") |