Skip to content

Commit a7ae28c

Browse files
authored
Merge pull request #24 from SLNE-Development/pre/development
Development (v1.5.0)
2 parents e816cc7 + 4c195cf commit a7ae28c

File tree

54 files changed

+1115
-803
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1115
-803
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ env:
1111
SLNE_SNAPSHOTS_REPO_PASSWORD: ${{ secrets.SLNE_SNAPSHOTS_REPO_PASSWORD }}
1212
SLNE_RELEASES_REPO_USERNAME: ${{ secrets.SLNE_RELEASES_REPO_USERNAME }}
1313
SLNE_RELEASES_REPO_PASSWORD: ${{ secrets.SLNE_RELEASES_REPO_PASSWORD }}
14-
MODULE_REGEX: "surf-npc-api.*-all\\.jar$|surf-npc-bukkit.*\\.jar$"
14+
MODULE_REGEX: "surf-npc-api.*-all\\.jar$|surf-npc-bukkit.*-all\\.jar$"
1515
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
1616

1717
jobs:

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.4.4-SNAPSHOT
4+
version=1.21.7-1.5.0-SNAPSHOT

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface SurfNpcApi {
3333
* @param uniqueName The unique name of the NPC.
3434
* @param skin The skin data of the NPC.
3535
* @param location The location of the NPC.
36-
* @param global Whether the NPC is global (default: true).
36+
* @param viewers The set of UUIDs of players who can see the NPC (default: null, meaning all players).
3737
* @param rotationType The rotation type of the NPC (default: FIXED).
3838
* @param fixedRotation The fixed rotation of the NPC, if applicable (default: null).
3939
* @param persistent Whether the NPC should be persistent (default: false).
@@ -46,7 +46,7 @@ interface SurfNpcApi {
4646
uniqueName: String,
4747
skin: NpcSkin,
4848
location: NpcLocation,
49-
global: Boolean = true,
49+
viewers: ObjectSet<UUID>? = null,
5050
rotationType: NpcRotationType = NpcRotationType.PER_PLAYER,
5151
fixedRotation: NpcRotation? = null,
5252
persistent: Boolean = false,

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

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
@file:Suppress("UNCHECKED_CAST")
2+
13
package dev.slne.surf.npc.api.dsl
24

5+
import dev.slne.surf.npc.api.event.NpcEvent
36
import dev.slne.surf.npc.api.npc.location.NpcLocation
47
import dev.slne.surf.npc.api.npc.property.NpcProperty
58
import dev.slne.surf.npc.api.npc.rotation.NpcRotation
@@ -8,8 +11,14 @@ import dev.slne.surf.npc.api.npc.skin.NpcSkin
811
import dev.slne.surf.npc.api.result.NpcCreationResult
912
import dev.slne.surf.npc.api.surfNpcApi
1013
import dev.slne.surf.surfapi.core.api.messages.builder.SurfComponentBuilder
14+
import dev.slne.surf.surfapi.core.api.util.mutableObject2ObjectMapOf
15+
import dev.slne.surf.surfapi.core.api.util.mutableObjectListOf
16+
import it.unimi.dsi.fastutil.objects.ObjectList
17+
import it.unimi.dsi.fastutil.objects.ObjectSet
1118
import net.kyori.adventure.text.format.NamedTextColor
1219
import org.bukkit.plugin.java.JavaPlugin
20+
import java.util.*
21+
import kotlin.reflect.KClass
1322

1423
/**
1524
* Builder class for creating NPCs using a DSL.
@@ -38,7 +47,7 @@ class NpcDslBuilder {
3847
/**
3948
* Whether the NPC is global. Defaults to true.
4049
*/
41-
var global: Boolean = true
50+
var viewers: ObjectSet<UUID>? = null
4251

4352
/**
4453
* The rotation type of the NPC. Defaults to PER_PLAYER.
@@ -65,6 +74,32 @@ class NpcDslBuilder {
6574
*/
6675
var glowingColor: NamedTextColor = NamedTextColor.WHITE
6776

77+
internal val eventHandlers =
78+
mutableObject2ObjectMapOf<KClass<out NpcEvent>, ObjectList<(NpcEvent) -> Unit>>()
79+
80+
/**
81+
* Registers an event handler for a specific type of `NpcEvent`.
82+
*
83+
* @param T The type of the `NpcEvent` this handler will process.
84+
* @param eventClass The class of the event type to handle.
85+
* @param handler The function that defines the logic for handling the specified type of `NpcEvent`.
86+
*/
87+
fun <T : NpcEvent> withEventHandler(eventClass: KClass<T>, handler: (T) -> Unit) {
88+
eventHandlers.computeIfAbsent(eventClass) { mutableObjectListOf() }
89+
.add { ev -> handler(ev as T) }
90+
}
91+
92+
/**
93+
* Registers an event handler for a specific type of `NpcEvent`.
94+
*
95+
* @param T The type of `NpcEvent` for which the handler is being registered.
96+
* @param handler A lambda function that defines the behavior when the event of type `T` is triggered.
97+
*/
98+
inline fun <reified T : NpcEvent> withEventHandler(noinline handler: (T) -> Unit) {
99+
withEventHandler(T::class, handler)
100+
}
101+
102+
68103
/**
69104
* Configures the skin of the NPC using a DSL block.
70105
*
@@ -143,26 +178,31 @@ suspend fun skin(name: String): NpcSkin {
143178
return surfNpcApi.getSkin(name)
144179
}
145180

146-
/**
147-
* Creates an NPC using a DSL block.
148-
*
149-
* @param plugin The JavaPlugin instance for the NPC creation.
150-
* @param block The DSL block for configuring the NPC.
151-
* @return The result of the NPC creation.
152-
*/
153181
fun npc(plugin: JavaPlugin, block: NpcDslBuilder.() -> Unit): NpcCreationResult {
154182
val builder = NpcDslBuilder().apply(block)
155-
return surfNpcApi.createNpc(
183+
val result = surfNpcApi.createNpc(
156184
displayName = SurfComponentBuilder.builder().apply(builder.displayName).build(),
157185
uniqueName = builder.uniqueName,
158186
skin = builder.skin,
159187
location = builder.location,
160-
global = builder.global,
188+
viewers = builder.viewers,
161189
rotationType = builder.rotationType,
162190
fixedRotation = builder.fixedRotation,
163191
persistent = builder.persistent,
164192
glowing = builder.glowing,
165193
glowingColor = builder.glowingColor,
166194
plugin = plugin
167195
)
168-
}
196+
197+
(result as? NpcCreationResult.Success)?.let { success ->
198+
builder.eventHandlers.forEach { (eventClass, handlersList) ->
199+
handlersList.forEach { handler ->
200+
success.npc.addEventHandler(eventClass as KClass<NpcEvent>) { ev ->
201+
handler(ev)
202+
}
203+
}
204+
}
205+
}
206+
207+
return result
208+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import org.bukkit.event.HandlerList
1212
* @property player The player who collided with the NPC.
1313
*/
1414
class NpcCollisionEvent(
15-
val npc: Npc,
16-
val player: Player
17-
) : Event() {
15+
override val player: Player,
16+
override val npc: Npc
17+
) : Event(), NpcPlayerEvent {
1818

1919
/**
2020
* Returns the handler list for this event.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import org.bukkit.event.HandlerList
1010
* @property npc The NPC that was created.
1111
*/
1212
class NpcCreateEvent(
13-
val npc: Npc
14-
) : Event() {
13+
override val npc: Npc
14+
) : Event(), NpcEvent {
1515

1616
/**
1717
* Returns the handler list for this event.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import org.bukkit.event.HandlerList
1010
* @property npc The NPC that was deleted.
1111
*/
1212
class NpcDeleteEvent(
13-
val npc: Npc
14-
) : Event() {
13+
override val npc: Npc
14+
) : Event(), NpcEvent {
1515

1616
/**
1717
* Returns the handler list for this event.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dev.slne.surf.npc.api.event
2+
3+
import dev.slne.surf.npc.api.npc.Npc
4+
5+
interface NpcEvent {
6+
val npc: Npc
7+
}

surf-npc-api/src/main/kotlin/dev/slne/surf/npc/api/event/NpcDespawnEvent.kt renamed to surf-npc-api/src/main/kotlin/dev/slne/surf/npc/api/event/NpcHideEvent.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import org.bukkit.event.HandlerList
1111
* @property npc The NPC that despawned.
1212
* @property player The player associated with the despawn event, if applicable.
1313
*/
14-
class NpcDespawnEvent(
15-
val npc: Npc,
16-
val player: Player
17-
) : Event() {
14+
class NpcHideEvent(
15+
override val player: Player,
16+
override val npc: Npc
17+
) : Event(), NpcPlayerEvent {
1818

1919
/**
2020
* Returns the handler list for this event.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import org.bukkit.event.HandlerList
1212
* @property player The player who interacted with the NPC.
1313
*/
1414
class NpcInteractEvent(
15-
val npc: Npc,
16-
val player: Player
17-
) : Event() {
15+
override val player: Player,
16+
override val npc: Npc
17+
) : Event(), NpcPlayerEvent {
1818

1919
/**
2020
* Returns the handler list for this event.

0 commit comments

Comments
 (0)