Tile Entity Menu¶
Nova uses InvUI for GUIs. Check out InvUI's documentation here.
Nova's TileEntity class has a menu: TileEntityMenu property which is used to manage this windows associated with the tile entity. Simply override it to add a GUI to your tile entity:
override val menu = TileEntityMenu.cachedWindow(GuiTextures.EXAMPLE_GUI_TEXTURE) { // (1)!
// (2)!
upperGui by /* ... */
}
-
TileEntityMenu.cachedWindowis recommended. TheTileEntityMenuwill remember the opened window, which prevents unnecessary re-creation and also prevents gui state (e.g. forPagedGui) from being forgotten immediately on close. You can, however, also useTileEntityMenu.windowfor an uncached window.Additionally, you can also specify the type of
WindowDslto use, for example if you want to use anAnvilWindowinstead. -
This is a standard
WindowDsl.() -> Unitblock (likewindow(player)). Each player will get their ownWindow.
Make sure that you have the TileEntityInteractive block behavior applied, otherwise the tile entity will not receive the click action and no menu will be opened.
Tracking additional Windows¶
The TileEntityMenu needs to be made aware of any additional windows, so that it can close them when the tile entity is destroyed or when the viewer moves out of range. You can do this by calling TileEntityMenu.register(Window).
Example button that opens and tracks a sub menu
The following function creates a UI item that lazily creates, tracks, and opens a sub-menu when clicked.
context(tileEntity: TileEntity, windowDsl: WindowDsl) // (1)!
fun openSubWindow(): Item = item {
itemProvider by GuiItems.OPEN_EXAMPLE_WINDOW_BTN
val exampleWindow by lazy {
val window = createExampleWindow(windowDsl.viewer, windowDsl.window) // (2)!
tileEntity.menu.register(window) // (3)!
window
}
onClick {
exampleWindow.open()
}
}
- Using context parameters to implicitly pass things like the
TileEntityandWindowDslmakes using this function much less verbose. - When creating your window, you can then use the
WindowDslcontext parameter for things like the viewer and the parent window. The parent window is useful if you want to have a "back" button. - Tracks the window as part of the TileEntity's menu.
TileEntityMenuonly keeps aWeakReference, so this will not cause memory leaks.