ChatManager.java 2.26 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.managers;

Jaida Wu's avatar
Jaida Wu committed
3
import emu.grasscutter.command.CommandMap;
Melledy's avatar
Melledy committed
4
5
6
7
8
9
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.net.packet.GenshinPacket;
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.server.packet.send.PacketPlayerChatNotify;
import emu.grasscutter.server.packet.send.PacketPrivateChatNotify;

Melledy's avatar
Melledy committed
10
11
12
import java.util.Arrays;
import java.util.List;

Melledy's avatar
Melledy committed
13
public class ChatManager {
Melledy's avatar
Melledy committed
14
15
	static final List<Character> PREFIXES = Arrays.asList('/', '!');
	
Melledy's avatar
Melledy committed
16
17
18
19
20
21
22
23
24
25
	private final GameServer server;
	
	public ChatManager(GameServer server) {
		this.server = server;
	}

	public GameServer getServer() {
		return server;
	}

Melledy's avatar
Melledy committed
26
	public void sendPrivateMessage(GenshinPlayer player, int targetUid, String message) {
Melledy's avatar
Melledy committed
27
28
29
30
31
32
		// Sanity checks
		if (message == null || message.length() == 0) {
			return;
		}
				
		// Check if command
Melledy's avatar
Melledy committed
33
34
		if (PREFIXES.contains(message.charAt(0))) {
			CommandMap.getInstance().invoke(player, message);
Melledy's avatar
Melledy committed
35
36
37
38
			return;
		}
		
		// Get target
39
		GenshinPlayer target = getServer().getPlayerByUid(targetUid);
Melledy's avatar
Melledy committed
40
41
42
43
44
45
		
		if (target == null) {
			return;
		}
		
		// Create chat packet
46
		GenshinPacket packet = new PacketPrivateChatNotify(player.getUid(), target.getUid(), message);
Melledy's avatar
Melledy committed
47
48
49
50
51
		
		player.sendPacket(packet);
		target.sendPacket(packet);
	}
	
Melledy's avatar
Melledy committed
52
	public void sendPrivateMessage(GenshinPlayer player, int targetUid, int emote) {
Melledy's avatar
Melledy committed
53
		// Get target
54
		GenshinPlayer target = getServer().getPlayerByUid(targetUid);
Melledy's avatar
Melledy committed
55
56
57
58
59
60
		
		if (target == null) {
			return;
		}
		
		// Create chat packet
61
		GenshinPacket packet = new PacketPrivateChatNotify(player.getUid(), target.getUid(), emote);
Melledy's avatar
Melledy committed
62
63
64
65
66
		
		player.sendPacket(packet);
		target.sendPacket(packet);
	}
	
Melledy's avatar
Melledy committed
67
	public void sendTeamMessage(GenshinPlayer player, int channel, String message) {
Melledy's avatar
Melledy committed
68
69
70
71
72
73
		// Sanity checks
		if (message == null || message.length() == 0) {
			return;
		}
				
		// Check if command
Melledy's avatar
Melledy committed
74
75
		if (PREFIXES.contains(message.charAt(0))) {
			CommandMap.getInstance().invoke(player, message);
Melledy's avatar
Melledy committed
76
77
78
79
80
81
82
			return;
		}

		// Create and send chat packet
		player.getWorld().broadcastPacket(new PacketPlayerChatNotify(player, channel, message));
	}

Melledy's avatar
Melledy committed
83
	public void sendTeamMessage(GenshinPlayer player, int channel, int icon) {
Melledy's avatar
Melledy committed
84
85
86
87
		// Create and send chat packet
		player.getWorld().broadcastPacket(new PacketPlayerChatNotify(player, channel, icon));
	}
}