Added configurable chat channels

This commit is contained in:
Teriuihi 2021-08-04 15:46:45 +02:00
parent 7c15731dcd
commit 9d9b8f0bf2
6 changed files with 140 additions and 25 deletions

View File

@ -238,6 +238,11 @@ public final class Config {
}
public static List<String> CHATCHANNEL_CHANNELS = List.of("ac");
private static void chatChannels() {
CHATCHANNEL_CHANNELS = getList("chat-channels", CHATCHANNEL_CHANNELS);
}
public static String SERVERMUTEPERMISSION = "command.mute-server";
public static String SPYPERMISSION = "chat.socialspy";
private static void permissions() {

View File

@ -11,9 +11,13 @@ import com.alttd.chat.listeners.PluginMessage;
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.Collections;
import java.util.List;
public class ChatPlugin extends JavaPlugin {
private static ChatPlugin instance;
@ -43,6 +47,7 @@ public class ChatPlugin extends JavaPlugin {
registerCommand("unignore", new Unignore());
registerCommand("muteserver", new MuteServer());
registerCommand("spy", new Spy());
registerCommand("chatchannel", new ChatChannel(), Config.CHATCHANNEL_CHANNELS);
messageChannel = Config.MESSAGECHANNEL;
getServer().getMessenger().registerOutgoingPluginChannel(this, messageChannel);
@ -60,8 +65,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,31 @@
package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class ChatChannel implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, 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, label, message);
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
return false;
}
}

View File

@ -15,10 +15,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;
@ -77,18 +79,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;
}
@ -126,6 +117,57 @@ public class ChatHandler {
sendPluginMessage(player, "globalchat", component);
}
public void chatChannel(Player player, String label, String message) {
if (!player.hasPermission("chat.channel." + label)) {
player.sendMessage(MiniMessage.get().parse("<red>You don't have permission to use this channel.</red>"));
return;
}
if (isMuted(player, message, "[" + StringUtils.capitalize(label) + " 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("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
Component component = miniMessage.parse(Config.GCFORMAT, templates);
sendChatChannelMessage(player, label, "chatchannel", component);
}
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;
}
private void sendPluginMessage(Player player, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
@ -143,7 +185,15 @@ public class ChatHandler {
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
public void sendChatChannelMessage(Player player, String label, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
out.writeUTF(label);
out.writeUTF(GsonComponentSerializer.gson().serialize(component));
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
// Start - move these to util
public static Component itemComponent(ItemStack item) {
Component component = Component.text("[i]", NamedTextColor.AQUA);
if(item.getType().equals(Material.AIR))

View File

@ -14,6 +14,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 +40,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": {
@ -64,6 +65,21 @@ public class PluginMessage implements PluginMessageListener {
ChatUser chatUser = ChatUserManager.getChatUser(UUID.fromString(in.readUTF()));
chatUser.removeIgnoredPlayers(UUID.fromString(in.readUTF()));
}
case "chatchannel": {
if (ChatPlugin.getInstance().serverMuted()) break;
String chatChannel = in.readUTF();
Component component = GsonComponentSerializer.gson().deserialize(in.readUTF());
new BukkitRunnable() {
@Override
public void run() {
Bukkit.getOnlinePlayers().stream()
.filter(p -> p.hasPermission("chat.channel." + chatChannel))
.forEach(p -> p.sendMessage(component));
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
}
default:
break;
}

View File

@ -27,3 +27,5 @@ commands:
permission: command.mute-server
spy:
permission: command.togglespy
chatchannel:
permission: chat.channel.chatchannel