Eliminated plugin and kSpigot parameters

This commit is contained in:
bluefireoly
2020-10-12 17:01:01 +02:00
parent 6b2f95c7fa
commit 29bd82803d
11 changed files with 63 additions and 69 deletions

View File

@@ -1,16 +1,16 @@
package net.axay.kspigot.event
import net.axay.kspigot.extensions.pluginManager
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.event.Event
import org.bukkit.event.EventPriority
import org.bukkit.event.HandlerList
import org.bukkit.event.Listener
import org.bukkit.plugin.Plugin
/**
* Shortcut for registering this listener on the given plugin.
*/
fun Listener.register(plugin: Plugin) = pluginManager.registerEvents(this, plugin)
fun Listener.register() = pluginManager.registerEvents(this, KSpigotMainInstance)
/**
* Shortcut for unregistering all events in this listener.
@@ -26,12 +26,11 @@ fun Listener.unregister() = HandlerList.unregisterAll(this)
* @param executor handles incoming events
*/
inline fun <reified T : Event> Listener.register(
plugin: Plugin,
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false,
noinline executor: (Listener, Event) -> Unit
) {
pluginManager.registerEvent(T::class.java, this, priority, executor, plugin, ignoreCancelled)
pluginManager.registerEvent(T::class.java, this, priority, executor, KSpigotMainInstance, ignoreCancelled)
}
/**
@@ -50,11 +49,10 @@ interface SingleListener<T : Event> : Listener {
* @param ignoreCancelled if manual cancellation should be ignored
*/
inline fun <reified T : Event> SingleListener<T>.register(
plugin: Plugin,
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false
) {
register<T>(plugin, priority, ignoreCancelled) { _, event ->
register<T>(priority, ignoreCancelled) { _, event ->
(event as? T)?.let { this.onEvent(it) }
}
}
@@ -65,7 +63,7 @@ inline fun <reified T : Event> SingleListener<T>.register(
* @param ignoreCancelled if manual cancellation should be ignored
* @param onEvent the event callback
*/
inline fun <reified T : Event> Plugin.listen(
inline fun <reified T : Event> listen(
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = false,
crossinline onEvent: (T) -> Unit
@@ -73,6 +71,6 @@ inline fun <reified T : Event> Plugin.listen(
val listener = object : SingleListener<T> {
override fun onEvent(event: T) = onEvent.invoke(event)
}
listener.register(this, priority, ignoreCancelled)
listener.register(priority, ignoreCancelled)
return listener
}

View File

@@ -1,27 +1,28 @@
package net.axay.kspigot.extensions.bukkit
import net.axay.kspigot.main.KSpigotMainInstance
import net.md_5.bungee.api.ChatColor
import org.bukkit.command.CommandSender
import org.bukkit.plugin.Plugin
/** @see printColoredPrefix */
fun CommandSender.print(text: String, plugin: Plugin? = null)
fun CommandSender.print(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.RESET, plugin?.name ?: "INFO", ChatColor.GRAY)
/** @see printColoredPrefix */
fun CommandSender.info(text: String, plugin: Plugin? = null)
fun CommandSender.info(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.WHITE, plugin?.name ?: "INFO", ChatColor.DARK_AQUA)
/** @see printColoredPrefix */
fun CommandSender.success(text: String, plugin: Plugin? = null)
fun CommandSender.success(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.GREEN, plugin?.name ?: "SUCCESS", ChatColor.DARK_AQUA)
/** @see printColoredPrefix */
fun CommandSender.warn(text: String, plugin: Plugin? = null)
fun CommandSender.warn(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.WHITE, plugin?.name?.plus(" - WARN") ?: "WARN", ChatColor.YELLOW)
/** @see printColoredPrefix */
fun CommandSender.error(text: String, plugin: Plugin? = null)
fun CommandSender.error(text: String, plugin: Plugin? = KSpigotMainInstance)
= printColoredPrefix(text, ChatColor.RED, plugin?.name?.plus(" - ERROR") ?: "ERROR", ChatColor.DARK_RED)
/**

View File

@@ -2,12 +2,12 @@ package net.axay.kspigot.extensions.bukkit
import net.axay.kspigot.annotations.NMS_General
import net.axay.kspigot.extensions.onlinePlayers
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.attribute.Attribute
import org.bukkit.craftbukkit.v1_16_R2.CraftWorld
import org.bukkit.entity.*
import org.bukkit.plugin.Plugin
/**
* Checks if the entity is completely in water.
@@ -72,15 +72,15 @@ fun Player.feedSaturate() {
/**
* Hides the player for all [onlinePlayers].
*/
fun Player.disappear(plugin: Plugin) {
onlinePlayers.filter { it != this }.forEach { it.hidePlayer(plugin, this) }
fun Player.disappear() {
onlinePlayers.filter { it != this }.forEach { it.hidePlayer(KSpigotMainInstance, this) }
}
/**
* Shows the player for all [onlinePlayers].
*/
fun Player.appear(plugin: Plugin) {
onlinePlayers.filter { it != this }.forEach { it.showPlayer(plugin, this) }
fun Player.appear() {
onlinePlayers.filter { it != this }.forEach { it.showPlayer(KSpigotMainInstance, this) }
}
/**

View File

@@ -3,13 +3,12 @@
package net.axay.kspigot.game
import net.axay.kspigot.extensions.broadcast
import net.axay.kspigot.main.KSpigot
import net.axay.kspigot.runnables.task
import net.axay.kspigot.main.KSpigotMainInstance
import net.md_5.bungee.api.ChatColor
class GamePhaseSystem(vararg gamePhases: GamePhase) {
val gamePhases = gamePhases.toMutableList()
fun begin(kSpigot: KSpigot) = gamePhases.removeAt(0).startIt(kSpigot, gamePhases)
fun begin() = gamePhases.removeAt(0).startIt(gamePhases)
}
fun counterMessage(
@@ -36,9 +35,9 @@ class GamePhase(
val end: (() -> Unit)?,
val counterMessage: ((secondsLeft: Long) -> String)?
) {
fun startIt(kSpigot: KSpigot, phaseQueue: MutableList<GamePhase>) {
fun startIt(phaseQueue: MutableList<GamePhase>) {
start?.invoke()
kSpigot.task(
KSpigotMainInstance.task(
period = 20,
howOften = (length / 20) + 1,
endCallback = {
@@ -46,7 +45,7 @@ class GamePhase(
end?.invoke()
if (phaseQueue.isNotEmpty())
phaseQueue.removeAt(0).startIt(kSpigot, phaseQueue)
phaseQueue.removeAt(0).startIt(phaseQueue)
}
) {

View File

@@ -3,7 +3,7 @@
package net.axay.kspigot.inventory
import net.axay.kspigot.event.listen
import net.axay.kspigot.main.KSpigot
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.Inventory
@@ -25,7 +25,7 @@ fun HumanEntity.openGUI(gui: InventoryGUI<*>, page: Int? = null): InventoryView?
// GUI HOLDER
class InventoryGUIHolder(kSpigot: KSpigot) : AutoCloseable {
class InventoryGUIHolder : AutoCloseable {
private val registered = HashSet<InventoryGUI<ForInventory>>()
@@ -39,7 +39,7 @@ class InventoryGUIHolder(kSpigot: KSpigot) : AutoCloseable {
init {
kSpigot.listen<InventoryClickEvent> {
listen<InventoryClickEvent> {
val clickedInv = it.clickedInventory ?: return@listen
@@ -76,7 +76,6 @@ class InventoryGUIClickEvent<T : ForInventory>(
private const val DEFAULT_PAGE = 1
class InventoryGUIData<T : ForInventory>(
val plugin: KSpigot,
val inventoryType: InventoryType<T>,
val title: String?,
internal val pages: Map<Int, InventoryGUIPage<T>>,
@@ -108,14 +107,14 @@ abstract class InventoryGUI<T : ForInventory>(
* (KSpigot will listen for actions in the inventory.)
*/
@Suppress("UNCHECKED_CAST")
fun register() = data.plugin.inventoryGUIHolder.register(this as InventoryGUI<ForInventory>)
fun register() = KSpigotMainInstance.inventoryGUIHolder.register(this as InventoryGUI<ForInventory>)
/**
* Stops KSpigot from listening to actions in this
* InventoryGUI anymore.
*/
@Suppress("UNCHECKED_CAST")
fun unregister() = data.plugin.inventoryGUIHolder.unregister(this as InventoryGUI<ForInventory>)
fun unregister() = KSpigotMainInstance.inventoryGUIHolder.unregister(this as InventoryGUI<ForInventory>)
/**
* Loads the specified page in order to display it in the GUI.

View File

@@ -2,16 +2,14 @@
package net.axay.kspigot.inventory
import net.axay.kspigot.main.KSpigot
import org.bukkit.inventory.ItemStack
fun <T : ForInventory> KSpigot.inventoryGUI(
fun <T : ForInventory> inventoryGUI(
type: InventoryType<T>,
builder: InventoryGUIBuilder<T>.() -> Unit,
) = InventoryGUIBuilder(this, type).apply(builder).build()
) = InventoryGUIBuilder(type).apply(builder).build()
class InventoryGUIBuilder<T : ForInventory>(
private val kSpigot: KSpigot,
val type: InventoryType<T>
) {
@@ -38,7 +36,7 @@ class InventoryGUIBuilder<T : ForInventory>(
}
internal fun build() = InventoryGUIShared(
InventoryGUIData(kSpigot, type, title, guiSlots, transitionTo, transitionFrom, onClickElement)
InventoryGUIData(type, title, guiSlots, transitionTo, transitionFrom, onClickElement)
).apply { register() }
}

View File

@@ -1,7 +1,6 @@
package net.axay.kspigot.inventory
import net.axay.kspigot.main.KSpigot
import net.axay.kspigot.runnables.task
import net.axay.kspigot.main.KSpigotMainInstance
abstract class InventoryGUIPageChangeCalculator {
@@ -54,7 +53,7 @@ internal fun InventoryGUI<*>.changePage(
val width = data.inventoryType.dimensions.width
changePageEffect(data.plugin, fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
changePageEffect(fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
if (ifInverted) {
loadPageUnsafe(fromPage, offsetHorizontally = currentOffset)
loadPageUnsafe(toPage, offsetHorizontally = -(width - currentOffset))
@@ -70,7 +69,7 @@ internal fun InventoryGUI<*>.changePage(
val height = data.inventoryType.dimensions.heigth
changePageEffect(data.plugin, fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
if (ifInverted) {
loadPageUnsafe(fromPage, offsetVertically = currentOffset)
loadPageUnsafe(toPage, offsetVertically = -(height - currentOffset))
@@ -86,7 +85,7 @@ internal fun InventoryGUI<*>.changePage(
val width = data.inventoryType.dimensions.width
changePageEffect(data.plugin, fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
changePageEffect(fromPageInt, toPageInt, width) { currentOffset, ifInverted ->
if (ifInverted) {
loadPageUnsafe(toPage, offsetHorizontally = -(width - currentOffset))
} else {
@@ -100,7 +99,7 @@ internal fun InventoryGUI<*>.changePage(
val height = data.inventoryType.dimensions.heigth
changePageEffect(data.plugin, fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
if (ifInverted) {
loadPageUnsafe(toPage, offsetVertically = -(height - currentOffset))
} else {
@@ -120,7 +119,6 @@ internal fun InventoryGUI<*>.changeGUI(
) = changePage(effect.effect, fromPage, toPage)
private inline fun changePageEffect(
kSpigot: KSpigot,
fromPage: Int,
toPage: Int,
doFor: Int,
@@ -130,7 +128,7 @@ private inline fun changePageEffect(
val ifInverted = fromPage >= toPage
var currentOffset = 1
kSpigot.task(
KSpigotMainInstance.task(
sync = true,
period = 1,
howOften = doFor.toLong()

View File

@@ -22,7 +22,7 @@ abstract class KSpigot : JavaPlugin() {
// lazy properties
private val kRunnableHolderProperty = lazy { KRunnableHolder() }
private val inventoryGUIHolderProperty = lazy { InventoryGUIHolder(this) }
private val inventoryGUIHolderProperty = lazy { InventoryGUIHolder() }
internal val kRunnableHolder by kRunnableHolderProperty
internal val inventoryGUIHolder by inventoryGUIHolderProperty
@@ -43,6 +43,7 @@ abstract class KSpigot : JavaPlugin() {
open fun shutdown() { }
final override fun onLoad() {
KSpigotMainInstance = this
load()
}
@@ -60,4 +61,6 @@ abstract class KSpigot : JavaPlugin() {
}
}
}
lateinit var KSpigotMainInstance: KSpigot private set

View File

@@ -2,15 +2,14 @@
package net.axay.kspigot.runnables
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.Bukkit
import org.bukkit.plugin.Plugin
/*
* Chainable bukkit runnable.
*/
class ChainedRunnablePart<T, R>(
val plugin: Plugin,
val runnable: (T?) -> R,
val sync: Boolean,
var previous: ChainedRunnablePart<*, T>? = null,
@@ -27,22 +26,22 @@ class ChainedRunnablePart<T, R>(
next?.start(result)
}
if (sync)
Bukkit.getScheduler().runTask(plugin, realRunnable)
Bukkit.getScheduler().runTask(KSpigotMainInstance, realRunnable)
else
Bukkit.getScheduler().runTaskAsynchronously(plugin, realRunnable)
Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, realRunnable)
}
}
// FIRST
fun <R> Plugin.firstDo(sync: Boolean, runnable: (Unit?) -> R)
= ChainedRunnablePart<Unit, R>(this, runnable, sync)
fun <R> Plugin.firstSync(runnable: (Unit?) -> R) = firstDo(true, runnable)
fun <R> Plugin.firstAsync(runnable: (Unit?) -> R) = firstDo(false, runnable)
fun <R> firstDo(sync: Boolean, runnable: (Unit?) -> R)
= ChainedRunnablePart<Unit, R>(runnable, sync)
fun <R> firstSync(runnable: (Unit?) -> R) = firstDo(true, runnable)
fun <R> firstAsync(runnable: (Unit?) -> R) = firstDo(false, runnable)
// THEN
fun <T, R, U> ChainedRunnablePart<T, R>.thenDo(sync: Boolean, runnable: (R?) -> U): ChainedRunnablePart<R, U> {
ChainedRunnablePart<R, U>(plugin, runnable, sync).apply {
ChainedRunnablePart<R, U>(runnable, sync).apply {
previous = this@thenDo
this@thenDo.next = this
return this

View File

@@ -2,9 +2,8 @@
package net.axay.kspigot.runnables
import net.axay.kspigot.main.KSpigot
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.Bukkit
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitRunnable
class KRunnableHolder : AutoCloseable {
@@ -49,7 +48,7 @@ abstract class KSpigotRunnable(
* @param endCallback code that should always be executed when the runnable ends
* @param runnable the runnable which should be executed each repetition
*/
fun KSpigot.task(
fun task(
sync: Boolean = true,
delay: Long = 0,
period: Long? = null,
@@ -87,32 +86,32 @@ fun KSpigot.task(
if (isCancelled) {
if (safe || ranOut)
kRunnableHolder.activate(this)
KSpigotMainInstance.kRunnableHolder.activate(this)
else
kRunnableHolder.remove(this)
KSpigotMainInstance.kRunnableHolder.remove(this)
}
}
}
if (endCallback != null) kRunnableHolder.add(bukkitRunnable, endCallback, safe)
if (endCallback != null) KSpigotMainInstance.kRunnableHolder.add(bukkitRunnable, endCallback, safe)
if (sync)
bukkitRunnable.runTaskTimer(this, delay, period ?: 20)
bukkitRunnable.runTaskTimer(KSpigotMainInstance, delay, period ?: 20)
else
bukkitRunnable.runTaskTimerAsynchronously(this, delay, period ?: 20)
bukkitRunnable.runTaskTimerAsynchronously(KSpigotMainInstance, delay, period ?: 20)
}
/**
* Starts a synchronous task.
*/
fun Plugin.sync(runnable: () -> Unit)
= Bukkit.getScheduler().runTask(this, runnable)
fun sync(runnable: () -> Unit)
= Bukkit.getScheduler().runTask(KSpigotMainInstance, runnable)
/**
* Starts an asynchronous task.
*/
fun Plugin.async(runnable: () -> Unit)
= Bukkit.getScheduler().runTaskAsynchronously(this, runnable)
fun async(runnable: () -> Unit)
= Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, runnable)

View File

@@ -1,6 +1,6 @@
package net.axay.kspigot.utils
import net.axay.kspigot.main.KSpigot
import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.command.CommandExecutor
import org.bukkit.command.TabCompleter
@@ -13,8 +13,8 @@ interface RegisterableCommand : CommandExecutor {
* @return true if the command was found -
* false if not
*/
fun registerCommand(commandName: String, kSpigot: KSpigot): Boolean {
kSpigot.getCommand(commandName)?.let {
fun registerAs(commandName: String): Boolean {
KSpigotMainInstance.getCommand(commandName)?.let {
it.setExecutor(this)
if (this is TabCompleter)
it.tabCompleter = this