Skip to content

Commit c7b473d

Browse files
committed
/unmute command implemented.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent b7cfc65 commit c7b473d

3 files changed

Lines changed: 140 additions & 1 deletion

File tree

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

Lines changed: 2 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.ClearChatCommand
55
import com.mairwunnx.projectessentials.chat.commands.MuteCommand
6+
import com.mairwunnx.projectessentials.chat.commands.UnmuteCommand
67
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
78
import com.mairwunnx.projectessentials.core.EssBase
89
import com.mairwunnx.projectessentials.core.extensions.empty
@@ -71,6 +72,7 @@ class EntryPoint : EssBase() {
7172
fun onServerStarting(it: FMLServerStartingEvent) {
7273
ClearChatCommand.register(it.commandDispatcher)
7374
MuteCommand.register(it.commandDispatcher)
75+
UnmuteCommand.register(it.commandDispatcher)
7476
}
7577

7678
@Suppress("UNUSED_PARAMETER")
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.mairwunnx.projectessentials.chat.commands
2+
3+
import com.mairwunnx.projectessentials.chat.EntryPoint
4+
import com.mairwunnx.projectessentials.chat.api.MuteAPI
5+
import com.mairwunnx.projectessentials.chat.models.ChatModelUtils
6+
import com.mairwunnx.projectessentials.core.extensions.isPlayerSender
7+
import com.mairwunnx.projectessentials.core.extensions.playerName
8+
import com.mairwunnx.projectessentials.core.extensions.sendMsg
9+
import com.mojang.brigadier.CommandDispatcher
10+
import com.mojang.brigadier.arguments.StringArgumentType
11+
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
12+
import com.mojang.brigadier.context.CommandContext
13+
import net.minecraft.command.CommandSource
14+
import net.minecraft.command.Commands
15+
import net.minecraft.command.arguments.EntityArgument
16+
import net.minecraft.entity.player.ServerPlayerEntity
17+
import net.minecraft.util.text.TranslationTextComponent
18+
import org.apache.logging.log4j.LogManager
19+
20+
object UnmuteCommand {
21+
private val logger = LogManager.getLogger()
22+
23+
fun register(dispatcher: CommandDispatcher<CommandSource>) {
24+
logger.info("Register \"/unmute\" command")
25+
26+
dispatcher.register(
27+
literal<CommandSource>("unmute")
28+
.then(
29+
Commands.argument(
30+
"player", EntityArgument.player()
31+
).executes {
32+
return@executes execute(it)
33+
}
34+
).then(
35+
Commands.argument(
36+
"player name", StringArgumentType.string()
37+
).executes {
38+
return@executes execute(it)
39+
}
40+
)
41+
)
42+
}
43+
44+
private fun execute(
45+
context: CommandContext<CommandSource>
46+
): Int {
47+
if (context.isPlayerSender() && !EntryPoint.hasPermission(
48+
context.source.asPlayer(),
49+
"ess.chat.unmute",
50+
3
51+
)
52+
) {
53+
sendMsg("chat", context.source, "chat.unmute_restricted")
54+
return 0
55+
}
56+
57+
val player = getPlayer(context)
58+
val playerName = getPlayerName(context)
59+
60+
if (player != null) {
61+
if (MuteAPI.unmutePlayer(player.name.string)) {
62+
if (ChatModelUtils.chatModel.mute.notifyAllAboutUnmute) {
63+
if (ChatModelUtils.chatModel.mute.notifyAllAboutMute) {
64+
context.source.server.playerList.sendMessage(
65+
TranslationTextComponent(
66+
"project_essentials_chat.notify_unmuted",
67+
player.name.string,
68+
context.playerName()
69+
)
70+
)
71+
}
72+
}
73+
74+
sendMsg(
75+
"chat",
76+
player.commandSource,
77+
"chat.youre_unmuted",
78+
context.playerName()
79+
)
80+
sendMsg(
81+
"chat",
82+
context.source,
83+
"chat.unmute_success",
84+
player.name.string
85+
)
86+
} else {
87+
sendMsg(
88+
"chat",
89+
context.source,
90+
"chat.unmute_failed",
91+
player.name.string
92+
)
93+
}
94+
} else if (playerName != null) {
95+
if (MuteAPI.unmutePlayer(playerName)) {
96+
if (ChatModelUtils.chatModel.mute.notifyAllAboutMute) {
97+
context.source.server.playerList.sendMessage(
98+
TranslationTextComponent(
99+
"project_essentials_chat.notify_unmuted",
100+
playerName,
101+
context.playerName()
102+
)
103+
)
104+
}
105+
106+
sendMsg(
107+
"chat",
108+
context.source,
109+
"chat.unmute_success",
110+
playerName
111+
)
112+
} else {
113+
sendMsg(
114+
"chat",
115+
context.source,
116+
"chat.unmute_failed",
117+
playerName
118+
)
119+
}
120+
}
121+
122+
return 0
123+
}
124+
125+
private fun getPlayer(context: CommandContext<CommandSource>): ServerPlayerEntity? = try {
126+
EntityArgument.getPlayer(context, "player")
127+
} catch (ex: IllegalArgumentException) {
128+
null
129+
}
130+
131+
private fun getPlayerName(context: CommandContext<CommandSource>): String? = try {
132+
StringArgumentType.getString(context, "player name")
133+
} catch (ex: IllegalArgumentException) {
134+
null
135+
}
136+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ data class ChatModel(
5353
data class Mute(
5454
var defaultReason: String = "Reason was not provided.",
5555
var ignoredPlayers: List<String> = listOf(),
56-
var notifyAllAboutMute: Boolean = true
56+
var notifyAllAboutMute: Boolean = true,
57+
var notifyAllAboutUnmute: Boolean = true
5758
)
5859
}

0 commit comments

Comments
 (0)