Skip to content

Conversation

@RodneyMKay
Copy link
Contributor

@RodneyMKay RodneyMKay commented Dec 22, 2025

Summary

This PR introduces a safe deserialization method for ItemStack to handle items containing unknown data without throwing exceptions, ensuring parity with vanilla behavior for inventory handling.

Background

In vanilla Minecraft, when a player joins with an item containing enchantments from a missing namespace (e.g., customenchant:autosmelt), the server logs a warning and allows the player to keep the item with the invalid enchantment stripped. Currently, ItemStack.deserializeBytes throws a hard error in these cases, providing no way to access this partial result.

Of course, ideally, plugin developers would implement their own data versioning system, where data from things like custom enchantments is migrated to a valid format automatically, but that is more complex to support. Allowing access to the vanilla-type behavior should be good enough for now.

This PR

A new method, deserializeBytesSafely, has been added alongside the standard deserializeBytes. This method returns a DataResult instead of throwing an exception, allowing the API to provide a partial result even when specific components fail to deserialize.

DataResult API

The DataResult API is implemented as a sealed class hierarchy, which is easy for pattern matching and restricts invalid usages. However, I also included several convenience methods on the top-level interface to simplify common operations. Other than that, it mimics the semantics of the com.mojang.serialization.DataResult from Mojang's DataFixerUpper.

This is what the API looks like in a simplified fashion:

public sealed interface DataResult<R> {
    record Success<R>(
        R result
    ) implements DataResult<R> {}

    record ErrorWithPartialResult<R>(
        String errorMessage,
        R partialResult
    ) implements DataResult<R> {}

    record Error<R>(
        String errorMessage
    ) implements DataResult<R> {}
    
    Optional<R> getResult();

    Optional<R> getPartialResult();

    Optional<String> getErrorMessage();

    R resultOrThrow();

    R partialResultOrThrow();
}

@github-project-automation github-project-automation bot moved this to Awaiting review in Paper PR Queue Dec 22, 2025
@RodneyMKay RodneyMKay changed the title Api for accessing partial item results Api for deserializing items with partially invalid data Dec 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Awaiting review

Development

Successfully merging this pull request may close these issues.

1 participant