任務系統快速遷移腳本模板
# 任務系統快速遷移腳本模板## 說明
此模板用於批量遷移C_NPCAction.java中的任務
---
## 遷移步驟
### 1. 找到任務區塊
搜索模式: `else if (((L1NpcInstance) obj).getNpcTemplate().get_npcId() == NPC_ID)`
### 2. 添加遷移助手
在區塊開始添加:
```java
// 遷移到新任務系統 API
QuestMigrationHelper migrationHelper = QuestMigrationHelper.getInstance();
```
### 3. 批量替換舊API
#### 模式1: get_step 替換
查找: `pc.getQuest().get_step(QUEST_ID)`
替換為: `migrationHelper.getStepUsingNewApi(QUEST_ID, pc)`
#### 模式2: set_step 替換
查找: `pc.getQuest().set_step(QUEST_ID, step)`
替換為: `migrationHelper.setStepUsingNewApi(QUEST_ID, step, pc)`
#### 模式3: 任務完成替換
查找: `pc.getQuest().set_step(QUEST_ID, 255)`
替換為: `migrationHelper.setStepUsingNewApi(QUEST_ID, L1Quest.QUEST_END, pc)`
---
## 自動化腳本 (Python)
```python
import re
def migrate_quest_file(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 統計遷移前
old_count = len(re.findall(r'pc\.getQuest\(\)\.(get_step|set_step)\(', content))
# 遷移模式
patterns = [
# set_step -> setStepUsingNewApi
(
r'(pc\.getQuest\(\)\.set_step\((\w+),\s*(\d+)\))',
lambda m: f'migrationHelper.setStepUsingNewApi({m.group(2)}, {m.group(3)}, pc)'
),
# get_step -> getStepUsingNewApi
(
r'(pc\.getQuest\(\)\.get_step\((\w+)\))',
lambda m: f'migrationHelper.getStepUsingNewApi({m.group(2)}, pc)'
),
]
# 執行替換
for pattern, replacement in patterns:
content = re.sub(pattern, replacement, content)
# 統計遷移後
new_count = len(re.findall(r'migrationHelper\.(getStepUsingNewApi|setStepUsingNewApi)\(', content))
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"遷移完成: {old_count} -> {new_count}")
# 使用方法
migrate_quest_file('src/l1j/server/server/clientpackets/C_NPCAction.java')
```
---
## 手動遷移檢查清單
### 遷移前
- [ ] 確認任務區塊邊界
- [ ] 記錄NPC ID和任務ID
- [ ] 備份原始代碼
### 遷移中
- [ ] 添加migrationHelper實例化
- [ ] 替換所有get_step調用
- [ ] 替換所有set_step調用
- [ ] 檢查QUEST_END常量使用
### 遷移後
- [ ] 編譯測試
- [ ] 功能測試
- [ ] 性能驗證
- [ ] 更新統計
---
## 編譯測試命令
```bash
./build/ant/bin/ant -f build/build.xml compile
```
---
## 常見錯誤處理
### 錯誤1: migrationHelper未定義
**原因**: 忘記在區塊開始實例化
**解決**: 添加 `QuestMigrationHelper migrationHelper = QuestMigrationHelper.getInstance();`
### 錯誤2: 編譯失敗
**原因**: 舊API調用未完全替換
**解決**: 搜索並替換所有舊API調用
### 錯誤3: 邏輯錯誤
**原因**: get_step返回值處理不正確
**解決**: 確認返回值類型匹配
---
## 性能驗證
### 資料庫查詢監控
```java
// 查詢前
long start = System.currentTimeMillis();
int step = migrationHelper.getStepUsingNewApi(questId, pc);
long end = System.currentTimeMillis();
System.out.println("查詢時間: " + (end - start) + "ms");
```
### 遷移統計
```java
QuestMigrationHelper migrationHelper = QuestMigrationHelper.getInstance();
System.out.println(migrationHelper.getMigrationStats());
```
---
**最後更新**: 2026年2月15日
頁:
[1]