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

fix: avoid NPE in binding for container title #47

Merged
merged 1 commit into from
Aug 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.terasology.nui.databinding.ReadOnlyBinding;
import org.terasology.nui.widgets.UILabel;

import java.util.Optional;

/**
* A UI screen to show a container inventory next to the player's inventory.
*/
Expand Down Expand Up @@ -47,16 +49,19 @@ public EntityRef get() {
containerTitle.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
Prefab parentPrefab = getPredictedInteractionTarget().getParentPrefab();
DisplayNameComponent displayName = parentPrefab.getComponent(DisplayNameComponent.class);
if (displayName != null) {
// The display name may contain a translatable string reference, thus we attempt to get the translation.
// If the string is just non-translatable display name the fallback mechanism will yield just the input string.
// NOTE: Unfortunately, this contract is not guaranteed by `TranslationSystem#translate(String)`.
return i18n.translate(displayName.name);
} else {
return parentPrefab.getName();
}
EntityRef interactionTarget = getPredictedInteractionTarget();

String name = Optional.ofNullable(interactionTarget.getComponent(DisplayNameComponent.class))
.map(displayName -> displayName.name)
.orElseGet(() ->
Optional.ofNullable(interactionTarget.getParentPrefab())
.map(Prefab::getName)
.orElse(""));

// The display name may contain a translatable string reference, thus we attempt to get the translation.
// If the string is just non-translatable display name the fallback mechanism will yield just the input string.
// NOTE: Unfortunately, this contract is not guaranteed by `TranslationSystem#translate(String)`.
return i18n.translate(name);
}
});
}
Expand Down