Skip to content

Commit

Permalink
6.0.12 Update codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Jan 1, 2024
1 parent 1b59942 commit a3079a6
Show file tree
Hide file tree
Showing 23 changed files with 201 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@file:Isolated
@file:Suppress("NOTHING_TO_INLINE")
package taboolib.common.io

import taboolib.common.Isolated
import java.io.File

/**
Expand All @@ -11,7 +14,7 @@ import java.io.File
* @param folder 该文件是否为文件夹(默认为否)
* @return 该文件自身
*/
fun newFile(file: File, path: String, create: Boolean = true, folder: Boolean = false): File {
inline fun newFile(file: File, path: String, create: Boolean = true, folder: Boolean = false): File {
return newFile(File(file, path), create, folder)
}

Expand All @@ -23,7 +26,7 @@ fun newFile(file: File, path: String, create: Boolean = true, folder: Boolean =
* @param folder 该文件是否为文件夹(默认为否)
* @return 该文件自身
*/
fun newFile(path: String, create: Boolean = true, folder: Boolean = false): File {
inline fun newFile(path: String, create: Boolean = true, folder: Boolean = false): File {
return newFile(File(path), create, folder)
}

Expand All @@ -35,7 +38,7 @@ fun newFile(path: String, create: Boolean = true, folder: Boolean = false): File
* @param folder 该文件是否为文件夹(默认为否)
* @return 该文件自身
*/
fun newFile(file: File, create: Boolean = true, folder: Boolean = false): File {
inline fun newFile(file: File, create: Boolean = true, folder: Boolean = false): File {
if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
}
Expand All @@ -57,7 +60,7 @@ fun newFile(file: File, create: Boolean = true, folder: Boolean = false): File {
* @param create 若目录不存在是否新建(默认为是)
* @return 该目录自身
*/
fun newFolder(folder: File, path: String, create: Boolean = true): File {
inline fun newFolder(folder: File, path: String, create: Boolean = true): File {
return newFile(folder, path, create, folder = true)
}

Expand All @@ -68,6 +71,6 @@ fun newFolder(folder: File, path: String, create: Boolean = true): File {
* @param create 若目录不存在是否新建(默认为是)
* @return 该目录自身
*/
fun newFolder(path: String, create: Boolean = true): File {
inline fun newFolder(path: String, create: Boolean = true): File {
return newFile(path, create, folder = true)
}
32 changes: 32 additions & 0 deletions common/src/main/kotlin/taboolib/common/io/FileDelete1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@file:Isolated

package taboolib.common.io

import taboolib.common.Isolated
import java.io.File
import java.util.concurrent.Executors
import java.util.concurrent.Future

/**
* 删除特定文件夹下的所有子文件
*/
fun File.deepDelete() {
if (exists()) {
if (isDirectory) {
listFiles()?.forEach { it.deepDelete() }
}
delete()
}
}

/**
* 复制文件或文件夹
* 若目标为文件夹则复制其所有子文件
*/
fun File.deepCopyTo(target: File) {
if (isDirectory) {
listFiles()?.forEach { it.deepCopyTo(File(target, it.name)) }
} else {
copyTo(target)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,4 @@ fun File.deepDeleteAsync(await: Boolean = false, futures: MutableSet<Future<*>>?
// Wait the task to finish before returning if await is true
// It shouldn't be called inside this function
if (await) future.get()
}

/**
* 删除特定文件夹下的所有子文件
*/
fun File.deepDelete() {
if (exists()) {
if (isDirectory) {
listFiles()?.forEach { it.deepDelete() }
}
delete()
}
}

/**
* 复制文件或文件夹
* 若目标为文件夹则复制其所有子文件
*/
fun File.deepCopyTo(target: File) {
if (isDirectory) {
listFiles()?.forEach { it.deepCopyTo(File(target, it.name)) }
} else {
copyTo(target)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@file:Isolated
@file:Suppress("NOTHING_TO_INLINE")

package taboolib.common.io

Expand All @@ -15,7 +16,7 @@ import java.security.MessageDigest
* @param algorithm 算法类型(可使用:md5, sha-1, sha-256 等)
* @return 数字签名
*/
fun String.digest(algorithm: String): String {
inline fun String.digest(algorithm: String): String {
val digest = MessageDigest.getInstance(algorithm)
digest.update(toByteArray(StandardCharsets.UTF_8))
return BigInteger(1, digest.digest()).toString(16)
Expand All @@ -27,7 +28,7 @@ fun String.digest(algorithm: String): String {
* @param algorithm 算法类型(可使用:md5, sha-1, sha-256 等)
* @return 数字签名
*/
fun File.digest(algorithm: String): String {
inline fun File.digest(algorithm: String): String {
return FileInputStream(this).use {
val digest = MessageDigest.getInstance(algorithm)
val buffer = ByteArray(1024)
Expand Down
27 changes: 15 additions & 12 deletions common/src/main/kotlin/taboolib/common/platform/PlatformFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import taboolib.common.platform.function.runningPlatform
import taboolib.common.platform.function.unregisterCommands
import java.util.concurrent.ConcurrentHashMap

@Suppress("UNCHECKED_CAST")
object PlatformFactory {

val awokenMap = ConcurrentHashMap<String, Any>()
Expand Down Expand Up @@ -104,30 +105,32 @@ object PlatformFactory {
/**
* 获取已被唤醒的 API 实例
*/
inline fun <reified T> getAPI(): T {
return awokenMap[T::class.java.name] as? T ?: error("API (${T::class.java.name}) not found, currently: ${awokenMap.keys}")
}
inline fun <reified T> getAPI() : T = getAPI(T::class.java.name)

/**
* 获取已被唤醒的 API 实例(可能为空)
*/
inline fun <reified T> getAPIOrNull(): T? {
return awokenMap[T::class.java.name] as? T
}
inline fun <reified T> getAPIOrNull() = awokenMap[T::class.java.name] as? T

/**
* 获取已被唤醒的 API 实例
*/
fun <T> getAPI(name: String) = (awokenMap[name] ?: error("API ($name) not found, currently: ${awokenMap.keys}")) as T

/**
* 获取已注册的跨平台服务
*/
inline fun <reified T> getService(): T {
return serviceMap[T::class.java.name] as? T ?: error("Service (${T::class.java}) not found, currently: ${serviceMap.keys}")
}
inline fun <reified T> getService() : T = getService(T::class.java.name)

/**
* 获取已注册的跨平台服务(可能为空)
*/
inline fun <reified T> getServiceOrNull(): T? {
return serviceMap[T::class.java.name] as? T
}
inline fun <reified T> getServiceOrNull() = serviceMap[T::class.java.name] as? T

/**
* 获取已注册的跨平台服务
*/
fun <T> getService(name: String) = (serviceMap[name] ?: error("Service ($name) not found, currently: ${serviceMap.keys}")) as T

/**
* 注册 API 实例
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@file:Isolated
@file:Suppress("NOTHING_TO_INLINE")

package taboolib.common.platform.function

import taboolib.common.Isolated
import taboolib.common.platform.PlatformFactory
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.ProxyPlayer
Expand All @@ -10,63 +14,63 @@ import java.util.*
/**
* 获取控制台
*/
fun console(): ProxyCommandSender {
inline fun console(): ProxyCommandSender {
return PlatformFactory.getService<PlatformAdapter>().console()
}

/**
* 将平台实现转换为跨平台实现
*/
fun adaptCommandSender(any: Any): ProxyCommandSender {
inline fun adaptCommandSender(any: Any): ProxyCommandSender {
return PlatformFactory.getService<PlatformAdapter>().adaptCommandSender(any)
}

/**
* 获取所有在线玩家
*/
fun onlinePlayers(): List<ProxyPlayer> {
inline fun onlinePlayers(): List<ProxyPlayer> {
return PlatformFactory.getService<PlatformAdapter>().onlinePlayers()
}

/**
* 将平台实现转换为跨平台实现
*/
fun adaptPlayer(any: Any): ProxyPlayer {
inline fun adaptPlayer(any: Any): ProxyPlayer {
return PlatformFactory.getService<PlatformAdapter>().adaptPlayer(any)
}

/**
* 通过名称获取玩家
*/
fun getProxyPlayer(name: String): ProxyPlayer? {
inline fun getProxyPlayer(name: String): ProxyPlayer? {
return onlinePlayers().firstOrNull { it.name.equals(name, true) }
}

/**
* 通过 UUID 获取玩家
*/
fun getProxyPlayer(uuid: UUID): ProxyPlayer? {
inline fun getProxyPlayer(uuid: UUID): ProxyPlayer? {
return onlinePlayers().firstOrNull { it.uniqueId == uuid }
}

/**
* 将平台实现转换为跨平台实现
*/
fun adaptLocation(any: Any): Location {
inline fun adaptLocation(any: Any): Location {
return PlatformFactory.getService<PlatformAdapter>().adaptLocation(any)
}

/**
* 将跨平台实现转换为平台实现
*/
@Suppress("UNCHECKED_CAST")
fun <T> platformLocation(location: Location): T {
inline fun <T> platformLocation(location: Location): T {
return PlatformFactory.getService<PlatformAdapter>().platformLocation(location) as T
}

/**
* 获取所有世界
*/
fun allWorlds(): List<String> {
inline fun allWorlds(): List<String> {
return PlatformFactory.getService<PlatformAdapter>().allWorlds()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@file:Isolated
@file:Suppress("NOTHING_TO_INLINE")

package taboolib.common.platform.function

import taboolib.common.Isolated
import taboolib.common.platform.PlatformFactory
import taboolib.common.platform.command.CommandCompleter
import taboolib.common.platform.command.CommandExecutor
Expand All @@ -15,7 +19,7 @@ import taboolib.common.platform.service.PlatformCommand
* @param completer 补全器
* @param commandBuilder 命令构建器
*/
fun registerCommand(command: CommandStructure, executor: CommandExecutor, completer: CommandCompleter, commandBuilder: CommandBase.() -> Unit) {
inline fun registerCommand(command: CommandStructure, executor: CommandExecutor, completer: CommandCompleter, noinline commandBuilder: CommandBase.() -> Unit) {
PlatformFactory.getService<PlatformCommand>().registerCommand(command, executor, completer, commandBuilder)
}

Expand All @@ -24,7 +28,7 @@ fun registerCommand(command: CommandStructure, executor: CommandExecutor, comple
*
* @param command 命令结构
*/
fun unregisterCommand(command: CommandStructure) {
inline fun unregisterCommand(command: CommandStructure) {
unregisterCommand(command.name)
command.aliases.forEach { unregisterCommand(it) }
}
Expand All @@ -34,13 +38,13 @@ fun unregisterCommand(command: CommandStructure) {
*
* @param command 命令名称
*/
fun unregisterCommand(command: String) {
inline fun unregisterCommand(command: String) {
PlatformFactory.getService<PlatformCommand>().unregisterCommand(command)
}

/**
* 注销所有命令
*/
fun unregisterCommands() {
inline fun unregisterCommands() {
PlatformFactory.getService<PlatformCommand>().unregisterCommands()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@file:Isolated
@file:Suppress("NOTHING_TO_INLINE")

package taboolib.common.platform.function

import taboolib.common.Isolated
Expand All @@ -16,7 +18,7 @@ inline val runningPlatform: Platform
/**
* 停用插件
*/
fun disablePlugin() {
inline fun disablePlugin() {
TabooLibCommon.setStopped(true)
}

Expand All @@ -26,7 +28,7 @@ fun disablePlugin() {
* @param lifeCycle 生命周期
* @param runnable 任务
*/
fun postpone(lifeCycle: LifeCycle = LifeCycle.ENABLE, runnable: Runnable) {
inline fun postpone(lifeCycle: LifeCycle = LifeCycle.ENABLE, runnable: Runnable) {
TabooLibCommon.postpone(lifeCycle, runnable)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
@file:Isolated
@file:Suppress("NOTHING_TO_INLINE")

package taboolib.common.platform.function

import taboolib.common.Isolated
import taboolib.common.platform.PlatformFactory
import taboolib.common.platform.event.ProxyEvent
import taboolib.common.platform.service.PlatformEvent

fun callEvent(proxyEvent: ProxyEvent) {
inline fun callEvent(proxyEvent: ProxyEvent) {
PlatformFactory.getService<PlatformEvent>().callEvent(proxyEvent)
}
Loading

0 comments on commit a3079a6

Please sign in to comment.