|
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.extensions.sendMsg |
6 | | -import com.mairwunnx.projectessentials.core.helpers.throwOnlyPlayerCan |
7 | | -import com.mairwunnx.projectessentials.core.helpers.throwPermissionLevel |
8 | | -import com.mairwunnx.projectessentials.home.EntryPoint |
9 | | -import com.mairwunnx.projectessentials.home.EntryPoint.Companion.hasPermission |
10 | | -import com.mairwunnx.projectessentials.home.HomeAPI |
11 | | -import com.mairwunnx.projectessentials.home.sendMessage |
12 | | -import com.mojang.brigadier.CommandDispatcher |
13 | | -import com.mojang.brigadier.arguments.BoolArgumentType |
14 | | -import com.mojang.brigadier.arguments.StringArgumentType |
15 | | -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.currentDimensionId |
| 7 | +import com.mairwunnx.projectessentials.core.api.v1.extensions.getPlayer |
| 8 | +import com.mairwunnx.projectessentials.core.api.v1.messaging.MessagingAPI |
| 9 | +import com.mairwunnx.projectessentials.core.api.v1.messaging.ServerMessagingAPI |
| 10 | +import com.mairwunnx.projectessentials.home.configurations.HomeConfigurationModel |
| 11 | +import com.mairwunnx.projectessentials.home.helpers.validateAndExecute |
| 12 | +import com.mairwunnx.projectessentials.home.homeConfiguration |
16 | 13 | import com.mojang.brigadier.context.CommandContext |
17 | 14 | import net.minecraft.command.CommandSource |
18 | | -import net.minecraft.command.Commands |
19 | | -import org.apache.logging.log4j.LogManager |
20 | 15 |
|
21 | | -internal object SetHomeCommand { |
22 | | - private val aliases = arrayOf("sethome", "esethome") |
23 | | - private val logger = LogManager.getLogger() |
| 16 | +object SetHomeCommand : CommandBase(setHomeLiteral, false) { |
| 17 | + override val name = "set-home" |
| 18 | + override fun process(context: CommandContext<CommandSource>) = 0.also { |
| 19 | + fun out(status: String, vararg args: String) = MessagingAPI.sendMessage( |
| 20 | + context.getPlayer()!!, "${MESSAGE_MODULE_PREFIX}home.sethome.$status", args = *args |
| 21 | + ) |
24 | 22 |
|
25 | | - fun register(dispatcher: CommandDispatcher<CommandSource>) { |
26 | | - logger.info("Register \"/sethome\" command") |
27 | | - applyCommandAliases() |
28 | | - |
29 | | - aliases.forEach { command -> |
30 | | - dispatcher.register( |
31 | | - literal<CommandSource>(command).executes { |
32 | | - return@executes execute(it) |
33 | | - }.then( |
34 | | - Commands.argument( |
35 | | - "home name", StringArgumentType.string() |
36 | | - ).executes { |
37 | | - return@executes execute(it) |
38 | | - }.then( |
39 | | - Commands.argument( |
40 | | - "override", BoolArgumentType.bool() |
41 | | - ).executes { |
42 | | - return@executes execute( |
43 | | - it, BoolArgumentType.getBool(it, "override") |
| 23 | + validateAndExecute(context, "ess.home.set", 0) { isServer -> |
| 24 | + if (isServer) { |
| 25 | + ServerMessagingAPI.throwOnlyPlayerCan() |
| 26 | + } else { |
| 27 | + val player = context.getPlayer()!! |
| 28 | + val name = if (CommandAPI.getStringExisting(context, "home")) { |
| 29 | + CommandAPI.getString(context, "home") |
| 30 | + } else "home" |
| 31 | + |
| 32 | + fun commit(user: HomeConfigurationModel.User?) { |
| 33 | + fun fromUser() = HomeConfigurationModel.User.Home( |
| 34 | + name, player.currentDimensionId, |
| 35 | + player.position.x, player.position.y, player.position.z, |
| 36 | + player.rotationYaw, player.rotationPitch |
| 37 | + ).also { out("success", name).also { super.process(context) } } |
| 38 | + |
| 39 | + user?.homes?.add(fromUser()) ?: run { |
| 40 | + homeConfiguration.users.add( |
| 41 | + HomeConfigurationModel.User( |
| 42 | + player.name.string, |
| 43 | + player.uniqueID.toString(), |
| 44 | + mutableListOf(fromUser()) |
44 | 45 | ) |
45 | | - } |
46 | | - ) |
47 | | - ) |
48 | | - ) |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - private fun applyCommandAliases() { |
53 | | - if (!EntryPoint.cooldownsInstalled) return |
54 | | - CommandsAliases.aliases["sethome"] = aliases.toMutableList() |
55 | | - } |
56 | | - |
57 | | - private fun execute( |
58 | | - c: CommandContext<CommandSource>, |
59 | | - override: Boolean = false |
60 | | - ): Int { |
61 | | - if (c.isPlayerSender()) { |
62 | | - val player = c.source.asPlayer() |
63 | | - if (hasPermission(player, "ess.home.set")) { |
64 | | - val homeName: String = try { |
65 | | - StringArgumentType.getString(c, "home name") |
66 | | - } catch (_: IllegalArgumentException) { |
67 | | - "home" |
| 46 | + ) |
| 47 | + } |
68 | 48 | } |
69 | 49 |
|
70 | | - val result = HomeAPI.create(player, homeName, override) |
71 | | - if (!result) { |
72 | | - sendMessage(c.source, "set.already_exist", homeName) |
73 | | - return 0 |
74 | | - } |
75 | | - |
76 | | - sendMessage(c.source, "set.success", homeName) |
77 | | - logger.info("Executed command \"/sethome\" from ${player.name.string}") |
78 | | - } else { |
79 | | - sendMsg("home", c.source, "home.set.restricted") |
80 | | - throwPermissionLevel(player.name.string, "sethome") |
| 50 | + homeConfiguration.users.asSequence().find { |
| 51 | + it.name == player.name.string || it.uuid == player.uniqueID.toString() |
| 52 | + }?.let { user -> |
| 53 | + user.homes.asSequence().find { |
| 54 | + it.home == name |
| 55 | + }?.let { out("exist", name) } ?: run { commit(user) } |
| 56 | + } ?: run { commit(null) } |
81 | 57 | } |
82 | | - } else { |
83 | | - throwOnlyPlayerCan("sethome") |
84 | 58 | } |
85 | | - return 0 |
86 | 59 | } |
87 | 60 | } |
0 commit comments