Class ByteBufHelper<T>

java.lang.Object
net.darkhax.bookshelf.api.data.bytebuf.ByteBufHelper<T>
Type Parameters:
T - The type being serialized.
Direct Known Subclasses:
RegistryByteBufHelper

public class ByteBufHelper<T> extends Object
Logic for serializing data using a byte buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteBufHelper(Function<net.minecraft.network.FriendlyByteBuf,T> reader, BiConsumer<net.minecraft.network.FriendlyByteBuf,T> writer, T... vargs)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    read(net.minecraft.network.FriendlyByteBuf buffer)
    Reads the data from the buffer directly.
    T[]
    readArray(net.minecraft.network.FriendlyByteBuf buffer)
    Reads an array of values from the buffer.
    <C extends Collection<T>>
    C
    readCollection(net.minecraft.network.FriendlyByteBuf buffer, Supplier<C> collectionSupplier)
    Reads a collection of values from the buffer.
    readList(net.minecraft.network.FriendlyByteBuf buffer)
    Reads a list of values from the buffer.
    readNullable(net.minecraft.network.FriendlyByteBuf buffer)
    Read a nullable value from the buffer.
    readOptional(net.minecraft.network.FriendlyByteBuf buffer)
    Reads an optional value from the buffer.
    readSet(net.minecraft.network.FriendlyByteBuf buffer)
    Reads a set of values from the buffer.
    net.minecraft.util.random.WeightedEntry.Wrapper<T>
    readWeighted(net.minecraft.network.FriendlyByteBuf buffer)
    Read a weighted value from the buffer.
    net.minecraft.util.random.SimpleWeightedRandomList<T>
    readWeightedList(net.minecraft.network.FriendlyByteBuf buffer)
    Read a weighted random list from the buffer.
    void
    write(net.minecraft.network.FriendlyByteBuf buffer, T toWrite)
    Writes the data to the buffer directly.
    void
    writeArray(net.minecraft.network.FriendlyByteBuf buffer, T[] array)
    Writes an array of values to the buffer.
    void
    writeCollection(net.minecraft.network.FriendlyByteBuf buffer, Collection<T> collection)
    Writes a collection of values to the buffer.
    void
    writeList(net.minecraft.network.FriendlyByteBuf buffer, List<T> toWrite)
    Writes a list of values to the buffer.
    void
    writeNullable(net.minecraft.network.FriendlyByteBuf buffer, T value)
    Writes a nullable value to the buffer.
    void
    writeOptional(net.minecraft.network.FriendlyByteBuf buffer, Optional<T> optional)
    Writes an optional value to the buffer.
    void
    writeSet(net.minecraft.network.FriendlyByteBuf buffer, Set<T> toWrite)
    Writes a set of values to the buffer.
    void
    writeWeighted(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.WeightedEntry.Wrapper<T> toWrite)
    Write a weighted value to the buffer.
    void
    writeWeightedList(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.SimpleWeightedRandomList<T> list)
    Write a weighted random list to the buffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteBufHelper

      public ByteBufHelper(Function<net.minecraft.network.FriendlyByteBuf,T> reader, BiConsumer<net.minecraft.network.FriendlyByteBuf,T> writer, T... vargs)
  • Method Details

    • read

      public T read(net.minecraft.network.FriendlyByteBuf buffer)
      Reads the data from the buffer directly.
      Parameters:
      buffer - The buffer to read from.
      Returns:
      The value that was read.
    • write

      public void write(net.minecraft.network.FriendlyByteBuf buffer, T toWrite)
      Writes the data to the buffer directly.
      Parameters:
      buffer - The buffer to write data to.
      toWrite - The value to write to the buffer.
    • readArray

      public T[] readArray(net.minecraft.network.FriendlyByteBuf buffer)
      Reads an array of values from the buffer.
      Parameters:
      buffer - The buffer to read from.
      Returns:
      An array of values read from the buffer.
    • writeArray

      public void writeArray(net.minecraft.network.FriendlyByteBuf buffer, T[] array)
      Writes an array of values to the buffer.
      Parameters:
      buffer - The buffer to read from.
      array - The array of values to write.
    • readCollection

      public <C extends Collection<T>> C readCollection(net.minecraft.network.FriendlyByteBuf buffer, Supplier<C> collectionSupplier)
      Reads a collection of values from the buffer.
      Type Parameters:
      C - The type of collection to produce.
      Parameters:
      buffer - The buffer to read from.
      collectionSupplier - A supplier that provides the collection to populate with values. For example ArrayList::new.
      Returns:
      A collection of values read from the buffer.
    • writeCollection

      public void writeCollection(net.minecraft.network.FriendlyByteBuf buffer, Collection<T> collection)
      Writes a collection of values to the buffer.
      Parameters:
      buffer - The buffer to write data to.
      collection - The collection of values to write.
    • readList

      public List<T> readList(net.minecraft.network.FriendlyByteBuf buffer)
      Reads a list of values from the buffer.
      Parameters:
      buffer - The buffer to read from.
      Returns:
      A list of values read from the buffer.
    • writeList

      public void writeList(net.minecraft.network.FriendlyByteBuf buffer, List<T> toWrite)
      Writes a list of values to the buffer.
      Parameters:
      buffer - The buffer to write data to.
      toWrite - The list of values to write.
    • readSet

      public Set<T> readSet(net.minecraft.network.FriendlyByteBuf buffer)
      Reads a set of values from the buffer.
      Parameters:
      buffer - The buffer to read data from.
      Returns:
      A set of values read from the buffer.
    • writeSet

      public void writeSet(net.minecraft.network.FriendlyByteBuf buffer, Set<T> toWrite)
      Writes a set of values to the buffer.
      Parameters:
      buffer - The buffer to write data to.
      toWrite - The set of values to write.
    • readOptional

      public Optional<T> readOptional(net.minecraft.network.FriendlyByteBuf buffer)
      Reads an optional value from the buffer.
      Parameters:
      buffer - The buffer to read data from.
      Returns:
      The optional value that was read from the buffer.
    • writeOptional

      public void writeOptional(net.minecraft.network.FriendlyByteBuf buffer, Optional<T> optional)
      Writes an optional value to the buffer.
      Parameters:
      buffer - The buffer to write data to.
      optional - The optional value to write.
    • readNullable

      @Nullable public T readNullable(net.minecraft.network.FriendlyByteBuf buffer)
      Read a nullable value from the buffer.
      Parameters:
      buffer - The buffer to read data from.
      Returns:
      The nullable value read from the buffer.
    • writeNullable

      public void writeNullable(net.minecraft.network.FriendlyByteBuf buffer, @Nullable T value)
      Writes a nullable value to the buffer.
      Parameters:
      buffer - The buffer to write data to.
      value - The nullable value to write.
    • readWeighted

      public net.minecraft.util.random.WeightedEntry.Wrapper<T> readWeighted(net.minecraft.network.FriendlyByteBuf buffer)
      Read a weighted value from the buffer.
      Parameters:
      buffer - The buffer to read data from.
      Returns:
      The weighted value read from the buffer.
    • writeWeighted

      public void writeWeighted(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.WeightedEntry.Wrapper<T> toWrite)
      Write a weighted value to the buffer.
      Parameters:
      buffer - The buffer to write data to.
      toWrite - The weighted value to write to the buffer.
    • readWeightedList

      public net.minecraft.util.random.SimpleWeightedRandomList<T> readWeightedList(net.minecraft.network.FriendlyByteBuf buffer)
      Read a weighted random list from the buffer.
      Parameters:
      buffer - The buffer to read data from.
      Returns:
      The weighted random list that was read from the buffer.
    • writeWeightedList

      public void writeWeightedList(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.SimpleWeightedRandomList<T> list)
      Write a weighted random list to the buffer.
      Parameters:
      buffer - The buffer to write data to.
      list - The weighted random list to write to the buffer.