-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add methods for checking whether multiple stacks can fit into or be extracted from an inventory #15769
base: master
Are you sure you want to change the base?
Add methods for checking whether multiple stacks can fit into or be extracted from an inventory #15769
Conversation
I agree that this would be a rather dirty and wasteful hack. But I think it hints at what at a proper solution should look like. Essentially, I think we want a proper "inventory list" data structure. This data structure should be copyable. Then you would simply get an inventory list, copy it, try to add / take the items, and if it worked, swap the original for the mutated copy.
Not much of a problem. Also: If you already depend on a mod for other library functions, this won't necessarily introduce an additional dependency. This argument is a very slippery slope: By this logic, Luanti would essentially have to become the union of all library mods on ContentDB, and the concept of a library mod should cease to exist. Note also that you could "bundle" libraries with your mod rather than having them be separate mods to avoid additional dependencies.
Implement it once, write some good unit tests, put it in a library mod. It's in many ways worse if we do the equivalent via Luanti's C++ codebase:
ConclusionI would strongly recommend doing this in a library mod first instead. Once/if that library mod sees widespread adoption, moving it to the engine can still be reconsidered. (In general, this is a path I'd like to become more standard for "convenience" features that can be implemented without issue in a library mod.) I think this PR is, as it stands, low priority and should potentially be rejected, I'm afraid. |
@appgurueu thank you for your opinion!
As I see, all the
Currently, my mod already have a set of functions, which do the same, but, I think, it'd be great to have this functionality for future generation of modders. Especially taking into account that there were problems with this on the server I'm playing on, when items were disappearing because there wasn't enough place for all of them in the inventory, because the mechanism of checking this in that mod was buggy (maybe there were more than one mod with this problem but don't remember).
I could do this, but other mod developers who need this functionality will need to do the same, i.e. implement and test it. They could, in theory, re-use my library, or I could also reuse someone else, but if my mod will depend on a third-party library, its creator should be trustworthy enough, otherwise he/she might just break my mod by a buggy patch, or if I just copy the code into my mod (if its license allows it), in case of possible fix in the upstream I might not know about it, and the bug will remain in my code. My arguments might be exaggerated, but taking into account that the feature might be demanded by different mods, and it might be difficult to implement correctly, I think, it'd be great to have it "out of the box". |
This feature seems quite useful to me, but I agree with appgurueu. It's only a convenience feature, so it is fine to make it low priority. |
I think, adding these functions won't make much sense, since we can easily do the same by subsequent calls to
That's what I already did. Moreover, my implementation also calculates the number of available extractions. The current patch is intended for future "generations" of mod developers, so that they won't need to implement the algorithm manually with all the risks, and just use the functions out of the box. |
It's an interesting idea. However, we already have a set of functions for working with inventory, which don't use such object, and can perform most of basic operations. Adding such object will require adding a set of dedicated functions which will mostly duplicate already existing functions, which don't use such data structures. |
This PR adds two methods for InvRef:
room_for_items(listname, stacks_array)
contains_items(listname, stacks_array, [match_meta])
These methods are similar to
room_for_item(listname, stack)
andcontains_item(listname, stack, [match_meta])
except that they perform the same check for several stacks instead of only one.Using the new functions is not the same as calling the existing (single-stack) functions for each stack separately, because in the latter case it'll check whether each separate stack can be added to the inventory without the other stacks, while the new functions check whether all the items from the list can be added to or removed from the inventory together.
This feature is useful when a mod needs to check whether a set of stacks can be fully extracted from or fully placed to an inventory.
Without this patch, there is no a simple way to do the same. Possible solutions:
To do
This PR is a Work in Progress
How to test
Before moving items between inventories, the two methods introduced in this patch will be called. If one of them returns false, the corresponding message will be printed into log. If the both functions returned true, but moving items between inventories actually failed because of lack of items or space, it'll print a log message with "AAAAAAAAAAAAAAAAAAA", which might indicate a bug in the newly implemented functions. Since the current wersion of
remove_item()
doesn't take metadata into account, the test uses a separate function which should do this.