InvokeHandler.java 2.11 KB
Newer Older
Melledy's avatar
Melledy committed
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
package emu.grasscutter.game;

import java.util.ArrayList;
import java.util.List;
import emu.grasscutter.net.packet.GenshinPacket;
import emu.grasscutter.net.proto.ForwardTypeOuterClass.ForwardType;

public class InvokeHandler<T> {
	private final List<T> entryListForwardAll;
	private final List<T> entryListForwardAllExceptCur;
	private final List<T> entryListForwardHost;
	private final Class<? extends GenshinPacket> packetClass;
	
	public InvokeHandler(Class<? extends GenshinPacket> packetClass) {
		this.entryListForwardAll = new ArrayList<>();
		this.entryListForwardAllExceptCur = new ArrayList<>();
		this.entryListForwardHost = new ArrayList<>();
		this.packetClass = packetClass;
	}

	public synchronized void addEntry(ForwardType forward, T entry) {
		switch (forward) {
			case ForwardToAll:
				entryListForwardAll.add(entry);
				break;
			case ForwardToAllExceptCur:
			case ForwardToAllExistExceptCur:
				entryListForwardAllExceptCur.add(entry);
				break;
			case ForwardToHost:
				entryListForwardHost.add(entry);
				break;
			default:
				break;
		}
	}
	
	public synchronized void update(GenshinPlayer player) {
		if (player.getWorld() == null) {
			this.entryListForwardAll.clear();
			this.entryListForwardAllExceptCur.clear();
			this.entryListForwardHost.clear();
			return;
		}
		
		try {
			if (entryListForwardAll.size() > 0) {
				GenshinPacket packet = packetClass.getDeclaredConstructor(List.class).newInstance(this.entryListForwardAll);
49
				player.getScene().broadcastPacket(packet);
Melledy's avatar
Melledy committed
50
51
52
53
				this.entryListForwardAll.clear();
			}
			if (entryListForwardAllExceptCur.size() > 0) {
				GenshinPacket packet = packetClass.getDeclaredConstructor(List.class).newInstance(this.entryListForwardAllExceptCur);
54
				player.getScene().broadcastPacketToOthers(player, packet);
Melledy's avatar
Melledy committed
55
56
57
58
59
60
61
62
63
64
65
66
				this.entryListForwardAllExceptCur.clear();
			}
			if (entryListForwardHost.size() > 0) {
				GenshinPacket packet = packetClass.getDeclaredConstructor(List.class).newInstance(this.entryListForwardHost);
				player.getWorld().getHost().sendPacket(packet);
				this.entryListForwardHost.clear();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}