Skip to content

Commit fc74f70

Browse files
committed
configure-home command implemented.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent c78ede8 commit fc74f70

File tree

1 file changed

+121
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)