Skip to main content

client — Client API

Phase: Client (client_scripts/)

The client global is available only in client-side scripts. It provides HUD rendering, keybindings, chat messages, player info, and client↔server networking.

Client scripts reload on F3+T (resource pack reload).


client.sendMessage(text)

Displays a message in the local chat window (not sent to other players).

client.sendMessage("Script loaded!")

client.getPlayer()table | nil

Returns a table with basic information about the local player, or nil if not in a world.

local p = client.getPlayer()
if p then
client.sendMessage("You are at " .. p.x .. ", " .. p.y .. ", " .. p.z)
end

Returned table fields

FieldTypeDescription
namestringPlayer name
x, y, znumberPosition (also available as pos.x, pos.y, pos.z)
pos{x, y, z}Position table
mainHandItemstringHeld item identifier

client.onHudRender(fn)

Registers a function that is called every frame while the HUD is rendered. The callback receives a draw context table e.

client.onHudRender(function(e)
e.drawText("Hello, HUD!", 10, 10, 0xFFFFFF)
end)

HUD context (e) fields and methods

MemberTypeDescription
e.screenWidthintScaled screen width in pixels
e.screenHeightintScaled screen height in pixels
e.tickDeltafloatPartial tick progress (0.0–1.0)
e.drawRect(x, y, w, h, argbColor)functionDraw a filled rectangle
e.drawText(text, x, y, rgbColor)functionDraw text with shadow
e.drawTextCentered(text, cx, y, rgbColor)functionDraw centred text
e.drawOverlay(argbColor)functionFill the entire screen with a colour overlay

Colour format

  • argbColor0xAARRGGBB (e.g. 0x80FF0000 = 50% transparent red)
  • rgbColor0xRRGGBB (e.g. 0xFFFFFF = white)
client.onHudRender(function(e)
-- Semi-transparent dark bar at the top
e.drawRect(0, 0, e.screenWidth, 14, 0x88000000)

-- Centred white text
e.drawTextCentered("Custom HUD", e.screenWidth / 2, 3, 0xFFFFFF)

-- Red health warning overlay
local p = client.getPlayer()
if p then
e.drawText("Pos: " .. math.floor(p.x) .. " " .. math.floor(p.y) .. " " .. math.floor(p.z), 4, 4, 0xFFFF55)
end
end)

client.registerKeybind(id, key, fn)

Registers a keybinding that appears in Minecraft's Controls settings. Calls fn each time the key is pressed.

ParameterTypeDescription
idstringUnique bind ID shown in Controls (prefixed with key.)
keystringKey name: bare letter ("R"), KEY_R, or GLFW_KEY_R all work
fnfunctionCallback fired on press
client.registerKeybind("mymod.reload", "R", function()
client.sendMessage("R key pressed!")
client.network.sendToServer("mymod:action", { type = "reload" })
end)

client.registerKeybind("mymod.menu", "KEY_M", function()
client.sendMessage("Opening menu...")
end)
note

Key bindings can only be registered once with Fabric. On F3+T reload the callback is updated, but the key assignment is preserved.


client.network

Client-side packet API for communicating with the server.

client.network.sendToServer(channel, data)

Sends a packet to the server.

client.network.sendToServer("myplugin:action", { type = "purchase", item = "sword" })

client.network.onReceiveFromServer(channel, fn)

Registers a handler for packets sent from the server on the given channel.

client.network.onReceiveFromServer("myplugin:update", function(data)
client.sendMessage("Server score: " .. data.score)
end)

See the Network API page for the server-side counterpart.