Chat/velocity/src/main/java/com/alttd/velocitychat/handlers/ChatHandler.java

373 lines
17 KiB
Java
Raw Normal View History

2021-12-11 18:35:35 +00:00
package com.alttd.velocitychat.handlers;
import com.alttd.chat.config.Config;
2022-01-29 22:21:35 +00:00
import com.alttd.chat.database.Queries;
2021-06-23 19:16:48 +00:00
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
2022-01-30 01:12:09 +00:00
import com.alttd.chat.managers.RegexManager;
2022-09-30 09:20:59 +00:00
import com.alttd.chat.objects.*;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.objects.chat_log.ChatLogType;
import com.alttd.chat.util.ALogger;
2022-01-29 13:53:47 +00:00
import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.VelocityChat;
2021-07-27 16:46:58 +00:00
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
2021-07-27 16:46:58 +00:00
import com.velocitypowered.api.proxy.ServerConnection;
import net.kyori.adventure.text.Component;
2025-06-20 22:53:55 +00:00
import net.kyori.adventure.text.ComponentLike;
2022-05-26 22:31:36 +00:00
import net.kyori.adventure.text.TextReplacementConfig;
2022-09-30 09:20:59 +00:00
import net.kyori.adventure.text.minimessage.MiniMessage;
2022-02-19 14:14:41 +00:00
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
2021-06-13 11:53:49 +00:00
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
2023-07-10 21:15:08 +00:00
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.jetbrains.annotations.Nullable;
2022-06-02 01:48:51 +00:00
import java.time.Duration;
2025-06-20 22:53:55 +00:00
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class ChatHandler {
private final ChatLogHandler chatLogHandler;
public ChatHandler(ChatLogHandler chatLogHandler) {
this.chatLogHandler = chatLogHandler;
}
2021-06-23 19:16:48 +00:00
public void privateMessage(String sender, String target, String message) {
UUID uuid = UUID.fromString(sender);
ChatUser senderUser = ChatUserManager.getChatUser(uuid);
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(uuid);
2025-06-20 22:53:55 +00:00
if (optionalPlayer.isEmpty()) {
return;
}
2021-06-23 19:16:48 +00:00
Player player = optionalPlayer.get();
2021-05-12 08:43:30 +00:00
2021-06-23 19:16:48 +00:00
Optional<Player> optionalPlayer2 = VelocityChat.getPlugin().getProxy().getPlayer(target);
2025-06-20 22:53:55 +00:00
if (optionalPlayer2.isEmpty()) {
return;
}
2021-06-23 19:16:48 +00:00
Player player2 = optionalPlayer2.get();
ChatUser targetUser = ChatUserManager.getChatUser(player2.getUniqueId());
2021-05-22 18:34:32 +00:00
2022-03-14 15:48:35 +00:00
TagResolver Placeholders = TagResolver.resolver(
2022-02-19 14:14:41 +00:00
Placeholder.component("sender", senderUser.getDisplayName()),
Placeholder.unparsed("sendername", player.getUsername()),
Placeholder.component("receiver", targetUser.getDisplayName()),
Placeholder.unparsed("receivername", player2.getUsername()),
Placeholder.component("message", GsonComponentSerializer.gson().deserialize(message)),
Placeholder.unparsed("server",
player.getCurrentServer().isPresent() ? player.getCurrentServer()
.get()
.getServerInfo()
.getName() : "Altitude"
)
);
2021-05-12 08:43:30 +00:00
2021-07-27 16:46:58 +00:00
ServerConnection serverConnection;
//Log handled on receiving server since it is only received on one server and that's where the ignore check happens
2025-06-20 22:53:55 +00:00
if (player.getCurrentServer().isPresent() && player2.getCurrentServer().isPresent()) {
2021-07-27 16:46:58 +00:00
// redirect to the sender
serverConnection = player.getCurrentServer().get();
Component component = Utility.parseMiniMessage(Config.MESSAGESENDER
.replaceAll("<sendername>", player.getUsername())
.replaceAll("<receivername>", player2.getUsername()), Placeholders
).asComponent();
2021-07-27 16:46:58 +00:00
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("privatemessageout");
2021-07-27 16:46:58 +00:00
buf.writeUTF(player.getUniqueId().toString());
buf.writeUTF(player2.getUsername());
buf.writeUTF(GsonComponentSerializer.gson().serialize(component));
2021-08-23 09:56:31 +00:00
buf.writeUTF(player2.getUniqueId().toString());
2021-07-27 16:46:58 +00:00
serverConnection.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), buf.toByteArray());
//redirect to the receiver
serverConnection = player2.getCurrentServer().get();
component = Utility.parseMiniMessage(Config.MESSAGERECIEVER
.replaceAll("<sendername>", player.getUsername())
.replaceAll("<receivername>", player2.getUsername()), Placeholders
).asComponent();
2021-07-27 16:46:58 +00:00
buf = ByteStreams.newDataOutput();
buf.writeUTF("privatemessagein");
2021-07-27 16:46:58 +00:00
buf.writeUTF(player2.getUniqueId().toString());
buf.writeUTF(player.getUsername());
2021-07-30 01:16:41 +00:00
buf.writeUTF(GsonComponentSerializer.gson().serialize(component));
2021-08-23 09:56:31 +00:00
buf.writeUTF(player.getUniqueId().toString());
2021-07-27 16:46:58 +00:00
serverConnection.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), buf.toByteArray());
}
2021-05-12 08:43:30 +00:00
}
public static void sendBlockedNotification(String prefix, Player player, String input, String target, ServerConnection serverConnection) {
2022-03-14 15:48:35 +00:00
TagResolver Placeholders = TagResolver.resolver(
Placeholder.unparsed("prefix", prefix),
2022-02-19 14:14:41 +00:00
Placeholder.parsed("displayname", Utility.getDisplayName(player.getUniqueId(), player.getUsername())),
2022-03-14 15:48:35 +00:00
Placeholder.unparsed("target", (target.isEmpty() ? " tried to say: " : " -> " + target + ": ")),
Placeholder.unparsed("input", input)
);
2025-06-20 22:53:55 +00:00
ComponentLike blockedNotification = Utility.parseMiniMessage(Config.NOTIFICATIONFORMAT, Placeholders);
2025-06-20 22:53:55 +00:00
serverConnection.getServer().getPlayersConnected().forEach(pl -> {
if (pl.hasPermission("chat.alert-blocked")) {
pl.sendMessage(blockedNotification);
}
});
player.sendMessage(Utility.parseMiniMessage("<red>The language you used in your message is not allowed, " +
2025-06-20 22:53:55 +00:00
"this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>"));
}
2025-06-20 22:53:55 +00:00
public void sendPartyMessage(Party party, Component message, @Nullable List<UUID> ignoredPlayers) {
VelocityChat.getPlugin().getProxy().getAllPlayers().stream()
.filter(pl -> {
UUID uuid = pl.getUniqueId();
2025-06-20 22:53:55 +00:00
if (ignoredPlayers != null && ignoredPlayers.contains(uuid)) {
return false;
2025-06-20 22:53:55 +00:00
}
return party.getPartyUsers().stream().anyMatch(pu -> pu.getUuid().equals(uuid));
}).forEach(pl -> {
pl.sendMessage(message);
2022-09-30 09:20:59 +00:00
// TODO forward sound to backend server.
// https://canary.discord.com/channels/514920774923059209/1020498592219271189
});
}
public void sendPartyMessage(UUID uuid, String message, Component item, ServerConnection serverConnection) {
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(uuid);
2025-06-20 22:53:55 +00:00
if (optionalPlayer.isEmpty()) {
return;
}
Player player = optionalPlayer.get();
ChatUser user = ChatUserManager.getChatUser(uuid);
Party party = PartyManager.getParty(user.getPartyId());
if (party == null) {
player.sendMessage(Utility.parseMiniMessage(Config.NOT_IN_A_PARTY));
return;
}
2025-06-20 22:53:55 +00:00
ComponentLike senderName = user.getDisplayName();
2022-01-30 01:12:09 +00:00
TagResolver placeholders = TagResolver.resolver(
2022-02-19 14:14:41 +00:00
Placeholder.component("sender", senderName),
2022-03-14 15:48:35 +00:00
Placeholder.component("sendername", senderName),
Placeholder.unparsed("partyname", party.getPartyName()),
Placeholder.component("message", parseMessageContent(player, message)),
2022-05-26 22:31:36 +00:00
Placeholder.unparsed("server", serverConnection.getServer().getServerInfo().getName())
);
2022-01-30 01:12:09 +00:00
Component partyMessage = Utility.parseMiniMessage(Config.PARTY_FORMAT, placeholders).asComponent()
.replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(item).build());
ModifiableString modifiableString = new ModifiableString(partyMessage);
if (!RegexManager.filterText(player.getUsername(), uuid, modifiableString, "party")) {
sendBlockedNotification("Party Language", player, message, "", serverConnection);
return; // the message was blocked
}
partyMessage = modifiableString.component();
sendPartyMessage(party, partyMessage, user.getIgnoredBy());
2022-01-30 01:12:09 +00:00
chatLogHandler.addChatLog(uuid,
serverConnection.getServer().getServerInfo().getName(),
PlainTextComponentSerializer.plainText().serialize(partyMessage),
ChatLogType.PARTY,
String.valueOf(party.getPartyId()),
null,
partyMessage,
false
);
ComponentLike spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, placeholders);
2025-06-20 22:53:55 +00:00
for (Player pl : serverConnection.getServer().getPlayersConnected()) {
if (pl.hasPermission(Config.SPYPERMISSION) && !party.getPartyUsersUuid().contains(pl.getUniqueId())) {
2022-01-30 01:12:09 +00:00
pl.sendMessage(spyMessage);
}
}
2023-07-10 21:15:08 +00:00
ALogger.info(PlainTextComponentSerializer.plainText().serialize(partyMessage));
2022-01-30 01:12:09 +00:00
}
2021-06-06 19:32:13 +00:00
public void globalAdminChat(String message) {
2021-06-13 11:53:49 +00:00
Component component = GsonComponentSerializer.gson().deserialize(message);
2021-06-06 19:32:13 +00:00
2025-06-20 22:53:55 +00:00
VelocityChat.getPlugin().getProxy().getAllPlayers()
.stream()
.filter(target -> target.hasPermission("command.chat.globaladminchat"))
.forEach(target -> target.sendMessage(component));
chatLogHandler.addChatLog(
new UUID(0, 0), //No origin uuid for GAC
"GAC", //No server for GAC
PlainTextComponentSerializer.plainText().serialize(component),
ChatLogType.GAC,
null,
null,
component,
false
);
2021-06-06 19:32:13 +00:00
}
2021-05-24 13:14:11 +00:00
public void globalAdminChat(CommandSource commandSource, String message) {
2025-06-20 22:53:55 +00:00
ComponentLike senderName = Component.text(Config.CONSOLENAME);
2021-05-24 13:14:11 +00:00
String serverName = "Altitude";
2025-06-20 22:53:55 +00:00
if (commandSource instanceof Player sender) {
2021-07-17 22:48:55 +00:00
ChatUser user = ChatUserManager.getChatUser(sender.getUniqueId());
2025-06-20 22:53:55 +00:00
if (user == null) {
return;
}
2021-07-17 22:48:55 +00:00
senderName = user.getDisplayName();
serverName = sender.getCurrentServer().isPresent() ? sender.getCurrentServer()
.get()
.getServerInfo()
.getName() : "Altitude";
2021-05-24 13:14:11 +00:00
}
2022-03-14 15:48:35 +00:00
TagResolver Placeholders = TagResolver.resolver(
2022-09-30 09:20:59 +00:00
Placeholder.component("message", parseMessageContent(commandSource, message)),
2022-02-19 14:14:41 +00:00
Placeholder.component("sender", senderName),
Placeholder.unparsed("server", serverName)
);
2021-05-24 13:14:11 +00:00
2025-06-20 22:53:55 +00:00
ComponentLike component = Utility.parseMiniMessage(Config.GACFORMAT, Placeholders);
2021-05-24 13:14:11 +00:00
2025-06-20 22:53:55 +00:00
VelocityChat.getPlugin().getProxy().getAllPlayers()
.stream()
.filter(target -> target.hasPermission("command.chat.globaladminchat"))
.forEach(target -> target.sendMessage(component));
2021-05-24 13:14:11 +00:00
}
2021-05-24 07:53:45 +00:00
public void sendMail(CommandSource commandSource, String recipient, String message) {
2025-06-20 22:53:55 +00:00
UUID uuid = Config.CONSOLEUUID;
2022-01-29 13:53:47 +00:00
String senderName = Config.CONSOLENAME;
UUID targetUUID;
if (commandSource instanceof Player player) {
uuid = player.getUniqueId();
senderName = player.getUsername();
2021-07-27 16:46:58 +00:00
}
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(recipient);
2022-01-29 13:53:47 +00:00
if (optionalPlayer.isEmpty()) {
targetUUID = ServerHandler.getPlayerUUID(recipient);
if (targetUUID == null) {
commandSource.sendMessage(Utility.parseMiniMessage(
"<red>A player with this name hasn't logged in recently.")); // TOOD load from config
2022-01-29 13:53:47 +00:00
return;
}
} else {
targetUUID = optionalPlayer.get().getUniqueId();
}
2022-06-02 01:48:51 +00:00
Mail mail = new Mail(targetUUID, uuid, message);
2022-01-29 13:53:47 +00:00
ChatUser chatUser = ChatUserManager.getChatUser(targetUUID);
if (chatUser.getIgnoredPlayers().contains(uuid)) {
commandSource.sendMessage(Utility.parseMiniMessage("<red>You cannot mail this player</red>"));
return;
}
2022-01-29 13:53:47 +00:00
chatUser.addMail(mail);
// TODO load from config
2022-03-14 15:48:35 +00:00
String finalSenderName = senderName;
optionalPlayer.ifPresent(player -> player.sendMessage(Utility.parseMiniMessage("<yellow>New mail from " + finalSenderName)));
commandSource.sendMessage(Utility.parseMiniMessage("<yellow>Sent mail to " + recipient + "!"));
2021-05-22 18:34:32 +00:00
}
public void readMail(CommandSource commandSource, String targetPlayer, String senderPlayer) {
2022-01-29 13:53:47 +00:00
UUID uuid = ServerHandler.getPlayerUUID(targetPlayer);
if (uuid == null) {
2022-01-29 22:21:35 +00:00
commandSource.sendMessage(Utility.parseMiniMessage(Config.mailNoUser));
2022-01-29 13:53:47 +00:00
return;
}
2022-01-29 13:53:47 +00:00
ChatUser chatUser = ChatUserManager.getChatUser(uuid);
if (senderPlayer == null) {
commandSource.sendMessage(parseMails(chatUser.getMails(), false));
}
UUID sender = ServerHandler.getPlayerUUID(senderPlayer);
if (sender == null) {
commandSource.sendMessage(Utility.parseMiniMessage(Config.mailNoUser));
return;
}
List<Mail> mails = chatUser.getMails().stream()
.filter(mail -> mail.getSender().equals(sender))
.toList();
commandSource.sendMessage(parseMails(mails, false));
2021-05-24 07:53:45 +00:00
}
public void readMail(CommandSource commandSource, boolean unread) {
2022-01-29 13:53:47 +00:00
if (commandSource instanceof Player player) {
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
2022-01-29 22:21:35 +00:00
commandSource.sendMessage(parseMails(unread ? chatUser.getUnReadMail() : chatUser.getMails(), unread));
2022-01-29 13:53:47 +00:00
}
}
2021-05-24 07:53:45 +00:00
2022-01-29 22:21:35 +00:00
private Component parseMails(List<Mail> mails, boolean mark) {
2025-06-20 22:53:55 +00:00
Component component = Utility.parseMiniMessage(Config.mailHeader).asComponent();
2022-01-29 13:53:47 +00:00
for (Mail mail : mails) {
2022-01-29 22:21:35 +00:00
if (mail.isUnRead() && mark) {
mail.setReadTime(System.currentTimeMillis());
Queries.markMailRead(mail);
}
2022-06-02 01:48:51 +00:00
Date date = new Date(mail.getSendTime());
2022-01-29 13:53:47 +00:00
ChatUser chatUser = ChatUserManager.getChatUser(mail.getSender());
2022-03-14 15:48:35 +00:00
TagResolver Placeholders = TagResolver.resolver(
2022-02-19 14:14:41 +00:00
Placeholder.component("staffprefix", chatUser.getStaffPrefix()),
Placeholder.component("sender", chatUser.getDisplayName()),
2022-05-30 20:10:54 +00:00
Placeholder.component("message", Utility.parseMiniMessage(mail.getMessage())),
2022-06-02 01:48:51 +00:00
Placeholder.unparsed("date", date.toString()),
Placeholder.unparsed("time_ago",
getTimeAgo(Duration.between(date.toInstant(), new Date().toInstant()))
)
);
2025-06-20 22:53:55 +00:00
ComponentLike mailMessage = Utility.parseMiniMessage(Config.mailBody, Placeholders);
2022-01-29 13:53:47 +00:00
component = component.append(Component.newline()).append(mailMessage);
}
2022-01-29 22:21:35 +00:00
component = component.append(Component.newline()).append(Utility.parseMiniMessage(Config.mailFooter));
2022-01-29 13:53:47 +00:00
return component;
2021-05-22 18:34:32 +00:00
}
2021-05-24 07:53:45 +00:00
2022-01-05 14:25:17 +00:00
public void mutePlayer(String uuid, boolean muted) {
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("chatpunishments");
buf.writeUTF(uuid);
buf.writeBoolean(muted);
}
2022-06-02 01:48:51 +00:00
private String getTimeAgo(Duration duration) {
StringBuilder stringBuilder = new StringBuilder();
2025-06-20 22:53:55 +00:00
if (duration.toDays() != 0) {
2022-06-02 01:48:51 +00:00
stringBuilder.append(duration.toDays()).append("d ");
2025-06-20 22:53:55 +00:00
}
if (duration.toHoursPart() != 0 || !stringBuilder.isEmpty()) {
2022-06-02 01:48:51 +00:00
stringBuilder.append(duration.toHoursPart()).append("h ");
2025-06-20 22:53:55 +00:00
}
2022-06-02 01:48:51 +00:00
stringBuilder.append(duration.toMinutesPart()).append("m ago");
return stringBuilder.toString();
}
2022-09-30 09:20:59 +00:00
private Component parseMessageContent(CommandSource source, String rawMessage) {
TagResolver.Builder tagResolver = TagResolver.builder();
Utility.formattingPerms.forEach((perm, pair) -> {
if (source.hasPermission(perm)) {
tagResolver.resolver(pair.getX());
}
});
MiniMessage miniMessage = MiniMessage.builder().tags(tagResolver.build()).build();
Component component = miniMessage.deserialize(rawMessage);
2025-06-20 22:53:55 +00:00
for (ChatFilter chatFilter : RegexManager.getEmoteFilters()) {
2022-09-30 09:20:59 +00:00
component = component.replaceText(
TextReplacementConfig.builder()
.times(Config.EMOTELIMIT)
.match(chatFilter.getRegex())
.replacement(chatFilter.getReplacement()).build());
}
return component;
}
}