Rework private messaging
This commit is contained in:
parent
bbfbfbb6f7
commit
32c07a4b14
|
|
@ -14,6 +14,7 @@ import net.kyori.adventure.text.minimessage.Template;
|
||||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
|
@ -34,6 +35,33 @@ public class ChatHandler {
|
||||||
GCNOTENABLED = miniMessage.parse(Config.GCNOTENABLED);
|
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) {
|
public void globalChat(Player player, String message) {
|
||||||
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
|
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
|
||||||
if(user == null) return;
|
if(user == null) return;
|
||||||
|
|
@ -77,7 +105,16 @@ public class ChatHandler {
|
||||||
private void sendPluginMessage(Player player, String channel, Component component) {
|
private void sendPluginMessage(Player player, String channel, Component component) {
|
||||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||||
out.writeUTF(channel);
|
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());
|
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ import java.util.Optional;
|
||||||
|
|
||||||
public class Message {
|
public class Message {
|
||||||
|
|
||||||
public Message(ProxyServer proxyServer) {
|
/*public Message(ProxyServer proxyServer) {
|
||||||
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
|
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
|
||||||
.<CommandSource>literal("message")
|
.<CommandSource>literal("message")
|
||||||
.requires(ctx -> ctx.hasPermission("command.proxy.message"))// TODO permission system? load permissions from config?
|
.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())
|
.<CommandSource, String>argument("player", StringArgumentType.word())
|
||||||
.suggests((context, builder) -> {
|
.suggests((context, builder) -> {
|
||||||
Collection<String> possibleValues = new ArrayList<>();
|
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());
|
possibleValues.add(player.getGameProfile().getName());
|
||||||
}
|
}
|
||||||
if(possibleValues.isEmpty()) return Suggestions.empty();
|
if(possibleValues.isEmpty()) return Suggestions.empty();
|
||||||
|
|
@ -48,14 +48,10 @@ public class Message {
|
||||||
|
|
||||||
if (playerOptional.isPresent()) {
|
if (playerOptional.isPresent()) {
|
||||||
Player receiver = playerOptional.get();
|
Player receiver = playerOptional.get();
|
||||||
proxyServer.getEventManager().fire(new PrivateMessageEvent(context.getSource(), receiver, context.getArgument("message", String.class))).thenAccept((event) -> {
|
VelocityChat.getPlugin().getChatHandler().privateMessage(context.getSource(), receiver, context.getArgument("message", String.class));
|
||||||
if(event.getResult() == ResultedEvent.GenericResult.allowed()) {
|
|
||||||
VelocityChat.getPlugin().getChatHandler().privateMessage(context.getSource(), receiver, context.getArgument("message", String.class));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
// TODO NOBODY TO REPLY TO
|
// TODO wrong player message?
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
})
|
})
|
||||||
|
|
@ -76,6 +72,6 @@ public class Message {
|
||||||
CommandMeta meta = metaBuilder.build();
|
CommandMeta meta = metaBuilder.build();
|
||||||
|
|
||||||
proxyServer.getCommandManager().register(meta, brigadierCommand);
|
proxyServer.getCommandManager().register(meta, brigadierCommand);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,60 +2,48 @@ package com.alttd.chat.handlers;
|
||||||
|
|
||||||
import com.alttd.chat.VelocityChat;
|
import com.alttd.chat.VelocityChat;
|
||||||
import com.alttd.chat.config.Config;
|
import com.alttd.chat.config.Config;
|
||||||
|
import com.alttd.chat.managers.ChatUserManager;
|
||||||
import com.alttd.chat.managers.RegexManager;
|
import com.alttd.chat.managers.RegexManager;
|
||||||
|
import com.alttd.chat.objects.ChatUser;
|
||||||
import com.alttd.chat.util.Utility;
|
import com.alttd.chat.util.Utility;
|
||||||
import com.velocitypowered.api.command.CommandSource;
|
import com.velocitypowered.api.command.CommandSource;
|
||||||
import com.velocitypowered.api.proxy.Player;
|
import com.velocitypowered.api.proxy.Player;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
|
import net.kyori.adventure.text.minimessage.Template;
|
||||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ChatHandler {
|
public class ChatHandler {
|
||||||
|
|
||||||
public void privateMessage(CommandSource commandSource, Player recipient, String message) {
|
public void privateMessage(String sender, String target, String message) {
|
||||||
// todo get the chatUserinstance of both players and or console - @teri should console be able to /msg?
|
UUID uuid = UUID.fromString(sender);
|
||||||
String senderName;
|
ChatUser senderUser = ChatUserManager.getChatUser(uuid);
|
||||||
String receiverName;
|
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(uuid);
|
||||||
if (commandSource instanceof Player) {
|
if(optionalPlayer.isEmpty()) return;
|
||||||
Player sender = (Player) commandSource;
|
Player player = optionalPlayer.get();
|
||||||
senderName = sender.getUsername();
|
|
||||||
//plugin.getChatHandler().getChatPlayer(sender.getUniqueId()).setReplyTarget(event.getRecipient().getUniqueId()); // TODO this needs to be cleaner
|
Optional<Player> optionalPlayer2 = VelocityChat.getPlugin().getProxy().getPlayer(target);
|
||||||
} else {
|
if(optionalPlayer2.isEmpty()) return;
|
||||||
senderName = Config.CONSOLENAME;
|
Player player2 = optionalPlayer2.get();
|
||||||
}
|
ChatUser targetUser = ChatUserManager.getChatUser(player2.getUniqueId());
|
||||||
receiverName = recipient.getUsername();
|
|
||||||
|
|
||||||
MiniMessage miniMessage = MiniMessage.get();
|
MiniMessage miniMessage = MiniMessage.get();
|
||||||
|
|
||||||
message = Utility.parseColors(message);
|
List<Template> templates = new ArrayList<>(List.of(
|
||||||
if(!commandSource.hasPermission("chat.format")) // Todo PR fix for '<3' to minimessage
|
Template.of("sender", senderUser.getDisplayName()),
|
||||||
message = miniMessage.stripTokens(message);
|
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(
|
Component senderMessage = miniMessage.parse(Config.MESSAGESENDER, templates);
|
||||||
// Template.of("sender", user.getDisplayName()),
|
Component receiverMessage = miniMessage.parse(Config.MESSAGERECIEVER, templates);
|
||||||
// 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
|
|
||||||
// ));
|
|
||||||
|
|
||||||
Map<String, String> map = new HashMap<>();
|
player.sendMessage(senderMessage);
|
||||||
|
player2.sendMessage(receiverMessage);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void globalAdminChat(String message) {
|
public void globalAdminChat(String message) {
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,9 @@ public class PluginMessageListener {
|
||||||
case "globaladminchat":
|
case "globaladminchat":
|
||||||
VelocityChat.getPlugin().getChatHandler().globalAdminChat(in.readUTF());
|
VelocityChat.getPlugin().getChatHandler().globalAdminChat(in.readUTF());
|
||||||
break;
|
break;
|
||||||
|
case "privatemessage":
|
||||||
|
VelocityChat.getPlugin().getChatHandler().privateMessage(in.readUTF(), in.readUTF(), in.readUTF());
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
VelocityChat.getPlugin().getLogger().info("server " + event.getSource());
|
VelocityChat.getPlugin().getLogger().info("server " + event.getSource());
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user