Events

React to player actions and server events

💡 Lua Tip: Use the onEvent function to handle all events in your scripts.

Event Types

Player Events

EventDescriptionData
PLAYER_JOIN Player connects to server player (id, name)
PLAYER_QUIT Player disconnects player (id, name)
PLAYER_DEATH Player dies player, killer, cause

Chat Events

-- Handle chat messages
function onEvent(event)
    if event.type == "CHAT_MESSAGE" then
        local player = event.player
        local message = event.message
        
        -- Check for commands
        if message:sub(1, 1) == "!" then
            local cmd = message:sub(2)
            handleCommand(player, cmd)
        end
    end
end

function handleCommand(player, cmd)
    if cmd == "help" then
        MCX.sendMessage(player.id, "Available commands: !help, !spawn, !kit")
    elseif cmd == "spawn" then
        MCX.teleport(player.id, 0, 64, 0)
    end
end

World Events

EventDescription
BLOCK_BREAKPlayer breaks a block
BLOCK_PLACEPlayer places a block
SCENE_CHANGEScene is switched

Complete Example

Welcome new players and give them a starter kit:

-- Welcome script
local welcomeMessages = {
    "Welcome to the server, {name}!",
    "Hello {name}, enjoy your stay!",
    "{name} has joined the adventure!"
}

function onEvent(event)
    if event.type == "PLAYER_JOIN" then
        local player = event.player
        local msg = welcomeMessages[math.random(#welcomeMessages)]
        msg = msg:gsub("{name}", player.name)
        
        -- Broadcast welcome
        MCX.broadcast("§a" .. msg)
        
        -- Send private welcome
        MCX.sendMessage(player.id, "§6Welcome! Type !kit to get started.")
        
        -- Give starter items (if implemented)
        -- MCX.giveItem(player.id, "wooden_sword", 1)
    end
end

Event Priority

Events are processed in order. You can cancel or modify actions:

-- Cancel certain events
function onEvent(event, actions)
    -- Prevent block breaking in spawn area
    if event.type == "BLOCK_BREAK" then
        local pos = event.position
        if pos.x > -50 and pos.x < 50 and pos.z > -50 and pos.z < 50 then
            -- Return empty actions to cancel
            return {}
        end
    end
    return actions
end