Skip to content

Commit 25f30c7

Browse files
committed
Implemented HomeAPI.remove method.
Implemented HomeAPI.takeAll method. Implemented HomeAPI.take method. Implemented HomeAPI.contains method. Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent bdce8ec commit 25f30c7

File tree

2 files changed

+91
-23
lines changed

2 files changed

+91
-23
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/home/api/HomeAPI.kt

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,14 @@ object HomeAPI {
3838
val zPos = owner.posZ.toInt()
3939
val yaw = owner.rotationYaw
4040
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-
}
41+
val homes = takeAll(owner)
42+
43+
if (override) {
44+
homes.removeAll {
45+
it.home == name
5246
}
47+
} else {
48+
if (contains(homes, name)) return false
5349
}
5450

5551
homes.add(
@@ -60,4 +56,80 @@ object HomeAPI {
6056
StorageBase.setData(playerUUID, HomeModel(homes))
6157
return true
6258
}
59+
60+
/**
61+
* Remove home for target player with specified name.
62+
*
63+
* @param owner ServerPlayerEntity class instance,
64+
* target home owner.
65+
* @param name home name to remove with default value `home`.
66+
*
67+
* @return true if home removing successful otherwise false.
68+
*
69+
* @since 1.14.4-1.2.0
70+
*/
71+
fun remove(
72+
owner: ServerPlayerEntity,
73+
name: String = "home"
74+
): Boolean {
75+
val playerUUID = owner.uniqueID.toString()
76+
val homes = takeAll(owner)
77+
78+
take(owner, name)?.let {
79+
homes.remove(it)
80+
StorageBase.setData(playerUUID, HomeModel(homes))
81+
return true
82+
}
83+
return false
84+
}
85+
86+
/**
87+
* @param owner ServerPlayerEntity class instance,
88+
* target home owner.
89+
*
90+
* @return all player registered homes.
91+
*
92+
* @since 1.14.4-1.2.0
93+
*/
94+
fun takeAll(owner: ServerPlayerEntity): MutableList<HomeModel.Home> =
95+
StorageBase.getData(owner.uniqueID.toString()).homes
96+
97+
/**
98+
* @param owner ServerPlayerEntity class instance,
99+
* target home owner.
100+
* @param name home name with default value `home`.
101+
*
102+
* @return null if home not exist otherwise home instance.
103+
*
104+
* @since 1.14.4-1.2.0
105+
*/
106+
fun take(
107+
owner: ServerPlayerEntity,
108+
name: String = "home"
109+
): HomeModel.Home? {
110+
val homes = takeAll(owner)
111+
homes.forEach {
112+
if (it.home == name) return it
113+
}
114+
return null
115+
}
116+
117+
/**
118+
* @param homeCollection player home collection. Can
119+
* be taken with `takeAll` method.
120+
* @param name home name.
121+
*
122+
* @return true if home exist otherwise false.
123+
*
124+
* @since 1.14.4-1.2.0
125+
*/
126+
fun contains(
127+
homeCollection: List<HomeModel.Home>,
128+
name: String
129+
): Boolean {
130+
homeCollection.forEach {
131+
if (it.home == name) return true
132+
}
133+
return false
134+
}
63135
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import com.mairwunnx.projectessentials.core.helpers.throwOnlyPlayerCan
66
import com.mairwunnx.projectessentials.core.helpers.throwPermissionLevel
77
import com.mairwunnx.projectessentials.home.EntryPoint
88
import com.mairwunnx.projectessentials.home.EntryPoint.Companion.hasPermission
9-
import com.mairwunnx.projectessentials.home.models.HomeModel
9+
import com.mairwunnx.projectessentials.home.api.HomeAPI
1010
import com.mairwunnx.projectessentials.home.sendMessage
11-
import com.mairwunnx.projectessentials.home.storage.StorageBase
1211
import com.mojang.brigadier.CommandDispatcher
1312
import com.mojang.brigadier.arguments.StringArgumentType
1413
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
@@ -48,22 +47,19 @@ object DelHomeCommand {
4847
if (c.isPlayerSender()) {
4948
val player = c.source.asPlayer()
5049
if (hasPermission(player, "ess.home.remove")) {
51-
val playerUUID = player.uniqueID.toString()
5250
val homeName: String = try {
5351
StringArgumentType.getString(c, "home name")
5452
} catch (_: IllegalArgumentException) {
5553
"home"
5654
}
57-
val homeModel = StorageBase.getData(playerUUID).homes
58-
StorageBase.getData(playerUUID).homes.forEach {
59-
if (it.home == homeName) {
60-
homeModel.remove(it)
61-
StorageBase.setData(playerUUID, HomeModel(homeModel))
62-
sendMessage(c.source, "remove.success", homeName)
63-
logger.info("Executed command \"/delhome\" from ${player.name.string}")
64-
return 0
65-
}
55+
56+
val result = HomeAPI.remove(player, homeName)
57+
if (result) {
58+
sendMessage(c.source, "remove.success", homeName)
59+
logger.info("Executed command \"/delhome\" from ${player.name.string}")
60+
return 0
6661
}
62+
6763
sendMessage(c.source, "not_found", homeName)
6864
} else {
6965
sendMessage(c.source, "remove.restricted")

0 commit comments

Comments
 (0)