Lua Scripting and Safety Watchdog
CraftingEngine integrates an embedded, high-performance LuaJ 3.0.1 scripting runtime that allows server administrators and developers to author dynamic conditions, procedural calculations, and custom stage actions.
1. Sandbox Architecture and Security
To protect server integrity, Lua execution is isolated by a strict security sandbox:
- No System/OS Access: Calls to
io.*,os.execute, and file-system manipulation are stripped from the environment. - Instruction Quota: Script execution is bounded by
scripting.max-instructions: 10000to prevent infinite loops (while true do). - Wall-Clock Watchdog: Any script exceeding
scripting.execution-timeout-ms: 50is forcibly interrupted.
2. Global Context Bindings
Every Lua script evaluated in a crafting context receives predefined globals:
-- Context variables injected into the Lua environmentlocal player_name = context.player.namelocal player_uuid = context.player.uuidlocal player_level = context.player.level
-- Station state variableslocal temp = context.station.get_variable("temperature")local pressure = context.station.get_variable("pressure")
-- Stage telemetrylocal strikes = context.stage.strikes_count3. Authoring Lua Scripts (presets/scripts/*.lua)
You can save reusable scripts in plugins/CraftingEngine/presets/scripts/:
Example: Dynamic Temperature Formula (calculate_cooling.lua)
function evaluate(current_temp, ambient_temp, elapsed_seconds) local k = 0.05 -- Cooling constant local result = ambient_temp + (current_temp - ambient_temp) * math.exp(-k * elapsed_seconds) return math.max(ambient_temp, result)endReferencing in Blueprints
stages: - id: "custom_cooling" on_tick: - action: "lua:presets/scripts/calculate_cooling.lua"4. Inline Mathematical Expressions
For simple calculations, CraftingEngine supports inline AST math expressions directly in YAML conditions:
requirements: conditions: - id: "heat_balance" condition: "math:(station.temperature * 1.5) > (player.level * 10)"Next Steps
- Developer API Guide - Register custom Java actions and conditions callable from Lua.
- Stages & Transitions - Learn how stages hook into Lua scripts.