|
1 | 1 | package com.mairwunnx.projectessentials.spawn.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.spawn.EntryPoint |
9 |
| -import com.mairwunnx.projectessentials.spawn.EntryPoint.Companion.hasPermission |
10 |
| -import com.mairwunnx.projectessentials.spawn.EntryPoint.Companion.modInstance |
11 |
| -import com.mairwunnx.projectessentials.spawn.models.SpawnModelBase |
12 |
| -import com.mairwunnx.projectessentials.spawn.sendMessage |
13 |
| -import com.mojang.brigadier.CommandDispatcher |
14 |
| -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.commands.back.BackLocationAPI |
| 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.forceTeleportToSpawn |
| 10 | +import com.mairwunnx.projectessentials.spawn.helpers.validateAndExecute |
15 | 11 | import com.mojang.brigadier.context.CommandContext
|
16 | 12 | import net.minecraft.command.CommandSource
|
17 |
| -import net.minecraft.command.Commands |
18 |
| -import net.minecraft.entity.player.ServerPlayerEntity |
19 |
| -import net.minecraft.world.dimension.DimensionType |
20 |
| -import org.apache.logging.log4j.LogManager |
21 | 13 |
|
22 |
| -object SpawnCommand { |
23 |
| - private val aliases = arrayOf("spawn", "espawn") |
24 |
| - private val logger = LogManager.getLogger() |
25 |
| - |
26 |
| - fun register(dispatcher: CommandDispatcher<CommandSource>) { |
27 |
| - logger.info("Register \"/spawn\" command") |
28 |
| - applyCommandAliases() |
29 |
| - |
30 |
| - aliases.forEach { command -> |
31 |
| - dispatcher.register( |
32 |
| - literal<CommandSource>(command).executes(::execute).then( |
33 |
| - Commands.literal("reload").executes(::reload) |
34 |
| - ).then( |
35 |
| - Commands.literal("save").executes(::save) |
36 |
| - ).then( |
37 |
| - Commands.literal("version").executes(::version) |
38 |
| - ) |
39 |
| - ) |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 |
| - private fun applyCommandAliases() { |
44 |
| - if (!EntryPoint.cooldownsInstalled) return |
45 |
| - CommandsAliases.aliases["spawn"] = aliases.toMutableList() |
46 |
| - } |
47 |
| - |
48 |
| - private fun execute(c: CommandContext<CommandSource>): Int { |
49 |
| - if (c.isPlayerSender()) { |
50 |
| - val playerName = c.source.asPlayer().name.string |
51 |
| - if (hasPermission(c.source.asPlayer(), "ess.spawn", 0)) { |
52 |
| - moveToSpawn(c.source.asPlayer()) |
53 |
| - logger.info("Executed command \"${c.input}\" from $playerName") |
54 |
| - sendMessage(c.source, "success") |
| 14 | +object SpawnCommand : CommandBase(spawnLiteral) { |
| 15 | + override val name = "spawn" |
| 16 | + override fun process(context: CommandContext<CommandSource>) = 0.also { |
| 17 | + validateAndExecute(context, "ess.spawn.teleport", 0) { isServer -> |
| 18 | + if (isServer) { |
| 19 | + ServerMessagingAPI.throwOnlyPlayerCan() |
55 | 20 | } else {
|
56 |
| - sendMessage(c.source, "restricted") |
57 |
| - throwPermissionLevel(playerName, "spawn") |
| 21 | + MessagingAPI.sendMessage( |
| 22 | + context.getPlayer()!!, "${MESSAGE_MODULE_PREFIX}spawn.success" |
| 23 | + ).also { BackLocationAPI.commit(context.getPlayer()!!) } |
| 24 | + forceTeleportToSpawn(context.getPlayer()!!).also { super.process(context) } |
58 | 25 | }
|
59 |
| - } else { |
60 |
| - throwOnlyPlayerCan("spawn") |
61 |
| - } |
62 |
| - return 0 |
63 |
| - } |
64 |
| - |
65 |
| - fun moveToSpawn(player: ServerPlayerEntity) { |
66 |
| - BackLocationProvider.commit(player) |
67 |
| - |
68 |
| - val xPos = SpawnModelBase.spawnModel.xPos |
69 |
| - val yPos = SpawnModelBase.spawnModel.yPos |
70 |
| - val zPos = SpawnModelBase.spawnModel.zPos |
71 |
| - val yaw = SpawnModelBase.spawnModel.yaw |
72 |
| - val pitch = SpawnModelBase.spawnModel.pitch |
73 |
| - val dimId = SpawnModelBase.spawnModel.worldId |
74 |
| - val targetWorld = player.server.getWorld( |
75 |
| - DimensionType.getById(dimId) ?: DimensionType.OVERWORLD |
76 |
| - ) |
77 |
| - player.teleport(targetWorld, xPos, yPos, zPos, yaw, pitch) |
78 |
| - } |
79 |
| - |
80 |
| - private fun reload(c: CommandContext<CommandSource>): Int { |
81 |
| - if (c.isPlayerSender()) { |
82 |
| - val playerName = c.source.asPlayer().name.string |
83 |
| - if (!hasPermission(c.source.asPlayer(), "ess.spawn.reload")) { |
84 |
| - sendMessage(c.source, "reload.restricted") |
85 |
| - throwPermissionLevel(playerName, "spawn reload") |
86 |
| - return 0 |
87 |
| - } |
88 |
| - } |
89 |
| - SpawnModelBase.loadData() |
90 |
| - SpawnModelBase.assignSpawn(c.source.server) |
91 |
| - if (c.isPlayerSender()) { |
92 |
| - val playerName = c.source.asPlayer().name.string |
93 |
| - logger.info("Executed command \"${c.input}\" from $playerName") |
94 |
| - sendMessage(c.source, "reload.success") |
95 |
| - } else { |
96 |
| - logger.info("World spawn configuration reloaded.") |
97 |
| - } |
98 |
| - return 0 |
99 |
| - } |
100 |
| - |
101 |
| - private fun save(c: CommandContext<CommandSource>): Int { |
102 |
| - if (c.isPlayerSender()) { |
103 |
| - val playerName = c.source.asPlayer().name.string |
104 |
| - if (!hasPermission(c.source.asPlayer(), "ess.spawn.save")) { |
105 |
| - sendMessage(c.source, "save.restricted") |
106 |
| - throwPermissionLevel(playerName, "spawn save") |
107 |
| - return 0 |
108 |
| - } |
109 |
| - } |
110 |
| - SpawnModelBase.saveData() |
111 |
| - if (c.isPlayerSender()) { |
112 |
| - val playerName = c.source.asPlayer().name.string |
113 |
| - logger.info("Executed command \"${c.input}\" from $playerName") |
114 |
| - sendMessage(c.source, "save.success") |
115 |
| - } else { |
116 |
| - logger.info("World spawn configuration saved.") |
117 |
| - } |
118 |
| - return 0 |
119 |
| - } |
120 |
| - |
121 |
| - private fun version(c: CommandContext<CommandSource>): Int { |
122 |
| - if (c.isPlayerSender()) { |
123 |
| - val playerName = c.source.asPlayer().name.string |
124 |
| - if (!hasPermission(c.source.asPlayer(), "ess.spawn.version", 3)) { |
125 |
| - sendMessage(c.source, "version.restricted") |
126 |
| - throwPermissionLevel(playerName, "spawn version") |
127 |
| - return 0 |
128 |
| - } |
129 |
| - } |
130 |
| - if (c.isPlayerSender()) { |
131 |
| - val playerName = c.source.asPlayer().name.string |
132 |
| - sendMessage( |
133 |
| - c.source, |
134 |
| - "version.success", |
135 |
| - modInstance.modName, |
136 |
| - modInstance.modVersion, |
137 |
| - modInstance.modMaintainer, |
138 |
| - modInstance.modTargetForge, |
139 |
| - modInstance.modTargetMC, |
140 |
| - modInstance.modSources, |
141 |
| - modInstance.modTelegram |
142 |
| - ) |
143 |
| - logger.info("Executed command \"${c.input}\" from $playerName") |
144 |
| - } else { |
145 |
| - logger.info(" ${modInstance.modName}") |
146 |
| - logger.info("Version: ${modInstance.modVersion}") |
147 |
| - logger.info("Maintainer: ${modInstance.modMaintainer}") |
148 |
| - logger.info("Target Forge version: ${modInstance.modTargetForge}") |
149 |
| - logger.info("Target Minecraft version: ${modInstance.modTargetMC}") |
150 |
| - logger.info("Source code: ${modInstance.modSources}") |
151 |
| - logger.info("Telegram chat: ${modInstance.modTelegram}") |
152 | 26 | }
|
153 |
| - return 0 |
154 | 27 | }
|
155 | 28 | }
|
0 commit comments