Skip to content

Commit dfc7de3

Browse files
committed
del-home command re-written.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 9509dd2 commit dfc7de3

File tree

2 files changed

+38
-69
lines changed

2 files changed

+38
-69
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/home/commands/CommandLiterals.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
@file:Suppress("DuplicatedCode")
2+
13
package com.mairwunnx.projectessentials.home.commands
24

3-
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI.getConfigurationByName
45
import com.mairwunnx.projectessentials.core.api.v1.extensions.getPlayer
56
import com.mairwunnx.projectessentials.core.api.v1.extensions.isPlayerSender
6-
import com.mairwunnx.projectessentials.home.configurations.HomeConfiguration
7+
import com.mairwunnx.projectessentials.home.homeConfiguration
78
import com.mojang.brigadier.arguments.StringArgumentType
89
import com.mojang.brigadier.builder.LiteralArgumentBuilder
910
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
@@ -17,9 +18,7 @@ inline val homeLiteral: LiteralArgumentBuilder<CommandSource>
1718
"home", StringArgumentType.string()
1819
).suggests { ctx, builder ->
1920
ISuggestionProvider.suggest(
20-
getConfigurationByName<HomeConfiguration>(
21-
"home"
22-
).take().let { model ->
21+
homeConfiguration.let { model ->
2322
if (ctx.isPlayerSender()) {
2423
model.users.asSequence().find {
2524
it.name == ctx.getPlayer()!!.name.string || it.uuid == ctx.getPlayer()!!.uniqueID.toString()
@@ -43,9 +42,7 @@ inline val delHomeLiteral: LiteralArgumentBuilder<CommandSource>
4342
"home", StringArgumentType.string()
4443
).suggests { ctx, builder ->
4544
ISuggestionProvider.suggest(
46-
getConfigurationByName<HomeConfiguration>(
47-
"home"
48-
).take().let { model ->
45+
homeConfiguration.let { model ->
4946
if (ctx.isPlayerSender()) {
5047
model.users.asSequence().find {
5148
it.name == ctx.getPlayer()!!.name.string || it.uuid == ctx.getPlayer()!!.uniqueID.toString()
Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,45 @@
11
package com.mairwunnx.projectessentials.home.commands
22

3-
import com.mairwunnx.projectessentials.cooldown.essentials.CommandsAliases
4-
import com.mairwunnx.projectessentials.core.extensions.isPlayerSender
5-
import com.mairwunnx.projectessentials.core.helpers.throwOnlyPlayerCan
6-
import com.mairwunnx.projectessentials.core.helpers.throwPermissionLevel
7-
import com.mairwunnx.projectessentials.home.EntryPoint
8-
import com.mairwunnx.projectessentials.home.EntryPoint.Companion.hasPermission
9-
import com.mairwunnx.projectessentials.home.HomeAPI
10-
import com.mairwunnx.projectessentials.home.sendMessage
11-
import com.mojang.brigadier.CommandDispatcher
12-
import com.mojang.brigadier.arguments.StringArgumentType
13-
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
3+
import com.mairwunnx.projectessentials.core.api.v1.MESSAGE_MODULE_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.messaging.MessagingAPI
8+
import com.mairwunnx.projectessentials.core.api.v1.messaging.ServerMessagingAPI
9+
import com.mairwunnx.projectessentials.home.helpers.validateAndExecute
10+
import com.mairwunnx.projectessentials.home.homeConfiguration
1411
import com.mojang.brigadier.context.CommandContext
1512
import net.minecraft.command.CommandSource
16-
import net.minecraft.command.Commands
17-
import org.apache.logging.log4j.LogManager
1813

19-
internal object DelHomeCommand {
20-
private val aliases = arrayOf("delhome", "edelhome", "removehome", "remhome")
21-
private val logger = LogManager.getLogger()
14+
object DelHomeCommand : CommandBase(delHomeLiteral, false) {
15+
override val name = "del-home"
2216

23-
fun register(dispatcher: CommandDispatcher<CommandSource>) {
24-
logger.info("Register \"/delhome\" command")
25-
aliases.forEach { command ->
26-
dispatcher.register(
27-
literal<CommandSource>(command).executes {
28-
return@executes execute(it)
29-
}.then(
30-
Commands.argument(
31-
"home name", StringArgumentType.string()
32-
).executes {
33-
return@executes execute(it)
34-
}
35-
)
36-
)
37-
}
38-
applyCommandAliases()
39-
}
40-
41-
private fun applyCommandAliases() {
42-
if (!EntryPoint.cooldownsInstalled) return
43-
CommandsAliases.aliases["delhome"] = aliases.toMutableList()
44-
}
17+
override fun process(context: CommandContext<CommandSource>) = 0.also {
18+
fun out(status: String, vararg args: String) = MessagingAPI.sendMessage(
19+
context.getPlayer()!!, "${MESSAGE_MODULE_PREFIX}home.delhome.$status", args = *args
20+
)
4521

46-
private fun execute(c: CommandContext<CommandSource>): Int {
47-
if (c.isPlayerSender()) {
48-
val player = c.source.asPlayer()
49-
if (hasPermission(player, "ess.home.remove")) {
50-
val homeName: String = try {
51-
StringArgumentType.getString(c, "home name")
52-
} catch (_: IllegalArgumentException) {
53-
"home"
54-
}
55-
56-
val result = HomeAPI.remove(player, homeName)
57-
if (result) {
58-
sendMessage(c.source, "remove.success", homeName)
59-
logger.info("Executed command \"/delhome\" from ${player.name.string}")
60-
return 0
61-
}
62-
63-
sendMessage(c.source, "not_found", homeName)
22+
validateAndExecute(context, "ess.home.remove", 0) { isServer ->
23+
if (isServer) {
24+
ServerMessagingAPI.throwOnlyPlayerCan()
6425
} else {
65-
sendMessage(c.source, "remove.restricted")
66-
throwPermissionLevel(player.name.string, "delhome")
26+
val player = context.getPlayer()!!
27+
val name = if (CommandAPI.getStringExisting(context, "home")) {
28+
CommandAPI.getString(context, "home")
29+
} else "home"
30+
31+
homeConfiguration.users.asSequence().find {
32+
it.name == player.name.string || it.uuid == player.uniqueID.toString()
33+
}?.let { user ->
34+
user.homes.asSequence().find {
35+
it.home == name
36+
}?.let {
37+
user.homes.removeIf { it.home == it.home }.also {
38+
out("success", name).also { super.process(context) }
39+
}
40+
} ?: run { out("not_found", name) }
41+
} ?: run { out("not_found", name) }
6742
}
68-
} else {
69-
throwOnlyPlayerCan("delhome")
7043
}
71-
return 0
7244
}
7345
}

0 commit comments

Comments
 (0)