Move streams exts from Utils to FriendlyStreams

This commit is contained in:
DBotThePony 2025-03-30 18:46:36 +07:00
parent ca95da8c6f
commit 844323ead6
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 71 additions and 71 deletions

View File

@ -1,14 +1,24 @@
package ru.dbotthepony.mc.otm.util
import io.netty.handler.codec.EncoderException
import net.minecraft.core.IdMap
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.NbtAccounter
import net.minecraft.nbt.NbtIo
import net.minecraft.network.FriendlyByteBuf
import net.minecraft.network.RegistryFriendlyByteBuf
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.ComponentSerialization
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.mc.otm.util.math.Decimal
import java.io.*
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.enums.EnumEntries
import kotlin.math.absoluteValue
// But seriously, Mojang, why would you need to derive from ByteBuf directly, when you can implement
@ -250,3 +260,64 @@ fun OutputStream.writeDecimal(value: Decimal) {
writeVarIntLE(bytes.size)
write(bytes)
}
fun <T : Any> FriendlyByteBuf.writeType(registry: IdMap<T>, value: T) {
writeInt(registry.getId(value))
}
fun FriendlyByteBuf.writeBlockType(value: Block) {
writeType(BuiltInRegistries.BLOCK, value)
}
fun FriendlyByteBuf.writeBlockState(value: BlockState) {
writeVarInt(Block.BLOCK_STATE_REGISTRY.getIdOrThrow(value))
}
fun FriendlyByteBuf.writeItemType(value: Item) {
writeType(BuiltInRegistries.ITEM, value)
}
fun RegistryFriendlyByteBuf.writeItem(value: ItemStack) {
ItemStack.OPTIONAL_STREAM_CODEC.encode(this, value)
}
fun RegistryFriendlyByteBuf.writeComponent(value: Component) {
ComponentSerialization.STREAM_CODEC.encode(this, value)
}
fun OutputStream.writeItemType(value: Item) {
writeVarIntLE(BuiltInRegistries.ITEM.getId(value))
}
fun <E : Enum<E>> FriendlyByteBuf.readEnum(entries: EnumEntries<E>): E {
return entries[readVarInt()]
}
fun <T : Any> FriendlyByteBuf.readType(registry: IdMap<T>): T {
val id = readInt()
return registry.byId(id) ?: throw NoSuchElementException("No such entry with ID $id")
}
fun FriendlyByteBuf.readBlockType(): Block {
return readType(BuiltInRegistries.BLOCK)
}
fun FriendlyByteBuf.readBlockState(): BlockState {
return Block.BLOCK_STATE_REGISTRY.byIdOrThrow(readVarInt())
}
fun FriendlyByteBuf.readItemType(): Item {
return readType(BuiltInRegistries.ITEM)
}
fun RegistryFriendlyByteBuf.readItem(): ItemStack {
return ItemStack.OPTIONAL_STREAM_CODEC.decode(this)
}
fun RegistryFriendlyByteBuf.readComponent(): Component {
return ComponentSerialization.STREAM_CODEC.decode(this)
}
fun InputStream.readItemType(): Item {
return BuiltInRegistries.ITEM.byId(readVarIntLE())
}

View File

@ -14,20 +14,14 @@ import net.minecraft.Util
import net.minecraft.core.BlockPos
import net.minecraft.core.Holder
import net.minecraft.core.HolderLookup
import net.minecraft.core.IdMap
import net.minecraft.core.Registry
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.network.FriendlyByteBuf
import net.minecraft.network.RegistryFriendlyByteBuf
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.ComponentContents
import net.minecraft.network.chat.ComponentSerialization
import net.minecraft.network.chat.contents.TranslatableContents
import net.minecraft.resources.ResourceKey
import net.minecraft.resources.ResourceLocation
import net.minecraft.tags.TagKey
import net.minecraft.world.entity.Entity
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.component.ItemAttributeModifiers
import net.minecraft.world.item.crafting.CraftingInput
@ -44,8 +38,6 @@ import net.neoforged.neoforge.items.IItemHandler
import ru.dbotthepony.mc.otm.util.math.BlockRotation
import ru.dbotthepony.mc.otm.util.math.BlockRotationFreedom
import ru.dbotthepony.mc.otm.util.math.Vector
import java.io.InputStream
import java.io.OutputStream
import java.lang.ref.Reference
import java.math.BigInteger
import java.time.Duration
@ -55,8 +47,6 @@ import java.util.concurrent.Future
import java.util.function.Supplier
import java.util.stream.Stream
import java.util.stream.StreamSupport
import kotlin.NoSuchElementException
import kotlin.enums.EnumEntries
import kotlin.jvm.optionals.getOrNull
import kotlin.reflect.KProperty
@ -129,67 +119,6 @@ inline fun itemAttributes(builder: ItemAttributeModifiers.Builder.() -> Unit): I
return value.build()
}
fun <T : Any> FriendlyByteBuf.writeType(registry: IdMap<T>, value: T) {
writeInt(registry.getId(value))
}
fun FriendlyByteBuf.writeBlockType(value: Block) {
writeType(BuiltInRegistries.BLOCK, value)
}
fun FriendlyByteBuf.writeBlockState(value: BlockState) {
writeVarInt(Block.BLOCK_STATE_REGISTRY.getIdOrThrow(value))
}
fun FriendlyByteBuf.writeItemType(value: Item) {
writeType(BuiltInRegistries.ITEM, value)
}
fun RegistryFriendlyByteBuf.writeItem(value: ItemStack) {
ItemStack.OPTIONAL_STREAM_CODEC.encode(this, value)
}
fun RegistryFriendlyByteBuf.writeComponent(value: Component) {
ComponentSerialization.STREAM_CODEC.encode(this, value)
}
fun OutputStream.writeItemType(value: Item) {
writeVarIntLE(BuiltInRegistries.ITEM.getId(value))
}
fun <E : Enum<E>> FriendlyByteBuf.readEnum(entries: EnumEntries<E>): E {
return entries[readVarInt()]
}
fun <T : Any> FriendlyByteBuf.readType(registry: IdMap<T>): T {
val id = readInt()
return registry.byId(id) ?: throw NoSuchElementException("No such entry with ID $id")
}
fun FriendlyByteBuf.readBlockType(): Block {
return readType(BuiltInRegistries.BLOCK)
}
fun FriendlyByteBuf.readBlockState(): BlockState {
return Block.BLOCK_STATE_REGISTRY.byIdOrThrow(readVarInt())
}
fun FriendlyByteBuf.readItemType(): Item {
return readType(BuiltInRegistries.ITEM)
}
fun RegistryFriendlyByteBuf.readItem(): ItemStack {
return ItemStack.OPTIONAL_STREAM_CODEC.decode(this)
}
fun RegistryFriendlyByteBuf.readComponent(): Component {
return ComponentSerialization.STREAM_CODEC.decode(this)
}
fun InputStream.readItemType(): Item {
return BuiltInRegistries.ITEM.byId(readVarIntLE())
}
operator fun <T : Comparable<T>> StateHolder<*, *>.get(property: Property<T>): T {
return getValue(property)
}