Skip to content

Commit 0a72a84

Browse files
Merge pull request #5 from SLNE-Development/feat/implement-static-and-dynamic-npcs
Feat/implement static and dynamic npcs
2 parents 62db386 + de9ff24 commit 0a72a84

File tree

12 files changed

+51
-11
lines changed

12 files changed

+51
-11
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.1.1-SNAPSHOT
4+
version=1.21.7-1.2.0-SNAPSHOT

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ interface SurfNpcApi {
3030
* @param global Whether the NPC is global (default: true).
3131
* @param rotationType The rotation type of the NPC (default: FIXED).
3232
* @param fixedRotation The fixed rotation of the NPC, if applicable (default: null).
33+
* @param persistent Whether the NPC should be persistent (default: false).
3334
* @return The result of the NPC creation.
3435
*/
3536
fun createNpc(
@@ -39,7 +40,8 @@ interface SurfNpcApi {
3940
location: NpcLocation,
4041
global: Boolean = true,
4142
rotationType: NpcRotationType = NpcRotationType.PER_PLAYER,
42-
fixedRotation: NpcRotation? = null
43+
fixedRotation: NpcRotation? = null,
44+
persistent: Boolean = false
4345
): NpcCreationResult
4446

4547
/**

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class NpcDslBuilder {
4848
*/
4949
var fixedRotation: NpcRotation? = null
5050

51+
/**
52+
* Whether the NPC should be persistent. Defaults to false.
53+
*/
54+
var persistent: Boolean = false
55+
5156
/**
5257
* Configures the skin of the NPC using a DSL block.
5358
*
@@ -141,6 +146,7 @@ fun npc(block: NpcDslBuilder.() -> Unit): NpcCreationResult {
141146
location = builder.location,
142147
global = builder.global,
143148
rotationType = builder.rotationType,
144-
fixedRotation = builder.fixedRotation
149+
fixedRotation = builder.fixedRotation,
150+
persistent = builder.persistent
145151
)
146152
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ interface Npc {
101101
*/
102102
fun addProperty(property: NpcProperty)
103103

104+
/**
105+
* Checks if the NPC is static, meaning it has a persistence property set to true.
106+
* If the NPC is static, it will be persistent across server restarts.
107+
*/
108+
fun isStatic() = properties.any { it.key == NpcProperty.Internal.PERSISTENCE && it.value.value as? Boolean ?: false }
109+
104110

105111
/**
106112
* Adds properties to the NPC.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,10 @@ interface NpcProperty {
5252
* The fixed rotation value for the NPC.
5353
*/
5454
const val ROTATION_FIXED = "rotation"
55+
56+
/**
57+
* The persistence of the NPC.
58+
*/
59+
const val PERSISTENCE = "persistence"
5560
}
5661
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class BukkitSurfNpcApi : SurfNpcApi, Services.Fallback {
3333
location: NpcLocation,
3434
global: Boolean,
3535
rotationType: NpcRotationType,
36-
fixedRotation: NpcRotation?
36+
fixedRotation: NpcRotation?,
37+
persistent: Boolean
3738
): NpcCreationResult {
3839
return npcController.createNpc(
3940
uniqueName,
@@ -42,7 +43,8 @@ class BukkitSurfNpcApi : SurfNpcApi, Services.Fallback {
4243
location,
4344
rotationType,
4445
fixedRotation ?: BukkitNpcRotation(0f, 0f),
45-
global
46+
global,
47+
persistent
4648
)
4749
}
4850

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class NpcCreateCommand(commandName: String) : CommandAPICommand(commandName) {
5656
BukkitNpcLocation(location.x, location.y, location.z, location.world.name),
5757
rotationType,
5858
BukkitNpcRotation(location.yaw, location.pitch),
59-
true
59+
true,
60+
persistent = true
6061
)
6162

6263
val npc = npcController.getNpc(uniqueName)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class NpcListCommand(commandName: String) : CommandAPICommand(commandName) {
4343
variableValue(npc.uniqueName)
4444
appendSpace()
4545
info("(${npc.id})")
46+
if (npc.isStatic()) {
47+
appendSpace()
48+
spacer("(Static)")
49+
}
4650
clickRunsCommand("/npc info ${npc.uniqueName}")
4751
}
4852
)

surf-npc-bukkit/src/main/kotlin/dev/slne/surf/npc/bukkit/controller/BukkitNpcController.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class BukkitNpcController : NpcController, Services.Fallback {
4040
location: NpcLocation,
4141
rotationType: NpcRotationType,
4242
rotation: NpcRotation,
43-
global: Boolean
43+
global: Boolean,
44+
persistent: Boolean
4445
): NpcCreationResult {
4546
val id = random.nextInt()
4647
val nameTagId = random.nextInt()
@@ -124,6 +125,14 @@ class BukkitNpcController : NpcController, Services.Fallback {
124125
)
125126
)
126127

128+
npc.addProperty(
129+
BukkitNpcProperty(
130+
NpcProperty.Internal.PERSISTENCE, persistent, propertyTypeRegistry.get(
131+
NpcPropertyType.Types.BOOLEAN
132+
) ?: error("BOOLEAN property type not found")
133+
)
134+
)
135+
127136
this.registerNpc(npc)
128137

129138
if (global) {

surf-npc-bukkit/src/main/kotlin/dev/slne/surf/npc/bukkit/storage/BukkitStorageService.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ class BukkitStorageService : StorageService, Services.Fallback {
9797
.filter { it.toString().endsWith(".yml") }
9898
.forEach(Files::delete)
9999

100-
npcController.getNpcs().forEach { npc ->
100+
val staticNpcs = npcController.getNpcs().filter { it.isStatic() }
101+
102+
staticNpcs.forEach { npc ->
101103
val file = npcFolder.resolve("${npc.uniqueName}.yml").toFile()
102104
val config = YamlConfiguration()
103105

@@ -118,8 +120,8 @@ class BukkitStorageService : StorageService, Services.Fallback {
118120
config.save(file)
119121
}
120122

121-
logger().atInfo().log("Successfully saved ${npcController.getNpcs().size} NPCs to files!")
122-
return npcController.getNpcs().size
123+
logger().atInfo().log("Successfully saved ${staticNpcs.size} NPCs to files!")
124+
return staticNpcs.size
123125
}
124126

125127
override fun import(fileName: String): Boolean {

0 commit comments

Comments
 (0)