Skip to content

Commit 53a542f

Browse files
committed
feat(platform): 优化部分顶层 platform 函数,使用缓存
1 parent 3815d40 commit 53a542f

File tree

11 files changed

+110
-39
lines changed

11 files changed

+110
-39
lines changed

common-platform-api/src/main/kotlin/taboolib/common/platform/function/Adapter.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,37 @@ import taboolib.common.platform.ProxyCommandSender
55
import taboolib.common.platform.ProxyPlayer
66
import taboolib.common.platform.service.PlatformAdapter
77
import taboolib.common.util.Location
8+
import taboolib.common.util.unsafeLazy
89
import java.util.*
910

11+
val adapterService by unsafeLazy { PlatformFactory.getService<PlatformAdapter>() }
12+
1013
/**
1114
* 获取控制台
1215
*/
1316
fun console(): ProxyCommandSender {
14-
return PlatformFactory.getService<PlatformAdapter>().console()
17+
return adapterService.console()
1518
}
1619

1720
/**
1821
* 将平台实现转换为跨平台实现
1922
*/
2023
fun adaptCommandSender(any: Any): ProxyCommandSender {
21-
return PlatformFactory.getService<PlatformAdapter>().adaptCommandSender(any)
24+
return adapterService.adaptCommandSender(any)
2225
}
2326

2427
/**
2528
* 获取所有在线玩家
2629
*/
2730
fun onlinePlayers(): List<ProxyPlayer> {
28-
return PlatformFactory.getService<PlatformAdapter>().onlinePlayers()
31+
return adapterService.onlinePlayers()
2932
}
3033

3134
/**
3235
* 将平台实现转换为跨平台实现
3336
*/
3437
fun adaptPlayer(any: Any): ProxyPlayer {
35-
return PlatformFactory.getService<PlatformAdapter>().adaptPlayer(any)
38+
return adapterService.adaptPlayer(any)
3639
}
3740

3841
/**
@@ -53,20 +56,20 @@ fun getProxyPlayer(uuid: UUID): ProxyPlayer? {
5356
* 将平台实现转换为跨平台实现
5457
*/
5558
fun adaptLocation(any: Any): Location {
56-
return PlatformFactory.getService<PlatformAdapter>().adaptLocation(any)
59+
return adapterService.adaptLocation(any)
5760
}
5861

5962
/**
6063
* 将跨平台实现转换为平台实现
6164
*/
6265
@Suppress("UNCHECKED_CAST")
6366
fun <T> platformLocation(location: Location): T {
64-
return PlatformFactory.getService<PlatformAdapter>().platformLocation(location) as T
67+
return adapterService.platformLocation(location) as T
6568
}
6669

6770
/**
6871
* 获取所有世界
6972
*/
7073
fun allWorlds(): List<String> {
71-
return PlatformFactory.getService<PlatformAdapter>().allWorlds()
74+
return adapterService.allWorlds()
7275
}

common-platform-api/src/main/kotlin/taboolib/common/platform/function/Command.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import taboolib.common.platform.command.CommandExecutor
66
import taboolib.common.platform.command.CommandStructure
77
import taboolib.common.platform.command.component.CommandBase
88
import taboolib.common.platform.service.PlatformCommand
9+
import taboolib.common.util.unsafeLazy
10+
11+
val commandService by unsafeLazy { PlatformFactory.getService<PlatformCommand>() }
912

1013
/**
1114
* 注册一个命令
@@ -16,7 +19,7 @@ import taboolib.common.platform.service.PlatformCommand
1619
* @param commandBuilder 命令构建器
1720
*/
1821
fun registerCommand(command: CommandStructure, executor: CommandExecutor, completer: CommandCompleter, commandBuilder: CommandBase.() -> Unit) {
19-
PlatformFactory.getService<PlatformCommand>().registerCommand(command, executor, completer, commandBuilder)
22+
commandService.registerCommand(command, executor, completer, commandBuilder)
2023
}
2124

2225
/**
@@ -35,12 +38,12 @@ fun unregisterCommand(command: CommandStructure) {
3538
* @param command 命令名称
3639
*/
3740
fun unregisterCommand(command: String) {
38-
PlatformFactory.getService<PlatformCommand>().unregisterCommand(command)
41+
commandService.unregisterCommand(command)
3942
}
4043

4144
/**
4245
* 注销所有命令
4346
*/
4447
fun unregisterCommands() {
45-
PlatformFactory.getService<PlatformCommand>().unregisterCommands()
48+
commandService.unregisterCommands()
4649
}

common-platform-api/src/main/kotlin/taboolib/common/platform/function/Executor.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ package taboolib.common.platform.function
22

33
import taboolib.common.platform.PlatformFactory
44
import taboolib.common.platform.service.PlatformExecutor
5+
import taboolib.common.util.unsafeLazy
6+
7+
val executorService by unsafeLazy { PlatformFactory.getService<PlatformExecutor>() }
58

69
/**
710
* 释放在预备阶段的调度器计划
811
* 这个方法只能执行一次且必须执行
912
*/
1013
fun startExecutor() {
11-
PlatformFactory.getService<PlatformExecutor>().start()
14+
executorService.start()
1215
}
1316

1417
/**
1518
* 执行一个任务
1619
*/
1720
fun runTask(executor: Runnable): PlatformExecutor.PlatformTask {
18-
return PlatformFactory.getService<PlatformExecutor>().submit(PlatformExecutor.PlatformRunnable(now = false, async = false, delay = 0, period = 0) { executor.run() })
21+
return executorService.submit(PlatformExecutor.PlatformRunnable(now = false, async = false, delay = 0, period = 0) { executor.run() })
1922
}
2023

2124
/**
@@ -35,7 +38,7 @@ fun submit(
3538
period: Long = 0,
3639
executor: PlatformExecutor.PlatformTask.() -> Unit,
3740
): PlatformExecutor.PlatformTask {
38-
return PlatformFactory.getService<PlatformExecutor>().submit(PlatformExecutor.PlatformRunnable(now, async, delay, period, executor))
41+
return executorService.submit(PlatformExecutor.PlatformRunnable(now, async, delay, period, executor))
3942
}
4043

4144
/**
@@ -53,5 +56,5 @@ fun submitAsync(
5356
period: Long = 0,
5457
executor: PlatformExecutor.PlatformTask.() -> Unit,
5558
): PlatformExecutor.PlatformTask {
56-
return PlatformFactory.getService<PlatformExecutor>().submit(PlatformExecutor.PlatformRunnable(now, true, delay, period, executor))
59+
return executorService.submit(PlatformExecutor.PlatformRunnable(now, true, delay, period, executor))
5760
}

common-platform-api/src/main/kotlin/taboolib/common/platform/function/IO.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ import taboolib.common.io.newFile
66
import taboolib.common.io.runningResources
77
import taboolib.common.platform.PlatformFactory
88
import taboolib.common.platform.service.PlatformIO
9+
import taboolib.common.util.unsafeLazy
910
import java.io.File
1011

12+
val ioService by unsafeLazy { PlatformFactory.getService<PlatformIO>() }
13+
val ioServiceOrNull by unsafeLazy { PlatformFactory.getServiceOrNull<PlatformIO>() }
14+
1115
/**
1216
* 获取控制台对象
1317
* 例如:
1418
* server<ConsoleCommandSender>()
1519
*/
1620
fun <T> server(): T {
17-
return PlatformFactory.getService<PlatformIO>().server()
21+
return ioService.server()
1822
}
1923

2024
/**
@@ -32,7 +36,7 @@ fun debug(vararg message: Any?) {
3236
* @param message 日志内容
3337
*/
3438
fun info(vararg message: Any?) {
35-
val service = PlatformFactory.getServiceOrNull<PlatformIO>()
39+
val service = ioServiceOrNull
3640
if (service != null) {
3741
service.info(*message)
3842
} else {
@@ -46,7 +50,7 @@ fun info(vararg message: Any?) {
4650
* @param message 日志内容
4751
*/
4852
fun severe(vararg message: Any?) {
49-
val service = PlatformFactory.getServiceOrNull<PlatformIO>()
53+
val service = ioServiceOrNull
5054
if (service != null) {
5155
service.severe(*message)
5256
} else {
@@ -60,7 +64,7 @@ fun severe(vararg message: Any?) {
6064
* @param message 日志内容
6165
*/
6266
fun warning(vararg message: Any?) {
63-
val service = PlatformFactory.getServiceOrNull<PlatformIO>()
67+
val service = ioServiceOrNull
6468
if (service != null) {
6569
service.warning(*message)
6670
} else {
@@ -76,7 +80,7 @@ fun warning(vararg message: Any?) {
7680
* @param target 资源文件目标路径
7781
*/
7882
fun releaseResourceFile(source: String, replace: Boolean = false, target: String = source): File {
79-
return PlatformFactory.getService<PlatformIO>().releaseResourceFile(source, target, replace)
83+
return ioService.releaseResourceFile(source, target, replace)
8084
}
8185

8286
/**
@@ -101,21 +105,21 @@ fun releaseResourceFolder(prefix: String, replace: Boolean = false) {
101105
* 获取当前插件的 Jar 文件对象
102106
*/
103107
fun getJarFile(): File {
104-
return PlatformFactory.getService<PlatformIO>().getJarFile()
108+
return ioService.getJarFile()
105109
}
106110

107111
/**
108112
* 获取当前插件的配置文件目录
109113
* 可能不存在,需要手动调用 mkdirs 方法创建
110114
*/
111115
fun getDataFolder(): File {
112-
return PlatformFactory.getService<PlatformIO>().getDataFolder()
116+
return ioService.getDataFolder()
113117
}
114118

115119
/**
116120
* 获取当前平台的信息
117121
* 用于 BStats 统计,无实际用途
118122
*/
119123
fun getPlatformData(): Map<String, Any> {
120-
return PlatformFactory.getService<PlatformIO>().getPlatformData()
124+
return ioService.getPlatformData()
121125
}

common-platform-api/src/main/kotlin/taboolib/common/platform/function/OpenContainer.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package taboolib.common.platform.function
33
import taboolib.common.OpenContainer
44
import taboolib.common.platform.PlatformFactory
55
import taboolib.common.platform.service.PlatformOpenContainer
6+
import taboolib.common.util.unsafeLazy
7+
8+
val openContainerService by unsafeLazy { PlatformFactory.getService<PlatformOpenContainer>() }
69

710
/**
811
* 获取当前服务端中运行的所有开放接口
912
*/
1013
fun getOpenContainers(): List<OpenContainer> {
11-
return PlatformFactory.getService<PlatformOpenContainer>().getOpenContainers()
14+
return openContainerService.getOpenContainers()
1215
}
1316

1417
/**
@@ -17,5 +20,5 @@ fun getOpenContainers(): List<OpenContainer> {
1720
* @param name 接口名称
1821
*/
1922
fun getOpenContainer(name: String): OpenContainer? {
20-
return PlatformFactory.getService<PlatformOpenContainer>().getOpenContainers().firstOrNull { it.name == name }
23+
return openContainerService.getOpenContainers(name)
2124
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package taboolib.common.platform.function
22

3-
import taboolib.common.platform.PlatformFactory
4-
import taboolib.common.platform.service.PlatformIO
3+
import taboolib.common.util.unsafeLazy
54

65
/**
76
* 获取当前插件名称
87
*/
9-
inline val pluginId: String
10-
get() = PlatformFactory.getService<PlatformIO>().pluginId
8+
val pluginId by unsafeLazy { ioService.pluginId }
119

1210
/**
1311
* 获取当前插件版本
1412
*/
15-
inline val pluginVersion: String
16-
get() = PlatformFactory.getService<PlatformIO>().pluginVersion
13+
val pluginVersion by unsafeLazy { ioService.pluginVersion }
1714

1815
/**
1916
* 当前是否在主线程中运行
2017
*/
2118
inline val isPrimaryThread: Boolean
22-
get() = PlatformFactory.getService<PlatformIO>().isPrimaryThread
19+
get() = ioService.isPrimaryThread

common-platform-api/src/main/kotlin/taboolib/common/platform/service/PlatformOpenContainer.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ import taboolib.common.platform.PlatformService
99
interface PlatformOpenContainer {
1010

1111
fun getOpenContainers(): List<OpenContainer>
12+
13+
fun getOpenContainers(name: String): OpenContainer?
1214
}

platform/platform-afybroker/src/main/kotlin/taboolib/platform/AfyBrokerOpenContainer.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import taboolib.common.platform.PlatformSide
99
import taboolib.common.platform.function.pluginId
1010
import taboolib.common.platform.service.PlatformOpenContainer
1111
import taboolib.platform.type.AfyBrokerContainer
12+
import java.util.concurrent.ConcurrentHashMap
1213

1314
/**
1415
* TabooLib
@@ -22,13 +23,26 @@ import taboolib.platform.type.AfyBrokerContainer
2223
@PlatformSide(Platform.AFYBROKER)
2324
class AfyBrokerOpenContainer : PlatformOpenContainer {
2425

25-
val pluginContainer = HashMap<String, OpenContainer>()
26+
val pluginContainer = ConcurrentHashMap<String, OpenContainer>()
2627

2728
override fun getOpenContainers(): List<OpenContainer> {
2829
return Broker.getPluginManager().plugins.filter { it.javaClass.name.endsWith("platform.AfyBrokerPlugin") && it.description.name != pluginId }.mapNotNull {
29-
pluginContainer.getOrPut(it.description.name) { AfyBrokerContainer(it) }
30+
pluginContainer.computeIfAbsent(it.description.name) { _ -> AfyBrokerContainer(it) }
3031
}.filter {
3132
it.isValid
3233
}
3334
}
35+
36+
override fun getOpenContainers(name: String): OpenContainer? {
37+
if (pluginContainer.containsKey(name)) {
38+
return pluginContainer[name]
39+
}
40+
val plugin = Broker.getPluginManager().getPlugin(name)
41+
if (plugin != null && plugin.javaClass.name.endsWith("platform.AfyBrokerPlugin")) {
42+
val container = AfyBrokerContainer(plugin)
43+
pluginContainer[name] = container
44+
return container
45+
}
46+
return null
47+
}
3448
}

platform/platform-bukkit-impl/src/main/kotlin/taboolib/platform/BukkitOpenContainer.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import taboolib.common.platform.PlatformSide
99
import taboolib.common.platform.function.pluginId
1010
import taboolib.common.platform.service.PlatformOpenContainer
1111
import taboolib.platform.type.BukkitContainer
12+
import java.util.concurrent.ConcurrentHashMap
1213

1314
/**
1415
* TabooLib
@@ -22,13 +23,26 @@ import taboolib.platform.type.BukkitContainer
2223
@PlatformSide(Platform.BUKKIT)
2324
class BukkitOpenContainer : PlatformOpenContainer {
2425

25-
val pluginContainer = HashMap<String, OpenContainer>()
26+
val pluginContainer = ConcurrentHashMap<String, OpenContainer>()
2627

2728
override fun getOpenContainers(): List<OpenContainer> {
2829
return Bukkit.getPluginManager().plugins.filter { it.javaClass.name.endsWith("platform.BukkitPlugin") && it.name != pluginId }.mapNotNull {
29-
pluginContainer.getOrPut(it.name) { BukkitContainer(it) }
30+
pluginContainer.computeIfAbsent(it.name) { _ -> BukkitContainer(it) }
3031
}.filter {
3132
it.isValid
3233
}
3334
}
35+
36+
override fun getOpenContainers(name: String): OpenContainer? {
37+
if (pluginContainer.containsKey(name)) {
38+
return pluginContainer[name]
39+
}
40+
val plugin = Bukkit.getPluginManager().getPlugin(name)
41+
if (plugin != null && plugin.javaClass.name.endsWith("platform.BukkitPlugin")) {
42+
val container = BukkitContainer(plugin)
43+
pluginContainer[name] = container
44+
return container
45+
}
46+
return null
47+
}
3448
}

0 commit comments

Comments
 (0)