Class ScreenEvents
Screen
s.
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static interface
static interface
static interface
static interface
static interface
static interface
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Event<ScreenEvents.AfterInit>
An event that is called aftera screen is initialized
to it's default state.static final Event<ScreenEvents.BeforeInit>
An event that is called beforea screen is initialized
to it's default state. -
Method Summary
Modifier and TypeMethodDescriptionstatic Event<ScreenEvents.AfterRender>
afterRender
(net.minecraft.client.gui.screen.Screen screen) An event that is called after a screen is rendered.static Event<ScreenEvents.AfterTick>
afterTick
(net.minecraft.client.gui.screen.Screen screen) An event that is called after a screen is ticked.static Event<ScreenEvents.BeforeRender>
beforeRender
(net.minecraft.client.gui.screen.Screen screen) An event that is called before a screen is rendered.static Event<ScreenEvents.BeforeTick>
beforeTick
(net.minecraft.client.gui.screen.Screen screen) An event that is called before a screen is ticked.static Event<ScreenEvents.Remove>
remove
(net.minecraft.client.gui.screen.Screen screen) An event that is called afterScreen.removed()
is called.
-
Field Details
-
BEFORE_INIT
An event that is called beforea screen is initialized
to it's default state. It should be noted some of the methods inScreens
such as a screen'stext renderer
may not be initialized yet, and as such their use is discouraged. You can still useAFTER_INIT
to register events such as keyboard and mouse events.The
ScreenExtensions
provided by theinfo
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:@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
An event that is called aftera 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
ticked
ordrawn
. Unless the element is button, you need to call the specifictick
andrender
methods in the corresponding screen events.This event can also indicate that the previous screen has been closed.
- See Also:
-
-
Method Details
-
remove
An event that is called afterScreen.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 eventsBEFORE_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
-