Skip to content

Commit 268540f

Browse files
Merge pull request #13 from SLNE-Development/feat/sealed-classes-and-equals-methods
Feat/sealed classes and equals methods
2 parents 5029538 + 01afd37 commit 268540f

File tree

10 files changed

+227
-87
lines changed

10 files changed

+227
-87
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
kotlin.code.style=official
22
kotlin.stdlib.default.dependency=false
33
org.gradle.parallel=true
4-
version=1.21.7-1.3.0-SNAPSHOT
4+
version=1.21.7-1.3.1-SNAPSHOT

surf-npc-api/src/main/kotlin/dev/slne/surf/npc/api/npc/Npc.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ interface Npc {
114114
* Checks if the NPC is static, meaning it has a persistence property set to true.
115115
* If the NPC is static, it will be persistent across server restarts.
116116
*/
117-
fun isStatic() = properties.any { it.key == NpcProperty.Internal.PERSISTENCE && it.value.value as? Boolean ?: false }
117+
fun isStatic() =
118+
properties.any { it.key == NpcProperty.Internal.PERSISTENCE && it.value.value as? Boolean ?: false }
118119

119120

120121
/**
Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,60 @@
11
package dev.slne.surf.npc.api.result
22

3+
import dev.slne.surf.npc.api.npc.Npc
4+
35
/**
4-
* Enum representing the result of an NPC creation attempt.
6+
* Represents the result of an NPC creation attempt.
57
*/
6-
enum class NpcCreationResult {
7-
/** Indicates that the NPC was successfully created. */
8-
SUCCESS,
8+
sealed class NpcCreationResult {
9+
/**
10+
* Indicates that the NPC was created successfully.
11+
*
12+
* @property npc The created NPC instance.
13+
*/
14+
data class Success(val npc: Npc) : NpcCreationResult()
15+
16+
/**
17+
* Indicates that the NPC creation failed.
18+
*
19+
* @property reason The reason for the failure.
20+
*/
21+
data class Failure(val reason: NpcCreationFailureReason) : NpcCreationResult()
22+
23+
/**
24+
* Checks if the result represents a successful NPC creation.
25+
*
26+
* @return `true` if the result is a success, otherwise `false`.
27+
*/
28+
fun isSuccess(): Boolean {
29+
return this is Success
30+
}
931

10-
/** Indicates that the NPC creation failed because an NPC with the same identifier already exists. */
11-
FAILED_ALREADY_EXISTS,
32+
/**
33+
* Checks if the result represents a failed NPC creation.
34+
*
35+
* @return `true` if the result is a failure, otherwise `false`.
36+
*/
37+
fun isFailure(): Boolean {
38+
return this is Failure
39+
}
40+
}
41+
42+
/**
43+
* Enumerates possible reasons for NPC creation failure.
44+
*/
45+
enum class NpcCreationFailureReason {
46+
/** An NPC with the same identifier already exists. */
47+
ALREADY_EXISTS,
1248

13-
/** Indicates that the NPC creation failed due to an invalid location. */
14-
FAILED_INVALID_LOCATION,
49+
/** The specified location is invalid. */
50+
INVALID_LOCATION,
1551

16-
/** Indicates that the NPC creation failed due to an invalid name. */
17-
FAILED_INVALID_NAME,
52+
/** The specified name is invalid. */
53+
INVALID_NAME,
1854

19-
/** Indicates that the NPC creation failed due to an invalid skin. */
20-
FAILED_INVALID_SKIN,
55+
/** The specified skin is invalid. */
56+
INVALID_SKIN,
2157

22-
/** Indicates that the NPC creation failed due to an unspecified reason. */
23-
FAILED_OTHER
58+
/** The failure reason is unknown. */
59+
UNKNOWN
2460
}

surf-npc-api/src/main/kotlin/dev/slne/surf/npc/api/result/NpcDeletionResult.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ enum class NpcDeletionResult {
1010
/** Indicates that the NPC deletion failed because the NPC was not found. */
1111
FAILED_NOT_FOUND,
1212

13+
/** Indicates that the NPC deletion failed because the NPC was not spawned. */
1314
FAILED_NOT_SPAWNED,
1415

1516
/** Indicates that the NPC deletion failed due to an unspecified reason. */
Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
package dev.slne.surf.npc.api.result
22

3+
import dev.slne.surf.npc.api.npc.Npc
4+
35
/**
4-
* Enum representing the result of an NPC respawn attempt.
6+
* Represents the result of an NPC despawn attempt.
57
*/
6-
enum class NpcDespawnResult {
7-
/** Indicates that the NPC was successfully respawned. */
8-
SUCCESS,
8+
sealed class NpcDespawnResult {
9+
/**
10+
* Indicates that the NPC was successfully despawned.
11+
*
12+
* @property npc The NPC instance that was successfully despawned.
13+
*/
14+
data class Success(val npc: Npc) : NpcDespawnResult()
15+
16+
/**
17+
* Indicates that the NPC despawn attempt failed.
18+
*
19+
* @property reason The reason for the despawn failure.
20+
*/
21+
data class Failure(val reason: NpcDespawnFailureReason) : NpcDespawnResult()
22+
23+
/**
24+
* Checks if the result represents a successful NPC despawn.
25+
*
26+
* @return `true` if the result is a success, otherwise `false`.
27+
*/
28+
fun isSuccess(): Boolean {
29+
return this is Success
30+
}
31+
32+
/**
33+
* Checks if the result represents a failed NPC despawn.
34+
*
35+
* @return `true` if the result is a failure, otherwise `false`.
36+
*/
37+
fun isFailure(): Boolean {
38+
return this is Failure
39+
}
40+
}
941

10-
/** Indicates that the NPC respawn failed because the NPC does not exist. */
11-
FAILED_NOT_EXIST,
42+
enum class NpcDespawnFailureReason {
43+
/** The NPC does not exist. */
44+
NOT_EXIST,
1245

13-
/** Indicates that the NPC respawn failed because the NPC is already spawned. */
14-
FAILED_ALREADY_SPAWNED,
46+
/** The NPC is not spawned. */
47+
NOT_SPAWNED,
1548

16-
/** Indicates that the NPC respawn failed due to no valid location being available. */
17-
FAILED_NO_LOCATION,
49+
/** The NPC is already despawned. */
50+
ALREADY_DESPAWNED,
1851

19-
/** Indicates that the NPC respawn failed due to an unspecified reason. */
20-
FAILED_OTHER
52+
/** The failure reason is unknown. */
53+
UNKNOWN
2154
}
Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
package dev.slne.surf.npc.api.result
22

3+
import dev.slne.surf.npc.api.npc.Npc
4+
35
/**
4-
* Enum representing the result of an NPC respawn attempt.
6+
* Represents the result of an NPC respawn attempt.
57
*/
6-
enum class NpcRespawnResult {
7-
/** Indicates that the NPC was successfully respawned. */
8-
SUCCESS,
8+
sealed class NpcRespawnResult {
9+
/**
10+
* Indicates that the NPC was successfully respawned.
11+
*
12+
* @property npc The NPC instance that was successfully respawned.
13+
*/
14+
data class Success(val npc: Npc) : NpcRespawnResult()
15+
16+
/**
17+
* Indicates that the NPC respawn attempt failed.
18+
*
19+
* @property reason The reason for the respawn failure.
20+
*/
21+
data class Failure(val reason: NpcRespawnFailureReason) : NpcRespawnResult()
22+
23+
/**
24+
* Checks if the result represents a successful NPC respawn.
25+
*
26+
* @return `true` if the result is a success, otherwise `false`.
27+
*/
28+
fun isSuccess(): Boolean {
29+
return this is Success
30+
}
31+
32+
/**
33+
* Checks if the result represents a failed NPC respawn.
34+
*
35+
* @return `true` if the result is a failure, otherwise `false`.
36+
*/
37+
fun isFailure(): Boolean {
38+
return this is Failure
39+
}
40+
}
941

10-
/** Indicates that the NPC respawn failed because the NPC does not exist. */
11-
FAILED_NOT_EXIST,
42+
enum class NpcRespawnFailureReason {
43+
/** The NPC does not exist. */
44+
NOT_EXIST,
1245

13-
/** Indicates that the NPC respawn failed because the NPC is already spawned. */
14-
FAILED_ALREADY_SPAWNED,
46+
/** The NPC is already spawned. */
47+
ALREADY_SPAWNED,
1548

16-
/** Indicates that the NPC respawn failed due to no valid location being available. */
17-
FAILED_NO_LOCATION,
49+
/** No valid location is available for respawning the NPC. */
50+
NO_LOCATION,
1851

19-
/** Indicates that the NPC respawn failed due to an unspecified reason. */
20-
FAILED_OTHER
52+
/** The failure reason is unknown. */
53+
UNKNOWN
2154
}
Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
package dev.slne.surf.npc.api.result
22

3+
import dev.slne.surf.npc.api.npc.Npc
4+
35
/**
4-
* Enum representing the result of an NPC respawn attempt.
6+
* Sealed class representing the result of an NPC spawn attempt.
57
*/
6-
enum class NpcSpawnResult {
7-
/** Indicates that the NPC was successfully respawned. */
8-
SUCCESS,
8+
sealed class NpcSpawnResult {
9+
/**
10+
* Represents a successful NPC spawn result.
11+
*
12+
* @property npc The NPC instance that was successfully spawned.
13+
*/
14+
data class Success(val npc: Npc) : NpcSpawnResult()
15+
16+
/**
17+
* Represents a failed NPC spawn result.
18+
*
19+
* @property reason The reason for the spawn failure.
20+
*/
21+
data class Failure(val reason: NpcSpawnFailureReason) : NpcSpawnResult()
22+
23+
/**
24+
* Checks if the result represents a successful NPC spawn.
25+
*
26+
* @return `true` if the result is a success, otherwise `false`.
27+
*/
28+
fun isSuccess(): Boolean {
29+
return this is Success
30+
}
31+
32+
/**
33+
* Checks if the result represents a failed NPC spawn.
34+
*
35+
* @return `true` if the result is a failure, otherwise `false`.
36+
*/
37+
fun isFailure(): Boolean {
38+
return this is Failure
39+
}
40+
}
941

10-
/** Indicates that the NPC respawn failed because the NPC does not exist. */
11-
FAILED_NOT_EXIST,
42+
enum class NpcSpawnFailureReason {
43+
/** The NPC does not exist. */
44+
NOT_EXIST,
1245

13-
/** Indicates that the NPC respawn failed because the NPC is already spawned. */
14-
FAILED_ALREADY_SPAWNED,
46+
/** The NPC is already spawned. */
47+
ALREADY_SPAWNED,
1548

16-
/** Indicates that the NPC respawn failed due to no valid location being available. */
17-
FAILED_NO_LOCATION,
49+
/** No valid location is available for spawning the NPC. */
50+
NO_LOCATION,
1851

19-
/** Indicates that the NPC respawn failed due to an unspecified reason. */
20-
FAILED_OTHER
52+
/** The failure reason is unknown. */
53+
UNKNOWN
2154
}

surf-npc-bukkit/src/main/kotlin/dev/slne/surf/npc/bukkit/command/sub/NpcCreateCommand.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,17 @@ class NpcCreateCommand(commandName: String) : CommandAPICommand(commandName) {
6060
persistent = true
6161
)
6262

63-
val npc = npcController.getNpc(uniqueName)
64-
65-
if (npc == null) {
63+
if (npcResult.isFailure()) {
6664
player.sendText {
6765
appendPrefix()
68-
error("Der Npc konnte nicht erstellt werden: ${npcResult.name}")
66+
error("Der Npc konnte nicht erstellt werden: ${(npcResult as? NpcCreationResult.Failure)?.reason}")
6967
}
7068
return@launch
7169
}
7270

73-
if (npcResult == NpcCreationResult.SUCCESS) {
74-
player.sendText {
75-
appendPrefix()
76-
success("Der Npc wurde erfolgreich erstellt.")
77-
}
78-
} else {
79-
player.sendText {
80-
appendPrefix()
81-
error("Der Npc konnte nicht erstellt werden: ${npcResult.name}")
82-
}
71+
player.sendText {
72+
appendPrefix()
73+
success("Der Npc wurde erfolgreich erstellt.")
8374
}
8475
}
8576
}

0 commit comments

Comments
 (0)