Skip to content

Commit 14874b9

Browse files
committed
feat: complete ThirdPartyAware plugin disabling
1 parent 33d798e commit 14874b9

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
11
package br.com.devsrsouza.kotlinbukkitapi.architecture
22

33
import br.com.devsrsouza.kotlinbukkitapi.architecture.extensions.WithPlugin
4+
import org.bukkit.Bukkit
45
import org.bukkit.event.EventHandler
6+
import org.bukkit.event.HandlerList
7+
import org.bukkit.event.Listener
58
import org.bukkit.event.server.PluginDisableEvent
69
import org.bukkit.plugin.Plugin
710
import java.util.WeakHashMap
811

912
/**
1013
* A holder for all instance of a class [T] that is bound a specific plugin.
1114
* It unregister when the plugin is disable.
12-
* TODO: plugin disable support
1315
*/
1416
public abstract class PluginDisableAwareController<T : PluginDisableAware> {
1517

1618
public abstract val factory: (plugin: Plugin) -> T
1719

1820
private val instances: WeakHashMap<Plugin, T> = WeakHashMap()
21+
private val listeners: WeakHashMap<Plugin, Listener> = WeakHashMap()
1922

2023
public fun get(plugin: Plugin): T {
21-
return instances.getOrPut(plugin) { factory(plugin) }
24+
return instances.getOrPut(plugin) {
25+
listeners.put(
26+
plugin,
27+
DisableListener(plugin, ::disable).apply {
28+
Bukkit.getServer().pluginManager.registerEvents(this, plugin)
29+
},
30+
)
31+
factory(plugin)
32+
}
2233
}
2334

2435
public fun ensureInitialized(plugin: Plugin) {
2536
get(plugin)
2637
}
2738

28-
@EventHandler
29-
public fun pluginDisableEvent(event: PluginDisableEvent) {
30-
// todo:
39+
private fun disable(plugin: Plugin) {
40+
instances.remove(plugin)?.onDisable()
41+
listeners.remove(plugin)?.also(HandlerList::unregisterAll)
3142
}
3243
}
3344

3445
public interface PluginDisableAware : WithPlugin<Plugin> {
3546
public fun onDisable()
3647
}
48+
49+
private class DisableListener(
50+
val plugin: Plugin,
51+
val onDisable: (Plugin) -> Unit,
52+
) : Listener {
53+
@EventHandler
54+
public fun pluginDisableEvent(event: PluginDisableEvent) {
55+
if (event.plugin.name == plugin.name) {
56+
onDisable(plugin)
57+
}
58+
}
59+
}

build.gradle.kts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,3 @@ plugins {
44
alias(libs.plugins.dependencyGraph)
55
alias(libs.plugins.maven) apply false
66
}
7-
8-
subprojects {
9-
repositories {
10-
mavenCentral()
11-
maven("https://repo.papermc.io/repository/maven-public/")
12-
}
13-
}

settings.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ pluginManagement {
88
}
99
}
1010

11+
dependencyResolutionManagement {
12+
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
13+
repositories {
14+
mavenCentral()
15+
maven("https://repo.papermc.io/repository/maven-public/")
16+
}
17+
}
18+
1119
rootProject.name = "KotlinBukkitAPI"
1220

1321
include(":utility")

0 commit comments

Comments
 (0)