diff --git a/src/main/java/net/draycia/simplechat/channels/ChatChannel.java b/src/main/java/net/draycia/simplechat/channels/ChatChannel.java index 142406a6..a25ba04a 100644 --- a/src/main/java/net/draycia/simplechat/channels/ChatChannel.java +++ b/src/main/java/net/draycia/simplechat/channels/ChatChannel.java @@ -71,6 +71,8 @@ public abstract class ChatChannel { public abstract String getToggleOnMessage(); + public abstract boolean filterEnabled(); + /** * @return If this channel is Towny's town chat. */ @@ -142,6 +144,7 @@ public abstract class ChatChannel { public abstract ChatChannel.Builder setToggleOnMessage(String toggleOnMessage); public abstract ChatChannel.Builder setShouldForwardFormatting(boolean forwardFormatting); public abstract ChatChannel.Builder setShouldBungee(boolean shouldBungee); + public abstract ChatChannel.Builder setFilterEnabled(boolean filterEnabled); } } diff --git a/src/main/java/net/draycia/simplechat/channels/impls/AllianceChatChannel.java b/src/main/java/net/draycia/simplechat/channels/impls/AllianceChatChannel.java index b7820758..599bed2e 100644 --- a/src/main/java/net/draycia/simplechat/channels/impls/AllianceChatChannel.java +++ b/src/main/java/net/draycia/simplechat/channels/impls/AllianceChatChannel.java @@ -12,8 +12,8 @@ import java.util.Map; public class AllianceChatChannel extends SimpleChatChannel { - AllianceChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, SimpleChat simpleChat) { - super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, simpleChat); + AllianceChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, boolean filterEnabled, SimpleChat simpleChat) { + super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, filterEnabled, simpleChat); } @Override diff --git a/src/main/java/net/draycia/simplechat/channels/impls/NationChatChannel.java b/src/main/java/net/draycia/simplechat/channels/impls/NationChatChannel.java index 59e59cae..ba0f8899 100644 --- a/src/main/java/net/draycia/simplechat/channels/impls/NationChatChannel.java +++ b/src/main/java/net/draycia/simplechat/channels/impls/NationChatChannel.java @@ -13,8 +13,8 @@ import java.util.Map; public class NationChatChannel extends SimpleChatChannel { - NationChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, SimpleChat simpleChat) { - super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, simpleChat); + NationChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, boolean filterEnabled, SimpleChat simpleChat) { + super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, filterEnabled, simpleChat); } @Override diff --git a/src/main/java/net/draycia/simplechat/channels/impls/PartyChatChannel.java b/src/main/java/net/draycia/simplechat/channels/impls/PartyChatChannel.java index 373fa5f3..a5f7c328 100644 --- a/src/main/java/net/draycia/simplechat/channels/impls/PartyChatChannel.java +++ b/src/main/java/net/draycia/simplechat/channels/impls/PartyChatChannel.java @@ -10,8 +10,8 @@ import java.util.Map; public class PartyChatChannel extends SimpleChatChannel { - PartyChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, SimpleChat simpleChat) { - super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, simpleChat); + PartyChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, boolean filterEnabled, SimpleChat simpleChat) { + super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, filterEnabled, simpleChat); } @Override diff --git a/src/main/java/net/draycia/simplechat/channels/impls/SimpleChatChannel.java b/src/main/java/net/draycia/simplechat/channels/impls/SimpleChatChannel.java index bdad4d31..3f7af48c 100644 --- a/src/main/java/net/draycia/simplechat/channels/impls/SimpleChatChannel.java +++ b/src/main/java/net/draycia/simplechat/channels/impls/SimpleChatChannel.java @@ -12,6 +12,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.javacord.api.entity.channel.ServerTextChannel; import org.javacord.api.entity.message.MessageAuthor; @@ -40,6 +41,7 @@ public class SimpleChatChannel extends ChatChannel { private String toggleOnMessage; private boolean forwardFormatting; private boolean shouldBungee; + private boolean filterEnabled; private DiscordWebhook discordWebhook = null; @@ -47,7 +49,7 @@ public class SimpleChatChannel extends ChatChannel { private SimpleChatChannel() { } - SimpleChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, SimpleChat simpleChat) { + SimpleChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, boolean filterEnabled, SimpleChat simpleChat) { this.color = color; this.id = id; this.formats = formats; @@ -61,6 +63,7 @@ public class SimpleChatChannel extends ChatChannel { this.toggleOnMessage = toggleOnMessage; this.forwardFormatting = forwardFormatting; this.shouldBungee = shouldBungee; + this.filterEnabled = filterEnabled; this.simpleChat = simpleChat; @@ -133,6 +136,7 @@ public class SimpleChatChannel extends ChatChannel { @Override public void sendMessage(OfflinePlayer player, String message) { + // Get player's formatting String group; if (player.isOnline()) { @@ -143,11 +147,28 @@ public class SimpleChatChannel extends ChatChannel { String messageFormat = getFormat(group); + // If the player isn't online (cross server message), use their normal name if (!player.isOnline()) { messageFormat = messageFormat.replace("%player_displayname%", "%player_name%"); } + // Chat filter + if (filterEnabled() && simpleChat.getConfig().contains("filters")) { + for (String entry : simpleChat.getConfig().getStringList("filters")) { + message = message.replaceAll(entry, simpleChat.getConfig().getString("filter-text", "****")); + } + } + // Chat placeholders + ConfigurationSection replacements = simpleChat.getConfig().getConfigurationSection("replacements"); + + if (replacements != null) { + for (String key : replacements.getKeys(false)) { + message = message.replace(key, replacements.getString(key)); + } + } + + // Call custom chat event ChannelChatEvent event = new ChannelChatEvent(player, this, messageFormat, message); Bukkit.getPluginManager().callEvent(event); @@ -156,6 +177,7 @@ public class SimpleChatChannel extends ChatChannel { return; } + // Parse placeholders messageFormat = PlaceholderAPI.setPlaceholders(player, event.getFormat()); // Convert legacy color codes to Mini color codes @@ -169,18 +191,23 @@ public class SimpleChatChannel extends ChatChannel { // Finally, parse remaining placeholders and parse format messageFormat = MiniMessageParser.handlePlaceholders(messageFormat, "message", event.getMessage()); + // Send message to players who can see it Component formattedMessage = MiniMessageParser.parseFormat(messageFormat); for (Player onlinePlayer : getAudience(player)) { simpleChat.getPlatform().player(onlinePlayer).sendMessage(formattedMessage); } + // Log message to console System.out.println(LegacyComponentSerializer.legacy().serialize(formattedMessage)); - if (player.isOnline()) { - if (shouldForwardFormatting()) { + // Route message to bungee / discord (if message originates from this server) + // Use instanceof and not isOnline, if this message originates from another then the instanceof will + // fail, but isOnline may succeed if the player is online on both servers (somehow). + if (player instanceof Player) { + if (shouldForwardFormatting() && shouldBungee()) { sendMessageToBungee(player.getPlayer(), component); - } else { + } else if (shouldBungee()) { sendMessageToBungee(player.getPlayer(), message); } @@ -314,6 +341,11 @@ public class SimpleChatChannel extends ChatChannel { return forwardFormatting; } + @Override + public boolean filterEnabled() { + return filterEnabled; + } + public static SimpleChatChannel.Builder builder(String name) { return new SimpleChatChannel.Builder(name); } @@ -333,6 +365,7 @@ public class SimpleChatChannel extends ChatChannel { private String toggleOnMessage = "You can now see chat!"; private boolean forwardFormatting = true; private boolean shouldBungee = false; + private boolean filterEnabled = true; private Builder() { } @@ -342,7 +375,7 @@ public class SimpleChatChannel extends ChatChannel { @Override public SimpleChatChannel build(SimpleChat simpleChat) { - return new SimpleChatChannel(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, simpleChat); + return new SimpleChatChannel(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, filterEnabled, simpleChat); } @Override @@ -440,6 +473,13 @@ public class SimpleChatChannel extends ChatChannel { return this; } + + @Override + public Builder setFilterEnabled(boolean filterEnabled) { + this.filterEnabled = filterEnabled; + + return this; + } } } diff --git a/src/main/java/net/draycia/simplechat/channels/impls/TownChatChannel.java b/src/main/java/net/draycia/simplechat/channels/impls/TownChatChannel.java index adb6a809..202a7360 100644 --- a/src/main/java/net/draycia/simplechat/channels/impls/TownChatChannel.java +++ b/src/main/java/net/draycia/simplechat/channels/impls/TownChatChannel.java @@ -12,8 +12,8 @@ import java.util.Map; public class TownChatChannel extends SimpleChatChannel { - TownChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, SimpleChat simpleChat) { - super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, simpleChat); + TownChatChannel(TextColor color, long id, Map formats, String webhook, boolean isDefault, boolean ignorable, String name, double distance, String switchMessage, String toggleOffMessage, String toggleOnMessage, boolean forwardFormatting, boolean shouldBungee, boolean filterEnabled, SimpleChat simpleChat) { + super(color, id, formats, webhook, isDefault, ignorable, name, distance, switchMessage, toggleOffMessage, toggleOnMessage, forwardFormatting, shouldBungee, filterEnabled, simpleChat); } @Override diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3b505901..8eacd4c9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -32,6 +32,15 @@ channels: ignorable: false should-bungee: false +filters: + - 'lag' + - 'la[g]+' + +filter-text: '****' + +replacements: + '%shrug%': '¯\_(ツ)_/¯' + bot-token: -1 language: