|
1 | 1 | package com.mairwunnx.projectessentials.chat.impl.parsers |
2 | 2 |
|
3 | 3 | import com.mairwunnx.projectessentials.chat.api.parser.IMessageParser |
| 4 | +import com.mairwunnx.projectessentials.chat.api.validator.ChatValidatorAPI |
| 5 | +import com.mairwunnx.projectessentials.chat.api.variables.ChatVariableAPI |
| 6 | +import com.mairwunnx.projectessentials.chat.chatSettingsConfiguration |
| 7 | +import com.mairwunnx.projectessentials.core.api.v1.extensions.empty |
| 8 | +import com.mairwunnx.projectessentials.core.api.v1.extensions.playSound |
| 9 | +import com.mairwunnx.projectessentials.core.api.v1.permissions.hasPermission |
4 | 10 | import net.minecraft.entity.player.ServerPlayerEntity |
| 11 | +import net.minecraft.util.SoundEvents |
5 | 12 | import net.minecraft.util.text.ITextComponent |
| 13 | +import net.minecraft.util.text.TextComponentUtils |
6 | 14 |
|
7 | | -class DefaultMessageParser : IMessageParser { |
8 | | - override fun parse(sender: ServerPlayerEntity, message: ITextComponent): String { |
9 | | - TODO("Not yet implemented") |
| 15 | +internal class DefaultMessageParser : IMessageParser { |
| 16 | + override fun parse(sender: ServerPlayerEntity, message: String): ITextComponent { |
| 17 | + val component = TextComponentUtils.toTextComponent { String.empty } |
| 18 | + val builder = StringBuilder() |
| 19 | + val common = !chatSettingsConfiguration.messaging.enableRangedChat |
| 20 | + val global = ChatValidatorAPI.validator.isGlobalChat(message) && !common |
| 21 | + return when { |
| 22 | + common -> chatSettingsConfiguration.messaging.messageCommonPattern |
| 23 | + global -> chatSettingsConfiguration.messaging.messageGlobalPattern |
| 24 | + else -> chatSettingsConfiguration.messaging.messageLocalPattern |
| 25 | + }.replace("&", "§").let { pattern -> |
| 26 | + val matches = Regex("(?!:.*)(%\\w+%)*").findAll(pattern).toList() |
| 27 | + val variables = matches.count() |
| 28 | + builder.append(pattern[0]) |
| 29 | + repeat(variables) { index -> |
| 30 | + matches[index].apply { |
| 31 | + if (value.isBlank()) { |
| 32 | + try { |
| 33 | + builder.append(pattern[range.first]) |
| 34 | + } catch (ex: IndexOutOfBoundsException) { |
| 35 | + // Ignored. |
| 36 | + } |
| 37 | + } else if (value in ChatVariableAPI.all().map { "%${it.variable}%" }) { |
| 38 | + component.appendSibling(TextComponentUtils.toTextComponent { builder.toString() }) |
| 39 | + if (value == "%message%") { |
| 40 | + component.appendSibling(getMessage(sender, message)) |
| 41 | + } else { |
| 42 | + component.appendSibling( |
| 43 | + ChatVariableAPI.all().find { |
| 44 | + it.variable == value.drop(1).dropLast(1) |
| 45 | + }?.process(sender) ?: TextComponentUtils.toTextComponent { "" } |
| 46 | + ).also { builder.clear() } |
| 47 | + } |
| 48 | + } else builder.append(String.empty) |
| 49 | + }.run { |
| 50 | + component.appendSibling( |
| 51 | + TextComponentUtils.toTextComponent { builder.toString() } |
| 52 | + ).also { builder.clear() } |
| 53 | + } |
| 54 | + }.let { component } |
| 55 | + } |
10 | 56 | } |
| 57 | + |
| 58 | + private fun getMessage(sender: ServerPlayerEntity, message: String): ITextComponent { |
| 59 | + var newMessage = message |
| 60 | + |
| 61 | + fun default() = TextComponentUtils.toTextComponent { message } |
| 62 | + |
| 63 | + if (message.first() == '!') message.drop(1) |
| 64 | + if (!hasPermission(sender, "ess.chat.mention", 0)) return default() |
| 65 | + |
| 66 | + if (chatSettingsConfiguration.mentions.mentionsEnabled) { |
| 67 | + val mentions = mutableListOf<String>() |
| 68 | + val anFormat = chatSettingsConfiguration.mentions.mentionAtFormat.replace("&", "§") |
| 69 | + val nameFormat = chatSettingsConfiguration.mentions.mentionNameFormat.replace("&", "§") |
| 70 | + |
| 71 | + Regex("@\\S[a-zA-Z0-9]*", RegexOption.IGNORE_CASE).findAll(message).forEach { |
| 72 | + if (it.value != "@e" && it.value != "@a" && it.value != "@p" && it.value != "@r" && it.value != "@s") { |
| 73 | + mentions.add(it.value) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + mentions.forEach { |
| 78 | + newMessage = message.replace( |
| 79 | + it, "$anFormat@$nameFormat${it.drop(1)}§r" |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + if (mentions.isNotEmpty()) { |
| 84 | + if (chatSettingsConfiguration.mentions.mentionInActionBar) { |
| 85 | + if ("@${chatSettingsConfiguration.mentions.mentionsAllLiteral}" in mentions) { |
| 86 | + if (chatSettingsConfiguration.mentions.mentionsAllEnabled) { |
| 87 | + if (hasPermission(sender, "ess.chat.mention.all", 3)) { |
| 88 | + sender.server.playerList.players.filter { |
| 89 | + it.name.string != sender.name.string |
| 90 | + }.forEach { notify(it, sender) } |
| 91 | + } |
| 92 | + } |
| 93 | + } else { |
| 94 | + sender.server.playerList.players.filter { |
| 95 | + "@${it.name.string}" in mentions |
| 96 | + }.forEach { notify(it, sender) } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return TextComponentUtils.toTextComponent { newMessage } |
| 102 | + } |
| 103 | + return default() |
| 104 | + } |
| 105 | + |
| 106 | + private fun notify(player: ServerPlayerEntity, sender: ServerPlayerEntity) = |
| 107 | + player.sendStatusMessage( |
| 108 | + TextComponentUtils.toTextComponent { |
| 109 | + chatSettingsConfiguration.mentions.mentionMessage.replace( |
| 110 | + "%player", sender.name.string |
| 111 | + ).replace("&", "§") |
| 112 | + }, true |
| 113 | + ).also { |
| 114 | + if (!chatSettingsConfiguration.mentions.mentionWithSound) return@also |
| 115 | + player.playSound(player, SoundEvents.UI_TOAST_IN) |
| 116 | + } |
11 | 117 | } |
0 commit comments