11package com.mairwunnx.projectessentials.chat
22
3- import com.mairwunnx.projectessentials.chat.models.ChatModelBase
3+ import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
44import com.mairwunnx.projectessentialscore.EssBase
5+ import com.mairwunnx.projectessentialscore.extensions.empty
56import com.mairwunnx.projectessentialscore.extensions.sendMsg
67import com.mairwunnx.projectessentialspermissions.permissions.PermissionsAPI
8+ import net.minecraft.entity.player.ServerPlayerEntity
79import net.minecraft.util.math.AxisAlignedBB
810import net.minecraft.util.text.TextComponentUtils
911import net.minecraft.util.text.event.ClickEvent
@@ -26,36 +28,57 @@ class EntryPoint : EssBase() {
2628 validateForgeVersion()
2729 logger.debug(" Register event bus for $modName mod ..." )
2830 MinecraftForge .EVENT_BUS .register(this )
29- ChatModelBase .loadData()
31+ loadAdditionalModules()
32+ ChatModelUtils .loadData()
33+ }
34+
35+ private fun loadAdditionalModules () {
36+ try {
37+ Class .forName(
38+ " com.mairwunnx.projectessentials.permissions.permissions.PermissionsAPI"
39+ )
40+ permissionsInstalled = true
41+ logger.info(" Permissions module found!" )
42+ } catch (_: ClassNotFoundException ) {
43+ // ignored
44+ }
3045 }
3146
3247 companion object {
3348 lateinit var modInstance: EntryPoint
49+ var permissionsInstalled: Boolean = false
50+
51+ fun hasPermission (player : ServerPlayerEntity , node : String , opLevel : Int = 0): Boolean =
52+ if (permissionsInstalled) {
53+ PermissionsAPI .hasPermission(player.name.string, node)
54+ } else {
55+ player.server.opPermissionLevel >= opLevel
56+ }
3457 }
3558
3659 @Suppress(" UNUSED_PARAMETER" )
3760 @SubscribeEvent
3861 fun onServerStopping (it : FMLServerStoppingEvent ) {
3962 logger.info(" Shutting down $modName mod ..." )
40- ChatModelBase .saveData()
63+ ChatModelUtils .saveData()
4164 }
4265
4366 @SubscribeEvent
4467 fun onChatMessage (event : ServerChatEvent ) {
45- if (! ChatModelBase .chatModel.messaging.chatEnabled) {
68+ if (! ChatModelUtils .chatModel.messaging.chatEnabled) {
4669 sendMsg(" chat" , event.player.commandSource, " chat.disabled" )
4770 event.isCanceled = true
4871 return
4972 }
5073
51- if (! PermissionsAPI . hasPermission(event.username , " ess.chat" )) {
74+ if (! hasPermission(event.player , " ess.chat" )) {
5275 sendMsg(" chat" , event.player.commandSource, " chat.restricted" )
5376 event.isCanceled = true
5477 return
5578 }
5679
5780 if (event.message.contains(" &" )) {
58- if (PermissionsAPI . hasPermission(event.username , " ess.chat.color" )) {
81+ if (hasPermission(event.player , " ess.chat.color" , 2 )) {
5982 event.component = TextComponentUtils .toTextComponent {
6083 event.message.replace(" &" , " §" )
6184 }
@@ -91,12 +114,22 @@ class EntryPoint : EssBase() {
91114 " %player" , event.username
92115 )
93116 }.style.setClickEvent(
94- ClickEvent (ClickEvent .Action .SUGGEST_COMMAND , " !@${event.username} " )
117+ if (ChatUtils .isCommonChat()) {
118+ ClickEvent (ClickEvent .Action .SUGGEST_COMMAND , " @${event.username} " )
119+ } else {
120+ ClickEvent (ClickEvent .Action .SUGGEST_COMMAND , " !@${event.username} " )
121+ }
95122 )
96123
124+ val group =
125+ when {
126+ permissionsInstalled -> PermissionsAPI .getUserGroup(event.username).name
127+ else -> String .empty
128+ }
129+
97130 event.component = TextComponentUtils .toTextComponent {
98131 ChatUtils .getMessagePattern(event).replace(
99- " %group" , PermissionsAPI .getUserGroup(event.username).name
132+ " %group" , group
100133 ).replace(
101134 " %player" , event.username
102135 ).replace(
@@ -111,11 +144,18 @@ class EntryPoint : EssBase() {
111144 )
112145 }.setStyle(nicknameComponent)
113146
114- val mentionSettings = ChatModelBase .chatModel.mentions
147+ val mentionSettings = ChatModelUtils .chatModel.mentions
115148 val mentions = mutableListOf<String >()
116149 if (mentionSettings.mentionsEnabled) {
117150 Regex (" @\\ S[a-zA-Z0-9]*" ).findAll(event.component.formattedText).forEach {
118- mentions.add(it.value)
151+ if (it.value != " @e" &&
152+ it.value != " @a" &&
153+ it.value != " @p" &&
154+ it.value != " @r" &&
155+ it.value != " @s"
156+ ) {
157+ mentions.add(it.value)
158+ }
119159 }
120160 }
121161 val anFormat = mentionSettings.mentionAtFormat.replace(" &" , " §" )
@@ -149,12 +189,12 @@ class EntryPoint : EssBase() {
149189 if (! ChatUtils .isGlobalChat(event)) {
150190 val players = event.player.serverWorld.getEntitiesWithinAABB(
151191 event.player.entity.javaClass, AxisAlignedBB (
152- event.player.posX - ChatModelBase .chatModel.messaging.localChatRange / 2 ,
153- event.player.posY - ChatModelBase .chatModel.messaging.localChatRange / 2 ,
154- event.player.posZ - ChatModelBase .chatModel.messaging.localChatRange / 2 ,
155- event.player.posX + ChatModelBase .chatModel.messaging.localChatRange / 2 ,
156- event.player.posY + ChatModelBase .chatModel.messaging.localChatRange / 2 ,
157- event.player.posZ + ChatModelBase .chatModel.messaging.localChatRange / 2
192+ event.player.posX - ChatModelUtils .chatModel.messaging.localChatRange / 2 ,
193+ event.player.posY - ChatModelUtils .chatModel.messaging.localChatRange / 2 ,
194+ event.player.posZ - ChatModelUtils .chatModel.messaging.localChatRange / 2 ,
195+ event.player.posX + ChatModelUtils .chatModel.messaging.localChatRange / 2 ,
196+ event.player.posY + ChatModelUtils .chatModel.messaging.localChatRange / 2 ,
197+ event.player.posZ + ChatModelUtils .chatModel.messaging.localChatRange / 2
158198 )
159199 )
160200 players.forEach {
0 commit comments