Make "PluginInstance" available internally

This commit is contained in:
Jakob K
2021-08-24 01:47:38 +02:00
parent 4d3e25af32
commit fa71b0cc0e
11 changed files with 48 additions and 43 deletions

View File

@@ -6,7 +6,7 @@ import net.axay.kspigot.event.listen
import net.axay.kspigot.extensions.bukkit.content
import net.axay.kspigot.items.itemStack
import net.axay.kspigot.items.meta
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
@@ -67,7 +67,7 @@ internal abstract class PlayerInputBook<T>(
}
companion object {
val idKey = NamespacedKey(KSpigotMainInstance, "kspigot_bookinput_id")
val idKey = NamespacedKey(PluginInstance, "kspigot_bookinput_id")
internal val usedIDs = ArrayList<Int>()

View File

@@ -1,9 +1,9 @@
package net.axay.kspigot.config
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import java.io.File
class PluginFile(path: String, child: String? = null) : File(
"${KSpigotMainInstance.dataFolder}",
"${PluginInstance.dataFolder}",
if (child == null) path else File(path, child).path
)

View File

@@ -1,7 +1,7 @@
package net.axay.kspigot.event
import net.axay.kspigot.extensions.pluginManager
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import org.bukkit.event.Event
import org.bukkit.event.EventPriority
import org.bukkit.event.HandlerList
@@ -25,7 +25,7 @@ inline fun <reified T : Event> Listener.register(
ignoreCancelled: Boolean = false,
noinline executor: (Listener, Event) -> Unit,
) {
pluginManager.registerEvent(T::class.java, this, priority, executor, KSpigotMainInstance, ignoreCancelled)
pluginManager.registerEvent(T::class.java, this, priority, executor, PluginInstance, ignoreCancelled)
}
/**
@@ -52,7 +52,7 @@ inline fun <reified T : Event> SingleListener<T>.register() {
this,
priority,
{ _, event -> (event as? T)?.let { this.onEvent(it) } },
KSpigotMainInstance,
PluginInstance,
ignoreCancelled
)
}

View File

@@ -1,6 +1,6 @@
package net.axay.kspigot.extensions
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import org.bukkit.Bukkit
import org.bukkit.NamespacedKey
import org.bukkit.command.CommandSender
@@ -46,4 +46,4 @@ val console get() = Bukkit.getConsoleSender()
/**
* Shortcut for creating a new [NamespacedKey]
*/
fun pluginKey(key: String) = NamespacedKey(KSpigotMainInstance, key)
fun pluginKey(key: String) = NamespacedKey(PluginInstance, key)

View File

@@ -1,6 +1,6 @@
package net.axay.kspigot.extensions.bukkit
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import org.bukkit.command.CommandExecutor
import org.bukkit.command.TabCompleter
@@ -10,7 +10,7 @@ import org.bukkit.command.TabCompleter
* @return If the command was registered successfully.
*/
fun CommandExecutor.register(commandName: String): Boolean {
KSpigotMainInstance.getCommand(commandName)?.let {
PluginInstance.getCommand(commandName)?.let {
it.setExecutor(this)
if (this is TabCompleter)
it.tabCompleter = this

View File

@@ -1,28 +1,28 @@
package net.axay.kspigot.extensions.bukkit
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
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? = KSpigotMainInstance) =
fun CommandSender.print(text: String, plugin: Plugin? = PluginInstance) =
printColoredPrefix(text, ChatColor.RESET, plugin?.name ?: "INFO", ChatColor.GRAY)
/** @see printColoredPrefix */
fun CommandSender.info(text: String, plugin: Plugin? = KSpigotMainInstance) =
fun CommandSender.info(text: String, plugin: Plugin? = PluginInstance) =
printColoredPrefix(text, ChatColor.WHITE, plugin?.name ?: "INFO", ChatColor.DARK_AQUA)
/** @see printColoredPrefix */
fun CommandSender.success(text: String, plugin: Plugin? = KSpigotMainInstance) =
fun CommandSender.success(text: String, plugin: Plugin? = PluginInstance) =
printColoredPrefix(text, ChatColor.GREEN, plugin?.name ?: "SUCCESS", ChatColor.DARK_AQUA)
/** @see printColoredPrefix */
fun CommandSender.warn(text: String, plugin: Plugin? = KSpigotMainInstance) =
fun CommandSender.warn(text: String, plugin: Plugin? = PluginInstance) =
printColoredPrefix(text, ChatColor.WHITE, plugin?.name?.plus(" - WARN") ?: "WARN", ChatColor.YELLOW)
/** @see printColoredPrefix */
fun CommandSender.error(text: String, plugin: Plugin? = KSpigotMainInstance) =
fun CommandSender.error(text: String, plugin: Plugin? = PluginInstance) =
printColoredPrefix(text, ChatColor.RED, plugin?.name?.plus(" - ERROR") ?: "ERROR", ChatColor.DARK_RED)
/**

View File

@@ -3,7 +3,7 @@ package net.axay.kspigot.extensions.bukkit
import net.axay.kspigot.annotations.NMS_General
import net.axay.kspigot.chat.literalText
import net.axay.kspigot.extensions.onlinePlayers
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import net.axay.kspigot.pluginmessages.PluginMessageConnect
import net.md_5.bungee.api.ChatMessageType
import org.bukkit.Location
@@ -73,28 +73,28 @@ fun Player.feedSaturate() {
* Hides the player for all [onlinePlayers].
*/
fun Player.disappear() {
onlinePlayers.filter { it != this }.forEach { it.hidePlayer(KSpigotMainInstance, this) }
onlinePlayers.filter { it != this }.forEach { it.hidePlayer(PluginInstance, this) }
}
/**
* Shows the player for all [onlinePlayers].
*/
fun Player.appear() {
onlinePlayers.filter { it != this }.forEach { it.showPlayer(KSpigotMainInstance, this) }
onlinePlayers.filter { it != this }.forEach { it.showPlayer(PluginInstance, this) }
}
/**
* Hides all online players from this player.
*/
fun Player.hideOnlinePlayers() {
onlinePlayers.filter { it != this }.forEach { this.hidePlayer(KSpigotMainInstance, it) }
onlinePlayers.filter { it != this }.forEach { this.hidePlayer(PluginInstance, it) }
}
/**
* Shows all online players to this player.
*/
fun Player.showOnlinePlayers() {
onlinePlayers.filter { it != this }.forEach { this.showPlayer(KSpigotMainInstance, it) }
onlinePlayers.filter { it != this }.forEach { this.showPlayer(PluginInstance, it) }
}
/**

View File

@@ -4,7 +4,7 @@ package net.axay.kspigot.gui
import net.axay.kspigot.event.listen
import net.axay.kspigot.extensions.bukkit.closeForViewers
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.event.player.PlayerQuitEvent
@@ -194,7 +194,7 @@ class GUIInstance<T : ForInventory>(
* (KSpigot will listen for actions in the inventory.)
*/
@Suppress("UNCHECKED_CAST")
fun register() = KSpigotMainInstance.guiHolder.register(this as GUIInstance<ForInventory>)
fun register() = PluginInstance.guiHolder.register(this as GUIInstance<ForInventory>)
/**
* Stops KSpigot from listening to actions in this
@@ -202,7 +202,7 @@ class GUIInstance<T : ForInventory>(
*/
fun unregister() {
@Suppress("UNCHECKED_CAST")
KSpigotMainInstance.guiHolder.unregister(this as GUIInstance<ForInventory>)
PluginInstance.guiHolder.unregister(this as GUIInstance<ForInventory>)
// unregister this inv from all elements
currentElements.forEach { it.stopUsing(this) }
currentElements.clear()

View File

@@ -9,9 +9,14 @@ import net.axay.kspigot.runnables.KRunnableHolder
import org.bukkit.plugin.java.JavaPlugin
/**
* The main plugin instance.
* The main plugin instance. Available with public visibility.
*/
lateinit var KSpigotMainInstance: KSpigot
val KSpigotMainInstance: KSpigot get() = PluginInstance
/**
* The main plugin instance. Less complicated name for internal usage.
*/
internal lateinit var PluginInstance: KSpigot
private set
/**
@@ -50,10 +55,10 @@ abstract class KSpigot : JavaPlugin() {
open fun shutdown() {}
final override fun onLoad() {
if (::KSpigotMainInstance.isInitialized) {
if (::PluginInstance.isInitialized) {
console.warn("The main instance of KSpigot has been modified, even though it has already been set by another plugin!")
}
KSpigotMainInstance = this
PluginInstance = this
load()
}

View File

@@ -1,7 +1,7 @@
package net.axay.kspigot.pluginmessages
import net.axay.kspigot.extensions.onlinePlayers
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import org.bukkit.entity.Player
import java.io.ByteArrayOutputStream
import java.io.DataInputStream
@@ -61,5 +61,5 @@ fun sendPluginMessageToBungeeCord(
if (onResponse != null)
BungeePluginMessageResponseCallback(subChannel, responseTimeout, onResponse)
player.sendPluginMessage(KSpigotMainInstance, "BungeeCord", msgbytes.toByteArray())
player.sendPluginMessage(PluginInstance, "BungeeCord", msgbytes.toByteArray())
}

View File

@@ -2,7 +2,7 @@
package net.axay.kspigot.runnables
import net.axay.kspigot.main.KSpigotMainInstance
import net.axay.kspigot.main.PluginInstance
import org.bukkit.Bukkit
import org.bukkit.scheduler.BukkitRunnable
@@ -77,21 +77,21 @@ fun task(
if (isCancelled) {
if (safe || ranOut)
KSpigotMainInstance.kRunnableHolder.activate(this)
PluginInstance.kRunnableHolder.activate(this)
else
KSpigotMainInstance.kRunnableHolder.remove(this)
PluginInstance.kRunnableHolder.remove(this)
}
}
}
if (endCallback != null) KSpigotMainInstance.kRunnableHolder.add(bukkitRunnable, endCallback, safe)
if (endCallback != null) PluginInstance.kRunnableHolder.add(bukkitRunnable, endCallback, safe)
if (period != null)
if (sync) bukkitRunnable.runTaskTimer(KSpigotMainInstance, delay, period)
else bukkitRunnable.runTaskTimerAsynchronously(KSpigotMainInstance, delay, period)
if (sync) bukkitRunnable.runTaskTimer(PluginInstance, delay, period)
else bukkitRunnable.runTaskTimerAsynchronously(PluginInstance, delay, period)
else
if (sync) bukkitRunnable.runTaskLater(KSpigotMainInstance, delay)
else bukkitRunnable.runTaskLaterAsynchronously(KSpigotMainInstance, delay)
if (sync) bukkitRunnable.runTaskLater(PluginInstance, delay)
else bukkitRunnable.runTaskLaterAsynchronously(PluginInstance, delay)
return bukkitRunnable
}
@@ -102,9 +102,9 @@ fun task(
*/
fun taskRunLater(delay: Long, sync: Boolean = true, runnable: () -> Unit) {
if (sync)
Bukkit.getScheduler().runTaskLater(KSpigotMainInstance, runnable, delay)
Bukkit.getScheduler().runTaskLater(PluginInstance, runnable, delay)
else
Bukkit.getScheduler().runTaskLaterAsynchronously(KSpigotMainInstance, runnable, delay)
Bukkit.getScheduler().runTaskLaterAsynchronously(PluginInstance, runnable, delay)
}
/**
@@ -122,9 +122,9 @@ fun taskRun(sync: Boolean = true, runnable: () -> Unit) {
/**
* Starts a synchronous task.
*/
fun sync(runnable: () -> Unit) = Bukkit.getScheduler().runTask(KSpigotMainInstance, runnable)
fun sync(runnable: () -> Unit) = Bukkit.getScheduler().runTask(PluginInstance, runnable)
/**
* Starts an asynchronous task.
*/
fun async(runnable: () -> Unit) = Bukkit.getScheduler().runTaskAsynchronously(KSpigotMainInstance, runnable)
fun async(runnable: () -> Unit) = Bukkit.getScheduler().runTaskAsynchronously(PluginInstance, runnable)