Skip to content

Commit 24ba785

Browse files
committed
Added base logic for setting and teleporting to home.
1 parent 9856f37 commit 24ba785

File tree

6 files changed

+246
-7
lines changed

6 files changed

+246
-7
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/projectessentialshome/EntryPoint.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.mairwunnx.projectessentials.projectessentialshome
22

3+
import com.mairwunnx.projectessentials.projectessentialshome.commands.HomeCommand
4+
import com.mairwunnx.projectessentials.projectessentialshome.commands.SetHomeCommand
35
import com.mairwunnx.projectessentials.projectessentialshome.storage.StorageBase
46
import com.mairwunnx.projectessentialscore.EssBase
7+
import com.mojang.brigadier.CommandDispatcher
8+
import net.minecraft.command.CommandSource
59
import net.minecraftforge.common.MinecraftForge
610
import net.minecraftforge.eventbus.api.SubscribeEvent
711
import net.minecraftforge.fml.common.Mod
12+
import net.minecraftforge.fml.event.server.FMLServerStartingEvent
813
import net.minecraftforge.fml.event.server.FMLServerStoppingEvent
914
import org.apache.logging.log4j.LogManager
1015

@@ -24,6 +29,20 @@ class EntryPoint : EssBase() {
2429
StorageBase.loadUserData()
2530
}
2631

32+
@SubscribeEvent
33+
fun onServerStarting(event: FMLServerStartingEvent) {
34+
logger.info("$modName starting mod loading ...")
35+
registerCommands(event.server.commandManager.dispatcher)
36+
}
37+
38+
private fun registerCommands(
39+
cmdDispatcher: CommandDispatcher<CommandSource>
40+
) {
41+
logger.info("Command registering is starting ...")
42+
HomeCommand.register(cmdDispatcher)
43+
SetHomeCommand.register(cmdDispatcher)
44+
}
45+
2746
@Suppress("UNUSED_PARAMETER")
2847
@SubscribeEvent
2948
fun onServerStopping(it: FMLServerStoppingEvent) {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.mairwunnx.projectessentials.projectessentialshome.commands
2+
3+
import com.mairwunnx.projectessentials.projectessentialshome.models.HomeModel
4+
import com.mairwunnx.projectessentials.projectessentialshome.storage.StorageBase
5+
import com.mairwunnx.projectessentialscooldown.essentials.CommandsAliases
6+
import com.mairwunnx.projectessentialscore.extensions.isPlayerSender
7+
import com.mairwunnx.projectessentialscore.extensions.sendMsg
8+
import com.mairwunnx.projectessentialscore.helpers.ONLY_PLAYER_CAN
9+
import com.mairwunnx.projectessentialscore.helpers.PERMISSION_LEVEL
10+
import com.mairwunnx.projectessentialspermissions.permissions.PermissionsAPI
11+
import com.mojang.brigadier.CommandDispatcher
12+
import com.mojang.brigadier.arguments.StringArgumentType
13+
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
14+
import com.mojang.brigadier.context.CommandContext
15+
import net.minecraft.command.CommandSource
16+
import net.minecraft.command.Commands
17+
import net.minecraft.entity.player.ServerPlayerEntity
18+
import net.minecraft.world.dimension.DimensionType
19+
import org.apache.logging.log4j.LogManager
20+
21+
object HomeCommand {
22+
private val aliases = arrayOf(
23+
"home", "ehome"
24+
)
25+
private val logger = LogManager.getLogger()
26+
27+
fun register(dispatcher: CommandDispatcher<CommandSource>) {
28+
logger.info(" - register \"/home\" command ...")
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+
}
39+
)
40+
)
41+
}
42+
applyCommandAliases()
43+
}
44+
45+
private fun applyCommandAliases() {
46+
try {
47+
Class.forName(
48+
"com.mairwunnx.projectessentialscooldown.essentials.CommandsAliases"
49+
)
50+
CommandsAliases.aliases["home"] = aliases.toMutableList()
51+
logger.info(" - applying aliases: $aliases")
52+
} catch (_: ClassNotFoundException) {
53+
// ignored
54+
}
55+
}
56+
57+
private fun execute(c: CommandContext<CommandSource>): Int {
58+
if (c.isPlayerSender()) {
59+
val player = c.source.asPlayer()
60+
if (PermissionsAPI.hasPermission(player.name.string, "ess.home")) {
61+
val playerUUID = player.uniqueID.toString()
62+
val homeName: String = try {
63+
StringArgumentType.getString(c, "home name")
64+
} catch (_: IllegalArgumentException) {
65+
"home"
66+
}
67+
val home = StorageBase.getData(playerUUID)
68+
home.homes.forEach {
69+
if (it.home == homeName) {
70+
moveToHome(player, it)
71+
return@forEach
72+
}
73+
}
74+
// sendMsg home not found
75+
} else {
76+
sendMsg("home", c.source, "home.restricted")
77+
logger.info(
78+
PERMISSION_LEVEL
79+
.replace("%0", player.name.string)
80+
.replace("%1", "home")
81+
)
82+
}
83+
} else {
84+
logger.info(ONLY_PLAYER_CAN.replace("%0", "home"))
85+
}
86+
return 0
87+
}
88+
89+
fun moveToHome(player: ServerPlayerEntity, home: HomeModel.Home) {
90+
val xPos = home.xPos.toDouble()
91+
val yPos = home.yPos.toDouble()
92+
val zPos = home.zPos.toDouble()
93+
val yaw = home.yaw
94+
val pitch = home.pitch
95+
val dimId = home.worldId
96+
val clientWorld = home.clientWorld
97+
val targetWorld = player.server.getWorld(
98+
DimensionType.getById(dimId) ?: DimensionType.OVERWORLD
99+
)
100+
if (player.world.worldInfo.worldName == clientWorld) {
101+
player.teleport(targetWorld, xPos, yPos, zPos, yaw, pitch)
102+
} else {
103+
// sendMsg home not found
104+
}
105+
}
106+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.mairwunnx.projectessentials.projectessentialshome.commands
2+
3+
import com.mairwunnx.projectessentials.projectessentialshome.models.HomeModel
4+
import com.mairwunnx.projectessentials.projectessentialshome.storage.StorageBase
5+
import com.mairwunnx.projectessentialscooldown.essentials.CommandsAliases
6+
import com.mairwunnx.projectessentialscore.extensions.isPlayerSender
7+
import com.mairwunnx.projectessentialscore.extensions.sendMsg
8+
import com.mairwunnx.projectessentialscore.helpers.ONLY_PLAYER_CAN
9+
import com.mairwunnx.projectessentialscore.helpers.PERMISSION_LEVEL
10+
import com.mairwunnx.projectessentialspermissions.permissions.PermissionsAPI
11+
import com.mojang.brigadier.CommandDispatcher
12+
import com.mojang.brigadier.arguments.StringArgumentType
13+
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
14+
import com.mojang.brigadier.context.CommandContext
15+
import net.minecraft.command.CommandSource
16+
import net.minecraft.command.Commands
17+
import org.apache.logging.log4j.LogManager
18+
19+
object SetHomeCommand {
20+
private val aliases = arrayOf(
21+
"sethome", "esethome"
22+
)
23+
private val logger = LogManager.getLogger()
24+
25+
fun register(dispatcher: CommandDispatcher<CommandSource>) {
26+
logger.info(" - register \"/sethome\" command ...")
27+
aliases.forEach { command ->
28+
dispatcher.register(
29+
literal<CommandSource>(command).executes {
30+
return@executes execute(it)
31+
}.then(
32+
Commands.argument(
33+
"home name", StringArgumentType.string()
34+
).executes {
35+
return@executes execute(it)
36+
}
37+
)
38+
)
39+
}
40+
applyCommandAliases()
41+
}
42+
43+
private fun applyCommandAliases() {
44+
try {
45+
Class.forName(
46+
"com.mairwunnx.projectessentialscooldown.essentials.CommandsAliases"
47+
)
48+
CommandsAliases.aliases["sethome"] = aliases.toMutableList()
49+
logger.info(" - applying aliases: $aliases")
50+
} catch (_: ClassNotFoundException) {
51+
// ignored
52+
}
53+
}
54+
55+
private fun execute(c: CommandContext<CommandSource>): Int {
56+
if (c.isPlayerSender()) {
57+
val player = c.source.asPlayer()
58+
if (PermissionsAPI.hasPermission(player.name.string, "ess.home.set")) {
59+
val playerUUID = player.uniqueID.toString()
60+
val homeName: String = try {
61+
StringArgumentType.getString(c, "home name")
62+
} catch (_: IllegalArgumentException) {
63+
"home"
64+
}
65+
val clientWorld = c.source.world.worldInfo.worldName
66+
val worldId = c.source.world.worldType.id
67+
val xPos = player.posX.toInt()
68+
val yPos = player.posY.toInt()
69+
val zPos = player.posZ.toInt()
70+
val yaw = player.rotationYaw
71+
val pitch = player.rotationPitch
72+
val homeModel = StorageBase.getData(playerUUID).homes
73+
homeModel.add(
74+
HomeModel.Home(
75+
homeName, clientWorld, worldId, xPos, yPos, zPos, yaw, pitch
76+
)
77+
)
78+
StorageBase.setData(playerUUID, HomeModel(homeModel))
79+
sendMsg("home", c.source, "home.set.success")
80+
logger.info("New home point for ${player.name.string} installed with data: ")
81+
logger.info(" - name: $homeName")
82+
logger.info(" - world / world id: $clientWorld / $worldId")
83+
logger.info(" - xpos: $xPos")
84+
logger.info(" - ypos: $yPos")
85+
logger.info(" - zpos: $zPos")
86+
logger.info(" - yaw: $yaw")
87+
logger.info(" - pitch: $pitch")
88+
logger.info("Executed command \"/sethome\" from ${player.name.string}")
89+
} else {
90+
sendMsg("home", c.source, "home.set.restricted")
91+
logger.info(
92+
PERMISSION_LEVEL
93+
.replace("%0", player.name.string)
94+
.replace("%1", "sethome")
95+
)
96+
}
97+
} else {
98+
logger.info(ONLY_PLAYER_CAN.replace("%0", "sethome"))
99+
}
100+
return 0
101+
}
102+
}

src/main/kotlin/com/mairwunnx/projectessentials/projectessentialshome/models/HomeModel.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable
55

66
@Serializable
77
data class HomeModel(
8-
var homes: List<Home> = emptyList()
8+
var homes: MutableList<Home> = mutableListOf()
99
) {
1010
@Serializable
1111
data class Home(
@@ -14,6 +14,8 @@ data class HomeModel(
1414
var worldId: Int = -1,
1515
var xPos: Int = -1,
1616
var yPos: Int = -1,
17-
var zPos: Int = -1
17+
var zPos: Int = -1,
18+
var yaw: Float = -1F,
19+
var pitch: Float = -1F
1820
)
1921
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.mairwunnx.projectessentials.projectessentialshome.models
2+
3+
object HomeModelBase {
4+
5+
}

src/main/kotlin/com/mairwunnx/projectessentials/projectessentialshome/storage/StorageBase.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import kotlinx.serialization.json.Json
77
import kotlinx.serialization.json.JsonConfiguration
88
import org.apache.logging.log4j.LogManager
99
import java.io.File
10+
import java.io.FileNotFoundException
1011
import kotlin.system.measureTimeMillis
1112

1213
@UseExperimental(UnstableDefault::class)
@@ -43,11 +44,15 @@ object StorageBase {
4344
users?.forEach {
4445
logger.debug(" - processing $it user home data ...")
4546
val userId = it
46-
val userDataRaw = File(
47-
userDataFolder + File.separator + it + File.separator + "home.json"
48-
).readText()
49-
val userHomeClass = Json.parse(HomeModel.serializer(), userDataRaw)
50-
userHomeData[userId] = userHomeClass
47+
try {
48+
val userDataRaw = File(
49+
userDataFolder + File.separator + it + File.separator + "home.json"
50+
).readText()
51+
val userHomeClass = Json.parse(HomeModel.serializer(), userDataRaw)
52+
userHomeData[userId] = userHomeClass
53+
} catch (_: FileNotFoundException) {
54+
logger.info(" - loading home data for $it skipped! not found!")
55+
}
5156
}
5257
}
5358
logger.info("Loading user home data done configurations with ${elapsedTime}ms")

0 commit comments

Comments
 (0)