Skip to content

Commit 1dba347

Browse files
committed
Compatibility with safe-localization messages added.
Signed-off-by: Pavel Erokhin (MairwunNx) <[email protected]>
1 parent 5d83d76 commit 1dba347

File tree

8 files changed

+120
-120
lines changed

8 files changed

+120
-120
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.mairwunnx.projectessentials.chat
33
import com.mairwunnx.projectessentials.chat.EntryPoint.Companion.hasPermission
44
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
55
import com.mairwunnx.projectessentials.core.extensions.empty
6-
import com.mairwunnx.projectessentials.core.extensions.sendMsg
76
import net.minecraft.util.Tuple
87
import net.minecraftforge.event.ServerChatEvent
98

@@ -33,8 +32,7 @@ object ChatUtils {
3332
fixedMessage = newMessage
3433
}
3534
} else {
36-
sendMsg(
37-
"chat",
35+
sendMessage(
3836
event.player.commandSource,
3937
"chat.blocked_word",
4038
it
@@ -55,8 +53,7 @@ object ChatUtils {
5553
val blockedChars = ChatModelUtils.chatModel.moderation.blockedChars
5654
blockedChars.forEach {
5755
if (event.message.contains(it)) {
58-
sendMsg(
59-
"chat",
56+
sendMessage(
6057
event.player.commandSource,
6158
"chat.blocked_char"
6259
)
@@ -74,8 +71,8 @@ object ChatUtils {
7471

7572
val maxLength = ChatModelUtils.chatModel.moderation.maxMessageLength
7673
if (event.message.length > maxLength) {
77-
sendMsg(
78-
"chat", event.player.commandSource, "chat.message_maxlength"
74+
sendMessage(
75+
event.player.commandSource, "chat.message_maxlength"
7976
)
8077
return false
8178
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import com.mairwunnx.projectessentials.chat.commands.*
55
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
66
import com.mairwunnx.projectessentials.chat.models.MuteModelUtils
77
import com.mairwunnx.projectessentials.core.EssBase
8+
import com.mairwunnx.projectessentials.core.configuration.localization.LocalizationConfigurationUtils
89
import com.mairwunnx.projectessentials.core.extensions.empty
9-
import com.mairwunnx.projectessentials.core.extensions.sendMsg
10+
import com.mairwunnx.projectessentials.core.localization.processLocalizations
1011
import com.mairwunnx.projectessentials.permissions.permissions.PermissionsAPI
1112
import net.minecraft.entity.player.ServerPlayerEntity
1213
import net.minecraft.util.math.AxisAlignedBB
@@ -131,8 +132,7 @@ class EntryPoint : EssBase() {
131132
val mutedBy = MuteAPI.getMuteInitiator(event.username)!!
132133
val reason = MuteAPI.getMuteReason(event.username)!!
133134

134-
sendMsg(
135-
"chat",
135+
sendMessage(
136136
event.player.commandSource,
137137
"chat.muted",
138138
mutedBy,
@@ -143,13 +143,13 @@ class EntryPoint : EssBase() {
143143
}
144144

145145
if (!ChatModelUtils.chatModel.messaging.chatEnabled) {
146-
sendMsg("chat", event.player.commandSource, "chat.disabled")
146+
sendMessage(event.player.commandSource, "chat.disabled")
147147
event.isCanceled = true
148148
return
149149
}
150150

151151
if (!hasPermission(event.player, "ess.chat")) {
152-
sendMsg("chat", event.player.commandSource, "chat.restricted")
152+
sendMessage(event.player.commandSource, "chat.restricted")
153153
event.isCanceled = true
154154
return
155155
}
@@ -163,8 +163,7 @@ class EntryPoint : EssBase() {
163163
) {
164164
ChatCooldown.addCooldown(event.player.name.string)
165165
} else {
166-
sendMsg(
167-
"chat",
166+
sendMessage(
168167
event.player.commandSource,
169168
"chat.cooldown_not_expired"
170169
)
@@ -183,8 +182,7 @@ class EntryPoint : EssBase() {
183182
)
184183
)
185184
) {
186-
sendMsg(
187-
"chat",
185+
sendMessage(
188186
event.player.commandSource,
189187
"chat.advertising_not_allowed"
190188
)
@@ -200,7 +198,7 @@ class EntryPoint : EssBase() {
200198
event.message.replace("&", "§")
201199
}
202200
} else {
203-
sendMsg("chat", event.player.commandSource, "chat.color_restricted")
201+
sendMessage(event.player.commandSource, "chat.color_restricted")
204202
event.isCanceled = true
205203
return
206204
}
@@ -306,8 +304,7 @@ class EntryPoint : EssBase() {
306304
}
307305
return
308306
} else {
309-
sendMsg(
310-
"chat",
307+
sendMessage(
311308
event.player.commandSource,
312309
"chat.mention_all_aborted"
313310
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mairwunnx.projectessentials.chat
2+
3+
import com.mairwunnx.projectessentials.core.configuration.localization.LocalizationConfigurationUtils
4+
import com.mairwunnx.projectessentials.core.extensions.sendMsg
5+
import com.mairwunnx.projectessentials.core.localization.sendMsgV2
6+
import net.minecraft.command.CommandSource
7+
8+
internal fun sendMessage(
9+
source: CommandSource,
10+
message: String,
11+
vararg args: String
12+
) {
13+
if (LocalizationConfigurationUtils.getConfig().enabled) {
14+
sendMsgV2(
15+
source.asPlayer(),
16+
"project_essentials_chat.$message", *args
17+
)
18+
} else {
19+
sendMsg(
20+
"chat", source, message, *args
21+
)
22+
}
23+
}

src/main/kotlin/com/mairwunnx/projectessentials/chat/commands/ClearChatCommand.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.mairwunnx.projectessentials.chat.commands
22

33
import com.mairwunnx.projectessentials.chat.EntryPoint
4+
import com.mairwunnx.projectessentials.chat.sendMessage
45
import com.mairwunnx.projectessentials.cooldown.essentials.CommandsAliases
56
import com.mairwunnx.projectessentials.core.extensions.isPlayerSender
67
import com.mairwunnx.projectessentials.core.extensions.playerName
7-
import com.mairwunnx.projectessentials.core.extensions.sendMsg
8-
import com.mairwunnx.projectessentials.core.helpers.PERMISSION_LEVEL
8+
import com.mairwunnx.projectessentials.core.helpers.throwPermissionLevel
99
import com.mojang.brigadier.CommandDispatcher
1010
import com.mojang.brigadier.arguments.BoolArgumentType
1111
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
@@ -65,12 +65,8 @@ object ClearChatCommand {
6565
}
6666
}
6767
} else {
68-
logger.warn(
69-
PERMISSION_LEVEL
70-
.replace("%0", context.playerName())
71-
.replace("%1", "clear-chat")
72-
)
73-
sendMsg("chat", context.source, "chat.clear_restricted")
68+
throwPermissionLevel(context.playerName(), "clear-chat")
69+
sendMessage(context.source, "chat.clear_restricted")
7470
}
7571
} else {
7672
logger.info("Command with parameter `clearOnlyForSender` can't be executed from server. Type `cls` if you use windows or `/clear` if you use mac os or linux.")
@@ -99,7 +95,7 @@ object ClearChatCommand {
9995
}
10096
}
10197
} else {
102-
sendMsg("chat", context.source, "chat.clear_other_restricted")
98+
sendMessage(context.source, "chat.clear_other_restricted")
10399
return 0
104100
}
105101
}

src/main/kotlin/com/mairwunnx/projectessentials/chat/commands/MuteCommand.kt

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import com.mairwunnx.projectessentials.chat.EntryPoint
44
import com.mairwunnx.projectessentials.chat.api.MuteAPI
55
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
66
import com.mairwunnx.projectessentials.chat.models.MuteModelUtils
7+
import com.mairwunnx.projectessentials.chat.sendMessage
78
import com.mairwunnx.projectessentials.core.extensions.isPlayerSender
89
import com.mairwunnx.projectessentials.core.extensions.playerName
9-
import com.mairwunnx.projectessentials.core.extensions.sendMsg
1010
import com.mojang.brigadier.CommandDispatcher
1111
import com.mojang.brigadier.arguments.BoolArgumentType
1212
import com.mojang.brigadier.arguments.StringArgumentType
@@ -16,7 +16,6 @@ import net.minecraft.command.CommandSource
1616
import net.minecraft.command.Commands
1717
import net.minecraft.command.arguments.EntityArgument
1818
import net.minecraft.entity.player.ServerPlayerEntity
19-
import net.minecraft.util.text.TranslationTextComponent
2019
import org.apache.logging.log4j.LogManager
2120

2221
object MuteCommand {
@@ -70,7 +69,7 @@ object MuteCommand {
7069
3
7170
)
7271
) {
73-
sendMsg("chat", context.source, "chat.mute_restricted")
72+
sendMessage(context.source, "chat.mute_restricted")
7473
return 0
7574
}
7675

@@ -79,8 +78,7 @@ object MuteCommand {
7978

8079
if (player != null) {
8180
if (player.name.string in ChatModelUtils.chatModel.mute.ignoredPlayers) {
82-
sendMsg(
83-
"chat",
81+
sendMessage(
8482
context.source,
8583
"chat.mute_failed_player_ignored",
8684
player.name.string
@@ -96,41 +94,37 @@ object MuteCommand {
9694
MuteModelUtils.addPlayer(player.name.string, context.playerName(), reason)
9795

9896
if (ChatModelUtils.chatModel.mute.notifyAllAboutMute) {
99-
context.source.server.playerList.sendMessage(
100-
TranslationTextComponent(
101-
"project_essentials_chat.notify_muted",
97+
context.source.server.playerList.players.forEach {
98+
sendMessage(
99+
it.commandSource, "notify_muted",
102100
player.name.string,
103101
context.playerName(),
104102
reason.replace(" ", " §c")
105103
)
106-
)
104+
}
107105
}
108106

109-
sendMsg(
110-
"chat",
107+
sendMessage(
111108
player.commandSource,
112109
"chat.lol_youre_muted",
113110
context.playerName(),
114111
reason.replace(" ", " §7")
115112
)
116-
sendMsg(
117-
"chat",
113+
sendMessage(
118114
context.source,
119115
"chat.mute_success",
120116
player.name.string
121117
)
122118
} else {
123-
sendMsg(
124-
"chat",
119+
sendMessage(
125120
context.source,
126121
"chat.mute_failed",
127122
player.name.string
128123
)
129124
}
130125
} else if (playerName != null) {
131126
if (playerName in ChatModelUtils.chatModel.mute.ignoredPlayers) {
132-
sendMsg(
133-
"chat",
127+
sendMessage(
134128
context.source,
135129
"chat.mute_failed_player_ignored",
136130
playerName
@@ -146,25 +140,23 @@ object MuteCommand {
146140
MuteModelUtils.addPlayer(playerName, context.playerName(), reason)
147141

148142
if (ChatModelUtils.chatModel.mute.notifyAllAboutMute) {
149-
context.source.server.playerList.sendMessage(
150-
TranslationTextComponent(
151-
"project_essentials_chat.notify_muted",
143+
context.source.server.playerList.players.forEach {
144+
sendMessage(
145+
it.commandSource, "notify_muted",
152146
playerName,
153147
context.playerName(),
154148
reason.replace(" ", " §c")
155149
)
156-
)
150+
}
157151
}
158152

159-
sendMsg(
160-
"chat",
153+
sendMessage(
161154
context.source,
162155
"chat.mute_success",
163156
playerName
164157
)
165158
} else {
166-
sendMsg(
167-
"chat",
159+
sendMessage(
168160
context.source,
169161
"chat.mute_failed",
170162
playerName

src/main/kotlin/com/mairwunnx/projectessentials/chat/commands/MutedPlayersCommand.kt

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,47 @@ package com.mairwunnx.projectessentials.chat.commands
22

33
import com.mairwunnx.projectessentials.chat.EntryPoint
44
import com.mairwunnx.projectessentials.chat.api.MuteAPI
5+
import com.mairwunnx.projectessentials.chat.sendMessage
56
import com.mairwunnx.projectessentials.core.extensions.isPlayerSender
6-
import com.mairwunnx.projectessentials.core.extensions.sendMsg
7+
import com.mairwunnx.projectessentials.core.localization.getLocalizedString
8+
import com.mojang.authlib.GameProfile
79
import com.mojang.brigadier.CommandDispatcher
810
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
911
import com.mojang.brigadier.context.CommandContext
1012
import net.minecraft.command.CommandSource
13+
import net.minecraft.entity.player.ServerPlayerEntity
14+
import net.minecraft.server.MinecraftServer
1115
import net.minecraft.util.text.TextComponentUtils
12-
import net.minecraft.util.text.TranslationTextComponent
16+
import net.minecraft.world.dimension.DimensionType
17+
import net.minecraftforge.common.util.FakePlayer
1318
import org.apache.logging.log4j.LogManager
19+
import java.util.*
1420

1521
object MutedPlayersCommand {
1622
private val logger = LogManager.getLogger()
17-
private val locMessageMutedPlayer = TranslationTextComponent(
18-
"project_essentials_chat.chat.out_muted_players"
19-
).formattedText
20-
private val locMessageMutedBy = TranslationTextComponent(
21-
"project_essentials_chat.chat.out_muted_by"
22-
).formattedText
23-
private val locMessageReason = TranslationTextComponent(
24-
"project_essentials_chat.chat.out_reason"
25-
).formattedText
26-
private val locMessageNone = TranslationTextComponent(
27-
"project_essentials_chat.chat.out_muted_players_none"
28-
).formattedText
23+
private const val domain = "project_essentials_chat.chat"
24+
private var player: ServerPlayerEntity? = null
25+
private var server: MinecraftServer? = null
26+
27+
private val fakePlayer by lazy {
28+
FakePlayer(
29+
server?.getWorld(DimensionType.OVERWORLD),
30+
GameProfile(UUID.randomUUID(), "#thisIsBug")
31+
)
32+
}
33+
34+
private val locMessageMutedPlayer = getLocalizedString(
35+
player ?: fakePlayer, "$domain.out_muted_players"
36+
)
37+
private val locMessageMutedBy = getLocalizedString(
38+
player ?: fakePlayer, "$domain.out_muted_by"
39+
)
40+
private val locMessageReason = getLocalizedString(
41+
player ?: fakePlayer, "$domain.out_reason"
42+
)
43+
private val locMessageNone = getLocalizedString(
44+
player ?: fakePlayer, "$domain.out_muted_players_none"
45+
)
2946

3047
fun register(dispatcher: CommandDispatcher<CommandSource>) {
3148
logger.info("Register \"/muted-players\" command")
@@ -38,16 +55,20 @@ object MutedPlayersCommand {
3855
private fun execute(
3956
context: CommandContext<CommandSource>
4057
): Int {
58+
server = context.source.server
59+
4160
if (context.isPlayerSender() && !EntryPoint.hasPermission(
4261
context.source.asPlayer(),
4362
"ess.chat.muted",
4463
3
4564
)
4665
) {
47-
sendMsg("chat", context.source, "chat.muted_restricted")
66+
sendMessage(context.source, "chat.muted_restricted")
4867
return 0
4968
}
5069

70+
if (context.isPlayerSender()) player = context.source.asPlayer()
71+
5172
val mutedPlayers = MuteAPI.getMutedPlayers()
5273
val message = buildString {
5374
this.append("§7$locMessageMutedPlayer:\n")

0 commit comments

Comments
 (0)