-
-
Notifications
You must be signed in to change notification settings - Fork 824
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 getBrush helper for use with instanceof pattern matching #1926
Add getBrush helper for use with instanceof pattern matching #1926
Conversation
Added methods to check if a item has a tool or brush tool bound. Added method to get a optional tool depending on the class it is assignable to. This avoids existence and instance checks and gives clearer communitcation Added method to retrieve a brush assigned to a brush tool if it matches a specific class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - this isn't a proper review as it's nearly midnight and I'm doing this from my phone, but my two immediate concerns are:
- This increases the number of calls to getTool in a few methods, would have to check performance implications
- We do have plans to overhaul tools in the future, and we'd have to ensure that these methods don't interfere with that - as adding methods just to deprecate them is non-ideal
You could reduce them to one when you would use optionals. This way you would only require one lookup. I already thought about that, but that would actually add even more methods and I didn't wanted to bloat the class even more. /**
* Get the tool assigned to the item if a tool is assigned.
*
* @param item the item type
* @return A optional holding the tool if a tool is bound.
*/
public Optional<Tool> getToolIfBound(ItemType item) {
return Optional.ofNullable(tools.get(item));
} This method could reduce the calls to 1. Or we stick to the nullable method and doing null checks. Both would work nearly the same way. I just like Optionals since they have a clearer communication to api users |
honestly not a fan of jamming optionals in to parts of the api that don't use them at all. |
Actually thought someone has to start it. But if you prefere Nullable annotations over Optionals I can stick with it. At the end it's just a suggestion from my side. |
Replace optionals with Nullable annotated methods Reduce hashmap lookups to 1
Because
This dramatically lowers the API surface, while still improving the API to be usable. P.S. If you don't understand what |
Sounds like a good change tho.
|
Yes,
I'm not sure yet, I'll consider after this PR what to do with everything using the deprecated method. Don't worry about cleaning that up for now, just apply the deprecation. |
I applied the changes. Regarding the failing workflows: I don't know in which way these are related to my changes. I don't see any changes which could cause these. |
oof, outdated checkstyle. if you can figure that out, push it with this, otherwise i'll just fix that later. |
Codecov Report
@@ Coverage Diff @@
## master #1926 +/- ##
============================================
- Coverage 11.13% 11.13% -0.01%
Complexity 1045 1045
============================================
Files 836 836
Lines 35824 35825 +1
Branches 4044 4044
============================================
Hits 3989 3989
- Misses 31651 31652 +1
Partials 184 184
Continue to review full report at Codecov.
|
Motivation
While working on a custom brush with the world api I developed several utility methods to make it easier working with world edit tools and especially brush tools.
I also noticed that some methods which would be nice to avoid boilerplate code are missing. This PR adds these methods which hopefully will help people working with brushes and tools.
Changes
Method to check tool bounding
boolean isTool(ItemType)
boolean isBrushTool(ItemType)
I added two methods which allows to check if a tool or a brush tool is bound on an item.
Previously the only check for a brush tool was to retrieve the tool and make an instance check. Retrieving the brush tool always creates a brush tool and makes it not possible to check for existence directly. This use case should be covered by the api actually.
Method to get tools based on their assignable class
<T extends Tool> Optional<T> getTool(ItemType, Class<T>)
One step further would be to also remove a second instance check which would be often on the user side.
A user might want to get its registered tool and needs to check if the returned tool matches the class of its tool.
The tool is retrieved by this method and checked for the required tool class.
The result already contains the tool cast to the requested class.
The result is wrapped into an optional to make the user aware of possible missing return values.
Method to get a brush based on their assignable class
<T extends Brush> Optional<T> getBrush(ItemType, Class<T>)
This method is a extension of the previous
getTool
method.It checks for a
BrushTool
, extracts the containing brush and checks if the brush is assignable to the provided class.I am a bit unsure where the logic of this class should be. It is more a delegate a to the brush class and part of the actual logic could also be included in the BrushTool class itself. However I think that dividing it wouldn't be a benefit so I left it in the LocalSession for now.
Gradle
In order to build and test my changes I needed to update to 7.2. I can remove this change, but it may be required to pass the actions.