Skip to content

Commit 9442ed6

Browse files
committed
Added mention settings for chat, added ability to configure colors.
1 parent 870c8dd commit 9442ed6

3 files changed

Lines changed: 71 additions & 21 deletions

File tree

src/main/kotlin/com/mairwunnx/projectessentials/chat/ChatUtils.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ object ChatUtils {
8484

8585
fun isGlobalChat(event: ServerChatEvent): Boolean = event.message.startsWith('!')
8686

87-
fun isCommonChat(): Boolean = !ChatModelBase.chatModel.messaging.enableRangedChat
87+
private fun isCommonChat(): Boolean = !ChatModelBase.chatModel.messaging.enableRangedChat
8888

8989
fun getMessagePattern(event: ServerChatEvent): String {
9090
return when {
@@ -93,4 +93,37 @@ object ChatUtils {
9393
else -> ChatModelBase.chatModel.messaging.messageLocalPattern
9494
}
9595
}
96+
97+
fun getMessageColor(event: ServerChatEvent): String {
98+
val messageRegex = Regex("(&.|§.){1,2}%message")
99+
when {
100+
isCommonChat() -> {
101+
val targetVariable = messageRegex.find(
102+
ChatModelBase.chatModel.messaging.messageCommonPattern
103+
)
104+
return getMessageVariable(targetVariable!!, "§f")
105+
}
106+
else -> return if (isGlobalChat(event)) {
107+
val targetVariable = messageRegex.find(
108+
ChatModelBase.chatModel.messaging.messageGlobalPattern
109+
)
110+
111+
getMessageVariable(targetVariable!!, "§f")
112+
} else {
113+
val targetVariable = messageRegex.find(
114+
ChatModelBase.chatModel.messaging.messageLocalPattern
115+
)
116+
getMessageVariable(targetVariable!!, "§7§o")
117+
}
118+
}
119+
}
120+
121+
private fun getMessageVariable(matchResult: MatchResult, callback: String): String {
122+
return matchResult.groups[0]?.value
123+
?.removeRange(
124+
matchResult.groups[0]?.value!!.lastIndexOf("%"),
125+
matchResult.groups[0]?.value!!.length
126+
)
127+
?.replace("&", "§") ?: callback
128+
}
96129
}

src/main/kotlin/com/mairwunnx/projectessentials/chat/EntryPoint.kt

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,9 @@ class EntryPoint : EssBase() {
7676
"%player", event.username
7777
)
7878
}.style.setClickEvent(
79-
ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/m ${event.username}")
79+
ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "@${event.username} ")
8080
)
8181

82-
// @MairwunNx -> &b@&dMairwunNx
83-
8482
event.component = TextComponentUtils.toTextComponent {
8583
ChatUtils.getMessagePattern(event).replace(
8684
"%group", PermissionsAPI.getUserGroup(event.username).name
@@ -98,31 +96,37 @@ class EntryPoint : EssBase() {
9896
)
9997
}.setStyle(nicknameComponent)
10098

99+
val mentionSettings = ChatModelBase.chatModel.mentions
101100
val mentions = mutableListOf<String>()
102-
Regex("@\\S[a-zA-Z0-9]*").findAll(event.component.formattedText).forEach {
103-
mentions.add(it.value)
101+
if (mentionSettings.mentionsEnabled) {
102+
Regex("@\\S[a-zA-Z0-9]*").findAll(event.component.formattedText).forEach {
103+
mentions.add(it.value)
104+
}
104105
}
106+
val anFormat = mentionSettings.mentionAtFormat.replace("&", "§")
107+
val nameFormat = mentionSettings.mentionNameFormat.replace("&", "§")
105108
mentions.forEach {
106109
event.component = TextComponentUtils.toTextComponent {
107110
event.component.formattedText.replace(
108-
it, "§c@§b${it.replace("@", "")}${when {
109-
ChatUtils.isGlobalChat(event) -> "§f"
110-
else -> if (ChatUtils.isCommonChat()) "§f" else "§7§o"
111-
}}"
111+
it,
112+
"$anFormat@$nameFormat${it.replace("@", "")}${ChatUtils.getMessageColor(event)}"
112113
)
113-
}
114+
}.setStyle(nicknameComponent)
114115
}
115116

116117
if (mentions.isNotEmpty()) {
117-
event.player.server.playerList.players.forEach {
118-
val sortedMentions = mentions.sorted()
119-
if ("@${it.name.string}" in sortedMentions) {
120-
it.sendStatusMessage(
121-
TextComponentUtils.toTextComponent {
122-
"§7you are mentioned by §l§7${event.player.name.string}§7 player, in the chat."
123-
},
124-
true
125-
)
118+
if (mentionSettings.mentionsEnabled && mentionSettings.mentionInActionBar) {
119+
event.player.server.playerList.players.forEach {
120+
if ("@${it.name.string}" in mentions) {
121+
it.sendStatusMessage(
122+
TextComponentUtils.toTextComponent {
123+
mentionSettings.mentionMessage.replace(
124+
"%player",
125+
event.player.name.string
126+
).replace("&", "§")
127+
}, true
128+
)
129+
}
126130
}
127131
}
128132
}

src/main/kotlin/com/mairwunnx/projectessentials/chat/models/ChatModel.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import kotlinx.serialization.Serializable
55
@Serializable
66
data class ChatModel(
77
var moderation: Moderation = Moderation(),
8-
var messaging: Messaging = Messaging()
8+
var messaging: Messaging = Messaging(),
9+
var mentions: Mentions = Mentions()
910
) {
1011
@Serializable
1112
data class Moderation(
@@ -24,4 +25,16 @@ data class ChatModel(
2425
var enableRangedChat: Boolean = true,
2526
var localChatRange: Int = 100
2627
)
28+
29+
@Serializable
30+
data class Mentions(
31+
var mentionsEnabled: Boolean = true,
32+
var mentionInActionBar: Boolean = true,
33+
/**
34+
* todo: move to language resources.
35+
*/
36+
var mentionMessage: String = "&7you are mentioned by &l&7%player&7 player, in the chat.",
37+
var mentionAtFormat: String = "&c",
38+
var mentionNameFormat: String = "&b"
39+
)
2740
}

0 commit comments

Comments
 (0)