Skip to content

Commit bf8e7a9

Browse files
Merge pull request #10 from SLNE-Development/feat/8-add-surf-glowing-api-support
Feat/8 add surf glowing api support
2 parents e686bba + 32eb019 commit bf8e7a9

File tree

10 files changed

+118
-75
lines changed

10 files changed

+118
-75
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import dev.slne.surf.surfapi.core.api.util.requiredService
1414
import it.unimi.dsi.fastutil.objects.ObjectList
1515
import it.unimi.dsi.fastutil.objects.ObjectSet
1616
import net.kyori.adventure.text.Component
17+
import net.kyori.adventure.text.format.NamedTextColor
1718
import java.util.*
1819

1920
/**
@@ -32,6 +33,8 @@ interface SurfNpcApi {
3233
* @param rotationType The rotation type of the NPC (default: FIXED).
3334
* @param fixedRotation The fixed rotation of the NPC, if applicable (default: null).
3435
* @param persistent Whether the NPC should be persistent (default: false).
36+
* @param glowing Whether the NPC should glow (default: false).
37+
* @param glowingColor The color of the glow effect (default: NamedTextColor.WHITE).
3538
* @return The result of the NPC creation.
3639
*/
3740
fun createNpc(
@@ -42,7 +45,9 @@ interface SurfNpcApi {
4245
global: Boolean = true,
4346
rotationType: NpcRotationType = NpcRotationType.PER_PLAYER,
4447
fixedRotation: NpcRotation? = null,
45-
persistent: Boolean = false
48+
persistent: Boolean = false,
49+
glowing: Boolean = false,
50+
glowingColor: NamedTextColor = NamedTextColor.WHITE
4651
): NpcCreationResult
4752

4853
/**

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dev.slne.surf.npc.api.npc.skin.NpcSkin
88
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
11+
import net.kyori.adventure.text.format.NamedTextColor
1112

1213
/**
1314
* Builder class for creating NPCs using a DSL.
@@ -53,6 +54,16 @@ class NpcDslBuilder {
5354
*/
5455
var persistent: Boolean = false
5556

57+
/**
58+
* Whether the NPC should glow. Defaults to false.
59+
*/
60+
var glowing: Boolean = false
61+
62+
/**
63+
* The color of the glow effect. Defaults to white.
64+
*/
65+
var glowingColor: NamedTextColor = NamedTextColor.WHITE
66+
5667
/**
5768
* Configures the skin of the NPC using a DSL block.
5869
*
@@ -147,6 +158,8 @@ fun npc(block: NpcDslBuilder.() -> Unit): NpcCreationResult {
147158
global = builder.global,
148159
rotationType = builder.rotationType,
149160
fixedRotation = builder.fixedRotation,
150-
persistent = builder.persistent
161+
persistent = builder.persistent,
162+
glowing = builder.glowing,
163+
glowingColor = builder.glowingColor
151164
)
152165
}

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
@@ -2,6 +2,7 @@ package dev.slne.surf.npc.api.npc
22

33
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
44
import dev.slne.surf.npc.api.npc.property.NpcProperty
5+
import dev.slne.surf.npc.api.npc.property.NpcPropertyType
56
import it.unimi.dsi.fastutil.objects.Object2ObjectMap
67
import it.unimi.dsi.fastutil.objects.ObjectSet
78
import org.bukkit.entity.Player
@@ -102,6 +103,13 @@ interface Npc {
102103
*/
103104
fun addProperty(property: NpcProperty)
104105

106+
/**
107+
* Adds multiple properties to the NPC.
108+
*
109+
* @param properties A variable number of properties to add, each represented as a Triple containing the key, value, and type.
110+
*/
111+
fun addProperties(vararg properties: Triple<String, Any, NpcPropertyType>)
112+
105113
/**
106114
* Checks if the NPC is static, meaning it has a persistence property set to true.
107115
* If the NPC is static, it will be persistent across server restarts.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,15 @@ interface NpcProperty {
5757
* The persistence of the NPC.
5858
*/
5959
const val PERSISTENCE = "persistence"
60+
61+
/**
62+
* The glowing effect status of the NPC.
63+
*/
64+
const val GLOWING_ENABLED = "glowing_enabled"
65+
66+
/**
67+
* The color of the glowing effect for the NPC.
68+
*/
69+
const val GLOWING_COLOR = "glowing_color"
6070
}
6171
}

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
@@ -22,6 +22,7 @@ import dev.slne.surf.npc.core.property.propertyTypeRegistry
2222
import it.unimi.dsi.fastutil.objects.ObjectList
2323
import it.unimi.dsi.fastutil.objects.ObjectSet
2424
import net.kyori.adventure.text.Component
25+
import net.kyori.adventure.text.format.NamedTextColor
2526
import net.kyori.adventure.util.Services
2627
import java.util.*
2728

@@ -35,7 +36,9 @@ class BukkitSurfNpcApi : SurfNpcApi, Services.Fallback {
3536
global: Boolean,
3637
rotationType: NpcRotationType,
3738
fixedRotation: NpcRotation?,
38-
persistent: Boolean
39+
persistent: Boolean,
40+
glowing: Boolean,
41+
glowingColor: NamedTextColor
3942
): NpcCreationResult {
4043
return npcController.createNpc(
4144
uniqueName,
@@ -45,7 +48,9 @@ class BukkitSurfNpcApi : SurfNpcApi, Services.Fallback {
4548
rotationType,
4649
fixedRotation ?: BukkitNpcRotation(0f, 0f),
4750
global,
48-
persistent
51+
persistent,
52+
glowing,
53+
glowingColor
4954
)
5055
}
5156

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

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import dev.slne.surf.surfapi.core.api.util.*
2727
import it.unimi.dsi.fastutil.objects.ObjectList
2828
import it.unimi.dsi.fastutil.objects.ObjectSet
2929
import net.kyori.adventure.text.Component
30+
import net.kyori.adventure.text.format.NamedTextColor
3031
import net.kyori.adventure.util.Services
3132
import java.util.*
3233

@@ -42,7 +43,9 @@ class BukkitNpcController : NpcController, Services.Fallback {
4243
rotationType: NpcRotationType,
4344
rotation: NpcRotation,
4445
global: Boolean,
45-
persistent: Boolean
46+
persistent: Boolean,
47+
glowing: Boolean,
48+
glowingColor: NamedTextColor
4649
): NpcCreationResult {
4750
val id = random.nextInt()
4851
val nameTagId = random.nextInt()
@@ -67,70 +70,62 @@ class BukkitNpcController : NpcController, Services.Fallback {
6770
uniqueName
6871
)
6972

70-
npc.addProperty(
71-
BukkitNpcProperty(
72-
NpcProperty.Internal.DISPLAYNAME, displayName, propertyTypeRegistry.get(
73-
NpcPropertyType.Types.COMPONENT
74-
) ?: error("Component property type not found")
75-
)
76-
)
77-
npc.addProperty(
78-
BukkitNpcProperty(
79-
NpcProperty.Internal.SKIN_OWNER, skinData.ownerName, propertyTypeRegistry.get(
80-
NpcPropertyType.Types.STRING
81-
) ?: error("STRING property type not found")
82-
)
83-
)
84-
npc.addProperty(
85-
BukkitNpcProperty(
86-
NpcProperty.Internal.SKIN_TEXTURE, skinData.value, propertyTypeRegistry.get(
87-
NpcPropertyType.Types.STRING
88-
) ?: error("STRING property type not found")
89-
)
90-
)
91-
npc.addProperty(
92-
BukkitNpcProperty(
93-
NpcProperty.Internal.SKIN_SIGNATURE, skinData.signature, propertyTypeRegistry.get(
94-
NpcPropertyType.Types.STRING
95-
) ?: error("STRING property type not found")
96-
)
97-
)
98-
npc.addProperty(
99-
BukkitNpcProperty(
100-
NpcProperty.Internal.LOCATION, location, propertyTypeRegistry.get(
101-
NpcPropertyType.Types.NPC_LOCATION
102-
) ?: error("LOCATION property type not found")
103-
)
104-
)
105-
npc.addProperty(
106-
BukkitNpcProperty(
73+
val componentType = propertyTypeRegistry.get(
74+
NpcPropertyType.Types.COMPONENT
75+
) ?: error("Component property type not found")
76+
77+
val booleanType = propertyTypeRegistry.get(
78+
NpcPropertyType.Types.BOOLEAN
79+
) ?: error("BOOLEAN property type not found")
80+
81+
val stringType = propertyTypeRegistry.get(
82+
NpcPropertyType.Types.STRING
83+
) ?: error("STRING property type not found")
84+
val npcLocationType = propertyTypeRegistry.get(
85+
NpcPropertyType.Types.NPC_LOCATION
86+
) ?: error("NPC_LOCATION property type not found")
87+
val npcRotationPropertyType = propertyTypeRegistry.get(
88+
NpcPropertyType.Types.NPC_ROTATION
89+
) ?: error("NPC_ROTATION property type not found")
90+
val namedTextColorType = propertyTypeRegistry.get(
91+
NpcPropertyType.Types.NAMED_TEXT_COLOR
92+
) ?: error("NAMED_TEXT_COLOR property type not found")
93+
94+
npc.addProperties(
95+
Triple(
96+
NpcProperty.Internal.DISPLAYNAME, displayName, componentType
97+
),
98+
Triple(
99+
NpcProperty.Internal.SKIN_OWNER, skinData.ownerName, stringType
100+
),
101+
Triple(
102+
NpcProperty.Internal.SKIN_TEXTURE, skinData.value, stringType
103+
),
104+
Triple(
105+
NpcProperty.Internal.SKIN_SIGNATURE, skinData.signature, stringType
106+
),
107+
Triple(
108+
NpcProperty.Internal.LOCATION, location, npcLocationType
109+
),
110+
Triple(
107111
NpcProperty.Internal.ROTATION_TYPE,
108112
rotationType == NpcRotationType.PER_PLAYER,
109-
propertyTypeRegistry.get(
110-
NpcPropertyType.Types.BOOLEAN
111-
) ?: error("BOOLEAN property type not found")
112-
)
113-
)
114-
npc.addProperty(
115-
BukkitNpcProperty(
116-
NpcProperty.Internal.ROTATION_FIXED, rotation, propertyTypeRegistry.get(
117-
NpcPropertyType.Types.NPC_ROTATION
118-
) ?: error("ROTATION property type not found")
119-
)
120-
)
121-
npc.addProperty(
122-
BukkitNpcProperty(
123-
NpcProperty.Internal.VISIBILITY_GLOBAL, global, propertyTypeRegistry.get(
124-
NpcPropertyType.Types.BOOLEAN
125-
) ?: error("BOOLEAN property type not found")
126-
)
127-
)
128-
129-
npc.addProperty(
130-
BukkitNpcProperty(
131-
NpcProperty.Internal.PERSISTENCE, persistent, propertyTypeRegistry.get(
132-
NpcPropertyType.Types.BOOLEAN
133-
) ?: error("BOOLEAN property type not found")
113+
booleanType
114+
),
115+
Triple(
116+
NpcProperty.Internal.ROTATION_FIXED, rotation, npcRotationPropertyType
117+
),
118+
Triple(
119+
NpcProperty.Internal.VISIBILITY_GLOBAL, global, booleanType
120+
),
121+
Triple(
122+
NpcProperty.Internal.PERSISTENCE, persistent, booleanType
123+
),
124+
Triple(
125+
NpcProperty.Internal.GLOWING_ENABLED, glowing, booleanType
126+
),
127+
Triple(
128+
NpcProperty.Internal.GLOWING_COLOR, glowingColor, namedTextColorType
134129
)
135130
)
136131

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class BukkitNpc(
6666
val location =
6767
this.getPropertyValue(NpcProperty.Internal.LOCATION, NpcLocation::class) ?: return
6868

69-
val glowing = this.getPropertyValue("glowing", Boolean::class) ?: false
69+
val glowing = this.getPropertyValue(NpcProperty.Internal.GLOWING_ENABLED, Boolean::class) ?: false
7070
val glowingColor =
71-
this.getPropertyValue("glowing_color", NamedTextColor::class) ?: NamedTextColor.WHITE
71+
this.getPropertyValue(NpcProperty.Internal.GLOWING_COLOR, NamedTextColor::class) ?: NamedTextColor.WHITE
7272

7373
profile.textureProperties.add(
7474
TextureProperty(
@@ -102,7 +102,7 @@ class BukkitNpc(
102102
user.sendPacket(createNametagMetadataPacket(nameTagId, displayName))
103103

104104
if (glowing) {
105-
glowingApi.makeGlowing(id, "npc_$id-glow", player, glowingColor)
105+
glowingApi.makeGlowing(id, "npc_$id", player, glowingColor)
106106
}
107107

108108
plugin.launch(plugin.entityDispatcher(player)) {
@@ -266,6 +266,10 @@ class BukkitNpc(
266266
properties[property.key] = property
267267
}
268268

269+
override fun addProperties(vararg properties: Triple<String, Any, NpcPropertyType>) = properties
270+
.map { BukkitNpcProperty(it.first, it.second, it.third) }
271+
.forEach { this.addProperty(it) }
272+
269273
override fun addProperties(vararg properties: NpcProperty) {
270274
properties.forEach {
271275
addProperty(it)

surf-npc-core/src/main/kotlin/dev/slne/surf/npc/core/controller/NpcController.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import dev.slne.surf.surfapi.core.api.util.requiredService
1515
import it.unimi.dsi.fastutil.objects.ObjectList
1616
import it.unimi.dsi.fastutil.objects.ObjectSet
1717
import net.kyori.adventure.text.Component
18+
import net.kyori.adventure.text.format.NamedTextColor
1819
import java.util.*
1920

2021
/**
@@ -42,7 +43,9 @@ interface NpcController {
4243
rotationType: NpcRotationType,
4344
rotation: NpcRotation,
4445
global: Boolean,
45-
persistent: Boolean = false
46+
persistent: Boolean = false,
47+
glowing: Boolean = false,
48+
glowingColor: NamedTextColor = NamedTextColor.WHITE
4649
): NpcCreationResult
4750

4851
/**

surf-npc-example-dsl/src/main/kotlin/dev/slne/surf/npc/example/SurfNpcExamplePlugin.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dev.slne.surf.npc.api.npc.rotation.NpcRotationType
88
import dev.slne.surf.npc.api.surfNpcApi
99
import dev.slne.surf.npc.example.listener.ExampleNpcListener
1010
import dev.slne.surf.surfapi.core.api.util.logger
11+
import net.kyori.adventure.text.format.NamedTextColor
1112
import net.kyori.adventure.text.minimessage.MiniMessage
1213
import org.bukkit.Bukkit
1314

@@ -49,6 +50,8 @@ class SurfNpcExamplePlugin() : SuspendingJavaPlugin() {
4950
global = true
5051
rotationType = NpcRotationType.PER_PLAYER
5152
persistent = false // This is already the default value, but can be set explicitly if needed.
53+
glowing = true
54+
glowingColor = NamedTextColor.RED
5255
}
5356

5457
/**

surf-npc-example/src/main/kotlin/dev/slne/surf/npc/example/SurfNpcExamplePlugin.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dev.slne.surf.npc.example
22

33
import com.github.shynixn.mccoroutine.folia.SuspendingJavaPlugin
44
import dev.slne.surf.npc.api.npc.property.NpcPropertyType
5-
import dev.slne.surf.npc.api.npc.rotation.NpcRotationType
65
import dev.slne.surf.npc.api.npc.skin.NpcSkin
76
import dev.slne.surf.npc.api.surfNpcApi
87
import dev.slne.surf.npc.example.listener.ExampleNpcListener
@@ -18,9 +17,7 @@ class SurfNpcExamplePlugin() : SuspendingJavaPlugin() {
1817
MiniMessage.miniMessage().deserialize("<rainbow>Example Npc by surf-npc-example"),
1918
"example_npc",
2019
createSkinData(),
21-
surfNpcApi.createLocation(0.0, 0.0, 0.0, "world"),
22-
true,
23-
NpcRotationType.PER_PLAYER
20+
surfNpcApi.createLocation(0.0, 0.0, 0.0, "world")
2421
)
2522

2623
val npc = surfNpcApi.getNpc("example_npc") ?: return run {

0 commit comments

Comments
 (0)