|
| 1 | +package de.lebaasti.core; |
| 2 | + |
| 3 | +import net.labymod.api.addon.LabyAddon; |
| 4 | +import net.labymod.api.client.chat.ChatMessage; |
| 5 | +import net.labymod.api.client.component.Component; |
| 6 | +import net.labymod.api.event.Subscribe; |
| 7 | +import net.labymod.api.event.client.chat.ChatReceiveEvent; |
| 8 | +import net.labymod.api.event.client.network.server.ServerJoinEvent; |
| 9 | +import net.labymod.api.event.client.network.server.SubServerSwitchEvent; |
| 10 | +import net.labymod.api.models.addon.annotation.AddonMain; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.HashMap; |
| 13 | + |
| 14 | +@AddonMain |
| 15 | +public class AntiChatSpam extends LabyAddon<Configuration> { |
| 16 | + |
| 17 | + @Override |
| 18 | + protected void enable() { |
| 19 | + this.registerSettingCategory(); |
| 20 | + |
| 21 | + this.logger().info("Enabled AntiChatSpam Addon"); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + protected Class<Configuration> configurationClass() { |
| 26 | + return Configuration.class; |
| 27 | + } |
| 28 | + |
| 29 | + private final HashMap<String, ArrayList<ChatMessage>> duplicateMessages = new HashMap<>(); |
| 30 | + private final HashMap<String, Integer> duplicateMessageCount = new HashMap<>(); |
| 31 | + |
| 32 | + @Subscribe |
| 33 | + public void onSubServerSwitch(SubServerSwitchEvent event) { |
| 34 | + duplicateMessages.clear(); |
| 35 | + duplicateMessageCount.clear(); |
| 36 | + } |
| 37 | + |
| 38 | + @Subscribe |
| 39 | + public void onServerJoin(ServerJoinEvent event) { |
| 40 | + duplicateMessages.clear(); |
| 41 | + duplicateMessageCount.clear(); |
| 42 | + } |
| 43 | + |
| 44 | + @Subscribe |
| 45 | + public void onChatReceive(ChatReceiveEvent event) { |
| 46 | + String originalPlainText = event.chatMessage().getOriginalPlainText(); |
| 47 | + ArrayList<ChatMessage> duplicates = duplicateMessages.computeIfAbsent(originalPlainText, key -> new ArrayList<>()); |
| 48 | + int count = duplicateMessageCount.compute(originalPlainText, (key, value) -> value == null ? 1 : value + 1); |
| 49 | + |
| 50 | + if (count >= configuration().amount().get()) { |
| 51 | + duplicates.forEach(ChatMessage::delete); |
| 52 | + duplicates.clear(); |
| 53 | + event.chatMessage().component().append(Component.text(" [x" + count + "]")); |
| 54 | + } |
| 55 | + |
| 56 | + duplicates.add(event.chatMessage()); |
| 57 | + } |
| 58 | +} |
0 commit comments