Jump to content

Applying values from a screen to a BlockEntity


TrickShotMLG

Recommended Posts

I am fairly new to mod programming and I am currently trying to create a GUI with a TextField where I can enter a text and store this input in the EntityBlock class of my specific block within a variable.

I can open the GUI and the input field is working, but the input data is not transferred to the EntityBlock persistently.

 

This is the init code with the logic for storing the entered text on the button click, which is in my screen class.

@Override
    protected void init() {
        super.init();

        // Create widgets
        portalNameTextField = new EditBox(
                this.font,
                this.leftPos + 7,
                this.topPos + 11,
                150,
                20,
                Component.literal("")
        );

        setPortalNameButton = new Button.Builder(
                Component.literal("OK"),
                (button) -> {
                    // copy text field value to portal name variable
                    this.menu.blockEntity.portalName = portalNameTextField.getValue();

                    // set data stored as dirty
                    this.menu.blockEntity.setChanged();
                }
            )
                .pos(20, 20)
                .size(50, 20)
                .build();

        // Focus the screen
        this.setFocused(true);

        // Add Widgets
        this.addRenderableWidget(portalNameTextField);
        this.addRenderableWidget(setPortalNameButton);
        setPortalNameButton.active = true;

        // Trying to set the focus on the widget
        portalNameTextField.setCanLoseFocus(false);
        portalNameTextField.active = true;
        portalNameTextField.setFocused(true);
    }

 

This is the code from my BlockEntity

@Override
    public void load(CompoundTag nbt) {
        super.load(nbt);

        // Load stored portal name from nbt data
        CompoundTag portablePortalsModData = nbt.getCompound(PortablePortalsMod.MODID);
        this.portalName = portablePortalsModData.getString("PORTAL_NAME");

        System.out.println("LOADING DATA: " + nbt);
    }

    @Override
    public void saveAdditional(CompoundTag nbt) {
        super.saveAdditional(nbt);

        // Save stored portal name to nbt data
        var portablePortalsModData = new CompoundTag();
        portablePortalsModData.putString("PORTAL_NAME", this.portalName);
        nbt.put(PortablePortalsMod.MODID, portablePortalsModData);

        System.out.println("SAVING DATA: " + nbt);
    }

 

However the console shows the following output, which is the default value that is stored as nbt and not the changed value

 

Screenshot of console and screen

 

Anybody got an idea how I could fix this problem to store the text input persistent in the block entity?

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.



×
×
  • Create New...

Important Information

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