Skip to content

Commit

Permalink
fix: retrieve character entity within binding
Browse files Browse the repository at this point in the history
In #45 the character entity was cached outside of the binding
evaluation. However, it in multiplayer games it can happen that it is
`null`, and therefore opening container screens on a connected client
fails.

This is fixed by retrieving the character entity with each binding
evaluation via a common helper method `getPredictedInteractionTarget`.
  • Loading branch information
skaldarnar committed Aug 8, 2021
1 parent 5e8a0e5 commit 7857c45
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.terasology.nui.widgets.UILabel;

/**
* A UI screen to show a container inventory next to the player's inventory.
*/
public class ContainerScreen extends CoreScreenLayer {

Expand All @@ -23,8 +24,6 @@ public class ContainerScreen extends CoreScreenLayer {
@In
private TranslationSystem i18n;

private InventoryGrid containerInventory;

@Override
public void initialise() {
InventoryGrid inventory = find("inventory", InventoryGrid.class);
Expand All @@ -37,19 +36,18 @@ public EntityRef get() {
inventory.setCellOffset(10);

UILabel containerTitle = find("containerTitle", UILabel.class);
containerInventory = find("container", InventoryGrid.class);
InventoryGrid containerInventory = find("container", InventoryGrid.class);

EntityRef characterEntity = localPlayer.getCharacterEntity();
containerInventory.bindTargetEntity(new ReadOnlyBinding<EntityRef>() {
@Override
public EntityRef get() {
return characterEntity.getComponent(CharacterComponent.class).predictedInteractionTarget;
return getPredictedInteractionTarget();
}
});
containerTitle.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
Prefab parentPrefab = characterEntity.getComponent(CharacterComponent.class).predictedInteractionTarget.getParentPrefab();
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.
Expand All @@ -63,6 +61,14 @@ public String get() {
});
}

/**
* Retrieve the predicted interaction target entity for the local player.
*/
private EntityRef getPredictedInteractionTarget() {
EntityRef characterEntity = localPlayer.getCharacterEntity();
return characterEntity.getComponent(CharacterComponent.class).predictedInteractionTarget;
}

@Override
public boolean isModal() {
return false;
Expand Down

0 comments on commit 7857c45

Please sign in to comment.