-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMuteCommands.kt
More file actions
115 lines (105 loc) · 4.62 KB
/
MuteCommands.kt
File metadata and controls
115 lines (105 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package net.quiltservertools.wires.command
import com.mojang.authlib.GameProfile
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import net.minecraft.command.argument.GameProfileArgumentType
import net.minecraft.server.command.CommandManager
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.quiltservertools.wires.Utils.parseTime
import net.quiltservertools.wires.config.Config
import net.quiltservertools.wires.util.Permissions.hasPermission
import java.time.Instant
object MuteCommands {
const val name = "mute"
const val serverMute = "servermute"
private const val noneProvided = "<none provided>"
fun registerCommands(dispatcher: CommandDispatcher<ServerCommandSource>) {
// Mute command
dispatcher.register(CommandManager.literal(name).requires { scs: ServerCommandSource ->
scs.hasPermission(name)
}
.then(CommandManager.argument("target", GameProfileArgumentType.gameProfile())
.executes {
mutePlayer(it.source, GameProfileArgumentType.getProfileArgument(it, "target").first(), -1, noneProvided)
}
.then(CommandManager.argument("time", StringArgumentType.string())
.executes { ctx: CommandContext<ServerCommandSource> ->
mutePlayer(
ctx.source,
GameProfileArgumentType.getProfileArgument(ctx, "target").first(),
parseTime(StringArgumentType.getString(ctx, "time")), noneProvided
)
}
.then(CommandManager.argument("reason", StringArgumentType.greedyString())
.executes { ctx: CommandContext<ServerCommandSource> ->
mutePlayer(
ctx.source,
GameProfileArgumentType.getProfileArgument(ctx, "target").first(),
parseTime(StringArgumentType.getString(ctx, "time")),
StringArgumentType.getString(ctx, "reason")
)
}
)
)
)
)
// Unmute command
dispatcher.register(CommandManager.literal("unmute")
.requires { it.hasPermission(name) }
.then(CommandManager.argument("target", GameProfileArgumentType.gameProfile())
.executes {
unmutePlayer(it.source, GameProfileArgumentType.getProfileArgument(it, "target").first())
}))
// Server mute command
dispatcher.register(CommandManager.literal(serverMute).requires { scs: ServerCommandSource ->
scs.hasPermission(serverMute)
}
.executes { ctx: CommandContext<ServerCommandSource> ->
serverMute(
ctx.source,
-1
)
}
.then(CommandManager.argument("time", StringArgumentType.string())
.executes { ctx: CommandContext<ServerCommandSource> ->
serverMute(
ctx.source,
parseTime(StringArgumentType.getString(ctx, "time"))
)
}))
}
private fun mutePlayer(scs: ServerCommandSource, profile: GameProfile?, seconds: Long, reason: String): Int {
if (profile == null) {
scs.sendError(Text.literal("Unable to locate player profile with provided username").formatted(Formatting.RED))
return 0
}
Config.mute(profile, seconds, reason)
scs.sendFeedback(
{ Text.literal("Muted ${profile.name} for${
if (seconds < 0) {
"ever"
} else {
" ${seconds - Instant.now().epochSecond} seconds"
}
}") },
true
)
return 1
}
private fun unmutePlayer(scs: ServerCommandSource, profile: GameProfile): Int {
Config.unmute(profile)
scs.sendFeedback(
{ Text.literal("Unmuted ${profile.name}") },
true
)
return 1
}
private fun serverMute(scs: ServerCommandSource, time: Long): Int {
val serverMuteStatus: Boolean = Config.serverMute(time)
scs.sendFeedback({ Text.literal("Server mute ${if (serverMuteStatus) "enabled" else "disabled"}") }, true)
return 1
}
}