Skip to content

Commit bb16770

Browse files
committed
Validating configuration argument added.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent f06f521 commit bb16770

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

src/main/kotlin/com/mairwunnx/projectessentials/core/impl/commands/ConfigureEssentialsCommand.kt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.mairwunnx.projectessentials.core.impl.commands
33
import com.mairwunnx.projectessentials.core.api.v1.MESSAGE_CORE_PREFIX
44
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI
55
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandBase
6-
import com.mairwunnx.projectessentials.core.api.v1.commands.arguments.StringArrayArgument
76
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI
87
import com.mairwunnx.projectessentials.core.api.v1.extensions.getPlayer
98
import com.mairwunnx.projectessentials.core.api.v1.extensions.isPlayerSender
@@ -13,7 +12,6 @@ import com.mairwunnx.projectessentials.core.api.v1.messaging.ServerMessagingAPI
1312
import com.mairwunnx.projectessentials.core.api.v1.permissions.hasPermission
1413
import com.mairwunnx.projectessentials.core.impl.commands.ConfigureEssentialsCommandAPI.requiredServerRestart
1514
import com.mairwunnx.projectessentials.core.impl.configurations.GeneralConfiguration
16-
import com.mojang.brigadier.CommandDispatcher
1715
import com.mojang.brigadier.context.CommandContext
1816
import net.minecraft.command.CommandSource
1917
import org.apache.logging.log4j.LogManager
@@ -55,28 +53,32 @@ object ConfigureEssentialsCommandAPI {
5553
fun getRequired() = requiredServerRestart.toList()
5654
}
5755

58-
internal object ConfigureEssentialsCommand : CommandBase(
59-
takeConfigureEssentialsLiteral(), false
56+
object ConfigureEssentialsCommand : CommandBase(
57+
configureEssentialsLiteral, false
6058
) {
6159
private val generalConfiguration by lazy {
6260
ConfigurationAPI.getConfigurationByName<GeneralConfiguration>("general")
6361
}
6462

6563
override val name = "configure-essentials"
6664

67-
/*
68-
This is a correction of the problem in order to get the list
69-
of settings in the configuration several times, because for
70-
the first time it is empty.
71-
*/
72-
override fun register(dispatcher: CommandDispatcher<CommandSource>) {
73-
this.literal = takeConfigureEssentialsLiteral()
74-
super.register(dispatcher)
75-
}
76-
7765
override fun process(context: CommandContext<CommandSource>): Int {
78-
val setting = StringArrayArgument.getValue(context, "setting")
66+
val setting = CommandAPI.getString(context, "setting")
7967
val value = CommandAPI.getString(context, "value")
68+
69+
if (!validateArguments(setting, value)) {
70+
if (context.isPlayerSender()) {
71+
MessagingAPI.sendMessage(
72+
context.getPlayer()!!,
73+
"$MESSAGE_CORE_PREFIX.configure.not_found",
74+
args = *arrayOf(name)
75+
)
76+
} else {
77+
ServerMessagingAPI.response { "Setting with name $name not exist or value has incorrect format." }
78+
}
79+
return 0
80+
}
81+
8082
val oldValue = generalConfiguration.take().getValue(setting).toString()
8183

8284
if (context.isPlayerSender()) {
@@ -124,4 +126,8 @@ internal object ConfigureEssentialsCommand : CommandBase(
124126
}
125127
return 0
126128
}
129+
130+
private fun validateArguments(setting: String, value: String) =
131+
generalConfiguration.take().keys.filter { it as String == setting }.count() > 1 &&
132+
value.isNotBlank() && !Regex("[=|:@$^*]").containsMatchIn(value)
127133
}

0 commit comments

Comments
 (0)