Chat/galaxy/src/main/java/com/alttd/chat/handler/ChatHandler.java

305 lines
13 KiB
Java
Raw Normal View History

2021-05-13 12:11:29 +00:00
package com.alttd.chat.handler;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config;
2021-05-15 19:16:01 +00:00
import com.alttd.chat.managers.ChatUserManager;
2021-06-13 11:53:49 +00:00
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.channels.CustomChannel;
2021-05-15 19:16:01 +00:00
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Party;
import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import litebans.api.Database;
2021-05-13 12:11:29 +00:00
import net.kyori.adventure.text.Component;
2021-07-27 16:46:58 +00:00
import net.kyori.adventure.text.format.NamedTextColor;
2021-05-13 12:11:29 +00:00
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
2021-06-13 11:53:49 +00:00
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
2021-05-13 12:11:29 +00:00
import org.bukkit.Bukkit;
import org.bukkit.Material;
2021-05-13 12:11:29 +00:00
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
2021-05-13 12:11:29 +00:00
import java.util.ArrayList;
import java.util.List;
2021-08-08 05:11:00 +00:00
import java.util.UUID;
2021-06-06 19:32:13 +00:00
import java.util.concurrent.TimeUnit;
2021-05-13 12:11:29 +00:00
public class ChatHandler {
2021-06-06 19:32:13 +00:00
private final ChatPlugin plugin;
private final MiniMessage miniMessage;
private final Component GCNOTENABLED;
2021-05-13 12:11:29 +00:00
public ChatHandler() {
plugin = ChatPlugin.getInstance();
2021-06-06 19:32:13 +00:00
miniMessage = MiniMessage.get();
GCNOTENABLED = miniMessage.parse(Config.GCNOTENABLED);
2021-05-13 12:11:29 +00:00
}
2021-06-23 19:16:48 +00:00
public void privateMessage(Player player, String target, String message) {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
2021-06-23 19:44:04 +00:00
user.setReplyTarget(target);
String updatedMessage = RegexManager.replaceText(player.getName(), player.getUniqueId(), message); // todo a better way for this
if(updatedMessage == null) {
GalaxyUtility.sendBlockedNotification("DM Language", player, message, target);
return; // the message was blocked
}
2021-06-23 19:16:48 +00:00
if(!player.hasPermission("chat.format")) {
updatedMessage = miniMessage.stripTokens(updatedMessage);
2021-08-05 08:12:48 +00:00
} else {
updatedMessage = Utility.parseColors(updatedMessage);
2021-06-23 19:16:48 +00:00
}
if(updatedMessage.contains("[i]"))
updatedMessage = updatedMessage.replace("[i]", "<[i]>");
2021-06-23 19:16:48 +00:00
List<Template> templates = new ArrayList<>(List.of(
Template.of("message", updatedMessage),
2021-07-30 01:16:41 +00:00
Template.of("sendername", player.getName()),
Template.of("receivername", target),
2021-07-27 16:46:58 +00:00
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
2021-06-23 19:16:48 +00:00
Component component = miniMessage.parse("<message>", templates);
sendPrivateMessage(player, target, "privatemessage", component);
2021-07-30 01:16:41 +00:00
Component spymessage = miniMessage.parse(Config.MESSAGESPY, templates);
for(Player pl : Bukkit.getOnlinePlayers()) {
2021-08-01 13:08:05 +00:00
if(pl.hasPermission(Config.SPYPERMISSION) && !pl.equals(player) && !pl.getName().equalsIgnoreCase(target)) { // todo add a toggle for social spy
2021-07-30 01:16:41 +00:00
pl.sendMessage(spymessage);
}
}
2021-06-23 19:16:48 +00:00
}
2021-06-02 17:55:55 +00:00
public void globalChat(Player player, String message) {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
2021-07-30 01:16:41 +00:00
if(!Utility.hasPermission(player.getUniqueId(), Config.GCPERMISSION)) {
2021-06-06 19:32:13 +00:00
player.sendMessage(GCNOTENABLED);// GC IS OFF INFORM THEM ABOUT THIS and cancel
return;
}
2021-08-04 13:46:45 +00:00
if (isMuted(player, message, "[GC Muted] ")) {
return;
}
2021-06-06 19:32:13 +00:00
long timeLeft = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - user.getGcCooldown());
2021-07-30 01:16:41 +00:00
if(timeLeft <= Config.GCCOOLDOWN && !player.hasPermission("chat.globalchat.cooldownbypass")) { // player is on cooldown and should wait x seconds
2021-07-27 16:46:58 +00:00
player.sendMessage(miniMessage.parse(Config.GCONCOOLDOWN, Template.of("cooldown", Config.GCCOOLDOWN-timeLeft+"")));
2021-06-02 17:55:55 +00:00
return;
}
2021-05-13 12:11:29 +00:00
2021-07-17 22:48:55 +00:00
Component senderName = user.getDisplayName();
Component prefix = user.getPrefix();
2021-06-02 17:55:55 +00:00
String updatedMessage = RegexManager.replaceText(player.getName(), player.getUniqueId(), message); // todo a better way for this
if(updatedMessage == null) {
GalaxyUtility.sendBlockedNotification("GC Language", player, message, "");
return; // the message was blocked
}
2021-06-13 11:53:49 +00:00
if(!player.hasPermission("chat.format")) {
updatedMessage = miniMessage.stripTokens(updatedMessage);
2021-08-05 08:12:48 +00:00
} else {
updatedMessage = Utility.parseColors(updatedMessage);
2021-06-13 11:53:49 +00:00
}
if(updatedMessage.contains("[i]"))
updatedMessage = updatedMessage.replace("[i]", "<[i]>"); // end of todo
2021-06-02 17:55:55 +00:00
List<Template> templates = new ArrayList<>(List.of(
Template.of("sender", senderName),
Template.of("prefix", prefix),
Template.of("message", updatedMessage),
2021-06-06 19:32:13 +00:00
Template.of("server", Bukkit.getServerName()),
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
2021-06-02 17:55:55 +00:00
Component component = miniMessage.parse(Config.GCFORMAT, templates);
2021-07-27 16:46:58 +00:00
user.setGcCooldown(System.currentTimeMillis());
2021-06-06 19:32:13 +00:00
sendPluginMessage(player, "globalchat", component);
}
2021-06-02 17:55:55 +00:00
public void chatChannel(Player player, CustomChannel channel, String message) {
2021-08-05 09:40:42 +00:00
if (!player.hasPermission(channel.getPermission())) {
2021-08-04 13:46:45 +00:00
player.sendMessage(MiniMessage.get().parse("<red>You don't have permission to use this channel.</red>"));
return;
}
2021-08-05 09:40:42 +00:00
if (isMuted(player, message, "[" + channel.getChannelName() + " Muted] ")) return;
2021-08-04 13:46:45 +00:00
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
Component senderName = user.getDisplayName();
String updatedMessage = RegexManager.replaceText(player.getName(), player.getUniqueId(), message);
if(updatedMessage == null) {
GalaxyUtility.sendBlockedNotification(channel.getChannelName() + " Language", player, message, "");
2021-08-04 13:46:45 +00:00
return; // the message was blocked
}
if(!player.hasPermission("chat.format")) {
updatedMessage = miniMessage.stripTokens(updatedMessage);
}
if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replace("[i]", "<[i]>");
List<Template> templates = new ArrayList<>(List.of(
Template.of("sender", senderName),
Template.of("message", updatedMessage),
Template.of("server", Bukkit.getServerName()),
2021-08-05 09:40:42 +00:00
Template.of("channel", channel.getChannelName()),
2021-08-04 13:46:45 +00:00
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
2021-08-05 09:40:42 +00:00
Component component = miniMessage.parse(channel.getFormat(), templates);
if (channel.isProxy()) {
sendChatChannelMessage(player, channel.getChannelName(), "chatchannel", component);
} else {
2021-08-08 05:11:00 +00:00
sendChatChannelMessage(channel, player.getUniqueId(), component);
2021-08-05 09:40:42 +00:00
}
2021-08-04 13:46:45 +00:00
}
public void partyMessage(Party party, Player player, String message) {
if (isMuted(player, message, "[" + party.getPartyName() + " Muted] ")) return;
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
Component senderName = user.getDisplayName();
String updatedMessage = RegexManager.replaceText(player.getName(), player.getUniqueId(), message);
if(updatedMessage == null) {
GalaxyUtility.sendBlockedNotification("Party Language", player, message, "");
return; // the message was blocked
}
if(!player.hasPermission("chat.format")) {
updatedMessage = miniMessage.stripTokens(updatedMessage);
}
if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replace("[i]", "<[i]>");
List<Template> templates = new ArrayList<>(List.of(
Template.of("sender", senderName),
2021-08-08 16:27:30 +00:00
Template.of("sendername", senderName),
Template.of("partyname", party.getPartyName()),
Template.of("message", updatedMessage),
Template.of("server", Bukkit.getServerName()),
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
Component component = miniMessage.parse(Config.PARTY_FORMAT, templates);
sendPartyMessage(player, party.getPartyId(), component);
2021-08-08 16:27:30 +00:00
Component spyMessage = miniMessage.parse(Config.PARTY_SPY, templates);
for(Player pl : Bukkit.getOnlinePlayers()) {
2021-08-08 20:07:47 +00:00
if(pl.hasPermission(Config.SPYPERMISSION) && !party.getPartyUsersUuid().contains(pl.getUniqueId())) { // todo add a toggle for social spy
2021-08-08 16:27:30 +00:00
pl.sendMessage(spyMessage);
}
}
}
private void sendChatChannelMessage(CustomChannel chatChannel, UUID uuid, Component component) {
2021-08-05 09:40:42 +00:00
if (!chatChannel.getServers().contains(Bukkit.getServerName())) return;
2021-08-04 13:46:45 +00:00
2021-08-05 09:40:42 +00:00
Bukkit.getServer().getOnlinePlayers().stream()
.filter(p -> p.hasPermission(chatChannel.getPermission()))
2021-08-08 05:11:00 +00:00
.filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(uuid))
2021-08-05 09:40:42 +00:00
.forEach(p -> p.sendMessage(component));
2021-08-04 13:46:45 +00:00
}
2021-06-06 19:32:13 +00:00
private void sendPluginMessage(Player player, String channel, Component component) {
2021-06-02 17:55:55 +00:00
ByteArrayDataOutput out = ByteStreams.newDataOutput();
2021-06-06 19:32:13 +00:00
out.writeUTF(channel);
2021-07-30 21:22:38 +00:00
out.writeUTF(player.getUniqueId().toString());
2021-06-23 19:16:48 +00:00
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));
2021-06-02 17:55:55 +00:00
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
2021-05-13 12:11:29 +00:00
2021-08-05 09:40:42 +00:00
public void sendChatChannelMessage(Player player, String chatChannelName, String channel, Component component) {
2021-08-04 13:46:45 +00:00
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
2021-08-05 09:40:42 +00:00
out.writeUTF(chatChannelName);
2021-08-08 05:11:00 +00:00
out.writeUTF(player.getUniqueId().toString());
2021-08-04 13:46:45 +00:00
out.writeUTF(GsonComponentSerializer.gson().serialize(component));
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
private void sendPartyMessage(Player player, int partyId, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("party");
out.writeUTF(String.valueOf(partyId));
out.writeUTF(player.getUniqueId().toString());
out.writeUTF(GsonComponentSerializer.gson().serialize(component));
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
2021-06-06 19:32:13 +00:00
// Start - move these to util
2021-08-04 13:46:45 +00:00
2021-08-05 09:40:42 +00:00
private boolean isMuted(Player player, String message, String prefix) {
if (Database.get().isPlayerMuted(player.getUniqueId(), null) || (ChatPlugin.getInstance().serverMuted() && !player.hasPermission("chat.bypass-server-muted"))) {
MiniMessage miniMessage = MiniMessage.get();
Component blockedNotification = miniMessage.parse("<red>" + prefix
+ Utility.getDisplayName(player.getUniqueId(), player.getName())
+ " tried to say: "
+ message + "</red>");
Bukkit.getOnlinePlayers().forEach(a ->{
if (a.hasPermission("chat.alert-blocked")) {
a.sendMessage(blockedNotification);//TODO make configurable (along with all the messages)
}
});
return true;
}
return false;
}
2021-06-13 11:53:49 +00:00
public static Component itemComponent(ItemStack item) {
2021-07-27 16:46:58 +00:00
Component component = Component.text("[i]", NamedTextColor.AQUA);
if(item.getType().equals(Material.AIR))
return component.color(NamedTextColor.WHITE);
2021-06-06 19:32:13 +00:00
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
if(dname) {
component = component.append(item.getItemMeta().displayName());
} else {
2021-07-27 16:46:58 +00:00
component = component.append(Component.text(materialToName(item.getType()), NamedTextColor.WHITE));
2021-06-06 19:32:13 +00:00
}
2021-07-27 16:46:58 +00:00
component = component.append(Component.text(" x" + item.getAmount(), NamedTextColor.AQUA));
2021-06-06 19:32:13 +00:00
component = component.hoverEvent(item.asHoverEvent());
return component;
}
private static String materialToName(Material m) {
if (m.equals(Material.TNT)) {
return "TNT";
}
String orig = m.toString().toLowerCase();
String[] splits = orig.split("_");
StringBuilder sb = new StringBuilder(orig.length());
int pos = 0;
for (String split : splits) {
sb.append(split);
int loc = sb.lastIndexOf(split);
char charLoc = sb.charAt(loc);
if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") ||
split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on")))
sb.setCharAt(loc, Character.toUpperCase(charLoc));
if (pos != splits.length - 1)
sb.append(' ');
++pos;
}
return sb.toString();
}
// end - move these to util
2021-05-13 12:11:29 +00:00
}