Skip to content

Commit e9f88d2

Browse files
committed
ConfigureBackupCommand.kt implemented.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 5207517 commit e9f88d2

1 file changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package com.mairwunnx.projectessentials.backup.commands
2+
3+
import com.mairwunnx.projectessentials.backup.configurations.BackupConfiguration
4+
import com.mairwunnx.projectessentials.core.api.v1.MESSAGE_CORE_PREFIX
5+
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI
6+
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandBase
7+
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI.getConfigurationByName
8+
import com.mairwunnx.projectessentials.core.api.v1.extensions.getPlayer
9+
import com.mairwunnx.projectessentials.core.api.v1.extensions.isPlayerSender
10+
import com.mairwunnx.projectessentials.core.api.v1.extensions.playerName
11+
import com.mairwunnx.projectessentials.core.api.v1.messaging.MessagingAPI
12+
import com.mairwunnx.projectessentials.core.api.v1.messaging.ServerMessagingAPI
13+
import com.mairwunnx.projectessentials.core.api.v1.permissions.hasPermission
14+
import com.mairwunnx.projectessentials.core.impl.commands.ConfigureEssentialsCommandAPI
15+
import com.mojang.brigadier.context.CommandContext
16+
import net.minecraft.command.CommandSource
17+
import org.apache.logging.log4j.LogManager
18+
19+
object ConfigureBackupCommand : CommandBase(configureBackupLiteral, false) {
20+
override val name = "configure-backup"
21+
22+
private val backupConfiguration by lazy {
23+
getConfigurationByName<BackupConfiguration>("backup").take()
24+
}
25+
26+
init {
27+
ConfigureEssentialsCommandAPI.required("backup-enabled")
28+
ConfigureEssentialsCommandAPI.required("first-launch-delay")
29+
}
30+
31+
fun backupEnabled(context: CommandContext<CommandSource>) = 0.also {
32+
val setting = "backup-enabled"
33+
validate(context, "ess.configure.backup.$setting", setting) {
34+
val value = CommandAPI.getBool(context, "value")
35+
val oldValue = backupConfiguration.backupEnabled
36+
backupConfiguration.backupEnabled = value
37+
changed(
38+
context, setting, oldValue.toString(), value.toString()
39+
).also { super.process(context) }
40+
}
41+
}
42+
43+
fun firstLaunchDelay(context: CommandContext<CommandSource>) = 0.also {
44+
val setting = "first-launch-delay"
45+
validate(context, "ess.configure.backup.$setting", setting) {
46+
val value = CommandAPI.getBool(context, "value")
47+
val oldValue = backupConfiguration.firstLaunchDelay
48+
backupConfiguration.firstLaunchDelay = value
49+
changed(
50+
context, setting, oldValue.toString(), value.toString()
51+
).also { super.process(context) }
52+
}
53+
}
54+
55+
fun backupConfigurations(context: CommandContext<CommandSource>) = 0.also {
56+
val setting = "backup-configurations"
57+
validate(context, "ess.configure.backup.$setting", setting) {
58+
val value = CommandAPI.getBool(context, "value")
59+
val oldValue = backupConfiguration.backupConfigurations
60+
backupConfiguration.backupConfigurations = value
61+
changed(
62+
context, setting, oldValue.toString(), value.toString()
63+
).also { super.process(context) }
64+
}
65+
}
66+
67+
fun backupCreationDelay(context: CommandContext<CommandSource>) = 0.also {
68+
val setting = "backup-creation-delay"
69+
validate(context, "ess.configure.backup.$setting", setting) {
70+
val value = CommandAPI.getInt(context, "value")
71+
val oldValue = backupConfiguration.backupCreationDelay
72+
backupConfiguration.backupCreationDelay = value
73+
changed(
74+
context, setting, oldValue.toString(), value.toString()
75+
).also { super.process(context) }
76+
}
77+
}
78+
79+
fun backupCompressionLevel(context: CommandContext<CommandSource>) = 0.also {
80+
val setting = "backup-compression-level"
81+
validate(context, "ess.configure.backup.$setting", setting) {
82+
val value = CommandAPI.getInt(context, "value")
83+
val oldValue = backupConfiguration.backupCompressionLevel
84+
backupConfiguration.backupCompressionLevel = value
85+
changed(
86+
context, setting, oldValue.toString(), value.toString()
87+
).also { super.process(context) }
88+
}
89+
}
90+
91+
fun maxBackupFiles(context: CommandContext<CommandSource>) = 0.also {
92+
val setting = "max-backup-files"
93+
validate(context, "ess.configure.backup.$setting", setting) {
94+
val value = CommandAPI.getInt(context, "value")
95+
val oldValue = backupConfiguration.maxBackupFiles
96+
backupConfiguration.maxBackupFiles = value
97+
changed(
98+
context, setting, oldValue.toString(), value.toString()
99+
).also { super.process(context) }
100+
}
101+
}
102+
103+
fun rollingBackupFilesEnabled(context: CommandContext<CommandSource>) = 0.also {
104+
val setting = "rolling-backup-files-enabled"
105+
validate(context, "ess.configure.backup.$setting", setting) {
106+
val value = CommandAPI.getBool(context, "value")
107+
val oldValue = backupConfiguration.rollingBackupFilesEnabled
108+
backupConfiguration.rollingBackupFilesEnabled = value
109+
changed(
110+
context, setting, oldValue.toString(), value.toString()
111+
).also { super.process(context) }
112+
}
113+
}
114+
115+
fun notifyPlayersAboutBackup(context: CommandContext<CommandSource>) = 0.also {
116+
val setting = "notify-players-about-backup"
117+
validate(context, "ess.configure.backup.$setting", setting) {
118+
val value = CommandAPI.getBool(context, "value")
119+
val oldValue = backupConfiguration.notifyPlayersAboutBackup
120+
backupConfiguration.notifyPlayersAboutBackup = value
121+
changed(
122+
context, setting, oldValue.toString(), value.toString()
123+
).also { super.process(context) }
124+
}
125+
}
126+
127+
fun purgeBackupOutDirectory(context: CommandContext<CommandSource>) = 0.also {
128+
val setting = "purge-backup-out-directory"
129+
validate(context, "ess.configure.backup.$setting", setting) {
130+
val value = CommandAPI.getBool(context, "value")
131+
val oldValue = backupConfiguration.purgeBackupOutDirectory
132+
backupConfiguration.purgeBackupOutDirectory = value
133+
changed(
134+
context, setting, oldValue.toString(), value.toString()
135+
).also { super.process(context) }
136+
}
137+
}
138+
139+
private fun validate(
140+
context: CommandContext<CommandSource>,
141+
node: String,
142+
setting: String,
143+
action: (isServer: Boolean) -> Unit
144+
) = context.getPlayer()?.let {
145+
if (hasPermission(it, node, 4)) {
146+
action(false)
147+
} else {
148+
MessagingAPI.sendMessage(
149+
context.getPlayer()!!,
150+
"$MESSAGE_CORE_PREFIX.configure.restricted",
151+
args = *arrayOf(setting)
152+
)
153+
}
154+
} ?: run { action(true) }
155+
156+
private fun changed(
157+
context: CommandContext<CommandSource>,
158+
setting: String,
159+
oldValue: String,
160+
value: String
161+
) = if (context.isPlayerSender()) {
162+
LogManager.getLogger().info(
163+
"Setting name `$setting` value changed by ${context.playerName()} from `$oldValue` to $value"
164+
)
165+
MessagingAPI.sendMessage(
166+
context.getPlayer()!!,
167+
"$MESSAGE_CORE_PREFIX.configure.successfully",
168+
args = *arrayOf(setting, oldValue, value)
169+
)
170+
} else {
171+
ServerMessagingAPI.response {
172+
"Setting name `$setting` value changed from `$oldValue` to $value"
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)