Skip to content

Commit 152e730

Browse files
committed
Saving muted players implemented.
Signed-off-by: Pavel Erokhin (MairwunNx) <[email protected]>
1 parent ab9a7b0 commit 152e730

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mairwunnx.projectessentials.chat
33
import com.mairwunnx.projectessentials.chat.api.MuteAPI
44
import com.mairwunnx.projectessentials.chat.commands.*
55
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
6+
import com.mairwunnx.projectessentials.chat.models.MuteModelUtils
67
import com.mairwunnx.projectessentials.core.EssBase
78
import com.mairwunnx.projectessentials.core.extensions.empty
89
import com.mairwunnx.projectessentials.core.extensions.sendMsg
@@ -33,6 +34,7 @@ class EntryPoint : EssBase() {
3334
MinecraftForge.EVENT_BUS.register(this)
3435
loadAdditionalModules()
3536
ChatModelUtils.loadData()
37+
MuteModelUtils.loadData()
3638
}
3739

3840
private fun loadAdditionalModules() {
@@ -79,6 +81,7 @@ class EntryPoint : EssBase() {
7981
@SubscribeEvent
8082
fun onServerStopping(it: FMLServerStoppingEvent) {
8183
ChatModelUtils.saveData()
84+
MuteModelUtils.saveData()
8285
}
8386

8487
@SubscribeEvent

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mairwunnx.projectessentials.chat.commands
33
import com.mairwunnx.projectessentials.chat.EntryPoint
44
import com.mairwunnx.projectessentials.chat.api.MuteAPI
55
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
6+
import com.mairwunnx.projectessentials.chat.models.MuteModelUtils
67
import com.mairwunnx.projectessentials.core.extensions.isPlayerSender
78
import com.mairwunnx.projectessentials.core.extensions.playerName
89
import com.mairwunnx.projectessentials.core.extensions.sendMsg
@@ -92,6 +93,8 @@ object MuteCommand {
9293
)
9394

9495
if (result) {
96+
MuteModelUtils.addPlayer(player.name.string, context.playerName(), reason)
97+
9598
if (ChatModelUtils.chatModel.mute.notifyAllAboutMute) {
9699
context.source.server.playerList.sendMessage(
97100
TranslationTextComponent(
@@ -140,6 +143,8 @@ object MuteCommand {
140143
)
141144

142145
if (result) {
146+
MuteModelUtils.addPlayer(playerName, context.playerName(), reason)
147+
143148
if (ChatModelUtils.chatModel.mute.notifyAllAboutMute) {
144149
context.source.server.playerList.sendMessage(
145150
TranslationTextComponent(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mairwunnx.projectessentials.chat.commands
33
import com.mairwunnx.projectessentials.chat.EntryPoint
44
import com.mairwunnx.projectessentials.chat.api.MuteAPI
55
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
6+
import com.mairwunnx.projectessentials.chat.models.MuteModelUtils
67
import com.mairwunnx.projectessentials.core.extensions.isPlayerSender
78
import com.mairwunnx.projectessentials.core.extensions.playerName
89
import com.mairwunnx.projectessentials.core.extensions.sendMsg
@@ -38,6 +39,7 @@ object UnmuteAllCommand {
3839
}
3940

4041
MuteAPI.unmuteAll()
42+
MuteModelUtils.removeAll()
4143

4244
if (ChatModelUtils.chatModel.mute.notifyAllAboutUnmuteAll) {
4345
context.source.server.playerList.sendMessage(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mairwunnx.projectessentials.chat.commands
33
import com.mairwunnx.projectessentials.chat.EntryPoint
44
import com.mairwunnx.projectessentials.chat.api.MuteAPI
55
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
6+
import com.mairwunnx.projectessentials.chat.models.MuteModelUtils
67
import com.mairwunnx.projectessentials.core.extensions.isPlayerSender
78
import com.mairwunnx.projectessentials.core.extensions.playerName
89
import com.mairwunnx.projectessentials.core.extensions.sendMsg
@@ -59,6 +60,8 @@ object UnmuteCommand {
5960

6061
if (player != null) {
6162
if (MuteAPI.unmutePlayer(player.name.string)) {
63+
MuteModelUtils.removePlayer(player.name.string)
64+
6265
if (ChatModelUtils.chatModel.mute.notifyAllAboutUnmute) {
6366
context.source.server.playerList.sendMessage(
6467
TranslationTextComponent(
@@ -91,6 +94,8 @@ object UnmuteCommand {
9194
}
9295
} else if (playerName != null) {
9396
if (MuteAPI.unmutePlayer(playerName)) {
97+
MuteModelUtils.removePlayer(playerName)
98+
9499
if (ChatModelUtils.chatModel.mute.notifyAllAboutUnmute) {
95100
context.source.server.playerList.sendMessage(
96101
TranslationTextComponent(

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ object ChatModelUtils {
2020
)
2121
File(chatConfig).writeText(defaultConfig)
2222
}
23-
val warpsConfigRaw = File(chatConfig).readText()
24-
chatModel = jsonInstance.parse(ChatModel.serializer(), warpsConfigRaw)
23+
val chatConfigRaw = File(chatConfig).readText()
24+
chatModel = jsonInstance.parse(ChatModel.serializer(), chatConfigRaw)
2525
}
2626

2727
fun saveData() {
2828
File(MOD_CONFIG_FOLDER).mkdirs()
29-
val spawnConfig = jsonInstance.stringify(
29+
val chatConfig = jsonInstance.stringify(
3030
ChatModel.serializer(), chatModel
3131
)
32-
File(chatConfig).writeText(spawnConfig)
32+
File(chatConfig).writeText(chatConfig)
3333
}
3434
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.mairwunnx.projectessentials.chat.models
2+
3+
import com.mairwunnx.projectessentials.chat.api.MuteAPI
4+
import com.mairwunnx.projectessentials.core.helpers.MOD_CONFIG_FOLDER
5+
import com.mairwunnx.projectessentials.core.helpers.jsonInstance
6+
import org.apache.logging.log4j.LogManager
7+
import java.io.File
8+
9+
object MuteModelUtils {
10+
private val mutedPlayersConfig = MOD_CONFIG_FOLDER + File.separator + "muted-players.json"
11+
private val logger = LogManager.getLogger()
12+
var muteModel = MuteModel()
13+
14+
fun loadData() {
15+
logger.info("Loading muted players configuration")
16+
if (!File(mutedPlayersConfig).exists()) {
17+
logger.warn("Muted players configuration file not exist! creating it now!")
18+
File(MOD_CONFIG_FOLDER).mkdirs()
19+
val defaultConfig = jsonInstance.stringify(
20+
MuteModel.serializer(), muteModel
21+
)
22+
File(mutedPlayersConfig).writeText(defaultConfig)
23+
}
24+
val mutedConfigRaw = File(mutedPlayersConfig).readText()
25+
muteModel = jsonInstance.parse(MuteModel.serializer(), mutedConfigRaw)
26+
processData()
27+
}
28+
29+
private fun processData() {
30+
muteModel.players.forEach {
31+
MuteAPI.mutePlayer(it.name, it.mutedBy, it.reason, false)
32+
}
33+
}
34+
35+
fun saveData() {
36+
File(MOD_CONFIG_FOLDER).mkdirs()
37+
val mutedConfig = jsonInstance.stringify(
38+
MuteModel.serializer(), muteModel
39+
)
40+
File(mutedConfig).writeText(mutedConfig)
41+
}
42+
43+
fun addPlayer(name: String, mutedBy: String, reason: String) {
44+
removePlayer(name)
45+
muteModel.players.add(MuteModel.Player(name, mutedBy, reason))
46+
}
47+
48+
fun removePlayer(name: String) {
49+
muteModel.players.removeIf {
50+
it.name == name
51+
}
52+
}
53+
54+
fun removeAll() = muteModel.players.clear()
55+
}

0 commit comments

Comments
 (0)