Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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
    }
    
  ...
}

 

 

  • Author
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.

  • Author

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

  • Author

Okay thanks. I already did that. I just was not sure if there is an easier solution :)

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.