|
1 | 1 | package com.mairwunnx.projectessentials.spawn.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.spawn.EntryPoint |
8 | | -import com.mairwunnx.projectessentials.spawn.EntryPoint.Companion.hasPermission |
9 | | -import com.mairwunnx.projectessentials.spawn.models.SpawnModelBase |
10 | | -import com.mairwunnx.projectessentials.spawn.sendMessage |
11 | | -import com.mojang.brigadier.CommandDispatcher |
12 | | -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.CommandBase |
| 5 | +import com.mairwunnx.projectessentials.core.api.v1.extensions.currentDimensionId |
| 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.spawn.helpers.validateAndExecute |
| 10 | +import com.mairwunnx.projectessentials.spawn.spawnConfiguration |
13 | 11 | import com.mojang.brigadier.context.CommandContext |
14 | 12 | import net.minecraft.command.CommandSource |
15 | 13 | import net.minecraft.util.math.BlockPos |
16 | | -import org.apache.logging.log4j.LogManager |
17 | 14 |
|
18 | | -object SetSpawnCommand { |
19 | | - private val aliases = listOf( |
20 | | - "setspawn", "esetspawn", "spawnpoint", "setworldspawn" |
21 | | - ) |
22 | | - private val logger = LogManager.getLogger() |
23 | | - |
24 | | - fun register(dispatcher: CommandDispatcher<CommandSource>) { |
25 | | - logger.info("Register \"/setspawn\" command") |
26 | | - applyCommandAliases() |
27 | | - |
28 | | - aliases.forEach { command -> |
29 | | - dispatcher.register( |
30 | | - literal<CommandSource>(command).executes(::execute) |
31 | | - ) |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - private fun applyCommandAliases() { |
36 | | - if (!EntryPoint.cooldownsInstalled) return |
37 | | - CommandsAliases.aliases["setspawn"] = aliases.toMutableList() |
38 | | - } |
39 | | - |
40 | | - private fun execute(c: CommandContext<CommandSource>): Int { |
41 | | - if (c.isPlayerSender()) { |
42 | | - val player = c.source.asPlayer() |
43 | | - if (hasPermission(player, "ess.spawn.set")) { |
44 | | - SpawnModelBase.spawnModel.xPos = player.posX |
45 | | - SpawnModelBase.spawnModel.yPos = player.posY |
46 | | - SpawnModelBase.spawnModel.zPos = player.posZ |
47 | | - SpawnModelBase.spawnModel.yaw = player.rotationYaw |
48 | | - SpawnModelBase.spawnModel.pitch = player.rotationPitch |
49 | | - SpawnModelBase.spawnModel.worldId = player.serverWorld.worldType.id |
50 | | - player.serverWorld.spawnPoint = BlockPos(player.posX, player.posY, player.posZ) |
51 | | - sendMessage(c.source, "set.success") |
52 | | - logger.info("New spawn point installed by ${player.name.string} with data: ") |
53 | | - logger.info(" - xpos: ${player.posX}") |
54 | | - logger.info(" - ypos: ${player.posY}") |
55 | | - logger.info(" - zpos: ${player.posZ}") |
56 | | - logger.info(" - yaw: ${player.rotationYaw}") |
57 | | - logger.info(" - pitch: ${player.rotationPitch}") |
58 | | - logger.info("Executed command \"${c.input}\" from ${player.name.string}") |
| 15 | +object SetSpawnCommand : CommandBase(setSpawnLiteral) { |
| 16 | + override val name = "set-spawn" |
| 17 | + override fun process(context: CommandContext<CommandSource>) = 0.also { |
| 18 | + validateAndExecute(context, "ess.spawn.set", 4) { isServer -> |
| 19 | + if (isServer) { |
| 20 | + ServerMessagingAPI.throwOnlyPlayerCan() |
59 | 21 | } else { |
60 | | - sendMessage(c.source, "set.restricted") |
61 | | - throwPermissionLevel(player.name.string, "setspawn") |
| 22 | + with(spawnConfiguration.take()) { |
| 23 | + xPos = context.getPlayer()!!.position.x |
| 24 | + yPos = context.getPlayer()!!.position.y |
| 25 | + zPos = context.getPlayer()!!.position.z |
| 26 | + yaw = context.getPlayer()!!.rotationYaw |
| 27 | + pitch = context.getPlayer()!!.rotationPitch |
| 28 | + dimensionId = context.getPlayer()!!.currentDimensionId |
| 29 | + }.also { |
| 30 | + context.getPlayer()!!.serverWorld.spawnPoint = BlockPos( |
| 31 | + context.getPlayer()!!.position.x, |
| 32 | + context.getPlayer()!!.position.y, |
| 33 | + context.getPlayer()!!.position.z |
| 34 | + ) |
| 35 | + MessagingAPI.sendMessage( |
| 36 | + context.getPlayer()!!, "${MESSAGE_MODULE_PREFIX}spawn.set.success" |
| 37 | + ).also { super.process(context) } |
| 38 | + } |
62 | 39 | } |
63 | | - } else { |
64 | | - throwOnlyPlayerCan("setspawn") |
65 | 40 | } |
66 | | - return 0 |
67 | 41 | } |
68 | 42 | } |
0 commit comments