Rework private messaging

This commit is contained in:
destro174 2021-06-23 21:16:48 +02:00
parent bbfbfbb6f7
commit 32c07a4b14
4 changed files with 71 additions and 47 deletions

View File

@ -14,6 +14,7 @@ import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -34,6 +35,33 @@ public class ChatHandler {
GCNOTENABLED = miniMessage.parse(Config.GCNOTENABLED);
}
public void privateMessage(Player player, String target, String message) {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
Component senderName = player.displayName();
String prefix = user.getPrefix();
message = RegexManager.replaceText(message); // todo a better way for this
if(message == null) return; // the message was blocked
if(!player.hasPermission("chat.format")) {
message = miniMessage.stripTokens(message);
} else {
message = Utility.parseColors(message);
}
if(message.contains("[i]"))
message = message.replace("[i]", "<[i]>");
List<Template> templates = new ArrayList<>(List.of(
Template.of("message", message),
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand())))); // yes cross server [i];)
Component component = miniMessage.parse("<message>", templates);
sendPrivateMessage(player, target, "privatemessage", component);
}
public void globalChat(Player player, String message) {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
if(user == null) return;
@ -77,7 +105,16 @@ public class ChatHandler {
private void sendPluginMessage(Player player, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
out.writeUTF(GsonComponentSerializer.gson().serialize(component)); // todo use a better component serializer ~ look into kyori
out.writeUTF(GsonComponentSerializer.gson().serialize(component));
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
private void sendPrivateMessage(Player player, String target, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
out.writeUTF(player.getUniqueId().toString());
out.writeUTF(target);
out.writeUTF(GsonComponentSerializer.gson().serialize(component));
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}

View File

@ -21,7 +21,7 @@ import java.util.Optional;
public class Message {
public Message(ProxyServer proxyServer) {
/*public Message(ProxyServer proxyServer) {
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
.<CommandSource>literal("message")
.requires(ctx -> ctx.hasPermission("command.proxy.message"))// TODO permission system? load permissions from config?
@ -29,7 +29,7 @@ public class Message {
.<CommandSource, String>argument("player", StringArgumentType.word())
.suggests((context, builder) -> {
Collection<String> possibleValues = new ArrayList<>();
for (Player player : proxyServer.getAllPlayers()) { // todo all chatplayers? this can be heavy
for (Player player : proxyServer.getAllPlayers()) {
possibleValues.add(player.getGameProfile().getName());
}
if(possibleValues.isEmpty()) return Suggestions.empty();
@ -48,14 +48,10 @@ public class Message {
if (playerOptional.isPresent()) {
Player receiver = playerOptional.get();
proxyServer.getEventManager().fire(new PrivateMessageEvent(context.getSource(), receiver, context.getArgument("message", String.class))).thenAccept((event) -> {
if(event.getResult() == ResultedEvent.GenericResult.allowed()) {
VelocityChat.getPlugin().getChatHandler().privateMessage(context.getSource(), receiver, context.getArgument("message", String.class));
}
});
VelocityChat.getPlugin().getChatHandler().privateMessage(context.getSource(), receiver, context.getArgument("message", String.class));
return 1;
} else {
// TODO NOBODY TO REPLY TO
// TODO wrong player message?
}
return 0;
})
@ -76,6 +72,6 @@ public class Message {
CommandMeta meta = metaBuilder.build();
proxyServer.getCommandManager().register(meta, brigadierCommand);
}
}*/
}

View File

@ -2,60 +2,48 @@ package com.alttd.chat.handlers;
import com.alttd.chat.VelocityChat;
import com.alttd.chat.config.Config;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
public class ChatHandler {
public void privateMessage(CommandSource commandSource, Player recipient, String message) {
// todo get the chatUserinstance of both players and or console - @teri should console be able to /msg?
String senderName;
String receiverName;
if (commandSource instanceof Player) {
Player sender = (Player) commandSource;
senderName = sender.getUsername();
//plugin.getChatHandler().getChatPlayer(sender.getUniqueId()).setReplyTarget(event.getRecipient().getUniqueId()); // TODO this needs to be cleaner
} else {
senderName = Config.CONSOLENAME;
}
receiverName = recipient.getUsername();
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();
Optional<Player> optionalPlayer2 = VelocityChat.getPlugin().getProxy().getPlayer(target);
if(optionalPlayer2.isEmpty()) return;
Player player2 = optionalPlayer2.get();
ChatUser targetUser = ChatUserManager.getChatUser(player2.getUniqueId());
MiniMessage miniMessage = MiniMessage.get();
message = Utility.parseColors(message);
if(!commandSource.hasPermission("chat.format")) // Todo PR fix for '<3' to minimessage
message = miniMessage.stripTokens(message);
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")));
message = RegexManager.replaceText(message); // this filters the message TODO should staff be able to bypass filters?
Component component = miniMessage.parse("<message>", templates);
// List<Template> templates = new ArrayList<>(List.of(
// Template.of("sender", user.getDisplayName()),
// Template.of("prefix", user.getPrefix()),
// Template.of("message", message),
// Template.of("server", player.getCurrentServer().isPresent() ? player.getCurrentServer().get().getServerInfo().getName() : "Altitude")
// /*,Template.of("[i]" , itemComponent(sender.getInventory().getItemInMainHand()))*/ //Todo move this into ChatFilters
// ));
Component senderMessage = miniMessage.parse(Config.MESSAGESENDER, templates);
Component receiverMessage = miniMessage.parse(Config.MESSAGERECIEVER, templates);
Map<String, String> map = new HashMap<>();
map.put("sender", senderName);
map.put("receiver", receiverName);
map.put("message", message);
// map.put("server", event.getRecipient().getCurrentServer().isPresent() ? event.getRecipient().getCurrentServer().get().getServerInfo().getName() : "Altitude");
Component senderMessage = miniMessage.parse(Config.MESSAGESENDER, map);
Component receiverMessage = miniMessage.parse(Config.MESSAGERECIEVER, map);
commandSource.sendMessage(senderMessage);
recipient.sendMessage(receiverMessage);
player.sendMessage(senderMessage);
player2.sendMessage(receiverMessage);
}
public void globalAdminChat(String message) {

View File

@ -38,6 +38,9 @@ public class PluginMessageListener {
case "globaladminchat":
VelocityChat.getPlugin().getChatHandler().globalAdminChat(in.readUTF());
break;
case "privatemessage":
VelocityChat.getPlugin().getChatHandler().privateMessage(in.readUTF(), in.readUTF(), in.readUTF());
break;
default:
VelocityChat.getPlugin().getLogger().info("server " + event.getSource());
break;