QuestSystem.java 3.72 KB
Newer Older
1
2
3
4
package emu.grasscutter.game.quest;

import java.util.Set;

Akka's avatar
Akka committed
5
6
import emu.grasscutter.data.excels.QuestData;
import emu.grasscutter.game.quest.handlers.QuestExecHandler;
7
8
9
import emu.grasscutter.server.game.BaseGameSystem;
import emu.grasscutter.server.game.GameServer;

10
11
import org.reflections.Reflections;

Melledy's avatar
Melledy committed
12
import emu.grasscutter.Grasscutter;
Akka's avatar
Akka committed
13
import emu.grasscutter.data.excels.QuestData.*;
14
15
16
17
18
import emu.grasscutter.game.quest.handlers.QuestBaseHandler;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

@SuppressWarnings("unchecked")
19
public class QuestSystem extends BaseGameSystem {
github-actions's avatar
github-actions committed
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
    private final Int2ObjectMap<QuestBaseHandler> condHandlers;
    private final Int2ObjectMap<QuestBaseHandler> contHandlers;
    private final Int2ObjectMap<QuestExecHandler> execHandlers;

    public QuestSystem(GameServer server) {
        super(server);

        this.condHandlers = new Int2ObjectOpenHashMap<>();
        this.contHandlers = new Int2ObjectOpenHashMap<>();
        this.execHandlers = new Int2ObjectOpenHashMap<>();

        this.registerHandlers();
    }

    public void registerHandlers() {
        this.registerHandlers(this.condHandlers, "emu.grasscutter.game.quest.conditions", QuestBaseHandler.class);
        this.registerHandlers(this.contHandlers, "emu.grasscutter.game.quest.content", QuestBaseHandler.class);
        this.registerHandlers(this.execHandlers, "emu.grasscutter.game.quest.exec", QuestExecHandler.class);
    }

    public <T> void registerHandlers(Int2ObjectMap<T> map, String packageName, Class<T> clazz) {
        Reflections reflections = new Reflections(packageName);
        var handlerClasses = reflections.getSubTypesOf(clazz);

        for (var obj : handlerClasses) {
            this.registerPacketHandler(map, obj);
        }
    }

    public <T> void registerPacketHandler(Int2ObjectMap<T> map, Class<? extends T> handlerClass) {
        try {
            QuestValue opcode = handlerClass.getAnnotation(QuestValue.class);

            if (opcode == null || opcode.value().getValue() <= 0) {
                return;
            }

            map.put(opcode.value().getValue(), handlerClass.newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // TODO make cleaner

    public boolean triggerCondition(GameQuest quest, QuestCondition condition, String paramStr, int... params) {
        QuestBaseHandler handler = condHandlers.get(condition.getType().getValue());

akatatsu27's avatar
akatatsu27 committed
68
69
        if (handler == null || quest.getQuestData() == null) {
            Grasscutter.getLogger().debug("Could not trigger condition {} at {}", condition.getType().getValue(), quest.getQuestData());
github-actions's avatar
github-actions committed
70
71
            return false;
        }
Akka's avatar
Akka committed
72

github-actions's avatar
github-actions committed
73
74
        return handler.execute(quest, condition, paramStr, params);
    }
Akka's avatar
Akka committed
75

github-actions's avatar
github-actions committed
76
77
    public boolean triggerContent(GameQuest quest, QuestCondition condition, String paramStr, int... params) {
        QuestBaseHandler handler = contHandlers.get(condition.getType().getValue());
78

akatatsu27's avatar
akatatsu27 committed
79
80
        if (handler == null || quest.getQuestData() == null) {
            Grasscutter.getLogger().debug("Could not trigger content {} at {}", condition.getType().getValue(), quest.getQuestData());
github-actions's avatar
github-actions committed
81
82
            return false;
        }
Akka's avatar
Akka committed
83

github-actions's avatar
github-actions committed
84
85
        return handler.execute(quest, condition, paramStr, params);
    }
Akka's avatar
Akka committed
86

github-actions's avatar
github-actions committed
87
88
    public boolean triggerExec(GameQuest quest, QuestExecParam execParam, String... params) {
        QuestExecHandler handler = execHandlers.get(execParam.getType().getValue());
89

akatatsu27's avatar
akatatsu27 committed
90
91
        if (handler == null || quest.getQuestData() == null) {
            Grasscutter.getLogger().debug("Could not trigger exec {} at {}", execParam.getType().getValue(), quest.getQuestData());
github-actions's avatar
github-actions committed
92
93
            return false;
        }
Akka's avatar
Akka committed
94

github-actions's avatar
github-actions committed
95
96
        return handler.execute(quest, execParam, params);
    }
97
}