Introduction
L2J Mobius Chronicle 4 features a powerful Java-based scripting system that enables server customization without modifying core game files. Scripts are automatically loaded at server startup and can be reloaded at runtime.Directory Structure
All scripts are located indist/game/data/scripts/ with the following organization:
Script Types
Quest Scripts
Extend theQuest class to implement player quests with state management, item rewards, and NPC interactions.
AI Scripts
Extend theScript class to define custom NPC behavior, boss mechanics, and zone interactions.
Handlers
Implement specific handler interfaces (IItemHandler, IAdminCommandHandler, etc.) to extend game functionality.
Script Loading Process
Server Initialization
The ScriptManager loads all scripts from the scripts directory during server startup.
Master Handler Execution
Master handler classes (e.g.,
QuestMasterHandler.java) instantiate all registered scripts of their type.quests/QuestMasterHandler.java
Script Registration
Each script registers itself with the appropriate manager during construction:
- Quests register with QuestManager
- Handlers register with HandlerManager
- AI scripts attach to specific NPCs
Base Script Classes
Quest Class
Script Class (for AI)
Handler Interface
Script Lifecycle
- Construction - Script instantiated via reflection
- Registration - Script registers event handlers and NPCs
- Active - Script responds to game events
- Unload - Script cleanup when reloading (optional
unload()method) - Reload - New instance created with updated code
Event Registration Methods
Scripts register for specific game events:| Method | Triggered When |
|---|---|
addStartNpc() | Player initiates conversation |
addTalkId() | Player talks to NPC |
addFirstTalkId() | First conversation with NPC |
addKillId() | Registered NPC is killed |
addAttackId() | Registered NPC is attacked |
addSpawnId() | Registered NPC spawns |
addSpellFinishedId() | NPC finishes casting skill |
addAggroRangeEnterId() | Player enters NPC aggro range |
Configuration
Reload Configuration
Scripts can be reloaded in two ways:- Admin Command
- Programmatic
Script Compilation
Scripts are compiled Java classes. After modifying:- Recompile the script class
- Place compiled
.classfile in appropriate package directory - Use
//reload scriptsor restart server
For production servers, compile scripts with the same Java version as the game server to avoid compatibility issues.
Best Practices
- Package Structure: Keep scripts in their designated packages
- Resource Cleanup: Implement
unload()for scripts that create timers or spawn NPCs - Error Handling: Use try-catch blocks to prevent script crashes from affecting server
- Constants: Define NPC IDs, item IDs as constants at class top
- Logging: Use appropriate log levels (INFO, WARNING, SEVERE)
Next Steps
Quest Scripts
Learn quest scripting with real examples
AI Scripts
Implement custom NPC and boss AI
Handlers
Extend game functionality with handlers