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

38 lines
790 B
Kotlin
Raw Normal View History

2021-03-23 02:01:33 +00:00
package net.shadowfacts.phycon.util
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
2021-12-22 23:59:51 +00:00
import net.minecraft.nbt.NbtCompound
2021-03-23 02:01:33 +00:00
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))
2021-12-22 23:59:51 +00:00
val tag: NbtCompound? =
if (stack.item.isDamageable || stack.item.isNbtSynced()) {
stack.nbt
2021-03-23 02:01:33 +00:00
} else {
null
}
2021-12-22 23:59:51 +00:00
writeNbt(tag)
2021-03-23 02:01:33 +00:00
}
}
fun PacketByteBuf.readItemStackWithoutCount(): ItemStack {
return if (!readBoolean()) {
ItemStack.EMPTY
} else {
val id = readVarInt()
val count = 1
val stack = ItemStack(Item.byRawId(id), count)
2021-12-22 23:59:51 +00:00
stack.nbt = readNbt()
2021-03-23 02:01:33 +00:00
stack
}
}