Skip to content

Commit fc6b8e9

Browse files
committed
Configure-warps command implemented.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent b23ff99 commit fc6b8e9

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.mairwunnx.projectessentials.warps.commands
2+
3+
import com.mairwunnx.projectessentials.core.api.v1.MESSAGE_CORE_PREFIX
4+
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI
5+
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandBase
6+
import com.mairwunnx.projectessentials.core.api.v1.extensions.getPlayer
7+
import com.mairwunnx.projectessentials.core.api.v1.extensions.isPlayerSender
8+
import com.mairwunnx.projectessentials.core.api.v1.extensions.playerName
9+
import com.mairwunnx.projectessentials.core.api.v1.messaging.MessagingAPI
10+
import com.mairwunnx.projectessentials.core.api.v1.messaging.ServerMessagingAPI
11+
import com.mairwunnx.projectessentials.core.api.v1.permissions.hasPermission
12+
import com.mairwunnx.projectessentials.warps.warpsSettingsConfiguration
13+
import com.mojang.brigadier.context.CommandContext
14+
import net.minecraft.command.CommandSource
15+
import org.apache.logging.log4j.LogManager
16+
17+
object ConfigureWarpsCommand : CommandBase(configureWarpsLiteral, false) {
18+
override val name = "configure-warps"
19+
20+
fun playSoundOnTeleport(context: CommandContext<CommandSource>): Int {
21+
validate(
22+
context,
23+
"ess.configure.warps.play-sound-on-teleport",
24+
"play-sound-on-teleport"
25+
) {
26+
val value = CommandAPI.getBool(context, "value")
27+
val oldValue = warpsSettingsConfiguration.playSoundOnTeleport
28+
warpsSettingsConfiguration.playSoundOnTeleport = value
29+
changed(
30+
context, "play-sound-on-teleport", oldValue.toString(), value.toString()
31+
).also { super.process(context) }
32+
}
33+
return 0
34+
}
35+
36+
fun showEffectOnTeleport(context: CommandContext<CommandSource>): Int {
37+
validate(
38+
context,
39+
"ess.configure.warps.show-effect-on-teleport",
40+
"show-effect-on-teleport"
41+
) {
42+
val value = CommandAPI.getBool(context, "value")
43+
val oldValue = warpsSettingsConfiguration.showEffectOnTeleport
44+
warpsSettingsConfiguration.showEffectOnTeleport = value
45+
changed(
46+
context, "show-effect-on-teleport", oldValue.toString(), value.toString()
47+
).also { super.process(context) }
48+
}
49+
return 0
50+
}
51+
52+
fun addResistanceEffect(context: CommandContext<CommandSource>): Int {
53+
validate(
54+
context,
55+
"ess.configure.warps.add-resistance-effect",
56+
"add-resistance-effect"
57+
) {
58+
val value = CommandAPI.getBool(context, "value")
59+
val oldValue = warpsSettingsConfiguration.addResistanceEffect
60+
warpsSettingsConfiguration.addResistanceEffect = value
61+
changed(
62+
context, "add-resistance-effect", oldValue.toString(), value.toString()
63+
).also { super.process(context) }
64+
}
65+
return 0
66+
}
67+
68+
fun resistanceEffectDuration(context: CommandContext<CommandSource>): Int {
69+
validate(
70+
context,
71+
"ess.configure.warps.resistance-effect-duration",
72+
"resistance-effect-duration"
73+
) {
74+
val value = CommandAPI.getInt(context, "value")
75+
val oldValue = warpsSettingsConfiguration.resistanceEffectDuration
76+
warpsSettingsConfiguration.resistanceEffectDuration = value
77+
changed(
78+
context, "resistance-effect-duration", oldValue.toString(), value.toString()
79+
).also { super.process(context) }
80+
}
81+
return 0
82+
}
83+
84+
private fun validate(
85+
context: CommandContext<CommandSource>,
86+
node: String,
87+
setting: String,
88+
action: (isServer: Boolean) -> Unit
89+
) = context.getPlayer()?.let {
90+
if (hasPermission(it, node, 4)) {
91+
action(false)
92+
} else {
93+
MessagingAPI.sendMessage(
94+
context.getPlayer()!!,
95+
"$MESSAGE_CORE_PREFIX.configure.restricted",
96+
args = *arrayOf(setting)
97+
)
98+
}
99+
} ?: run { action(true) }
100+
101+
private fun changed(
102+
context: CommandContext<CommandSource>,
103+
setting: String,
104+
oldValue: String,
105+
value: String
106+
) = if (context.isPlayerSender()) {
107+
LogManager.getLogger().info(
108+
"Setting name `$setting` value changed by ${context.playerName()} from `$oldValue` to $value"
109+
)
110+
MessagingAPI.sendMessage(
111+
context.getPlayer()!!,
112+
"$MESSAGE_CORE_PREFIX.configure.successfully",
113+
args = *arrayOf(setting, oldValue, value)
114+
)
115+
} else {
116+
ServerMessagingAPI.response {
117+
"Setting name `$setting` value changed from `$oldValue` to $value"
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)