Class ScreenEvents

java.lang.Object
net.fabricmc.fabric.api.client.screen.v1.ScreenEvents

@Environment(CLIENT) public final class ScreenEvents extends Object
Holds events related to Screens.

Some events require a screen instance in order to obtain an event instance. The events that require a screen instance can be identified by the use of a method passing a screen instance. All events in ScreenKeyboardEvents and ScreenMouseEvents require a screen instance. This registration model is used since a screen being (re)initialized will reset the screen to it's default state, therefore reverting all changes a mod developer may have applied to a screen. Furthermore this design was chosen to reduce the amount of wasted iterations of events as a mod developer would only need to register screen events for rendering, ticking, keyboards and mice if needed on a per instance basis.

The primary entrypoint into a screen is when it is being opened, this is signified by an event before and after initialization of the screen.

See Also:
  • Field Details

    • BEFORE_INIT

      public static final Event<ScreenEvents.BeforeInit> BEFORE_INIT
      An event that is called before a screen is initialized to it's default state. It should be noted some of the methods in Screens such as a screen's text renderer may not be initialized yet, and as such their use is discouraged. You can still use AFTER_INIT to register events such as keyboard and mouse events.

      The ScreenExtensions provided by the info parameter may be used to register tick, render events, keyboard, mouse, additional and removal of child elements (including buttons). For example, to register an event on inventory like screens after render, the following code could be used:

      
       &#64;Override
       public void onInitializeClient() {
       	ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
       		if (screen instanceof AbstractInventoryScreen) {
       			ScreenEvents.getAfterRenderEvent(screen).register((matrices, mouseX, mouseY, tickDelta) -> {
       				...
       			});
       		}
       	});
       }
       

      This event indicates a screen has been resized, and therefore is being re-initialized. This event can also indicate that the previous screen has been changed.

      See Also:
    • AFTER_INIT

      public static final Event<ScreenEvents.AfterInit> AFTER_INIT
      An event that is called after a screen is initialized to it's default state.

      Typically this event is used to modify a screen after the screen has been initialized. Modifications such as changing sizes of buttons, removing buttons and adding/removing child elements to the screen can be done safely using this event.

      For example, to add a button to the title screen, the following code could be used:

      
       ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
       	if (screen instanceof TitleScreen) {
       		Screens.getButtons(screen).add(new ButtonWidget(...));
       	}
       });
       

      Note that by adding an element to a screen, the element is not automatically

      invalid @link
      ticked
      or drawn. Unless the element is button, you need to call the specific
      invalid @link
      tick
      and render methods in the corresponding screen events.

      This event can also indicate that the previous screen has been closed.

      See Also:
  • Method Details

    • remove

      public static Event<ScreenEvents.Remove> remove(net.minecraft.client.gui.screen.Screen screen)
      An event that is called after Screen.removed() is called. This event signifies that the screen is now closed.

      This event is typically used to undo any screen specific state changes such as setting the keyboard to receive repeat events or terminate threads spawned by a screen. This event may precede initialization events BEFORE_INIT but there is no guarantee that event will be called immediately afterwards.

    • beforeRender

      public static Event<ScreenEvents.BeforeRender> beforeRender(net.minecraft.client.gui.screen.Screen screen)
      An event that is called before a screen is rendered.
      Returns:
      the event
    • afterRender

      public static Event<ScreenEvents.AfterRender> afterRender(net.minecraft.client.gui.screen.Screen screen)
      An event that is called after a screen is rendered.
      Returns:
      the event
    • beforeTick

      public static Event<ScreenEvents.BeforeTick> beforeTick(net.minecraft.client.gui.screen.Screen screen)
      An event that is called before a screen is ticked.
      Returns:
      the event
    • afterTick

      public static Event<ScreenEvents.AfterTick> afterTick(net.minecraft.client.gui.screen.Screen screen)
      An event that is called after a screen is ticked.
      Returns:
      the event