PhysicalConnectivity/src/main/kotlin/net/shadowfacts/phycon/util/PacketByteBuf.kt

38 lines
790 B
Kotlin

package net.shadowfacts.phycon.util
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtCompound
import net.minecraft.network.PacketByteBuf
/**
* @author shadowfacts
*/
fun PacketByteBuf.writeItemStackWithoutCount(stack: ItemStack) {
if (stack.isEmpty) {
writeBoolean(false)
} else {
writeBoolean(true)
writeVarInt(Item.getRawId(stack.item))
val tag: NbtCompound? =
if (stack.item.isDamageable || stack.item.isNbtSynced()) {
stack.nbt
} else {
null
}
writeNbt(tag)
}
}
fun PacketByteBuf.readItemStackWithoutCount(): ItemStack {
return if (!readBoolean()) {
ItemStack.EMPTY
} else {
val id = readVarInt()
val count = 1
val stack = ItemStack(Item.byRawId(id), count)
stack.nbt = readNbt()
stack
}
}