Skip to content

Commit 1664c2d

Browse files
v2.2.2:
- Fixed Location loading from the config when the world is not loaded yet
1 parent 4a052cf commit 1664c2d

5 files changed

Lines changed: 42 additions & 6 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group "de.randombyte"
9-
version "2.2.1"
9+
version "2.2.2"
1010

1111
repositories {
1212
jcenter()

src/main/kotlin/de/randombyte/unity/Unity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Unity @Inject constructor(
5656
companion object {
5757
const val ID = "unity"
5858
const val NAME = "Unity"
59-
const val VERSION = "2.2.1"
59+
const val VERSION = "2.2.2"
6060
const val AUTHOR = "RandomByte"
6161

6262
const val NUCLEUS_ID = "nucleus"

src/main/kotlin/de/randombyte/unity/commands/HomeCommand.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package de.randombyte.unity.commands
22

33
import de.randombyte.kosp.extensions.toText
44
import de.randombyte.unity.config.Config
5+
import de.randombyte.unity.config.Config.Unity.HomeLocation.*
6+
import de.randombyte.unity.config.Config.Unity.HomeLocation.Set
57
import de.randombyte.unity.config.ConfigAccessor
68
import org.spongepowered.api.command.CommandException
79
import org.spongepowered.api.command.CommandResult
@@ -12,8 +14,12 @@ class HomeCommand(
1214
configAccessor: ConfigAccessor
1315
) : UnityCommand(configAccessor) {
1416
override fun executedByUnityMember(player: Player, args: CommandContext, thisUnity: Config.Unity, config: Config): CommandResult {
15-
val home = thisUnity.home ?: throw CommandException("No home set!".toText())
16-
player.location = home
17+
val home = thisUnity.tryGetHomeLocation()
18+
when (home) {
19+
NotSet -> throw CommandException("No home set!".toText())
20+
Unreachable -> throw CommandException("The home is not reachable! The world is not loaded.".toText())
21+
is Set -> player.location = home.location
22+
}
1723

1824
return CommandResult.success()
1925
}

src/main/kotlin/de/randombyte/unity/commands/SetHomeCommand.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package de.randombyte.unity.commands
22

33
import de.randombyte.kosp.extensions.green
44
import de.randombyte.unity.config.Config
5+
import de.randombyte.unity.config.Config.Unity.ConfigLocation
56
import de.randombyte.unity.config.ConfigAccessor
67
import org.spongepowered.api.command.CommandResult
78
import org.spongepowered.api.command.args.CommandContext
@@ -12,7 +13,8 @@ class SetHomeCommand(
1213
) : UnityCommand(configAccessor) {
1314
override fun executedByUnityMember(player: Player, args: CommandContext, thisUnity: Config.Unity, config: Config): CommandResult {
1415
val newHome = player.location
15-
configAccessor.set(config.copy(unities = (config.unities - thisUnity) + thisUnity.copy(home = newHome)))
16+
configAccessor.set(config.copy(
17+
unities = (config.unities - thisUnity) + thisUnity.copy(home = ConfigLocation(newHome))))
1618

1719
thisUnity.sendMessage("New home set".green())
1820

src/main/kotlin/de/randombyte/unity/config/Config.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package de.randombyte.unity.config
22

33
import de.randombyte.kosp.extensions.*
44
import de.randombyte.kosp.fixedTextTemplateOf
5+
import de.randombyte.unity.config.Config.Unity.HomeLocation.*
6+
import de.randombyte.unity.config.Config.Unity.HomeLocation.Set
57
import ninja.leaping.configurate.objectmapping.Setting
68
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable
9+
import org.spongepowered.api.Sponge
710
import org.spongepowered.api.text.Text
811
import org.spongepowered.api.text.TextTemplate
912
import org.spongepowered.api.world.Location
@@ -24,9 +27,22 @@ data class Config(
2427
data class Unity(
2528
@Setting("member1") val member1: UUID = UUID(0, 0),
2629
@Setting("member2") val member2: UUID = UUID(0, 0),
27-
@Setting("home") val home: Location<World>? = null,
30+
@Setting("home") val home: ConfigLocation? = null,
2831
@Setting("date") val date: Date = Date.from(Instant.EPOCH)
2932
) {
33+
34+
// retro fits the Sponge Location serialization format, but we have control over when to
35+
// load and construct the Location
36+
@ConfigSerializable
37+
class ConfigLocation(
38+
@Setting("WorldUuid") val worldUuid: UUID = UUID(0, 0),
39+
@Setting("X") val x: Double = 0.0,
40+
@Setting("Y") val y: Double = 0.0,
41+
@Setting("Z") val z: Double = 0.0
42+
) {
43+
constructor(location: Location<World>) : this(location.extent.uniqueId, location.x, location.y, location.z)
44+
}
45+
3046
fun sendMessage(text: Text) {
3147
member1.getPlayer()?.sendMessage(text)
3248
member2.getPlayer()?.sendMessage(text)
@@ -37,6 +53,18 @@ data class Config(
3753
member2 == member -> member1
3854
else -> throw RuntimeException("Getting other member failed, report to developer!")
3955
}
56+
57+
sealed class HomeLocation {
58+
object NotSet : HomeLocation()
59+
object Unreachable : HomeLocation()
60+
class Set(val location: Location<World>) : HomeLocation()
61+
}
62+
63+
fun tryGetHomeLocation(): HomeLocation {
64+
if (home == null) return NotSet
65+
val world = Sponge.getServer().getWorld(home.worldUuid).orNull() ?: return Unreachable
66+
return Set(world.getLocation(home.x, home.y, home.z))
67+
}
4068
}
4169

4270
@ConfigSerializable

0 commit comments

Comments
 (0)