Grasscutter/src/main/java/emu/grasscutter/game/managers/ChatManager.java

88 lines
2.2 KiB
Java
Raw Normal View History

2022-04-17 20:43:07 +08:00
package emu.grasscutter.game.managers;
import emu.grasscutter.command.CommandMap;
2022-04-27 12:24:25 +08:00
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.BasePacket;
2022-04-17 20:43:07 +08:00
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.server.packet.send.PacketPlayerChatNotify;
import emu.grasscutter.server.packet.send.PacketPrivateChatNotify;
2022-04-19 12:35:01 +08:00
import java.util.Arrays;
import java.util.List;
2022-04-17 20:43:07 +08:00
public class ChatManager {
2022-04-19 12:35:01 +08:00
static final List<Character> PREFIXES = Arrays.asList('/', '!');
2022-04-17 20:43:07 +08:00
private final GameServer server;
public ChatManager(GameServer server) {
this.server = server;
}
public GameServer getServer() {
return server;
}
public void sendPrivateMessage(Player player, int targetUid, String message) {
2022-04-17 20:43:07 +08:00
// Sanity checks
if (message == null || message.length() == 0) {
return;
}
2022-05-04 14:32:09 +08:00
// Get target
Player target = getServer().getPlayerByUid(targetUid);
2022-04-17 20:43:07 +08:00
// Check if command
2022-04-19 12:35:01 +08:00
if (PREFIXES.contains(message.charAt(0))) {
CommandMap.getInstance().invoke(player, target, message.substring(1));
2022-04-17 20:43:07 +08:00
return;
}
if (target == null) {
return;
}
// Create chat packet
BasePacket packet = new PacketPrivateChatNotify(player.getUid(), target.getUid(), message);
2022-04-17 20:43:07 +08:00
player.sendPacket(packet);
target.sendPacket(packet);
}
public void sendPrivateMessage(Player player, int targetUid, int emote) {
2022-04-17 20:43:07 +08:00
// Get target
Player target = getServer().getPlayerByUid(targetUid);
2022-04-17 20:43:07 +08:00
if (target == null) {
return;
}
// Create chat packet
BasePacket packet = new PacketPrivateChatNotify(player.getUid(), target.getUid(), emote);
2022-04-17 20:43:07 +08:00
player.sendPacket(packet);
target.sendPacket(packet);
}
public void sendTeamMessage(Player player, int channel, String message) {
2022-04-17 20:43:07 +08:00
// Sanity checks
if (message == null || message.length() == 0) {
return;
}
// Check if command
2022-04-19 12:35:01 +08:00
if (PREFIXES.contains(message.charAt(0))) {
2022-05-04 14:32:09 +08:00
CommandMap.getInstance().invoke(player, null, message);
2022-04-17 20:43:07 +08:00
return;
}
// Create and send chat packet
player.getWorld().broadcastPacket(new PacketPlayerChatNotify(player, channel, message));
}
public void sendTeamMessage(Player player, int channel, int icon) {
2022-04-17 20:43:07 +08:00
// Create and send chat packet
player.getWorld().broadcastPacket(new PacketPlayerChatNotify(player, channel, icon));
}
}