Jump to content

[1.16.5] Sync data from tile entity to screen


J3ramy

Recommended Posts

Hello guys,

I am trying to sync an integer value from a tile entity with my screen class. It works in general, but not the first time when I open the screen after loading the world.

If I open it a second time it works.

I am using the screen container class and IIntArray for the data. Maybe you can see what's wrong :)

 

Tile Entity

Spoiler
public class BrakeOperatingPanelTile extends TileEntity {

    protected final IIntArray data = new IIntArray() {
        public int get(int index) {
            switch(index) {
                case 0:
                    return BrakeOperatingPanelTile.this.targetVelocity;
                default: return 0;
            }
        }

        public void set(int index, int value) {
            switch(index) {
                case 0: BrakeOperatingPanelTile.this.targetVelocity = value;
            }
        }

        public int size() {
            return 1;
        }
    };

    public IIntArray getData(){
        return this.data;
    }

    private int targetVelocity = 1;

    public BrakeOperatingPanelTile(TileEntityType<?> tileEntityType) {
        super(tileEntityType);
    }

    public BrakeOperatingPanelTile(){
        this(ModTileEntities.BRAKE_OPERATING_PANEL_TILE.get());
    }

    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        super.read(state, nbt);

        if(nbt.contains("targetSpeed")){
            this.targetVelocity = nbt.getInt("targetSpeed");
        }
    }

    @Override
    public CompoundNBT write(CompoundNBT nbt) {

        nbt.putInt("targetSpeed", this.targetVelocity);

        return super.write(nbt);
    }
}

 

Container

Spoiler
public class BrakeOperatingPanelContainer extends Container {

    private final BrakeOperatingPanelTile tileEntity;

    public TileEntity getTileEntity() {
        return this.tileEntity;
    }

    private final IIntArray data;

    public IIntArray getData() {
        return this.data;
    }

    public BrakeOperatingPanelContainer(int windowId, IIntArray data, BrakeOperatingPanelTile tile){
        super(ModContainers.BRAKE_OPERATING_PANEL_CONTAINER.get(), windowId);
        this.tileEntity = tile;

        assertIntArraySize(data, 1);
        this.data = data;
        this.trackIntArray(data);
    }
}

 

Screen

Spoiler
public class BrakeOperatingPanelScreen extends ContainerScreen<BrakeOperatingPanelContainer> {

    private final ResourceLocation GUI = new ResourceLocation(CoasterideMod.MOD_ID, "textures/gui/brake_operating_panel_gui.png");
    
    private BrakeOperatingPanelContainer container;
    private BrakeOperatingPanelTile tileEntity;
    
    public BrakeOperatingPanelScreen(BrakeOperatingPanelContainer screenContainer, PlayerInventory inv, ITextComponent title) {
        super(screenContainer, inv, title);
        this.container = screenContainer;
        this.tileEntity = (BrakeOperatingPanelTile) screenContainer.getTileEntity();
    }

    @Override
    public void init(Minecraft minecraft, int width, int height) {
        super.init(minecraft, width, height);

        this.targetVelocity = container.getData().get(0);
        System.out.println(this.targetVelocity); //Is 1 (initial value in tile entity) instead of another value
    }
    
  ...
}

 

 

Link to comment
Share on other sites

Quote

The tracked data values will only update after opening the screen, they will not be correct immediately in init()

Okay and is there a solution?

 

I removed the field in the container class

Spoiler
public class BrakeOperatingPanelContainer extends Container {

    private final BrakeOperatingPanelTile tileEntity;

    public BrakeOperatingPanelTile getTileEntity() {
        return this.tileEntity;
    }

    public BrakeOperatingPanelContainer(int windowId, IIntArray data, BrakeOperatingPanelTile tile){
        super(ModContainers.BRAKE_OPERATING_PANEL_CONTAINER.get(), windowId);
        this.tileEntity = tile;

        assertIntArraySize(data, 1);
        this.trackIntArray(data);
    }
}

 

and accessed the data directly via the tile entity in the screen class

Spoiler
@Override
    public void init(Minecraft minecraft, int width, int height) {
        super.init(minecraft, width, height);

        this.targetVelocity = container.getTileEntity().getData().get(0);
        System.out.println(this.targetVelocity); //still 1
    }

 

But it is still 1.

Link to comment
Share on other sites

I'm sorry.

I thought you mean the field in the container class...

 

But it works now. Thank you very much!

 

Btw. Is it possible to get other data like booleans instead of only using ints with this method?

Edited by J3ramy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.