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

walkmc.Resource.kt Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package walkmc

import org.bukkit.event.*
import walkmc.annotation.*
import walkmc.extensions.*
import kotlin.reflect.*
import kotlin.reflect.full.*

/**
 * Represents a resource. Resources can be used to declare functions that's
 * is automatically called when the plugin is enabled.
 *
 * To register a function, you must annotate them with [Component].
 */
abstract class Resource(val plugin: Plugin) : Listener

/**
 * Register a event listener with the specified event in generic type in this server.
 */
inline fun  Resource.listening(
   priority: EventPriority = EventPriority.NORMAL,
   ignoreCancelled: Boolean = false,
   noinline action: T.() -> Unit,
) = listening(plugin, priority, ignoreCancelled, action)

/**
 * Register a event listener with the specified event in generic type in this server.
 */
inline fun  Resource.subscribe(
   priority: EventPriority = EventPriority.NORMAL,
   ignoreCancelled: Boolean = false,
   noinline action: T.() -> Unit,
) = subscribe(plugin, priority, ignoreCancelled, action)

/**
 * Register a event listener with the specified event in generic type in this server.
 */
inline fun  Resource.subscribeSuspend(
   priority: EventPriority = EventPriority.NORMAL,
   ignoreCancelled: Boolean = false,
   noinline action: suspend T.() -> Unit,
) = subscribeSuspend(plugin, priority, ignoreCancelled, action)

/**
 * Returns all functions that's is annoted with [Component].
 */
fun Resource.getComponents(): List> {
   return this::class.declaredMemberFunctions
      .filter { it.hasAnnotation() }
}

/**
 * Returns all functions that's is annoted with [Component] and their load value is equals to [load].
 */
fun Resource.getComponents(load: Load): List> {
   return getComponents()
      .filter { it.findAnnotation()!!.load == load }
}

/**
 * Returns all functions that's is annoted with [Component]
 * and their load value is equals to [load].
 *
 * This also sorts the returned components with the priority of them.
 */
fun Resource.getComponentsSorted(load: Load): List> {
   return getComponents(load)
      .sortedBy { it.findAnnotation()!!.priority }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy