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?