Skip to content

Commit 79a15ca

Browse files
committed
Home command re-written.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 8771877 commit 79a15ca

File tree

2 files changed

+55
-87
lines changed

2 files changed

+55
-87
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/home/ModuleObject.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package com.mairwunnx.projectessentials.home
44

55
import com.mairwunnx.projectessentials.core.api.v1.IMCLocalizationMessage
66
import com.mairwunnx.projectessentials.core.api.v1.IMCProvidersMessage
7+
import com.mairwunnx.projectessentials.core.api.v1.commands.back.BackLocationAPI
8+
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI
79
import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI.subscribeOn
810
import com.mairwunnx.projectessentials.core.api.v1.events.forge.ForgeEventType
911
import com.mairwunnx.projectessentials.core.api.v1.events.forge.InterModEnqueueEventData
@@ -12,11 +14,30 @@ import com.mairwunnx.projectessentials.home.commands.DelHomeCommand
1214
import com.mairwunnx.projectessentials.home.commands.HomeCommand
1315
import com.mairwunnx.projectessentials.home.commands.SetHomeCommand
1416
import com.mairwunnx.projectessentials.home.configurations.HomeConfiguration
17+
import com.mairwunnx.projectessentials.home.configurations.HomeConfigurationModel
1518
import com.mairwunnx.projectessentials.home.configurations.HomeSettingsConfiguration
19+
import net.minecraft.entity.player.ServerPlayerEntity
20+
import net.minecraft.world.dimension.DimensionType
1621
import net.minecraftforge.common.MinecraftForge.EVENT_BUS
1722
import net.minecraftforge.fml.InterModComms
1823
import net.minecraftforge.fml.common.Mod
1924

25+
val homeConfiguration by lazy {
26+
ConfigurationAPI.getConfigurationByName<HomeConfiguration>("home").take()
27+
}
28+
29+
val homeSettingsConfiguration by lazy {
30+
ConfigurationAPI.getConfigurationByName<HomeSettingsConfiguration>("home-settings").take()
31+
}
32+
33+
fun teleportToHome(player: ServerPlayerEntity, home: HomeConfigurationModel.User.Home) {
34+
BackLocationAPI.commit(player)
35+
val world = player.server.getWorld(
36+
DimensionType.getById(home.dimensionId) ?: DimensionType.OVERWORLD
37+
)
38+
player.teleport(world, home.xPos + 0.5, home.yPos + 0.5, home.zPos + 0.5, home.yaw, home.pitch)
39+
}
40+
2041
@Mod("project_essentials_home")
2142
class ModuleObject : IModule {
2243
override val name = this::class.java.`package`.implementationTitle.split(" ").last()
Lines changed: 34 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,45 @@
11
package com.mairwunnx.projectessentials.home.commands
22

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
1612
import com.mojang.brigadier.context.CommandContext
1713
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
2214

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+
)
6521

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()
6825
} 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") }
7142
}
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}")
9643
}
9744
}
9845
}

0 commit comments

Comments
 (0)