Skip to content

Commit

Permalink
fix: avoid NPE in binding for container title (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
skaldarnar committed Aug 14, 2021
1 parent 37e15ae commit 72e9184
Showing 1 changed file with 15 additions and 10 deletions.
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

0 comments on commit 72e9184

Please sign in to comment.