Skip to content

Commit ac53c1c

Browse files
committed
feat: update SurfBukkitPacketApi to prefer explicit plugin parameter for lore listener registration
1 parent a5501ad commit ac53c1c

1 file changed

Lines changed: 69 additions & 50 deletions

File tree

  • surf-api-bukkit/surf-api-bukkit-api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/packet

surf-api-bukkit/surf-api-bukkit-api/src/main/kotlin/dev/slne/surf/surfapi/bukkit/api/packet/SurfBukkitPacketApi.kt

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,88 @@ import org.bukkit.plugin.Plugin
1010
/**
1111
* The SurfBukkitPacketApi interface extends packet handling capabilities for Bukkit environments.
1212
*
13-
* It provides methods for registering and unregistering lore listeners, enabling dynamic modification
14-
* of item stack lore based on custom logic. It also includes global registration methods for listening
15-
* to all items and utilities for managing these listeners efficiently.
13+
* Provides methods for registering and unregistering lore listeners, enabling dynamic modification
14+
* of item stack lore based on custom logic. Includes global registration for all items and utilities
15+
* for managing these listeners efficiently.
1616
*
17-
* This API allows developers to enhance item lore dynamically, providing a flexible system for plugins
18-
* that need to alter or interact with lore without directly modifying the underlying item stack data.
17+
* Prefer the overloads that accept an explicit [Plugin] parameter over the deprecated ones,
18+
* as automatic caller-plugin detection via `getCallingPlugin` is unreliable across different
19+
* call-site configurations.
1920
*/
2021
interface SurfBukkitPacketApi {
2122

2223
/**
23-
* Registers a listener for modifying the lore of a specific item stack identified by the given key.
24-
*
25-
* @param identifier A unique key representing the item to listen for. Must not be null.
26-
* @param listener The listener that modifies the lore of the item stack. Must not be null.
27-
*
28-
* Example Usage:
29-
* ```
30-
* val key = NamespacedKey("myplugin", "custom_item")
31-
* surfBukkitPacketApi.registerPacketLoreListener(key, SurfBukkitPacketLoreHandler { lore, _, _ ->
32-
* lore.add(Component.text("Special Lore!"))
33-
* })
34-
* ```
24+
* Registers a listener for modifying the lore of a specific item stack identified by [identifier].
25+
*
26+
* @param identifier A unique key representing the item to listen for.
27+
* @param listener The listener that modifies the lore of the item stack.
28+
*
29+
* @deprecated Automatic plugin detection via `getCallingPlugin` is unreliable. Use
30+
* [registerPacketLoreListener(Plugin, NamespacedKey, SurfBukkitPacketLoreHandler)] instead
31+
* and pass your plugin instance explicitly.
3532
*/
33+
@Deprecated(
34+
message = "Automatic plugin detection is unreliable. Pass your plugin instance explicitly.",
35+
replaceWith = ReplaceWith("registerPacketLoreListener(plugin, identifier, listener)")
36+
)
3637
fun registerPacketLoreListener(
3738
identifier: NamespacedKey,
3839
listener: SurfBukkitPacketLoreHandler
3940
) {
40-
registerPacketLoreListener(getCallingPlugin(), identifier, listener)
41+
registerPacketLoreListener(getCallingPlugin(2), identifier, listener)
4142
}
4243

44+
/**
45+
* Registers a listener for modifying the lore of a specific item stack identified by [identifier].
46+
*
47+
* This is the preferred overload. The [plugin] reference is used to properly manage the
48+
* listener lifecycle — all listeners registered under a plugin are automatically cleaned up
49+
* when [unregisterPacketLoreListener(Plugin)] is called.
50+
*
51+
* @param plugin The plugin registering the listener. Used for lifecycle management.
52+
* @param identifier A unique key representing the item to listen for.
53+
* @param listener The listener that modifies the lore of the item stack.
54+
*/
4355
fun registerPacketLoreListener(
4456
plugin: Plugin,
4557
identifier: NamespacedKey,
4658
listener: SurfBukkitPacketLoreHandler
4759
)
4860

4961
/**
50-
* Registers a simplified packet lore listener for a specific item.
62+
* Registers a simplified listener for modifying the lore of a specific item stack identified by [identifier].
63+
*
64+
* Delegates to [registerPacketLoreListener(Plugin, NamespacedKey, SurfBukkitPacketLoreHandler)].
5165
*
52-
* @param identifier A unique key representing the item to listen for. Must not be null.
53-
* @param listener The simplified packet lore listener that focuses solely on modifying the lore list.
66+
* @param identifier A unique key representing the item to listen for.
67+
* @param listener The simplified lore listener that focuses solely on modifying the lore list.
5468
*
55-
* This method delegates to the standard [registerPacketLoreListener] implementation.
69+
* @deprecated Automatic plugin detection is unreliable. Use
70+
* [registerPacketLoreListener(Plugin, NamespacedKey, SurfBukkitPacketLoreHandlerSimple)] instead
71+
* and pass your plugin instance explicitly.
5672
*/
73+
@Deprecated(
74+
message = "Automatic plugin detection is unreliable. Pass your plugin instance explicitly.",
75+
replaceWith = ReplaceWith("registerPacketLoreListener(plugin, identifier, listener)")
76+
)
5777
fun registerPacketLoreListener(
5878
identifier: NamespacedKey,
5979
listener: SurfBukkitPacketLoreHandlerSimple
6080
) {
61-
registerPacketLoreListener(getCallingPlugin(), identifier, listener)
81+
registerPacketLoreListener(getCallingPlugin(2), identifier, listener)
6282
}
6383

84+
/**
85+
* Registers a simplified listener for modifying the lore of a specific item stack identified by [identifier].
86+
*
87+
* This is the preferred overload. Delegates to
88+
* [registerPacketLoreListener(Plugin, NamespacedKey, SurfBukkitPacketLoreHandler)].
89+
* The [plugin] reference is used to properly manage the listener lifecycle.
90+
*
91+
* @param plugin The plugin registering the listener. Used for lifecycle management.
92+
* @param identifier A unique key representing the item to listen for.
93+
* @param listener The simplified lore listener that focuses solely on modifying the lore list.
94+
*/
6495
fun registerPacketLoreListener(
6596
plugin: Plugin,
6697
identifier: NamespacedKey,
@@ -70,30 +101,27 @@ interface SurfBukkitPacketApi {
70101
}
71102

72103
/**
73-
* Registers a packet lore listener globally to handle lore modifications for all items.
104+
* Registers a lore listener globally to handle lore modifications for all items.
74105
*
75-
* @param plugin The plugin registering the listener. Used to manage lifecycle and cleanup.
76-
* @param listener The lore listener to handle lore modifications globally.
106+
* Unlike the key-based overloads, this listener fires for every item stack regardless of
107+
* its identifier. Use this only when you genuinely need to intercept all items, as it has
108+
* a broader performance impact.
77109
*
78-
* Example Usage:
79-
* ```
80-
* surfBukkitPacketApi.registerPacketLoreListenerGlobal(myPlugin, SurfBukkitPacketLoreHandler { lore, _, _ ->
81-
* lore.add(Component.text("Global Lore Modification"))
82-
* })
83-
* ```
110+
* @param plugin The plugin registering the listener. Used for lifecycle management.
111+
* @param listener The lore listener to handle lore modifications globally.
84112
*/
85113
fun registerPacketLoreListenerGlobal(
86114
plugin: Plugin,
87115
listener: SurfBukkitPacketLoreHandler
88116
)
89117

90118
/**
91-
* Registers a simplified packet lore listener globally for all items.
119+
* Registers a simplified lore listener globally for all items.
92120
*
93-
* @param plugin The plugin registering the listener. Used for proper cleanup during plugin shutdown.
94-
* @param listener The simplified lore listener that focuses solely on the lore list.
121+
* Delegates to [registerPacketLoreListenerGlobal(Plugin, SurfBukkitPacketLoreHandler)].
95122
*
96-
* This method delegates to the standard [registerPacketLoreListenerGlobal] implementation.
123+
* @param plugin The plugin registering the listener. Used for lifecycle management.
124+
* @param listener The simplified lore listener that focuses solely on the lore list.
97125
*/
98126
fun registerPacketLoreListenerGlobal(
99127
plugin: Plugin,
@@ -103,26 +131,19 @@ interface SurfBukkitPacketApi {
103131
}
104132

105133
/**
106-
* Unregisters a previously registered packet lore listener identified by the given key.
134+
* Unregisters the packet lore listener associated with the given [identifier].
107135
*
108136
* @param identifier The key identifying the listener to unregister.
109-
*
110-
* Example Usage:
111-
* ```
112-
* surfBukkitPacketApi.unregisterPacketLoreListener(NamespacedKey("myplugin", "custom_item"))
113-
* ```
114137
*/
115138
fun unregisterPacketLoreListener(identifier: NamespacedKey)
116139

117140
/**
118-
* Unregisters all packet lore listeners associated with the given plugin.
141+
* Unregisters all packet lore listeners associated with the given [plugin].
119142
*
120-
* @param plugin The plugin whose listeners should be unregistered.
143+
* Call this during plugin shutdown to ensure all listeners registered under this plugin
144+
* are properly cleaned up.
121145
*
122-
* Example Usage:
123-
* ```
124-
* surfBukkitPacketApi.unregisterPacketLoreListener(myPlugin)
125-
* ```
146+
* @param plugin The plugin whose listeners should be unregistered.
126147
*/
127148
fun unregisterPacketLoreListener(plugin: Plugin)
128149

@@ -131,5 +152,3 @@ interface SurfBukkitPacketApi {
131152
val instance = requiredService<SurfBukkitPacketApi>()
132153
}
133154
}
134-
135-
val surfBukkitPacketApi get() = SurfBukkitPacketApi.instance

0 commit comments

Comments
 (0)