Api for deserializing items with partially invalid data #13423
+225
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a safe deserialization method for
ItemStackto 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.DataResultfrom Mojang's DataFixerUpper.This is what the API looks like in a simplified fashion: