UI Behavior Example
Current MBD2 Minecraft 1.21.1 / MBD2 21.1.1Current UI LDLib2 2.2.39
js
// kubejs/server_scripts/mbd2_ui.js
MBDMachineEvents.onUI('example:processor', wrapper => {
const { machine, ui } = wrapper.event
const reset = ui.selectId('reset_button').findFirst().orElse(null)
if (reset === null) return
reset.addServerEventListener(UIEvents.MOUSE_DOWN, event => {
if (event.button !== 0) return
const data = machine.customData.copy()
data.remove('uses')
machine.setCustomData(data)
})
})This requires a UIElement with ID reset_button in the editor. Layout, LSS, components, propagation, and data binding are maintained by LDLib2; continue with LDLib2 KubeJS UI support and the UIElement API.
Modify the client Recipe Viewer UI
js
// kubejs/client_scripts/mbd2_recipe_ui.js
MBDRecipeTypeEvents.onRecipeUI('example:processor', wrapper => {
const { recipe, ui } = wrapper.event
const output = ui.selectId('main_output').findFirst().orElse(null)
if (output === null) return
output.addEventListener(UIEvents.HOVER_TOOLTIPS, event => {
event.hoverTooltips = HoverTooltips.empty().append(`Recipe: ${recipe.id}`)
})
})onRecipeUI is a client recipe-type event, so it registers ordinary client UIElement listeners with addEventListener. Never mutate a machine or server storage here.
HoverTooltips.empty().append(...) takes Components; a JS string is coerced for you.
Which listener to use
| Call | Runs on | Use for |
|---|---|---|
addEventListener(type, cb) | Client | Tooltips, visual state, anything local |
addServerEventListener(type, cb) | Server, via RPC | Machine and storage changes |
A JavaScript lambda created inside the server onUI callback does not become client code — pick the listener explicitly.
js
// ❌ Legacy Widget API — these methods do not exist on the 1.21.1 UI
ui.getFirstWidgetById('reset_button').setOnPressCallback(...)