Created configurable chat channels

This commit is contained in:
2021-08-05 11:40:42 +02:00
parent 9d9b8f0bf2
commit b9fae1cb35
8 changed files with 196 additions and 49 deletions
@@ -8,6 +8,7 @@ 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;
@@ -15,7 +16,6 @@ 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 {
@@ -47,7 +47,9 @@ public class ChatPlugin extends JavaPlugin {
registerCommand("unignore", new Unignore());
registerCommand("muteserver", new MuteServer());
registerCommand("spy", new Spy());
registerCommand("chatchannel", new ChatChannel(), Config.CHATCHANNEL_CHANNELS);
for (Channel channel : Channel.getChannels()) {
this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(channel));
}
messageChannel = Config.MESSAGECHANNEL;
getServer().getMessenger().registerOutgoingPluginChannel(this, messageChannel);
@@ -1,17 +1,32 @@
package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import net.kyori.adventure.text.minimessage.MiniMessage;
import com.alttd.chat.objects.Channel;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
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());
}
public class ChatChannel implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public boolean execute(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) {
if(!(sender instanceof Player player)) { // must be a player
return true;
}
@@ -22,7 +37,7 @@ public class ChatChannel implements CommandExecutor {
new BukkitRunnable() {
@Override
public void run() {
ChatPlugin.getInstance().getChatHandler().chatChannel(player, label, message);
ChatPlugin.getInstance().getChatHandler().chatChannel(player, channel, message);
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
@@ -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;
@@ -117,13 +119,13 @@ public class ChatHandler {
sendPluginMessage(player, "globalchat", component);
}
public void chatChannel(Player player, String label, String message) {
if (!player.hasPermission("chat.channel." + label)) {
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, "[" + StringUtils.capitalize(label) + " Muted] ")) return;
if (isMuted(player, message, "[" + channel.getChannelName() + " Muted] ")) return;
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
Component senderName = user.getDisplayName();
@@ -144,28 +146,24 @@ public class ChatHandler {
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(Config.GCFORMAT, templates);
sendChatChannelMessage(player, label, "chatchannel", component);
Component component = miniMessage.parse(channel.getFormat(), templates);
if (channel.isProxy()) {
sendChatChannelMessage(player, channel.getChannelName(), "chatchannel", component);
} else {
sendChatChannelMessage(channel, 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>");
private void sendChatChannelMessage(Channel chatChannel, Component component) {
if (!chatChannel.getServers().contains(Bukkit.getServerName())) return;
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;
Bukkit.getServer().getOnlinePlayers().stream()
.filter(p -> p.hasPermission(chatChannel.getPermission()))
.forEach(p -> p.sendMessage(component));
}
private void sendPluginMessage(Player player, String channel, Component component) {
@@ -185,15 +183,33 @@ public class ChatHandler {
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
}
public void sendChatChannelMessage(Player player, String label, String channel, Component component) {
public void sendChatChannelMessage(Player player, String chatChannelName, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel);
out.writeUTF(label);
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))
@@ -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;
@@ -60,31 +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;
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());
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
+1 -3
View File
@@ -26,6 +26,4 @@ commands:
muteserver:
permission: command.mute-server
spy:
permission: command.togglespy
chatchannel:
permission: chat.channel.chatchannel
permission: command.togglespy