Question In memory PersistentDataContainer?

  • After careful consideration and due to limited usage, we’ve made the decision to discontinue the PaperMC forums. Moving forward, we recommend using Hangar for plugin uploads, and for all other community discussions and support, please join us on Discord.

Krysztal112233

New member
Mar 22, 2024
1
0
1
Hello everyone, I suddenly had an idea while working on PDC:

I noticed that PersistentDataContainer seems to be serializable into []byte

Java:
public interface PersistentDataContainerView {

   ......

    /**
     * Serialize this {@link PersistentDataContainer} instance to a
     * byte array.
     *
     * @return a binary representation of this container
     * @throws java.io.IOException if we fail to write this container to a byte array
     */
    byte[] serializeToBytes() throws java.io.IOException;
}

and can be deserialized in the PersistentDataContainer

Java:
public interface PersistentDataContainer extends io.papermc.paper.persistence.PersistentDataContainerView { // Paper - split up view and mutable

    ......
    
    /**
     * Read values from a serialised byte array into this
     * {@link PersistentDataContainer} instance.
     * This method has the same effect as
     * <code>PersistentDataContainer#readFromBytes(bytes, true)</code>
     *
     * @param bytes the byte array to read from
     * @throws java.io.IOException if the byte array has an invalid format
     */
    default void readFromBytes(final byte @NotNull [] bytes) throws java.io.IOException {
        this.readFromBytes(bytes, true);
    }
    // Paper end - byte array serialization
}

Is it possible to create a PDC in memory to store a deep copy of the PDC obtained from the game, such as an ItemStack, and process it outside the main thread?