ScriptEngine class manages the loading, compilation, and execution of all Java scripts in the L2J Mobius server, including quests, AI behaviors, and handler implementations.
Source: org.l2jmobius.gameserver.scripting.ScriptEngineLocation:
java/org/l2jmobius/gameserver/scripting/ScriptEngine.java:51
Overview
ScriptEngine provides:- Automatic discovery and compilation of
.javascripts - Hot-reload capability for script modifications
- Exclusion filtering via
Scripts.xml - Master handler registration system
- Error reporting and script debugging
Class Structure
Key Constants
Root directory for all scripts:
data/scripts/Main handler registration file:
handlers/MasterHandler.javaEffect handler registration file:
handlers/EffectMasterHandler.javaScript Loading Process
Core Methods
getInstance()
reload()
- Clears existing script cache
- Reloads
Scripts.xmlexclusions - Rescans script directory
- Recompiles modified scripts
- Re-registers all handlers
executeScript()
Script file to execute
true if script executed successfully, false otherwise
Usage:
executeScriptsList()
Script Directory Structure
Configuration
Scripts.xml
Define scripts to exclude from loading:Development Mode
Enable script debugging inconfig/Development.ini:
- Detailed compilation error messages
- Stack traces for script exceptions
- Performance profiling per script
Script Types
Quest Scripts
AI Scripts
Handler Scripts
MasterHandler.java.
Error Handling
ScriptEngine provides detailed error reporting:Common Errors
Compilation Error
Compilation Error
Cause: Syntax error in scriptSolution: Check script syntax and imports
Class Not Found
Class Not Found
Cause: Missing dependency or incorrect packageSolution: Verify imports and class paths
Script Excluded
Script Excluded
Cause: Script listed in
Scripts.xmlSolution: Remove from exclusion list or verify intentional exclusionDuplicate Registration
Duplicate Registration
Cause: Script instantiated multiple timesSolution: Ensure script constructor doesn’t call
new on itselfHot Reload
Reload scripts without server restart:Hot Reload Limitations:
- Cannot reload scripts with active timers
- May cause memory leaks if not properly unloaded
- Quest states are preserved
- Use
//reload handlersfor handler changes
Performance Considerations
Script Caching
Compiled scripts are cached in memory:Compilation Time
Typical script compilation times:- Simple quest: 50-100ms
- Complex AI: 200-500ms
- Handler: 30-80ms
- Full reload: 5-15 seconds (600+ scripts)
Memory Usage
- Per script class: ~10-50 KB
- Total script cache: ~30-100 MB
- Compilation overhead: +50 MB during reload
Related Classes
- Quest - Base class for quest scripts
- AbstractNpcAI - Base class for AI scripts
- IAdminCommandHandler - Interface for admin command handlers
- ScriptExecutor - Handles Java compilation and execution
- ScriptFileManager - Manages script file discovery
Best Practices
Script Development:
- Use descriptive class and file names
- Follow naming conventions (
Q00001_QuestName.java) - Add scripts to
Scripts.xmlif incomplete - Test scripts on development server first
- Use
//reload scriptfor iterative development - Clean up timers and listeners in
onAdvEvent - Avoid static variables (cause issues on reload)
- Use logging for debugging (
LOGGER.info())
Admin Commands
Manage scripts via in-game commands:Debugging Scripts
Enable detailed logging:Integration with GameServer
ScriptEngine is initialized during GameServer startup:- Database initialization
- Data loading (items, NPCs, skills)
- ScriptEngine initialization ← Scripts loaded here
- Network startup
- Server ready