Skip to content

Commit bdce8ec

Browse files
committed
Implemented HomeAPI.create method.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent bcdc4bc commit bdce8ec

File tree

2 files changed

+68
-38
lines changed

2 files changed

+68
-38
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.mairwunnx.projectessentials.home.api
2+
3+
import com.mairwunnx.projectessentials.home.models.HomeModel
4+
import com.mairwunnx.projectessentials.home.storage.StorageBase
5+
import net.minecraft.entity.player.ServerPlayerEntity
6+
7+
/**
8+
* Home API base class. Contains all
9+
* methods for interaction with player homes.
10+
*
11+
* @since 1.14.4-1.2.0
12+
*/
13+
object HomeAPI {
14+
/**
15+
* Create home for target player with specified name.
16+
*
17+
* @param owner ServerPlayerEntity class instance,
18+
* target home owner.
19+
* @param name new home name with default value `home`.
20+
* @param override if true then if world existing with same
21+
* name then it world will be replaced with new home record,
22+
* default value is `false`.
23+
*
24+
* @return true if home creating successful otherwise false.
25+
*
26+
* @since 1.14.4-1.2.0
27+
*/
28+
fun create(
29+
owner: ServerPlayerEntity,
30+
name: String = "home",
31+
override: Boolean = false
32+
): Boolean {
33+
val playerUUID = owner.uniqueID.toString()
34+
val clientWorld = owner.commandSource.world.worldInfo.worldName
35+
val worldId = owner.commandSource.world.worldType.id
36+
val xPos = owner.posX.toInt()
37+
val yPos = owner.posY.toInt()
38+
val zPos = owner.posZ.toInt()
39+
val yaw = owner.rotationYaw
40+
val pitch = owner.rotationPitch
41+
val homes = StorageBase.getData(playerUUID).homes
42+
43+
if (homes.isNotEmpty()) {
44+
if (override) {
45+
homes.removeAll {
46+
it.home == name
47+
}
48+
} else {
49+
homes.forEach {
50+
if (it.home == name) return false
51+
}
52+
}
53+
}
54+
55+
homes.add(
56+
HomeModel.Home(
57+
name, clientWorld, worldId, xPos, yPos, zPos, yaw, pitch
58+
)
59+
)
60+
StorageBase.setData(playerUUID, HomeModel(homes))
61+
return true
62+
}
63+
}

src/main/kotlin/com/mairwunnx/projectessentials/home/commands/SetHomeCommand.kt

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import com.mairwunnx.projectessentials.core.helpers.throwOnlyPlayerCan
77
import com.mairwunnx.projectessentials.core.helpers.throwPermissionLevel
88
import com.mairwunnx.projectessentials.home.EntryPoint
99
import com.mairwunnx.projectessentials.home.EntryPoint.Companion.hasPermission
10-
import com.mairwunnx.projectessentials.home.models.HomeModel
10+
import com.mairwunnx.projectessentials.home.api.HomeAPI
1111
import com.mairwunnx.projectessentials.home.sendMessage
12-
import com.mairwunnx.projectessentials.home.storage.StorageBase
1312
import com.mojang.brigadier.CommandDispatcher
1413
import com.mojang.brigadier.arguments.BoolArgumentType
1514
import com.mojang.brigadier.arguments.StringArgumentType
@@ -62,51 +61,19 @@ object SetHomeCommand {
6261
if (c.isPlayerSender()) {
6362
val player = c.source.asPlayer()
6463
if (hasPermission(player, "ess.home.set")) {
65-
val playerUUID = player.uniqueID.toString()
6664
val homeName: String = try {
6765
StringArgumentType.getString(c, "home name")
6866
} catch (_: IllegalArgumentException) {
6967
"home"
7068
}
71-
val clientWorld = c.source.world.worldInfo.worldName
72-
val worldId = c.source.world.worldType.id
73-
val xPos = player.posX.toInt()
74-
val yPos = player.posY.toInt()
75-
val zPos = player.posZ.toInt()
76-
val yaw = player.rotationYaw
77-
val pitch = player.rotationPitch
78-
val homeModel = StorageBase.getData(playerUUID).homes
7969

80-
if (!override) {
81-
homeModel.forEach {
82-
if (it.home == homeName) {
83-
sendMessage(c.source, "set.already_exist", homeName)
84-
return 0
85-
}
86-
}
87-
} else {
88-
homeModel.removeAll {
89-
it.home == homeName
90-
}
70+
val result = HomeAPI.create(player, homeName, override)
71+
if (!result) {
72+
sendMessage(c.source, "set.already_exist", homeName)
73+
return 0
9174
}
9275

93-
homeModel.add(
94-
HomeModel.Home(
95-
homeName, clientWorld, worldId, xPos, yPos, zPos, yaw, pitch
96-
)
97-
)
98-
StorageBase.setData(playerUUID, HomeModel(homeModel))
9976
sendMessage(c.source, "set.success", homeName)
100-
logger.info(
101-
"\n" +
102-
"New home point for ${player.name.string} installed with data: \n" +
103-
" - name: $homeName\n" +
104-
" - world / world id: $clientWorld / $worldId\n" +
105-
" - xpos: $xPos\n" +
106-
" - ypos: $yPos\n" +
107-
" - zpos: $zPos\n" +
108-
" - pitch: $pitch"
109-
)
11077
logger.info("Executed command \"/sethome\" from ${player.name.string}")
11178
} else {
11279
sendMsg("home", c.source, "home.set.restricted")

0 commit comments

Comments
 (0)