Make it more convenient to implement item/player/entity abilities that modify node drops #15872
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.
Context
core.node_dig(pos, node, digger)
usescore.get_node_drops(node, toolname)
to determine the drops that should result from digging a node. The builtin implementation ofget_node_drops
uses a well documented not so simple algorithm to compute the drops from data recorded in the node and tool definition while builtinnode_dig
handles varied tasks like removing the node, applying wear to the wielded tool, and calling callbacks. For most games/mods it's not desirable to completely replace the builtin implementations, but rather hook into them to e.g. implement player abilities or tool enchantments that may modify yield. But since the builtin implementation ofget_node_drops
only needs the name of the tool and the node data, nothing more is available and while the other function related to drops,core.handle_node_drops(pos, drops, digger)
, has access to thedigger
and therefore the wielded tool, that one is only called after wear is applied and the tool potentially destroyed. Workarounds are possible (e.g. overridingnode_dig
to add some fields to thenode
parameter that is currently passed unmodified toget_node_drops
and then calling the originalnode_dig
function), but a satisfying solution within the existing API at least isn't obvious.Proposed solution
The simple solution proposed in this PR is making
node_dig
also pass thetool
itemstack and thedigger
object reference toget_node_drops
. These aren't used by the builtin implementation, therefore there is no functional change, but provide overrides with the access to relevant meta data. An argument could be made to also passpos
to get access to node metadata, and I'm open to adding that, too, but my feeling is thatpreserve_metadata
in the node def is already a viable option for use cases related to node metadata.Related issues
This PR is perhaps a tiny little bit related to (closed) issue #13740 in so far as it opens more ways to modify node drops.
Status
This PR is Ready for Review.
How to test
Since this is mostly a documentation change, there should be no observable change at all as long as mods are not using the additional parameters provided to
core.get_node_drops
.