Skip to content
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

refactor(StartingInventory): Overhaul system; Improve documentation #13

Merged
merged 6 commits into from
May 25, 2020

Conversation

skaldarnar
Copy link
Contributor

@skaldarnar skaldarnar commented May 24, 2020

This PRs does an overhaul of the StartingInventorySystem to make it more concise. We are logging a few more warnings in case something is off, and also have more documentation now.

This makes somewhat extensive use of functional features and Java streams, so any feedback on comprehensibility is welcome =)

Furthermore, methods resolveAs{Block,Item} might be better of in InventoryUtils to be available for others to use. We're differentiating between items and blocks in way to many places ...

Simplify method signatures and and control flow.
- use an optional supplier to resolve an inventory item either as block or as item.
- clean-up the dependency between `addToInventory` and `addNestedInventory`
- add documentation for `resolveAsBlock` and `resolveAsItem`
@skaldarnar skaldarnar changed the title refactor(StartingInventory): Overhaul system refactor(StartingInventory): Overhaul system; Improve documentation May 24, 2020
Comment on lines 94 to 95
Optional<Supplier<EntityRef>> possibleItem =
resolveAsBlock(item).map(Optional::of).orElseGet(() -> resolveAsItem(item));
Copy link
Contributor Author

@skaldarnar skaldarnar May 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this understandable? Should we change this? Here are some alternatives:

Suggested change
Optional<Supplier<EntityRef>> possibleItem =
resolveAsBlock(item).map(Optional::of).orElseGet(() -> resolveAsItem(item));
Optional<Supplier<EntityRef>> possibleItem = resolveAsBlock(item);
if (!possibleItem.isPresent()) {
possibleItem = resolveAsItem(item);
}
if (!possibleItem.isPresent()) {
// use the item as it is present
}
Suggested change
Optional<Supplier<EntityRef>> possibleItem =
resolveAsBlock(item).map(Optional::of).orElseGet(() -> resolveAsItem(item));
final Supplier<EntityRef> possibleItem =
resolveAsBlock(item).orElse(resolveAsItem(item).orElse(null));
if (possibleItem != null) {
// use the item as it is present
}

Copy link
Contributor Author

@skaldarnar skaldarnar May 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Optional::or (Java 9) mentioned in the comments this part would become:

final Optional<Supplier<EntityRef>> possibleItem = 
    resolveAsBlock(item.uri).or(() -> resolveAsItem(item.uri))

@skaldarnar skaldarnar marked this pull request as ready for review May 24, 2020 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants