Skip to content

UI Behavior Example ​

Current MBD2 Minecraft 1.21.1 / MBD2 21.1.1Current UI LDLib2 2.2.39
MBD2 Machine UI editor with a stable reset_button element ID
Create the element and ID in the editor, then attach server behavior with `onUI`.
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 ​

CallRuns onUse for
addEventListener(type, cb)ClientTooltips, visual state, anything local
addServerEventListener(type, cb)Server, via RPCMachine and storage changes

A JavaScript lambda created inside the server onUI callback does not become client code — pick the listener explicitly.

Legacy; do not copy to 1.21.1 Minecraft 1.20.1 / MBD2 1.0.x
js
// ❌ Legacy Widget API — these methods do not exist on the 1.21.1 UI
ui.getFirstWidgetById('reset_button').setOnPressCallback(...)

Released under the MIT License.