ScriptMonsterSpawnService.java 2.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package emu.grasscutter.scripts.service;

import emu.grasscutter.data.GameData;
import emu.grasscutter.data.def.MonsterData;
import emu.grasscutter.data.def.WorldLevelData;
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.scripts.SceneScriptManager;
import emu.grasscutter.scripts.constants.EventType;
import emu.grasscutter.scripts.data.SceneMonster;
import emu.grasscutter.scripts.data.ScriptArgs;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ScriptMonsterSpawnService {

    private final SceneScriptManager sceneScriptManager;
    private final List<Consumer<EntityMonster>> onMonsterCreatedListener = new ArrayList<>();

    private final List<Consumer<EntityMonster>> onMonsterDeadListener = new ArrayList<>();

    public ScriptMonsterSpawnService(SceneScriptManager sceneScriptManager){
        this.sceneScriptManager = sceneScriptManager;
    }

    public void addMonsterCreatedListener(Consumer<EntityMonster> consumer){
        onMonsterCreatedListener.add(consumer);
    }
    public void addMonsterDeadListener(Consumer<EntityMonster> consumer){
        onMonsterCreatedListener.add(consumer);
    }

    public void onMonsterDead(EntityMonster entityMonster){
        onMonsterCreatedListener.stream().forEach(l -> l.accept(entityMonster));
    }
    public void spawnMonster(int groupId, SceneMonster monster) {
        if(monster == null){
            return;
        }

        MonsterData data = GameData.getMonsterDataMap().get(monster.monster_id);

        if (data == null) {
            return;
        }

        // Calculate level
        int level = monster.level;

        if (sceneScriptManager.getScene().getDungeonData() != null) {
            level = sceneScriptManager.getScene().getDungeonData().getShowLevel();
        } else if (sceneScriptManager.getScene().getWorld().getWorldLevel() > 0) {
            WorldLevelData worldLevelData = GameData.getWorldLevelDataMap().get(sceneScriptManager.getScene().getWorld().getWorldLevel());

            if (worldLevelData != null) {
                level = worldLevelData.getMonsterLevel();
            }
        }

        // Spawn mob
        EntityMonster entity = new EntityMonster(sceneScriptManager.getScene(), data, monster.pos, level);
        entity.getRotation().set(monster.rot);
        entity.setGroupId(groupId);
        entity.setConfigId(monster.config_id);

        onMonsterCreatedListener.forEach(action -> action.accept(entity));

        sceneScriptManager.getScene().addEntity(entity);

        sceneScriptManager.callEvent(EventType.EVENT_ANY_MONSTER_LIVE, new ScriptArgs(entity.getConfigId()));
    }
}