Difference between revisions of "Using FriendlyByteBuf"

From Forge Community Wiki
(Created page with "<code>PacketBuffer</code>s are essentially a byte array of zero or more bytes to sync information across a network. It works similarly to a queue: the information is written i...")
 
(Update to 1.17)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<code>PacketBuffer</code>s are essentially a byte array of zero or more bytes to sync information across a network. It works similarly to a queue: the information is written in to a specific order and read in the order it was written.
+
<code>FriendlyByteBuf</code>s are essentially a byte array of zero or more bytes to sync information across a network. It works similarly to a queue: the information is written in to a specific order and read in the order it was written.
  
== Using <code>PacketBuffer</code>s ==
+
== Using <code>FriendlyByteBuf</code>s ==
  
In most cases, one will use a premade <code>PacketBuffer</code> passed in from some network. Most of the time this is a heap buffer of some sort; however, understanding how it works is best left as an explanation of data structures.
+
In most cases, one will use a premade <code>FriendlyByteBuf</code> passed in from some network. Most of the time this is a heap buffer of some sort; however, understanding how it works is best left as an explanation of data structures.
  
When you are <code>writing</code> to a <code>PacketBuffer</code>, you are calling one of the many write functions to store information as bytes (e.g. <code>writeBlockPos</code> or <code>writeVarInt</code>). There are a couple of helpers for objects like <code>CompoundNBT</code>, <code>ItemStack</code>s, etc. if they are needed.
+
When you are '''writing''' to a <code>FriendlyByteBuf</code>, you are calling one of the many write functions to store information as bytes (e.g. <code>writeBlockPos</code> or <code>writeVarInt</code>). There are a couple of helpers for objects like <code>CompoundTag</code>, <code>ItemStack</code>s, etc. if they are needed.
  
When you are <code>reading</code> from a <code>PacketBuffer</code>, you are calling the equivalent read function in the same order. For example, if you call <code>writeBlockPos</code> and then <code>writeVarInt</code>, you would call <code>readBlockPos</code> and then <code>readVarInt</code> in that order. Each of these method returns the value from the buffer.
+
When you are '''reading''' from a <code>FriendlyByteBuf</code>, you are calling the equivalent read function in the same order. For example, if you call <code>writeBlockPos</code> and then <code>writeVarInt</code>, you would call <code>readBlockPos</code> and then <code>readVarInt</code> in that order. Each of these method returns the value from the buffer.

Latest revision as of 18:28, 2 August 2021

FriendlyByteBufs are essentially a byte array of zero or more bytes to sync information across a network. It works similarly to a queue: the information is written in to a specific order and read in the order it was written.

Using FriendlyByteBufs

In most cases, one will use a premade FriendlyByteBuf passed in from some network. Most of the time this is a heap buffer of some sort; however, understanding how it works is best left as an explanation of data structures.

When you are writing to a FriendlyByteBuf, you are calling one of the many write functions to store information as bytes (e.g. writeBlockPos or writeVarInt). There are a couple of helpers for objects like CompoundTag, ItemStacks, etc. if they are needed.

When you are reading from a FriendlyByteBuf, you are calling the equivalent read function in the same order. For example, if you call writeBlockPos and then writeVarInt, you would call readBlockPos and then readVarInt in that order. Each of these method returns the value from the buffer.