|
1 | 1 | package com.mairwunnx.projectessentials.home.commands |
2 | 2 |
|
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 |
14 | 11 | import com.mojang.brigadier.context.CommandContext |
15 | 12 | import net.minecraft.command.CommandSource |
16 | | -import net.minecraft.command.Commands |
17 | | -import org.apache.logging.log4j.LogManager |
18 | 13 |
|
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" |
22 | 16 |
|
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 | + ) |
45 | 21 |
|
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() |
64 | 25 | } 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) } |
67 | 42 | } |
68 | | - } else { |
69 | | - throwOnlyPlayerCan("delhome") |
70 | 43 | } |
71 | | - return 0 |
72 | 44 | } |
73 | 45 | } |
0 commit comments