ServerQuestHandler.java 2.81 KB
Newer Older
1
2
3
4
5
6
package emu.grasscutter.game.quest;

import java.util.Set;

import org.reflections.Reflections;

Melledy's avatar
Melledy committed
7
8
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.def.QuestData.QuestCondition;
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
import emu.grasscutter.game.quest.handlers.QuestBaseHandler;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

@SuppressWarnings("unchecked")
public class ServerQuestHandler {
	private final Int2ObjectMap<QuestBaseHandler> condHandlers;
	private final Int2ObjectMap<QuestBaseHandler> contHandlers;
	private final Int2ObjectMap<QuestBaseHandler> execHandlers;
	
	public ServerQuestHandler() {
		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");
		this.registerHandlers(this.contHandlers, "emu.grasscutter.game.quest.content");
		this.registerHandlers(this.execHandlers, "emu.grasscutter.game.quest.exec");
	}
	
	public void registerHandlers(Int2ObjectMap<QuestBaseHandler> map, String packageName) {
		Reflections reflections = new Reflections(packageName);
		Set<?> handlerClasses = reflections.getSubTypesOf(QuestBaseHandler.class);
		
		for (Object obj : handlerClasses) {
			this.registerPacketHandler(map, (Class<? extends QuestBaseHandler>) obj);
		}
	}

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

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

			QuestBaseHandler packetHandler = (QuestBaseHandler) handlerClass.newInstance();

			map.put(opcode.value().getValue(), packetHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// TODO make cleaner
	
	public boolean triggerCondition(GameQuest quest, QuestCondition condition, int... params) {
		QuestBaseHandler handler = condHandlers.get(condition.getType().getValue());

Melledy's avatar
Melledy committed
63
		if (handler == null || quest.getData() == null) {
64
65
66
67
68
69
70
71
72
			return false;
		}
		
		return handler.execute(quest, condition, params);
	}
	
	public boolean triggerContent(GameQuest quest, QuestCondition condition, int... params) {
		QuestBaseHandler handler = contHandlers.get(condition.getType().getValue());

Melledy's avatar
Melledy committed
73
		if (handler == null || quest.getData() == null) {
74
75
76
77
78
79
80
81
82
			return false;
		}
		
		return handler.execute(quest, condition, params);
	}
	
	public boolean triggerExec(GameQuest quest, QuestCondition condition, int... params) {
		QuestBaseHandler handler = execHandlers.get(condition.getType().getValue());

Melledy's avatar
Melledy committed
83
		if (handler == null || quest.getData() == null) {
84
85
86
87
88
89
			return false;
		}
		
		return handler.execute(quest, condition, params);
	}
}