Skip to content

Commit 87dbf89

Browse files
Merge pull request #15 from SLNE-Development/feat/14-add-owner-ship-to-npcs-with-spawn-reason
Feat/14 add owner ship to npcs with spawn reason
2 parents 268540f + 484d9d3 commit 87dbf89

File tree

23 files changed

+221
-52
lines changed

23 files changed

+221
-52
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.1-SNAPSHOT
4+
version=1.21.7-1.4.0-SNAPSHOT

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.slne.surf.npc.api
22

33
import dev.slne.surf.npc.api.npc.Npc
4+
import dev.slne.surf.npc.api.npc.NpcCreatorType
45
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
56
import dev.slne.surf.npc.api.npc.location.NpcLocation
67
import dev.slne.surf.npc.api.npc.property.NpcProperty
@@ -17,6 +18,7 @@ import it.unimi.dsi.fastutil.objects.ObjectList
1718
import it.unimi.dsi.fastutil.objects.ObjectSet
1819
import net.kyori.adventure.text.Component
1920
import net.kyori.adventure.text.format.NamedTextColor
21+
import org.bukkit.plugin.java.JavaPlugin
2022
import java.util.*
2123

2224
/**
@@ -49,7 +51,9 @@ interface SurfNpcApi {
4951
fixedRotation: NpcRotation? = null,
5052
persistent: Boolean = false,
5153
glowing: Boolean = false,
52-
glowingColor: NamedTextColor = NamedTextColor.WHITE
54+
glowingColor: NamedTextColor = NamedTextColor.WHITE,
55+
plugin: JavaPlugin,
56+
npcCreatorType: NpcCreatorType = NpcCreatorType.Plugin(plugin.name)
5357
): NpcCreationResult
5458

5559
/**

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dev.slne.surf.npc.api.result.NpcCreationResult
99
import dev.slne.surf.npc.api.surfNpcApi
1010
import dev.slne.surf.surfapi.core.api.messages.builder.SurfComponentBuilder
1111
import net.kyori.adventure.text.format.NamedTextColor
12+
import org.bukkit.plugin.java.JavaPlugin
1213

1314
/**
1415
* Builder class for creating NPCs using a DSL.
@@ -145,10 +146,11 @@ suspend fun skin(name: String): NpcSkin {
145146
/**
146147
* Creates an NPC using a DSL block.
147148
*
149+
* @param plugin The JavaPlugin instance for the NPC creation.
148150
* @param block The DSL block for configuring the NPC.
149151
* @return The result of the NPC creation.
150152
*/
151-
fun npc(block: NpcDslBuilder.() -> Unit): NpcCreationResult {
153+
fun npc(plugin: JavaPlugin, block: NpcDslBuilder.() -> Unit): NpcCreationResult {
152154
val builder = NpcDslBuilder().apply(block)
153155
return surfNpcApi.createNpc(
154156
displayName = SurfComponentBuilder.builder().apply(builder.displayName).build(),
@@ -160,6 +162,7 @@ fun npc(block: NpcDslBuilder.() -> Unit): NpcCreationResult {
160162
fixedRotation = builder.fixedRotation,
161163
persistent = builder.persistent,
162164
glowing = builder.glowing,
163-
glowingColor = builder.glowingColor
165+
glowingColor = builder.glowingColor,
166+
plugin = plugin
164167
)
165168
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ interface Npc {
117117
fun isStatic() =
118118
properties.any { it.key == NpcProperty.Internal.PERSISTENCE && it.value.value as? Boolean ?: false }
119119

120+
/**
121+
* Checks if the NPC is created by a plugin.
122+
*
123+
* @return True if the NPC is created by a plugin, false otherwise.
124+
*/
125+
fun isFromPlugin() =
126+
properties.any { it.key == NpcProperty.Internal.CREATOR_TYPE && it.value.value is NpcCreatorType.Plugin }
127+
120128

121129
/**
122130
* Adds properties to the NPC.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dev.slne.surf.npc.api.npc
2+
3+
/**
4+
* Represents the type of an NPC creator.
5+
*/
6+
sealed class NpcCreatorType() {
7+
8+
/**
9+
* Represents a player as the creator of the NPC.
10+
*
11+
* @property name The name of the player who created the NPC.
12+
*/
13+
data class Player(val name: String) : NpcCreatorType()
14+
15+
/**
16+
* Represents a plugin as the creator of the NPC.
17+
*
18+
* @property name The name of the plugin that created the NPC.
19+
*/
20+
data class Plugin(val name: String) : NpcCreatorType()
21+
22+
/**
23+
* Checks if the creator type is a player.
24+
*
25+
* @return `true` if the creator is a player, otherwise `false`.
26+
*/
27+
fun isPlayer(): Boolean {
28+
return this is Player
29+
}
30+
31+
/**
32+
* Checks if the creator type is a plugin.
33+
*
34+
* @return `true` if the creator is a plugin, otherwise `false`.
35+
*/
36+
fun isPlugin(): Boolean {
37+
return this is Plugin
38+
}
39+
40+
/**
41+
* Returns the name of the creator.
42+
*
43+
* @return The name of the creator, whether it's a player or a plugin.
44+
*/
45+
46+
fun name(): String {
47+
return when (this) {
48+
is Player -> name
49+
is Plugin -> name
50+
}
51+
}
52+
}

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
@@ -57,5 +57,10 @@ interface NpcProperty {
5757
* The color of the glowing effect for the NPC.
5858
*/
5959
const val GLOWING_COLOR = "glowing_color"
60+
61+
/**
62+
* The type of the creator of the NPC.
63+
*/
64+
const val CREATOR_TYPE = "creator_type"
6065
}
6166
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,10 @@ interface NpcPropertyType {
8888
* Represents a skin data property type for an NPC.
8989
*/
9090
const val SKIN_DATA = "skin_data"
91+
92+
/**
93+
* The type of NPC creator.
94+
*/
95+
const val NPC_CREATOR_TYPE = "npc_creator_type"
9196
}
9297
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class BukkitMain : SuspendingJavaPlugin() {
3333
propertyTypeRegistry.register(NamedTextColorPropertyType(NpcPropertyType.Types.NAMED_TEXT_COLOR))
3434
propertyTypeRegistry.register(NpcRotationPropertyType(NpcPropertyType.Types.NPC_ROTATION))
3535
propertyTypeRegistry.register(SkinDataPropertyType(NpcPropertyType.Types.SKIN_DATA))
36+
propertyTypeRegistry.register(NpcCreatorTypePropertyType(NpcPropertyType.Types.NPC_CREATOR_TYPE))
3637

3738
storageService.initialize()
3839
storageService.loadNpcs()

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dev.slne.surf.npc.bukkit.api
33
import com.google.auto.service.AutoService
44
import dev.slne.surf.npc.api.SurfNpcApi
55
import dev.slne.surf.npc.api.npc.Npc
6+
import dev.slne.surf.npc.api.npc.NpcCreatorType
67
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
78
import dev.slne.surf.npc.api.npc.location.NpcLocation
89
import dev.slne.surf.npc.api.npc.property.NpcProperty
@@ -25,6 +26,7 @@ import it.unimi.dsi.fastutil.objects.ObjectSet
2526
import net.kyori.adventure.text.Component
2627
import net.kyori.adventure.text.format.NamedTextColor
2728
import net.kyori.adventure.util.Services
29+
import org.bukkit.plugin.java.JavaPlugin
2830
import java.util.*
2931

3032
@AutoService(SurfNpcApi::class)
@@ -39,7 +41,9 @@ class BukkitSurfNpcApi : SurfNpcApi, Services.Fallback {
3941
fixedRotation: NpcRotation?,
4042
persistent: Boolean,
4143
glowing: Boolean,
42-
glowingColor: NamedTextColor
44+
glowingColor: NamedTextColor,
45+
plugin: JavaPlugin,
46+
npcCreatorType: NpcCreatorType
4347
): NpcCreationResult {
4448
return npcController.createNpc(
4549
uniqueName,
@@ -51,7 +55,8 @@ class BukkitSurfNpcApi : SurfNpcApi, Services.Fallback {
5155
global,
5256
persistent,
5357
glowing,
54-
glowingColor
58+
glowingColor,
59+
NpcCreatorType.Plugin(plugin.name)
5560
)
5661
}
5762

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dev.jorel.commandapi.kotlindsl.getValue
66
import dev.jorel.commandapi.kotlindsl.playerExecutor
77
import dev.jorel.commandapi.kotlindsl.stringArgument
88
import dev.jorel.commandapi.kotlindsl.textArgument
9+
import dev.slne.surf.npc.api.npc.NpcCreatorType
910
import dev.slne.surf.npc.api.npc.rotation.NpcRotationType
1011
import dev.slne.surf.npc.api.result.NpcCreationResult
1112
import dev.slne.surf.npc.bukkit.command.argument.rotationTypeArgument
@@ -31,6 +32,7 @@ class NpcCreateCommand(commandName: String) : CommandAPICommand(commandName) {
3132
val skin: String by args
3233
val rotationType: NpcRotationType by args
3334
val location = player.location
35+
val createdBy = player.name
3436

3537
if (!this.isValidName(name)) {
3638
player.sendText {
@@ -57,7 +59,8 @@ class NpcCreateCommand(commandName: String) : CommandAPICommand(commandName) {
5759
rotationType,
5860
BukkitNpcRotation(location.yaw, location.pitch),
5961
true,
60-
persistent = true
62+
persistent = true,
63+
npcCreatorType = NpcCreatorType.Player(createdBy)
6164
)
6265

6366
if (npcResult.isFailure()) {

0 commit comments

Comments
 (0)