|
1 | 1 | package com.mairwunnx.projectessentials.chat.impl.handlers |
2 | 2 |
|
| 3 | +import com.mairwunnx.projectessentials.chat.api.ChatMuteAPI |
| 4 | +import com.mairwunnx.projectessentials.chat.api.ChatSlowmodeAPI |
| 5 | +import com.mairwunnx.projectessentials.chat.api.parser.ChatParserAPI |
| 6 | +import com.mairwunnx.projectessentials.chat.api.validator.ChatValidatorAPI |
| 7 | +import com.mairwunnx.projectessentials.chat.chatSettingsConfiguration |
| 8 | +import com.mairwunnx.projectessentials.core.api.v1.MESSAGE_MODULE_PREFIX |
| 9 | +import com.mairwunnx.projectessentials.core.api.v1.extensions.currentDimension |
| 10 | +import com.mairwunnx.projectessentials.core.api.v1.messaging.MessagingAPI |
| 11 | +import com.mairwunnx.projectessentials.core.api.v1.permissions.hasPermission |
| 12 | +import net.minecraft.network.play.server.SChatPacket |
| 13 | +import net.minecraft.util.text.ChatType |
3 | 14 | import net.minecraftforge.event.ServerChatEvent |
4 | 15 |
|
5 | 16 | internal object ChatMessageHandler { |
| 17 | + private inline fun ServerChatEvent.cancelAndReturn(action: () -> Unit) { |
| 18 | + run { this.isCanceled = true }.also { action() } |
| 19 | + } |
| 20 | + |
6 | 21 | fun handle(event: ServerChatEvent) { |
| 22 | + var message = event.message |
| 23 | + |
| 24 | + fun out(action: String, vararg args: String) = MessagingAPI.sendMessage( |
| 25 | + event.player, "${MESSAGE_MODULE_PREFIX}chat.$action", args = *args |
| 26 | + ) |
| 27 | + |
| 28 | + if (!chatSettingsConfiguration.messaging.chatEnabled) { |
| 29 | + if (!hasPermission(event.player, "ess.chat.disabled.except", 4)) { |
| 30 | + out("disabled").run { event.cancelAndReturn { return } } |
| 31 | + } |
| 32 | + } |
| 33 | + if (!hasPermission(event.player, "ess.chat", 0)) { |
| 34 | + out("restricted").run { event.cancelAndReturn { return } } |
| 35 | + } |
| 36 | + |
| 37 | + if (ChatMuteAPI.isInMute(event.player.name.string, event.player.uniqueID.toString())) { |
| 38 | + val entry = ChatMuteAPI.getMutePlayer( |
| 39 | + event.player.name.string, event.player.uniqueID.toString() |
| 40 | + ) |
| 41 | + |
| 42 | + fun reason() = entry?.reason?.replace(" ", " §7") |
| 43 | + ?: chatSettingsConfiguration.mute.defaultReason.replace(" ", " §7") |
| 44 | + |
| 45 | + out( |
| 46 | + if (entry?.isTemp == true) "tempmuted" else "muted", |
| 47 | + entry?.mutedBy ?: "#server", reason() |
| 48 | + ).run { event.cancelAndReturn { return } } |
| 49 | + } |
| 50 | + |
| 51 | + if (chatSettingsConfiguration.moderation.messagingSlowMode > 0) { |
| 52 | + if (!hasPermission(event.player, "ess.chat.slowmode.except", 3)) { |
| 53 | + val isExpired = ChatSlowmodeAPI.isExpired( |
| 54 | + event.player.name.string, |
| 55 | + chatSettingsConfiguration.moderation.messagingSlowMode |
| 56 | + ) |
| 57 | + if (isExpired) { |
| 58 | + ChatSlowmodeAPI.action(event.player.name.string) |
| 59 | + } else { |
| 60 | + val left = chatSettingsConfiguration.moderation.messagingSlowMode.minus( |
| 61 | + ChatSlowmodeAPI.passed(event.player.name.string) |
| 62 | + ).toString() |
| 63 | + out("slowmode", left).run { event.cancelAndReturn { return } } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (chatSettingsConfiguration.filters.filterAdvertising) { |
| 69 | + if (!hasPermission(event.player, "ess.chat.filter.advertising.bypass", 3)) { |
| 70 | + if (ChatValidatorAPI.validator.isContainsAdvertising(message)) { |
| 71 | + out("advertising").run { event.cancelAndReturn { return } } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (chatSettingsConfiguration.filters.filterChars) { |
| 77 | + if (!hasPermission(event.player, "ess.chat.filter.chars.bypass", 3)) { |
| 78 | + if ( |
| 79 | + ChatValidatorAPI.validator.isContainsBlockedChars(message) || |
| 80 | + !ChatValidatorAPI.validator.isContainsAllowedChars(message) |
| 81 | + ) out("chars").run { event.cancelAndReturn { return } } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (chatSettingsConfiguration.filters.filterExceeds) { |
| 86 | + if (!hasPermission(event.player, "ess.chat.filter.length.bypass", 3)) { |
| 87 | + if (ChatValidatorAPI.validator.isMessageExceeds(message)) { |
| 88 | + out("length").run { event.cancelAndReturn { return } } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (chatSettingsConfiguration.filters.filterWords) { |
| 94 | + if (!hasPermission(event.player, "ess.chat.filter.words.bypass", 3)) { |
| 95 | + if (ChatValidatorAPI.validator.isContainsBlockedWord(message)) { |
| 96 | + if ( |
| 97 | + chatSettingsConfiguration.moderation.modifyBlockedWords && |
| 98 | + chatSettingsConfiguration.moderation.blockedWordsMask.isNotEmpty() |
| 99 | + ) { |
| 100 | + chatSettingsConfiguration.moderation.blockedWords.forEach { |
| 101 | + message = message.replace( |
| 102 | + it, chatSettingsConfiguration.moderation.blockedWordsMask |
| 103 | + ) |
| 104 | + } |
| 105 | + } else out("words").run { event.cancelAndReturn { return } } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (chatSettingsConfiguration.moderation.colorsAllowed) { |
| 111 | + if ("&" in message) { |
| 112 | + if (hasPermission(event.player, "ess.chat.color", 2)) { |
| 113 | + out("color").run { event.cancelAndReturn { return } } |
| 114 | + } else { |
| 115 | + message = message.replace("&", "§") |
| 116 | + } |
| 117 | + } |
| 118 | + } else if ("&" in message) out("color").run { event.cancelAndReturn { return } } |
7 | 119 |
|
| 120 | + if (chatSettingsConfiguration.messaging.enableRangedChat) { |
| 121 | + if (!ChatValidatorAPI.validator.isGlobalChat(message)) { |
| 122 | + event.player.server.playerList.sendToAllNearExcept( |
| 123 | + null, |
| 124 | + event.player.positionVec.x, |
| 125 | + event.player.positionVec.y, |
| 126 | + event.player.positionVec.z, |
| 127 | + chatSettingsConfiguration.messaging.localChatRange.toDouble(), |
| 128 | + event.player.currentDimension, |
| 129 | + SChatPacket(ChatParserAPI.parser.parse(event.player, message), ChatType.SYSTEM) |
| 130 | + ).run { event.cancelAndReturn { return } } |
| 131 | + } |
| 132 | + } |
| 133 | + event.component = ChatParserAPI.parser.parse(event.player, message) |
8 | 134 | } |
9 | 135 | } |
0 commit comments