Skip to content

Commit fe2e9a2

Browse files
committed
HomeAPI fully implemented.
HomeAPI implemented save method. HomeAPI implemented reload method. `StorageBase` now is internal. Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 25f30c7 commit fe2e9a2

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mairwunnx.projectessentials.home
33
import com.mairwunnx.projectessentials.core.EssBase
44
import com.mairwunnx.projectessentials.core.configuration.localization.LocalizationConfigurationUtils
55
import com.mairwunnx.projectessentials.core.localization.processLocalizations
6+
import com.mairwunnx.projectessentials.home.api.HomeAPI
67
import com.mairwunnx.projectessentials.home.commands.DelHomeCommand
78
import com.mairwunnx.projectessentials.home.commands.HomeCommand
89
import com.mairwunnx.projectessentials.home.commands.SetHomeCommand
@@ -62,7 +63,7 @@ class EntryPoint : EssBase() {
6263
@Suppress("UNUSED_PARAMETER")
6364
@SubscribeEvent
6465
fun onServerStopping(it: FMLServerStoppingEvent) {
65-
StorageBase.saveUserData()
66+
HomeAPI.save()
6667
}
6768

6869
private fun loadAdditionalModules() {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ object HomeAPI {
121121
*
122122
* @return true if home exist otherwise false.
123123
*
124+
* @see takeAll
125+
*
124126
* @since 1.14.4-1.2.0
125127
*/
126128
fun contains(
@@ -132,4 +134,22 @@ object HomeAPI {
132134
}
133135
return false
134136
}
137+
138+
/**
139+
* Saves user data to local storage, to json file.
140+
*/
141+
fun save() = StorageBase.saveUserData()
142+
143+
/**
144+
* Reloading configuration from local storage,
145+
* with saving if argument `withSaving` true.
146+
*
147+
* @param withSaving if true then configuration
148+
* will be saved before reloading. Default values
149+
* is true.
150+
*/
151+
fun reload(withSaving: Boolean = true) {
152+
if (withSaving) save()
153+
StorageBase.loadUserData()
154+
}
135155
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ 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.api.HomeAPI
1011
import com.mairwunnx.projectessentials.home.models.HomeModel
1112
import com.mairwunnx.projectessentials.home.sendMessage
12-
import com.mairwunnx.projectessentials.home.storage.StorageBase
1313
import com.mojang.brigadier.CommandDispatcher
1414
import com.mojang.brigadier.arguments.StringArgumentType
1515
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
@@ -53,19 +53,17 @@ object HomeCommand {
5353
if (c.isPlayerSender()) {
5454
val player = c.source.asPlayer()
5555
if (hasPermission(player, "ess.home")) {
56-
val playerUUID = player.uniqueID.toString()
5756
val homeName: String = try {
5857
StringArgumentType.getString(c, "home name")
5958
} catch (_: IllegalArgumentException) {
6059
"home"
6160
}
62-
val home = StorageBase.getData(playerUUID)
63-
home.homes.forEach {
64-
if (it.home == homeName) {
65-
moveToHome(player, it)
66-
return 0
67-
}
61+
62+
HomeAPI.take(player, homeName)?.let {
63+
moveToHome(player, it)
64+
return 0
6865
}
66+
6967
sendMessage(c.source, "not_found", homeName)
7068
logger.info("Player ${player.name.string} try teleport to not exist home $homeName")
7169
} else {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import java.io.File
88
import java.io.FileNotFoundException
99
import kotlin.system.measureTimeMillis
1010

11-
object StorageBase {
11+
internal object StorageBase {
1212
private val logger = LogManager.getLogger()
1313
private val userHomeData = hashMapOf<String, HomeModel>()
1414
private val userDataFolder = MOD_CONFIG_FOLDER + File.separator + "user-data"
1515

16-
fun getData(uuid: String): HomeModel {
16+
internal fun getData(uuid: String): HomeModel {
1717
if (userHomeData.containsKey(uuid)) {
1818
val requestedData = userHomeData[uuid] ?: HomeModel()
1919
logger.debug("Requested home data ($requestedData) for UUID ($uuid).")
@@ -23,12 +23,12 @@ object StorageBase {
2323
return HomeModel()
2424
}
2525

26-
fun setData(uuid: String, data: HomeModel) {
26+
internal fun setData(uuid: String, data: HomeModel) {
2727
userHomeData[uuid] = data
2828
logger.debug("Installed home data (${data}) for UUID ($uuid).")
2929
}
3030

31-
fun loadUserData() {
31+
internal fun loadUserData() {
3232
logger.info("Loading user home data configuration")
3333

3434
File(userDataFolder).mkdirs()
@@ -56,7 +56,7 @@ object StorageBase {
5656
logger.info("Loading user home data done configurations with ${elapsedTime}ms")
5757
}
5858

59-
fun saveUserData() {
59+
internal fun saveUserData() {
6060
File(userDataFolder).mkdirs()
6161
userHomeData.keys.forEach {
6262
val userId = it

0 commit comments

Comments
 (0)