|
1 | 1 | package com.mairwunnx.projectessentials.home.commands |
2 | 2 |
|
3 | | -import com.mairwunnx.projectessentials.cooldown.essentials.CommandsAliases |
4 | | -import com.mairwunnx.projectessentials.core.backlocation.BackLocationProvider |
5 | | -import com.mairwunnx.projectessentials.core.extensions.isPlayerSender |
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.models.HomeModel |
12 | | -import com.mairwunnx.projectessentials.home.sendMessage |
13 | | -import com.mojang.brigadier.CommandDispatcher |
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.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 |
| 11 | +import com.mairwunnx.projectessentials.home.teleportToHome |
16 | 12 | import com.mojang.brigadier.context.CommandContext |
17 | 13 | import net.minecraft.command.CommandSource |
18 | | -import net.minecraft.command.Commands |
19 | | -import net.minecraft.entity.player.ServerPlayerEntity |
20 | | -import net.minecraft.world.dimension.DimensionType |
21 | | -import org.apache.logging.log4j.LogManager |
22 | 14 |
|
23 | | -internal object HomeCommand { |
24 | | - private val aliases = arrayOf("home", "ehome") |
25 | | - private val logger = LogManager.getLogger() |
26 | | - |
27 | | - fun register(dispatcher: CommandDispatcher<CommandSource>) { |
28 | | - logger.info("Register \"/home\" command") |
29 | | - applyCommandAliases() |
30 | | - |
31 | | - aliases.forEach { command -> |
32 | | - dispatcher.register( |
33 | | - literal<CommandSource>(command).executes { |
34 | | - return@executes execute(it) |
35 | | - }.then( |
36 | | - Commands.argument( |
37 | | - "home name", StringArgumentType.string() |
38 | | - ).executes { |
39 | | - return@executes execute(it) |
40 | | - } |
41 | | - ) |
42 | | - ) |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - private fun applyCommandAliases() { |
47 | | - if (!EntryPoint.cooldownsInstalled) return |
48 | | - CommandsAliases.aliases["home"] = aliases.toMutableList() |
49 | | - } |
50 | | - |
51 | | - private fun execute(c: CommandContext<CommandSource>): Int { |
52 | | - if (c.isPlayerSender()) { |
53 | | - val player = c.source.asPlayer() |
54 | | - if (hasPermission(player, "ess.home")) { |
55 | | - val homeName: String = try { |
56 | | - StringArgumentType.getString(c, "home name") |
57 | | - } catch (_: IllegalArgumentException) { |
58 | | - "home" |
59 | | - } |
60 | | - |
61 | | - HomeAPI.take(player, homeName)?.let { |
62 | | - moveToHome(player, it) |
63 | | - return 0 |
64 | | - } |
| 15 | +object HomeCommand : CommandBase(homeLiteral) { |
| 16 | + override val name = "home" |
| 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.$status", args = *args |
| 20 | + ) |
65 | 21 |
|
66 | | - sendMessage(c.source, "not_found", homeName) |
67 | | - logger.info("Player ${player.name.string} try teleport to not exist home $homeName") |
| 22 | + validateAndExecute(context, "ess.home.teleport", 0) { isServer -> |
| 23 | + if (isServer) { |
| 24 | + ServerMessagingAPI.throwOnlyPlayerCan() |
68 | 25 | } else { |
69 | | - sendMessage(c.source, "restricted") |
70 | | - throwPermissionLevel(player.name.string, "home") |
| 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 | + teleportToHome(player, it).also { |
| 38 | + out("success").also { super.process(context) } |
| 39 | + } |
| 40 | + } ?: run { out("not_found") } |
| 41 | + } ?: run { out("not_found") } |
71 | 42 | } |
72 | | - } else { |
73 | | - throwOnlyPlayerCan("home") |
74 | | - } |
75 | | - return 0 |
76 | | - } |
77 | | - |
78 | | - private fun moveToHome(player: ServerPlayerEntity, home: HomeModel.Home) { |
79 | | - val xPos = home.xPos.toDouble() |
80 | | - val yPos = home.yPos.toDouble() |
81 | | - val zPos = home.zPos.toDouble() |
82 | | - val yaw = home.yaw |
83 | | - val pitch = home.pitch |
84 | | - val dimId = home.worldId |
85 | | - val clientWorld = home.clientWorld |
86 | | - val targetWorld = player.server.getWorld( |
87 | | - DimensionType.getById(dimId) ?: DimensionType.OVERWORLD |
88 | | - ) |
89 | | - if (player.world.worldInfo.worldName == clientWorld) { |
90 | | - BackLocationProvider.commit(player) |
91 | | - player.teleport(targetWorld, xPos, yPos, zPos, yaw, pitch) |
92 | | - sendMessage(player.commandSource, "success", home.home) |
93 | | - } else { |
94 | | - sendMessage(player.commandSource, "not_found", home.home) |
95 | | - logger.info("Player ${player.name.string} try teleport to not exist home ${home.home}") |
96 | 43 | } |
97 | 44 | } |
98 | 45 | } |
0 commit comments