QuestSystem.java 3.39 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 {
20
21
	private final Int2ObjectMap<QuestBaseHandler> condHandlers;
	private final Int2ObjectMap<QuestBaseHandler> contHandlers;
Akka's avatar
Akka committed
22
23
	private final Int2ObjectMap<QuestExecHandler> execHandlers;

24
25
26
	public QuestSystem(GameServer server) {
	    super(server);
	    
Akka's avatar
Akka committed
27
		this.condHandlers = new Int2ObjectOpenHashMap<>();
28
29
		this.contHandlers = new Int2ObjectOpenHashMap<>();
		this.execHandlers = new Int2ObjectOpenHashMap<>();
Akka's avatar
Akka committed
30

31
32
33
34
		this.registerHandlers();
	}

	public void registerHandlers() {
Akka's avatar
Akka committed
35
36
37
		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);
38
	}
Akka's avatar
Akka committed
39
40

	public <T> void registerHandlers(Int2ObjectMap<T> map, String packageName, Class<T> clazz) {
41
		Reflections reflections = new Reflections(packageName);
Akka's avatar
Akka committed
42
43
44
45
		var handlerClasses = reflections.getSubTypesOf(clazz);

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

Akka's avatar
Akka committed
49
	public <T> void registerPacketHandler(Int2ObjectMap<T> map, Class<? extends T> handlerClass) {
50
51
52
53
54
55
56
		try {
			QuestValue opcode = handlerClass.getAnnotation(QuestValue.class);

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

Akka's avatar
Akka committed
57
			map.put(opcode.value().getValue(), handlerClass.newInstance());
58
59
60
61
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
Akka's avatar
Akka committed
62

63
	// TODO make cleaner
Akka's avatar
Akka committed
64
65

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

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

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

	public boolean triggerContent(GameQuest quest, QuestCondition condition, String paramStr, int... params) {
77
78
		QuestBaseHandler handler = contHandlers.get(condition.getType().getValue());

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

		return handler.execute(quest, condition, paramStr, params);
85
	}
Akka's avatar
Akka committed
86
87
88

	public boolean triggerExec(GameQuest quest, QuestExecParam execParam, String... params) {
		QuestExecHandler handler = execHandlers.get(execParam.getType().getValue());
89

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

		return handler.execute(quest, execParam, params);
96
97
	}
}