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

145 lines
6.6 KiB
Java
Raw Normal View History

2021-05-13 12:11:59 +00:00
package com.alttd.chat.handlers;
2021-05-22 18:34:32 +00:00
import com.alttd.chat.VelocityChat;
import com.alttd.chat.config.Config;
2021-06-23 19:16:48 +00:00
import com.alttd.chat.managers.ChatUserManager;
2021-06-02 17:55:55 +00:00
import com.alttd.chat.managers.RegexManager;
2021-06-23 19:16:48 +00:00
import com.alttd.chat.objects.ChatUser;
2021-07-27 16:46:58 +00:00
import com.alttd.chat.objects.Mail;
2021-05-22 18:34:32 +00:00
import com.alttd.chat.util.Utility;
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.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
2021-06-23 19:16:48 +00:00
import net.kyori.adventure.text.minimessage.Template;
2021-06-13 11:53:49 +00:00
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
2021-06-23 19:16:48 +00:00
import java.util.*;
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
2021-06-23 19:16:48 +00:00
MiniMessage miniMessage = MiniMessage.get();
2021-05-22 18:34:32 +00:00
2021-06-23 19:16:48 +00:00
List<Template> templates = new ArrayList<>(List.of(
Template.of("sender", senderUser.getDisplayName()),
2021-07-27 16:46:58 +00:00
Template.of("sendername", player.getUsername()),
2021-06-23 19:16:48 +00:00
Template.of("receiver", targetUser.getDisplayName()),
2021-07-27 16:46:58 +00:00
Template.of("receivername", player2.getUsername()),
Template.of("message", GsonComponentSerializer.gson().deserialize(message)),
2021-06-23 19:16:48 +00:00
Template.of("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;
if(player.getCurrentServer().isPresent() && player2.getCurrentServer().isPresent()) {
// redirect to the sender
serverConnection = player.getCurrentServer().get();
Component component = miniMessage.parse(Config.MESSAGESENDER, templates);
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
2021-07-30 01:16:41 +00:00
buf.writeUTF("privatemessage");
2021-07-27 16:46:58 +00:00
buf.writeUTF(player.getUniqueId().toString());
buf.writeUTF(player2.getUsername());
buf.writeUTF(GsonComponentSerializer.gson().serialize(component));
serverConnection.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), buf.toByteArray());
//redirect to the receiver
serverConnection = player2.getCurrentServer().get();
component = miniMessage.parse(Config.MESSAGERECIEVER, templates);
buf = ByteStreams.newDataOutput();
2021-07-30 01:16:41 +00:00
buf.writeUTF("privatemessage");
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-07-27 16:46:58 +00:00
serverConnection.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), buf.toByteArray());
}
2021-05-12 08:43:30 +00:00
2021-07-27 16:46:58 +00:00
// ChatUser targetUser = ChatUserManager.getChatUser(player2.getUniqueId());
//
// MiniMessage miniMessage = MiniMessage.get();
//
// List<Template> templates = new ArrayList<>(List.of(
// Template.of("sender", senderUser.getDisplayName()),
// Template.of("receiver", targetUser.getDisplayName()),
// Template.of("message", message),
// Template.of("server", player.getCurrentServer().isPresent() ? player.getCurrentServer().get().getServerInfo().getName() : "Altitude")));
//
// Component senderMessage = miniMessage.parse(Config.MESSAGESENDER, templates);
// Component receiverMessage = miniMessage.parse(Config.MESSAGERECIEVER, templates);
//
// player.sendMessage(senderMessage);
// player2.sendMessage(receiverMessage);
2021-05-12 08:43:30 +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
VelocityChat.getPlugin().getProxy().getAllPlayers().stream().filter(target -> target.hasPermission("command.proxy.globaladminchat")/*TODO permission*/).forEach(target -> {
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";
}
MiniMessage miniMessage = MiniMessage.get();
2021-07-17 22:48:55 +00:00
List<Template> templates = new ArrayList<>(List.of(
Template.of("message", message),
Template.of("sender", senderName),
Template.of("server", serverName)));
2021-05-24 13:14:11 +00:00
2021-07-17 22:48:55 +00:00
Component component = miniMessage.parse(Config.GACFORMAT, templates);
2021-05-24 13:14:11 +00:00
VelocityChat.getPlugin().getProxy().getAllPlayers().stream().filter(target -> target.hasPermission("command.proxy.globaladminchat")/*TODO permission*/).forEach(target -> {
target.sendMessage(component);
});
}
2021-05-22 18:34:32 +00:00
/**
* constructs a mail object and notifies all involved players about it
* / mail send playerA,playerB,playerC message
*/
2021-05-24 07:53:45 +00:00
public void sendMail(CommandSource commandSource, String recipient, String message) {
2021-07-27 16:46:58 +00:00
UUID uuid;
if (commandSource instanceof Player) {
uuid = ((Player) commandSource).getUniqueId();
} else {
uuid = Config.CONSOLEUUID;
}
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(recipient);
//Mail mail = new Mail()
2021-05-24 07:53:45 +00:00
// todo construct the mail and notify the player if online?
2021-05-22 18:34:32 +00:00
}
2021-05-24 07:53:45 +00:00
public void readMail(CommandSource commandSource, String targetPlayer, boolean unread) {
}
public void readMail(CommandSource commandSource, boolean unread) {
2021-05-22 18:34:32 +00:00
}
2021-05-24 07:53:45 +00:00
2021-06-27 17:58:34 +00:00
}