Reference

Examples

Complete mods you can paste straight into the game, from a HUD panel to waypoints, keystrokes, a notes menu and a calculator.

Publicité

Every example on this page is a whole mod. To try one, copy it, open Mods, click Import mod, paste it into the box and click Import pasted text. Then read the code and change it. That is the fastest way to learn.

Info panel

FPS, coordinates and the biome in a corner of your choice, with settings for everything.

-- name: Info Panel
-- version: 1.0.0

settings.toggle("showCoords", { label = "Show coordinates", default = true })
settings.toggle("showBiome", { label = "Show biome", default = false })
settings.choice("corner", { label = "Corner", options = { "left", "right" }, default = "left" })
settings.color("textColor", { label = "Text color", default = "#FFFFFF" })

hud.add("info", function(ctx)
  if not player.exists() then return end

  local lines = { "FPS: " .. game.fps() }
  if settings.showCoords then
    local x, y, z = player.blockPos()
    table.insert(lines, "XYZ: " .. x .. " " .. y .. " " .. z)
    table.insert(lines, "Facing: " .. player.facing())
  end
  if settings.showBiome then
    local biome = (player.biome() or "unknown"):gsub("^minecraft:", "")
    table.insert(lines, "Biome: " .. biome)
  end

  local width = 0
  for _, line in ipairs(lines) do
    width = math.max(width, ctx:textWidth(line))
  end

  local x = 4
  if settings.corner == "right" then
    x = ctx.width - width - 6
  end

  ctx:rect(x - 2, 2, width + 6, #lines * 10 + 4, "#80000000")
  for i, line in ipairs(lines) do
    ctx:text(line, x + 1, 5 + (i - 1) * 10, settings.textColor)
  end
end)

Keystrokes

Shows WASD, the mouse buttons and the space bar in the top right corner, and lights them up while they are held.

-- name: Keystrokes
-- version: 1.0.0

local KEYS = {
  { label = "W", key = "w", x = 26, y = 0 },
  { label = "A", key = "a", x = 0, y = 26 },
  { label = "S", key = "s", x = 26, y = 26 },
  { label = "D", key = "d", x = 52, y = 26 },
}

local function box(ctx, x, y, w, label, down)
  ctx:rect(x, y, w, 24, down and "#C0FFFFFF" or "#80000000")
  ctx:centeredText(label, x + w / 2, y + 8, down and "black" or "white")
end

hud.add("keystrokes", function(ctx)
  local left, top = ctx.width - 80, 4
  for _, k in ipairs(KEYS) do
    box(ctx, left + k.x, top + k.y, 24, k.label, keys.isDown(k.key))
  end
  box(ctx, left, top + 52, 37, "LMB", mouse.isDown("left"))
  box(ctx, left + 39, top + 52, 37, "RMB", mouse.isDown("right"))
  box(ctx, left, top + 78, 76, "SPACE", keys.isDown("space"))
end)

Waypoints

Save spots with /wp add home, list them with /wp, and see their names and distances floating in the world. Waypoints are saved in storage, per dimension.

-- name: Waypoints
-- version: 1.0.0

storage.waypoints = storage.waypoints or {}

local function names()
  local list = {}
  for name in pairs(storage.waypoints) do
    table.insert(list, name)
  end
  table.sort(list)
  return list
end

commands.register("wp", function(args)
  local action, name = args[1], args[2]
  if action == "add" and name then
    local x, y, z = player.pos()
    storage.waypoints[name] = { x = x, y = y, z = z, dim = player.dimension() }
    mod.save()
    chat.print("Saved waypoint " .. name)
  elseif action == "remove" and name then
    storage.waypoints[name] = nil
    mod.save()
    chat.print("Removed waypoint " .. name)
  else
    local list = names()
    chat.print("Waypoints: " .. (#list > 0 and table.concat(list, ", ") or "none yet"))
    chat.print("Use /wp add <name> or /wp remove <name>")
  end
end, "Save, remove and list waypoints")

hud.add("waypoints", function(ctx)
  if not player.exists() then return end
  local dim = player.dimension()
  local px, py, pz = player.pos()
  for name, wp in pairs(storage.waypoints) do
    if wp.dim == dim then
      local sx, sy = camera.project(wp.x, wp.y + 2, wp.z)
      if sx then
        local dist = math.floor(math.sqrt((wp.x - px) ^ 2 + (wp.y - py) ^ 2 + (wp.z - pz) ^ 2))
        ctx:centeredText(name .. " (" .. dist .. "m)", sx, sy, "aqua")
      end
    end
  end
end)

Notice storage.waypoints = storage.waypoints or {} at the top. It sets a field of storage, so it is saved. Replacing storage itself would not be.

Chat timestamps

Puts the time in front of every chat message.

-- name: Chat Time
-- version: 1.0.0

on("chat_receive", function(e)
  e.message = "[" .. os.date("%H:%M") .. "] " .. e.message
end)

Changing e.message turns the message into plain text, so any colors from the server are lost.

Toggle sprint

Press R to start sprinting all the time, and press it again to stop.

-- name: Toggle Sprint
-- version: 1.0.0

local sprinting = false

keys.bind({
  id = "toggle",
  label = "Toggle sprint",
  default = "r",
  onPress = function()
    sprinting = not sprinting
    ui.notify("Toggle Sprint", sprinting and "On" or "Off")
  end,
})

on("tick", function()
  if sprinting and player.exists() and not player.isSneaking() then
    player.setSprinting(true)
  end
end)

Notes

A notes page you open with N. Whatever you type is saved.

-- name: Notes
-- version: 1.0.0

local menu = ui.screen({
  title = "Notes",
  init = function(s)
    s:textArea({
      value = storage.notes or "",
      hint = "Write anything here...",
      width = 300,
      height = 150,
      onChange = function(text)
        storage.notes = text
      end,
    })
    s:button({ text = "Done", onClick = function() s:close() end })
  end,
  onClose = function()
    mod.save()
  end,
})

keys.bind({
  id = "open",
  label = "Open notes",
  default = "n",
  onPress = function() ui.open(menu) end,
})

Typing an N in the notes does not open them again, because keybinds are off while a screen is open unless you set inScreens = true.

Calculator

Type /calc 2 * (3 + 4) and get the answer in chat. The math library works too: /calc math.sqrt(2).

-- name: Calculator
-- version: 1.0.0

commands.register("calc", function(args, raw)
  local fn = load("return " .. raw, "calc", "t", { math = math })
  local ok, result = false, "that is not a sum"
  if fn then
    ok, result = pcall(fn)
  end
  if ok then
    chat.print(raw .. " = " .. tostring(result))
  else
    chat.print("Could not work out " .. raw .. ": " .. tostring(result))
  end
end, "Work out a sum, like /calc 2 * (3 + 4)")

load turns the text after /calc into a tiny Lua function. The last argument gives that function its own small world with only math in it, so a sum cannot reach the rest of your mod.