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

274 lines
14 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;
2021-06-23 19:16:48 +00:00
import com.alttd.chat.objects.ChatUser;
2022-01-29 13:53:47 +00:00
import com.alttd.chat.objects.Mail;
import com.alttd.chat.objects.Party;
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;
2022-02-05 20:16:20 +00:00
import net.kyori.adventure.text.minimessage.placeholder.Replacement;
2021-06-13 11:53:49 +00:00
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jetbrains.annotations.Nullable;
2021-06-23 19:16:48 +00:00
import java.util.*;
import java.util.concurrent.TimeUnit;
public class ChatHandler {
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);
if(optionalPlayer.isEmpty()) return;
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);
if(optionalPlayer2.isEmpty()) return;
Player player2 = optionalPlayer2.get();
ChatUser targetUser = ChatUserManager.getChatUser(player2.getUniqueId());
2021-05-22 18:34:32 +00:00
2022-02-05 20:16:20 +00:00
Map<String, Replacement<?>> placeholders = new HashMap<>();
placeholders.put("sender", Replacement.component(senderUser.getDisplayName()));
placeholders.put("sendername", Replacement.miniMessage(player.getUsername()));
placeholders.put("receiver", Replacement.component(targetUser.getDisplayName()));
placeholders.put("receivername", Replacement.miniMessage(player2.getUsername()));
placeholders.put("message", Replacement.component(GsonComponentSerializer.gson().deserialize(message)));
placeholders.put("server", Replacement.miniMessage(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;
if(player.getCurrentServer().isPresent() && player2.getCurrentServer().isPresent()) {
// redirect to the sender
serverConnection = player.getCurrentServer().get();
2022-02-05 20:16:20 +00:00
Component component = Utility.parseMiniMessage(Config.MESSAGESENDER, placeholders);
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();
2022-02-05 20:16:20 +00:00
component = Utility.parseMiniMessage(Config.MESSAGERECIEVER, placeholders);
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-02-05 20:16:20 +00:00
Map<String, Replacement<?>> placeholders = new HashMap<>();
placeholders.put("prefix", Replacement.miniMessage(prefix));
placeholders.put("displayname", Replacement.miniMessage(Utility.getDisplayName(player.getUniqueId(), player.getUsername())));
placeholders.put("target", Replacement.miniMessage((target.isEmpty() ? " tried to say: " : " -> " + target + ": ")));
placeholders.put("input", Replacement.miniMessage(input));
Component blockedNotification = Utility.parseMiniMessage(Config.NOTIFICATIONFORMAT, placeholders);
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, " +
"this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>"));
}
public void sendPartyMessage(Party party, Component message, @Nullable List<UUID> ignoredPlayers)
{
VelocityChat.getPlugin().getProxy().getAllPlayers().stream()
.filter(pl -> {
UUID uuid = pl.getUniqueId();
if (ignoredPlayers != null && ignoredPlayers.contains(uuid))
return false;
return party.getPartyUsers().stream().anyMatch(pu -> pu.getUuid().equals(uuid));
}).forEach(pl -> {
pl.sendMessage(message);
});
}
public void sendPartyMessage(UUID uuid, String message, Component item, ServerConnection serverConnection) {
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(uuid);
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;
}
2022-01-30 01:12:09 +00:00
Component senderName = user.getDisplayName();
String updatedMessage = RegexManager.replaceText(player.getUsername(), uuid, message);
2022-01-30 01:12:09 +00:00
if(updatedMessage == null) {
sendBlockedNotification("Party Language", player, message, "", serverConnection);
2022-01-30 01:12:09 +00:00
return; // the message was blocked
}
if(!player.hasPermission("chat.format")) {
updatedMessage = Utility.stripTokens(updatedMessage);
}
if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replace("[i]", "<[i]>");
updatedMessage = Utility.formatText(updatedMessage);
2022-02-05 20:16:20 +00:00
Map<String, Replacement<?>> placeholders = new HashMap<>();
placeholders.put("sender", Replacement.component(senderName));
placeholders.put("party", Replacement.miniMessage(party.getPartyName()));
placeholders.put("message", Replacement.miniMessage(updatedMessage));
placeholders.put("server", Replacement.miniMessage(serverConnection.getServer().getServerInfo().getName()));
placeholders.put("[i]", Replacement.component(item));
2022-01-30 01:12:09 +00:00
2022-02-05 20:16:20 +00:00
Component partyMessage = Utility.parseMiniMessage(Config.PARTY_FORMAT, placeholders);
sendPartyMessage(party, partyMessage, user.getIgnoredBy());
2022-01-30 01:12:09 +00:00
2022-02-05 20:16:20 +00:00
Component spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, placeholders);
2022-01-30 01:12:09 +00:00
for(Player pl : serverConnection.getServer().getPlayersConnected()) {
if(pl.hasPermission(Config.SPYPERMISSION) && !party.getPartyUsersUuid().contains(pl.getUniqueId())) {
pl.sendMessage(spyMessage);
}
}
}
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
VelocityChat.getPlugin().getProxy().getAllPlayers().stream().filter(target -> target.hasPermission("command.chat.globaladminchat")/*TODO permission*/).forEach(target -> {
2021-06-06 19:32:13 +00:00
target.sendMessage(component);
});
}
2021-05-24 13:14:11 +00:00
public void globalAdminChat(CommandSource commandSource, String message) {
2021-07-17 22:48:55 +00:00
Component senderName = Component.text(Config.CONSOLENAME);
2021-05-24 13:14:11 +00:00
String serverName = "Altitude";
if (commandSource instanceof Player) {
Player sender = (Player) commandSource;
2021-07-17 22:48:55 +00:00
ChatUser user = ChatUserManager.getChatUser(sender.getUniqueId());
if(user == null) return;
senderName = user.getDisplayName();
2021-05-24 13:14:11 +00:00
serverName = sender.getCurrentServer().isPresent() ? sender.getCurrentServer().get().getServerInfo().getName() : "Altitude";
}
2022-02-05 20:16:20 +00:00
Map<String, Replacement<?>> placeholders = new HashMap<>();
placeholders.put("message", Replacement.miniMessage(Utility.formatText(message)));
placeholders.put("sender", Replacement.component(senderName));
placeholders.put("server", Replacement.miniMessage(serverName));
2021-05-24 13:14:11 +00:00
2022-02-05 20:16:20 +00:00
Component component = Utility.parseMiniMessage(Config.GACFORMAT, placeholders);
2021-05-24 13:14:11 +00:00
VelocityChat.getPlugin().getProxy().getAllPlayers().stream().filter(target -> target.hasPermission("command.chat.globaladminchat")/*TODO permission*/).forEach(target -> {
2021-05-24 13:14:11 +00:00
target.sendMessage(component);
});
}
2021-05-24 07:53:45 +00:00
public void sendMail(CommandSource commandSource, String recipient, String message) {
2022-01-29 13:53:47 +00:00
UUID uuid = Config.CONSOLEUUID;;
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
}
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
return;
}
} else {
targetUUID = optionalPlayer.get().getUniqueId();
}
if (!commandSource.hasPermission("chat.format"))
message = Utility.stripTokens(message);
else
message = Utility.parseColors(message);
Mail mail = new Mail(targetUUID, uuid, message);
2022-01-29 13:53:47 +00:00
ChatUser chatUser = ChatUserManager.getChatUser(targetUUID);
chatUser.addMail(mail);
// TODO load from config
2022-02-05 20:16:20 +00:00
Map<String, Replacement<?>> placeholders = new HashMap<>();
placeholders.put("sender", Replacement.miniMessage(senderName));
optionalPlayer.ifPresent(player -> player.sendMessage(Utility.parseMiniMessage(Config.mailReceived, placeholders)));
2021-05-22 18:34:32 +00:00
}
2022-01-29 22:21:35 +00:00
public void readMail(CommandSource commandSource, String targetPlayer) {
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;
}
ChatUser chatUser = ChatUserManager.getChatUser(uuid);
2022-01-29 22:21:35 +00:00
commandSource.sendMessage(parseMails(chatUser.getMails(), 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) {
Component component = Utility.parseMiniMessage(Config.mailHeader);
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-01-29 13:53:47 +00:00
ChatUser chatUser = ChatUserManager.getChatUser(mail.getSender());
Date sentTime = new Date(mail.getSendTime());
2022-02-05 20:16:20 +00:00
Map<String, Replacement<?>> placeholders = new HashMap<>();
placeholders.put("staffprefix", Replacement.component(chatUser.getStaffPrefix()));
placeholders.put("sender", Replacement.component(chatUser.getDisplayName()));
placeholders.put("message", Replacement.miniMessage(mail.getMessage()));
placeholders.put("date", Replacement.miniMessage(sentTime.toString()));
placeholders.put("time_ago", Replacement.miniMessage(String.valueOf(TimeUnit.MILLISECONDS.toDays(new Date().getTime() - sentTime.getTime()))));
Component 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
public void partyChat(String partyId, UUID uuid, Component message) {
Party party = PartyManager.getParty(Integer.parseInt(partyId));
if (party == null) {
ALogger.warn("Received a non existent party");
return;
}
List<UUID> ignoredPlayers = ChatUserManager.getChatUser(uuid).getIgnoredPlayers();
List<UUID> partyUsersUuid = party.getPartyUsersUuid();
VelocityChat.getPlugin().getProxy().getAllPlayers().stream()
.filter(p -> partyUsersUuid.contains(p.getUniqueId()))
.filter(p -> !ignoredPlayers.contains(p.getUniqueId()))
.forEach(p -> p.sendMessage(message));
}
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);
}
2021-06-27 17:58:34 +00:00
}