Merge branch 'main' into party

This commit is contained in:
Teriuihi 2021-08-05 11:49:19 +02:00
commit c4f56a9d21
11 changed files with 306 additions and 30 deletions

View File

@ -1,5 +1,6 @@
package com.alttd.chat.config;
import com.alttd.chat.objects.Channel;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
@ -14,9 +15,7 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.regex.Pattern;
public final class Config {
@ -238,6 +237,25 @@ public final class Config {
}
private static void chatChannels() {
ConfigurationNode node = getNode("chat-channels");
if (node.isEmpty()) {
getString("chat-channels.ac.format", "<white><gray><sender></gray> <hover:show_text:on <server>><yellow>to <channel></yellow></hover><gray>: <message>");
getList("chat-channels.ac.servers", List.of("lobby"));
getBoolean("chat-channels.ac.proxy", false);
node = getNode("chat-channels");
}
for (ConfigurationNode configurationNode : node.getChildrenMap().values()) {
String channelName = Objects.requireNonNull(configurationNode.getKey()).toString();
String key = "chat-channels." + channelName + ".";
new Channel(channelName,
getString(key + "format", ""),
getList(key + "servers", Collections.EMPTY_LIST),
getBoolean(key + "proxy", false));
}
}
public static String SERVERMUTEPERMISSION = "command.mute-server";
public static String SPYPERMISSION = "chat.socialspy";
private static void permissions() {

View File

@ -173,8 +173,15 @@ public final class RegexConfig {
String regex = entry.getValue().getNode("regex").getString();
String replacement = entry.getValue().getNode("replacement").getString();
List<String> exclusions = entry.getValue().getNode("exclusions").getList(TypeToken.of(String.class), new ArrayList<>());
ChatFilter chatFilter = new ChatFilter(name, type, regex, replacement, exclusions);
RegexManager.addFilter(chatFilter);
if (type == null || type.isEmpty() || regex == null || regex.isEmpty()) {
ALogger.warn("Filter: " + name + " was set up incorrectly");
} else {
if (replacement == null || replacement.isEmpty()) {
replacement = name;
}
ChatFilter chatFilter = new ChatFilter(name, type, regex, replacement, exclusions);
RegexManager.addFilter(chatFilter);
}
} catch(ObjectMappingException ex) {
}
});

View File

@ -0,0 +1,49 @@
package com.alttd.chat.objects;
import java.util.*;
public class Channel {
public static HashMap<String, Channel> channels = new HashMap<>();
private String permission;
private String channelName;
private String format;
private List<String> servers;
private boolean proxy;
public Channel(String channelName, String format, List<String> servers, boolean proxy) {
this.permission = "chat.channel." + channelName.toLowerCase();
this.channelName = channelName;
this.format = format;
this.servers = servers;
this.proxy = proxy;
channels.put(channelName.toLowerCase(), this);
}
public static Collection<Channel> getChannels() {
return channels.values();
}
public String getPermission() {
return permission;
}
public String getChannelName() {
return channelName;
}
public String getFormat() {
return format;
}
public List<String> getServers() {
return servers;
}
public boolean isProxy() {
return proxy;
}
public static Channel getChatChannel(String channelName) {
return channels.get(channelName.toLowerCase());
}
}

View File

@ -8,12 +8,16 @@ import com.alttd.chat.handler.ChatHandler;
import com.alttd.chat.listeners.ChatListener;
import com.alttd.chat.listeners.PlayerListener;
import com.alttd.chat.listeners.PluginMessage;
import com.alttd.chat.objects.Channel;
import com.alttd.chat.util.ALogger;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
public class ChatPlugin extends JavaPlugin {
private static ChatPlugin instance;
@ -43,6 +47,9 @@ public class ChatPlugin extends JavaPlugin {
registerCommand("unignore", new Unignore());
registerCommand("muteserver", new MuteServer());
registerCommand("spy", new Spy());
for (Channel channel : Channel.getChannels()) {
this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(channel));
}
messageChannel = Config.MESSAGECHANNEL;
getServer().getMessenger().registerOutgoingPluginChannel(this, messageChannel);
@ -60,8 +67,14 @@ public class ChatPlugin extends JavaPlugin {
}
}
public void registerCommand(String commandName, CommandExecutor CommandExecutor) {
getCommand(commandName).setExecutor(CommandExecutor);
public void registerCommand(String commandName, CommandExecutor commandExecutor) {
getCommand(commandName).setExecutor(commandExecutor);
}
public void registerCommand(String commandName, CommandExecutor commandExecutor, List<String> aliases) {
PluginCommand command = getCommand(commandName);
command.setAliases(aliases);
command.setExecutor(commandExecutor);
}
public static ChatPlugin getInstance() {

View File

@ -0,0 +1,46 @@
package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.objects.Channel;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
public class ChatChannel extends BukkitCommand {
Channel channel;
String command;
public ChatChannel(Channel channel) {
super(channel.getChannelName().toLowerCase());
this.channel = channel;
this.command = channel.getChannelName().toLowerCase();
this.description = "Chat channel named " + channel.getChannelName() + ".";
this.usageMessage = "/" + command + " <message>";
this.setAliases(Collections.emptyList());
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) {
if(!(sender instanceof Player player)) { // must be a player
return true;
}
if(args.length == 0) return false;
String message = StringUtils.join(args, " ", 0, args.length);
new BukkitRunnable() {
@Override
public void run() {
ChatPlugin.getInstance().getChatHandler().chatChannel(player, channel, message);
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
return false;
}
}

View File

@ -1,9 +1,11 @@
package com.alttd.chat.handler;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.commands.ChatChannel;
import com.alttd.chat.config.Config;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.Channel;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.Utility;
@ -15,10 +17,12 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.StringUtil;
import java.util.ArrayList;
import java.util.List;
@ -48,6 +52,8 @@ public class ChatHandler {
if(!player.hasPermission("chat.format")) {
updatedMessage = miniMessage.stripTokens(updatedMessage);
} else {
updatedMessage = Utility.parseColors(updatedMessage);
}
if(updatedMessage.contains("[i]"))
@ -77,18 +83,7 @@ public class ChatHandler {
return;
}
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>[GC Muted] "
+ 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)
}
});
if (isMuted(player, message, "[GC Muted] ")) {
return;
}
@ -109,6 +104,8 @@ public class ChatHandler {
if(!player.hasPermission("chat.format")) {
updatedMessage = miniMessage.stripTokens(updatedMessage);
} else {
updatedMessage = Utility.parseColors(updatedMessage);
}
if(updatedMessage.contains("[i]"))
@ -126,6 +123,53 @@ public class ChatHandler {
sendPluginMessage(player, "globalchat", component);
}
public void chatChannel(Player player, Channel channel, String message) {
if (!player.hasPermission(channel.getPermission())) {
player.sendMessage(MiniMessage.get().parse("<red>You don't have permission to use this channel.</red>"));
return;
}
if (isMuted(player, message, "[" + channel.getChannelName() + " 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("GC 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),
Template.of("message", updatedMessage),
Template.of("server", Bukkit.getServerName()),
Template.of("channel", channel.getChannelName()),
Template.of("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
Component component = miniMessage.parse(channel.getFormat(), templates);
if (channel.isProxy()) {
sendChatChannelMessage(player, channel.getChannelName(), "chatchannel", component);
} else {
sendChatChannelMessage(channel, component);
}
}
private void sendChatChannelMessage(Channel chatChannel, Component component) {
if (!chatChannel.getServers().contains(Bukkit.getServerName())) return;
Bukkit.getServer().getOnlinePlayers().stream()
.filter(p -> p.hasPermission(chatChannel.getPermission()))
.forEach(p -> p.sendMessage(component));
}
private void sendPluginMessage(Player player, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
@ -143,7 +187,33 @@ public class ChatHandler {
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
public void sendChatChannelMessage(Player player, String chatChannelName, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
out.writeUTF(chatChannelName);
out.writeUTF(GsonComponentSerializer.gson().serialize(component));
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
// Start - move these to util
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;
}
public static Component itemComponent(ItemStack item) {
Component component = Component.text("[i]", NamedTextColor.AQUA);
if(item.getType().equals(Material.AIR))

View File

@ -69,6 +69,8 @@ public class ChatListener implements Listener, ChatRenderer {
if(!player.hasPermission("chat.format")) {
message = miniMessage.stripTokens(message);
} else {
message = Utility.parseColors(message);
}
if(message.contains("[i]"))

View File

@ -4,6 +4,7 @@ import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.Channel;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility;
@ -14,6 +15,7 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.UUID;
@ -39,17 +41,17 @@ public class PluginMessage implements PluginMessageListener {
break;
}
case "globalchat": {
if (ChatPlugin.getInstance().serverGlobalChatEnabled() && !ChatPlugin.getInstance().serverMuted()) {
UUID uuid = UUID.fromString(in.readUTF());
String message = in.readUTF();
if (!ChatPlugin.getInstance().serverGlobalChatEnabled() || ChatPlugin.getInstance().serverMuted()) break;
Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission(Config.GCPERMISSION)).forEach(p -> {
ChatUser chatUser = ChatUserManager.getChatUser(p.getUniqueId());
if (!chatUser.getIgnoredPlayers().contains(uuid)) {
p.sendMessage(GsonComponentSerializer.gson().deserialize(message));
}
});
}
UUID uuid = UUID.fromString(in.readUTF());
String message = in.readUTF();
Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission(Config.GCPERMISSION)).forEach(p -> {
ChatUser chatUser = ChatUserManager.getChatUser(p.getUniqueId());
if (!chatUser.getIgnoredPlayers().contains(uuid)) {
p.sendMessage(GsonComponentSerializer.gson().deserialize(message));
}
});
break;
}
case "ignore": {
@ -59,16 +61,58 @@ public class PluginMessage implements PluginMessageListener {
if(!chatUser.getIgnoredPlayers().contains(targetUUID)) {
chatUser.addIgnoredPlayers(targetUUID);
}
break;
}
case "unignore": {
ChatUser chatUser = ChatUserManager.getChatUser(UUID.fromString(in.readUTF()));
chatUser.removeIgnoredPlayers(UUID.fromString(in.readUTF()));
break;
}
case "chatchannel": {
if (ChatPlugin.getInstance().serverMuted()) break;
chatChannel(in);
}
default:
break;
}
}
private void chatChannel(ByteArrayDataInput in) {
Channel chatChannel = null;
Component component = null;
try {
chatChannel = Channel.getChatChannel(in.readUTF());
component = GsonComponentSerializer.gson().deserialize(in.readUTF());
} catch (Exception e) { //Idk the exception for reading too far into in.readUTF()
e.printStackTrace();
}
if (chatChannel == null) {
ALogger.warn("Received ChatChannel message for non existent channel.");
return;
}
if (!chatChannel.getServers().contains(Bukkit.getServerName())) {
ALogger.warn("Received ChatChannel message for the wrong server.");
return;
}
if (component == null) {
ALogger.warn("Didn't receive a valid message for ChatChannel " + chatChannel.getChannelName() + ".");
}
final Channel finalChatChannel = chatChannel;
final Component finalComponent = component;
new BukkitRunnable() {
@Override
public void run() {
Bukkit.getOnlinePlayers().stream()
.filter(p -> p.hasPermission(finalChatChannel.getPermission()))
.forEach(p -> p.sendMessage(finalComponent));
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
}
}
/* // todo implement AdvancedChatFilter for this like this
// send pluginmessage to backend server and return a shatteredcomponent to be reparsed by proxy

View File

@ -43,7 +43,7 @@ public class ServerWrapper {
public void sendJoinLeaveMessage(UUID uuid, Component component) {
if(joinMessages())
getRegisteredServer().getPlayersConnected().stream()
.filter(p -> ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(uuid))
.filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(uuid))
.forEach(p -> p.sendMessage(component));
}
}

View File

@ -43,6 +43,7 @@ public class ServerHandler {
// Component component = GsonComponentSerializer.gson().deserialize(message);
servers.stream()
.filter(ServerWrapper::globalChat)
.map(ServerWrapper::getRegisteredServer)
.forEach(registeredServer -> {
ByteArrayDataOutput buf = ByteStreams.newDataOutput();

View File

@ -1,14 +1,21 @@
package com.alttd.chat.listeners;
import com.alttd.chat.VelocityChat;
import com.alttd.chat.objects.Channel;
import com.alttd.chat.util.ALogger;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.Optional;
import java.util.UUID;
public class PluginMessageListener {
@ -43,8 +50,27 @@ public class PluginMessageListener {
case "privatemessage": // TODO redirect this to the server that player is on
VelocityChat.getPlugin().getChatHandler().privateMessage(in.readUTF(), in.readUTF(), in.readUTF());
break;
case "chatchannel": {
String channelName = in.readUTF();
Channel chatChannel = Channel.getChatChannel(channelName);
if (chatChannel == null) {
ALogger.warn("Received non existent channel" + channelName +".");
break;
}
ProxyServer proxy = VelocityChat.getPlugin().getProxy();
chatChannel.getServers().forEach(server -> proxy.getServer(server).ifPresent(registeredServer ->
registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), event.getData())));
break;
}
default:
VelocityChat.getPlugin().getLogger().info("server " + event.getSource());
ProxyServer proxy = VelocityChat.getPlugin().getProxy();
for (RegisteredServer registeredServer : proxy.getAllServers()) {
registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), event.getData());
}
break;
}
}