All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.carcadex.kotlinbukkitkit.extensions.ExEvent.kt Maven / Gradle / Ivy

The newest version!
/*
ORIGINAL PACKAGE: package br.com.devsrsouza.kotlinbukkitapi.extensions
ORIGINAL REPOSITORY: https://github.com/DevSrSouza/KotlinBukkitAPI
AUTHOR: https://github.com/DevSrSouza

Thanks DevSrSouza for KotlinBukkitAPI
 */

package tech.carcadex.kotlinbukkitkit.extensions

import tech.carcadex.kotlinbukkitkit.architecture.extensions.WithPlugin
import org.bukkit.Bukkit
import org.bukkit.event.Event
import org.bukkit.event.EventPriority
import org.bukkit.event.HandlerList
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerMoveEvent
import org.bukkit.plugin.Plugin
import kotlin.reflect.KClass

public inline fun  KListener<*>.event(
    priority: EventPriority = EventPriority.NORMAL,
    ignoreCancelled: Boolean = false,
    noinline block: T.() -> Unit,
): Unit = event(plugin, priority, ignoreCancelled, block)

public fun  KListener<*>.event(
    type: KClass,
    priority: EventPriority = EventPriority.NORMAL,
    ignoreCancelled: Boolean = false,
    block: T.() -> Unit,
): Unit = event(plugin, type, priority, ignoreCancelled, block)

public inline fun  Listener.event(
    plugin: Plugin,
    priority: EventPriority = EventPriority.NORMAL,
    ignoreCancelled: Boolean = false,
    noinline block: T.() -> Unit,
) {
    event(plugin, T::class, priority, ignoreCancelled, block)
}

public fun  Listener.event(
    plugin: Plugin,
    type: KClass,
    priority: EventPriority = EventPriority.NORMAL,
    ignoreCancelled: Boolean = false,
    block: T.() -> Unit,
) {
    Bukkit.getServer().pluginManager.registerEvent(
        type.java,
        this,
        priority,
        { _, event ->
            if (type.isInstance(event)) {
                (event as? T)?.block()
            }
        },
        plugin,
        ignoreCancelled,
    )
}

public inline fun WithPlugin<*>.events(block: KListener<*>.() -> Unit): SimpleKListener = plugin.events(block)
public inline fun Plugin.events(block: KListener<*>.() -> Unit): SimpleKListener = SimpleKListener(this).apply(block)

public fun Listener.registerEvents(plugin: Plugin): Unit = plugin.server.pluginManager.registerEvents(this, plugin)

public fun Listener.unregisterListener(): Unit = HandlerList.unregisterAll(this)

public fun Event.callEvent(): Unit = Bukkit.getServer().pluginManager.callEvent(this)

public val PlayerMoveEvent.displaced: Boolean
    get() = this.from.x != this.to?.x || this.from.y != this.to?.y || this.from.z != this.to?.z

// TODO: remove KListener and move to Context Receivers
public interface KListener : Listener, WithPlugin

public class SimpleKListener(override val plugin: Plugin) : KListener




© 2015 - 2024 Weber Informatics LLC | Privacy Policy