
walkmc.extensions.Server.kt Maven / Gradle / Ivy
@file:Suppress("NOTHING_TO_INLINE")
package walkmc.extensions
import org.bukkit.*
import org.bukkit.command.*
import org.bukkit.entity.*
import org.bukkit.event.inventory.*
import org.bukkit.inventory.*
import org.bukkit.plugin.*
import org.bukkit.plugin.messaging.*
import org.bukkit.scheduler.*
import walkmc.extensions.strings.*
import java.io.*
import java.util.*
/**
* Gets the current server of the plugin.
*/
val server: Server get() = Bukkit.getServer()
/**
* Gets all online players of this server.
*/
val onlinePlayers: Collection get() = Bukkit.getOnlinePlayers()
/**
* Gets all offline players of this server.
*/
val offlinePlayers: Array get() = Bukkit.getOfflinePlayers()
/**
* Gets all banned players of this server.
*/
val bannedPlayers: Set get() = Bukkit.getBannedPlayers()
/**
* Gets all worlds loaded of this server.
*/
val worlds: List get() = Bukkit.getWorlds()
/**
* Gets the [ConsoleCommandSender] of this server.
*/
val console: ConsoleCommandSender get() = Bukkit.getConsoleSender()
/**
* Gets the IP of this server.
*/
val serverIp: String get() = Bukkit.getIp()
/**
* Gets the current version of this server.
*/
val version: String get() = Bukkit.getVersion()
/**
* Gets the current bukkit version of this server.
*/
val bukkitVersion: String get() = Bukkit.getBukkitVersion()
/**
* Gets the whitelist of this server.
*/
var whitelist: Boolean
get() = Bukkit.hasWhitelist()
set(value) = Bukkit.setWhitelist(value)
/**
* Gets the MOTD of this server.
*/
val motd: String get() = Bukkit.getMotd()
/**
* Gets the plugin manager of this server.
*/
val pluginManager: PluginManager get() = Bukkit.getPluginManager()
/**
* Gets the messenger of this server.
*/
val messenger: Messenger get() = Bukkit.getMessenger()
/**
* Gets the services manager of this server.
*/
val servicesManager: ServicesManager get() = Bukkit.getServicesManager()
/**
* Gets the server name.
*/
val serverName: String get() = Bukkit.getServerName()
/**
* Gets the scheduler of this server.
*/
val scheduler: BukkitScheduler get() = Bukkit.getScheduler()
/**
* Gets all plugins from this server.
*/
val plugins: Array get() = pluginManager.plugins
/**
* Gets if the current thread is the primary thread of the server.
*/
val isPrimaryThread: Boolean get() = Bukkit.isPrimaryThread()
/**
* Dispatchs the specified command in the console.
*/
fun dispatchCommand(command: String) = Bukkit.dispatchCommand(console, command)
/**
* Dispatchs all specifieds commands in the console.
*/
fun dispatchCommands(commands: Iterable) {
for (command in commands)
dispatchCommand(command)
}
/**
* Logs a message to console of this server.
*/
fun log(message: Any) = console.log("§f$message")
/**
* Logs a error message to console of this server.
*/
fun logError(message: Any) = console.log("§cERROR §7- §f$message")
/**
* Logs a warning message to console of this server.
*/
fun logWarning(message: Any) = console.log("§eWARNING §7- §f$message")
/**
* Logs a broadcast message to all players of this server.
*/
fun broadcast(message: Any) = Bukkit.broadcastMessage("$message")
/**
* Logs a broadcast message to all players of this server thats contains the specified [permission].
*/
fun broadcast(message: Any, permission: String) = Bukkit.broadcast("$message", permission)
/**
* Logs a broadcast message to all players of this server with satisfy the given [filter].
*/
inline fun broadcast(message: Any, filter: (Player) -> Boolean) {
onlinePlayers
.filter(filter)
.forEach { it.log(message) }
}
/**
* Returns if the specified [plugin] is enabled.
*/
fun isPluginEnabled(plugin: Plugin) = pluginManager.isPluginEnabled(plugin)
/**
* Returns if the specified [plugin] is enabled.
*/
fun isPluginEnabled(plugin: String) = pluginManager.isPluginEnabled(plugin)
/**
* Checks if the specified [plugin] is enabled, otherwise logs the given [message].
*/
fun checkPluginEnabled(plugin: Plugin, message: String): Boolean {
if (isPluginEnabled(plugin))
return true
logError(message)
return false
}
/**
* Checks if the specified [plugin] is enabled, otherwise logs the given [message].
*/
fun checkPluginEnabled(plugin: String, message: String): Boolean {
if (isPluginEnabled(plugin))
return true
logError(message)
return false
}
/**
* Gets a plugin by name in this server.
*/
fun plugin(name: String): Plugin = pluginManager.getPlugin(name)
/**
* Gets a plugin by name in this server.
*/
@JvmName("pluginOf")
fun plugin(name: String): Plugin = pluginManager.getPlugin(name).cast()
/**
* Loads all plugins inside of the [directory]
*/
fun loadPlugins(directory: File): Array = pluginManager.loadPlugins(directory)
/**
* Loads the specified plugin by file.
*/
fun loadPlugin(file: File): Plugin = pluginManager.loadPlugin(file)
/**
* Loads the specified plugin by file or null if it is invalid.
*/
fun loadPluginOrNull(file: File): Plugin? {
return try {
pluginManager.loadPlugin(file)
} catch (ex: Exception) {
null
}
}
/**
* Gets a player by UUID in this server.
*/
fun player(id: UUID): Player = Bukkit.getPlayer(id)
/**
* Gets a player by name in this server.
*/
fun player(name: String): Player = Bukkit.getPlayer(name)
/**
* Gets a offline player by UUID in this server.
*/
fun offlinePlayer(id: UUID): OfflinePlayer = Bukkit.getOfflinePlayer(id)
/**
* Gets a offline player by name in this server.
*/
fun offlinePlayer(name: String): OfflinePlayer = Bukkit.getOfflinePlayer(name)
/**
* Gets a world by UUID in this server.
*/
fun world(id: UUID): World = Bukkit.getWorld(id)
/**
* Gets a world by name in this server.
*/
fun world(name: String): World = Bukkit.getWorld(name)
/**
* Parses a the given [str] as location.
*
* @param fully if the parser must verify for yaw/pitch.
*/
fun location(str: String, fully: Boolean = false): Location {
return if (fully) fullLocation(str) else simpleLocation(str)
}
/**
* Internally parses the given [str] as location.
*/
internal fun simpleLocation(str: String): Location {
val split = str.split(':')
return Location(
split[0].toWorld(),
split[1].toInt(),
split[2].toInt(),
split[3].toInt()
)
}
/**
* Internally parses the given [str] as location.
*/
internal fun fullLocation(str: String): Location {
val split = str.split(':')
return Location(
split[0].toWorld(),
split[1].toDouble(),
split[2].toDouble(),
split[3].toDouble(),
split[4].toFloat(),
split[5].toFloat()
)
}
/**
* Creates a inventory with the specified title and size.
*/
fun inventory(
title: String,
size: Int,
holder: InventoryHolder? = null
): Inventory = Bukkit.createInventory(holder, size, title)
/**
* Creates a inventory with the specified title and size.
*/
fun inventory(
type: InventoryType,
title: String,
holder: InventoryHolder? = null
): Inventory = Bukkit.createInventory(holder, type, title)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy