Interface SerializableConsumer<T>

Type Parameters:
T - The type of the input to the operation
All Superinterfaces:
Consumer<T>, Serializable
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface SerializableConsumer<T> extends Consumer<T>, Serializable
Represents a Consumer<T> that is Serializable. This means that any actions performed in the consumer must only be operations that can be serialized.

For example, the following usage is not allowed:

 String s = "Hello, World!";

 SerializableConsumer<String> consumer = s -> {
    System.out.println(s);
    System.out.println("Hello, World!");
    System.out.println("Goodbye, World!");
 }
 
This is because the System.out.println method is not serializable. You cannot use or construct non-serializable objects inside of the consumer, and must instead carry out all non-serializable actions outside the consumer. This means that any spigot-side stuff like calling events or sending packets cannot be done inside the consumer.

You also can't call any methods or use fields outside of the consumer, since they will not be updated when the consumer is deserialized. For example:

 private String field = "Hello, World!";

 public void someMethod() {
     SerializableConsumer<String> consumer = s -> {
         field = s;
     };
 }
 
In this case, the field will not be updated when the consumer is deserialized, because the code will be executed on the user client.

Instead, you should use a SerializableConsumer like this:

 String str = "";

 SerializableConsumer<String> consumer = s -> {
     s = s + "Hello, World!";
     s = s + " ";
     s = s + "Goodbye, World!";
 };

 consumer.accept(str);
 System.out.println(str); // Prints "Hello, World! Goodbye, World!"
 
If you need to perform an operation that is not serializable using a SerializableConsumer (e.g. with Screens), try utilizing other methods such as the Event or Instruction API.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> SerializableConsumer<T>
    fromByteArray(byte[] bytes)
    Deserializes a consumer from a byte array.
    default byte[]
    Serializes this consumer into a byte array.

    Methods inherited from interface java.util.function.Consumer

    accept, andThen
  • Method Details

    • toByteArray

      default byte[] toByteArray()
      Serializes this consumer into a byte array.
      Returns:
      Byte Array
    • fromByteArray

      static <T> SerializableConsumer<T> fromByteArray(byte[] bytes)
      Deserializes a consumer from a byte array.
      Type Parameters:
      T - The type of the input to the operation
      Parameters:
      bytes - Byte Array
      Returns:
      Deserialized Consumer