35 lines
883 B
Kotlin
35 lines
883 B
Kotlin
|
package net.shadowfacts.phycon.util
|
||
|
|
||
|
import net.minecraft.inventory.BasicInventory
|
||
|
import net.minecraft.item.ItemStack
|
||
|
import net.minecraft.nbt.CompoundTag
|
||
|
import net.minecraft.nbt.ListTag
|
||
|
import java.lang.RuntimeException
|
||
|
|
||
|
/**
|
||
|
* @author shadowfacts
|
||
|
*/
|
||
|
fun BasicInventory.toTag(): ListTag {
|
||
|
val list = ListTag()
|
||
|
for (slot in 0 until invSize) {
|
||
|
val stack = getInvStack(slot)
|
||
|
if (!stack.isEmpty) {
|
||
|
val stackTag = stack.toTag(CompoundTag())
|
||
|
stackTag.putInt("Slot", slot)
|
||
|
list.add(stackTag)
|
||
|
}
|
||
|
}
|
||
|
return list
|
||
|
}
|
||
|
|
||
|
fun BasicInventory.fromTag(list: ListTag) {
|
||
|
if (list.listType != 10) throw RuntimeException("Can't decode BasicInventory from list tag that does not contain compound tags")
|
||
|
this.clear()
|
||
|
for (tag in list) {
|
||
|
val compound = tag as CompoundTag
|
||
|
val stack = ItemStack.fromTag(compound)
|
||
|
val slot = compound.getInt("Slot")
|
||
|
setInvStack(slot, stack)
|
||
|
}
|
||
|
}
|