-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: complete ThirdPartyAware plugin disabling
- Loading branch information
1 parent
33d798e
commit 14874b9
Showing
3 changed files
with
36 additions
and
12 deletions.
There are no files selected for viewing
33 changes: 28 additions & 5 deletions
33
...tecture/src/main/kotlin/br/com/devsrsouza/kotlinbukkitapi/architecture/ThirdPartyAware.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,59 @@ | ||
package br.com.devsrsouza.kotlinbukkitapi.architecture | ||
|
||
import br.com.devsrsouza.kotlinbukkitapi.architecture.extensions.WithPlugin | ||
import org.bukkit.Bukkit | ||
import org.bukkit.event.EventHandler | ||
import org.bukkit.event.HandlerList | ||
import org.bukkit.event.Listener | ||
import org.bukkit.event.server.PluginDisableEvent | ||
import org.bukkit.plugin.Plugin | ||
import java.util.WeakHashMap | ||
|
||
/** | ||
* A holder for all instance of a class [T] that is bound a specific plugin. | ||
* It unregister when the plugin is disable. | ||
* TODO: plugin disable support | ||
*/ | ||
public abstract class PluginDisableAwareController<T : PluginDisableAware> { | ||
|
||
public abstract val factory: (plugin: Plugin) -> T | ||
|
||
private val instances: WeakHashMap<Plugin, T> = WeakHashMap() | ||
private val listeners: WeakHashMap<Plugin, Listener> = WeakHashMap() | ||
|
||
public fun get(plugin: Plugin): T { | ||
return instances.getOrPut(plugin) { factory(plugin) } | ||
return instances.getOrPut(plugin) { | ||
listeners.put( | ||
plugin, | ||
DisableListener(plugin, ::disable).apply { | ||
Bukkit.getServer().pluginManager.registerEvents(this, plugin) | ||
}, | ||
) | ||
factory(plugin) | ||
} | ||
} | ||
|
||
public fun ensureInitialized(plugin: Plugin) { | ||
get(plugin) | ||
} | ||
|
||
@EventHandler | ||
public fun pluginDisableEvent(event: PluginDisableEvent) { | ||
// todo: | ||
private fun disable(plugin: Plugin) { | ||
instances.remove(plugin)?.onDisable() | ||
listeners.remove(plugin)?.also(HandlerList::unregisterAll) | ||
} | ||
} | ||
|
||
public interface PluginDisableAware : WithPlugin<Plugin> { | ||
public fun onDisable() | ||
} | ||
|
||
private class DisableListener( | ||
val plugin: Plugin, | ||
val onDisable: (Plugin) -> Unit, | ||
) : Listener { | ||
@EventHandler | ||
public fun pluginDisableEvent(event: PluginDisableEvent) { | ||
if (event.plugin.name == plugin.name) { | ||
onDisable(plugin) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters