Yes, you need to subscribe to `GuiScreenEvent.InitGuiEvent.Pr
e event)` in order to add widgets, the event itself has a method `addWidget`.
Here's an example of how it could be used:
@SubscribeEvent
public void addCustomButtonToInventory(GuiScreenEvent.InitGuiEvent.Pre event)
{
if (event.getGui() instanceof InventoryScreen)
{
event.addWidget(new Button(x, y, width, height, text, button -> {
Minecraft.getInstance().displayGuiScreen(new CustomGUI());
}));
}
}
If you aren't familiar with the `button -> {...}` syntax, this Button constructor takes an Button.IPressable as a parameter, which is the behavior that happens when you click. Equivalent code would look like this:
@SubscribeEvent
public void addCustomButtonToInventory(GuiScreenEvent.InitGuiEvent.Pre event)
{
if (event.getGui() instanceof InventoryScreen)
{
event.addWidget(new Button(x, y, width, height, text, new CustomAction()));
}
}
class CustomAction implements IPressable {
public void onPress(Button button) {
Minecraft.getInstance().displayGuiScreen(new CustomGUI());
}
}
Hope this helps!