Skip to content

Commit 3b3cd67

Browse files
committed
feat(FoliaExecutor): 新增 Folia 区域调度器相关函数
1 parent 4df24c8 commit 3b3cd67

File tree

1 file changed

+274
-0
lines changed
  • platform/platform-bukkit-impl/src/main/kotlin/taboolib/platform/util

1 file changed

+274
-0
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
package taboolib.platform.util
2+
3+
import io.papermc.paper.threadedregions.scheduler.ScheduledTask
4+
import org.bukkit.Chunk
5+
import org.bukkit.Location
6+
import org.bukkit.World
7+
import org.bukkit.block.Block
8+
import org.bukkit.entity.Entity
9+
import taboolib.common.platform.function.submit as submitPlatform
10+
import taboolib.common.platform.service.PlatformExecutor
11+
import taboolib.platform.BukkitExecutor
12+
import taboolib.platform.BukkitPlugin
13+
import taboolib.platform.Folia
14+
import taboolib.platform.FoliaExecutor
15+
16+
// ============================================
17+
// Location 扩展函数
18+
// ============================================
19+
20+
/**
21+
* 在指定位置执行一个任务(支持 Folia 区域调度)
22+
*
23+
* @param executor 任务
24+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
25+
*/
26+
@JvmOverloads
27+
fun Location.runTask(executor: Runnable, useScheduler: Boolean = true): PlatformExecutor.PlatformTask {
28+
// 如果不是 Folia 环境
29+
if (!Folia.isFolia) {
30+
return if (useScheduler) {
31+
submitPlatform(now = false, async = false, delay = 0, period = 0) { executor.run() }
32+
} else {
33+
executor.run()
34+
BukkitExecutor.BukkitPlatformTask { }
35+
}
36+
}
37+
38+
// Folia 环境下,使用 RegionScheduler
39+
val scheduledTask = FoliaExecutor.REGION_SCHEDULER.run(BukkitPlugin.getInstance(), this) {
40+
executor.run()
41+
}
42+
43+
return BukkitExecutor.BukkitPlatformTask { scheduledTask.cancel() }
44+
}
45+
46+
/**
47+
* 在指定位置注册一个调度器(支持 Folia 区域调度)
48+
*
49+
* @param now 是否立即执行
50+
* @param async 是否异步执行(如果为 true,将使用全局调度器)
51+
* @param delay 延迟执行时间(tick)
52+
* @param period 重复执行时间(tick)
53+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
54+
* @param executor 调度器具体行为
55+
*/
56+
@JvmOverloads
57+
fun Location.submit(
58+
now: Boolean = false,
59+
async: Boolean = false,
60+
delay: Long = 0,
61+
period: Long = 0,
62+
useScheduler: Boolean = true,
63+
executor: PlatformExecutor.PlatformTask.() -> Unit,
64+
): PlatformExecutor.PlatformTask {
65+
// 如果是异步执行、或不是 Folia 环境
66+
if (!Folia.isFolia) {
67+
return if (useScheduler || async) {
68+
submitPlatform(now, async, delay, period, executor)
69+
} else {
70+
val task = BukkitExecutor.BukkitPlatformTask { }
71+
if (now) {
72+
executor(task)
73+
}
74+
task
75+
}
76+
}
77+
78+
// 如果是异步执行,使用原来的 submit
79+
if (async) {
80+
return submitPlatform(now, async, delay, period, executor)
81+
}
82+
83+
// Folia 环境下,使用 RegionScheduler 在指定位置执行
84+
var scheduledTask: ScheduledTask? = null
85+
86+
if (now) {
87+
// 立即执行
88+
val task = BukkitExecutor.BukkitPlatformTask { scheduledTask?.cancel() }
89+
executor(task)
90+
return task
91+
}
92+
93+
// 延迟或定时执行
94+
scheduledTask = if (period < 1) {
95+
// 单次执行
96+
if (delay < 1) {
97+
FoliaExecutor.REGION_SCHEDULER.run(BukkitPlugin.getInstance(), this) { task ->
98+
val platformTask = BukkitExecutor.BukkitPlatformTask { task.cancel() }
99+
executor(platformTask)
100+
}
101+
} else {
102+
FoliaExecutor.REGION_SCHEDULER.runDelayed(BukkitPlugin.getInstance(), this, { task ->
103+
val platformTask = BukkitExecutor.BukkitPlatformTask { task.cancel() }
104+
executor(platformTask)
105+
}, delay.coerceAtLeast(1))
106+
}
107+
} else {
108+
// 重复执行
109+
FoliaExecutor.REGION_SCHEDULER.runAtFixedRate(BukkitPlugin.getInstance(), this, { task ->
110+
val platformTask = BukkitExecutor.BukkitPlatformTask { task.cancel() }
111+
executor(platformTask)
112+
}, delay.coerceAtLeast(1), period)
113+
}
114+
115+
return BukkitExecutor.BukkitPlatformTask { scheduledTask.cancel() }
116+
}
117+
118+
// ============================================
119+
// Entity 扩展函数
120+
// ============================================
121+
122+
/**
123+
* 在实体所在位置执行一个任务(Folia 安全)
124+
*
125+
* @param executor 任务
126+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
127+
*/
128+
@JvmOverloads
129+
fun Entity.runTask(executor: Runnable, useScheduler: Boolean = true): PlatformExecutor.PlatformTask {
130+
return location.runTask(executor, useScheduler)
131+
}
132+
133+
/**
134+
* 在实体所在位置注册一个调度器(Folia 安全)
135+
*
136+
* @param now 是否立即执行
137+
* @param async 是否异步执行
138+
* @param delay 延迟执行时间(tick)
139+
* @param period 重复执行时间(tick)
140+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
141+
* @param executor 调度器具体行为
142+
*/
143+
@JvmOverloads
144+
fun Entity.submit(
145+
now: Boolean = false,
146+
async: Boolean = false,
147+
delay: Long = 0,
148+
period: Long = 0,
149+
useScheduler: Boolean = true,
150+
executor: PlatformExecutor.PlatformTask.() -> Unit,
151+
): PlatformExecutor.PlatformTask {
152+
return location.submit(now, async, delay, period, useScheduler, executor)
153+
}
154+
155+
// ============================================
156+
// Block 扩展函数
157+
// ============================================
158+
159+
/**
160+
* 在方块所在位置执行一个任务(Folia 安全)
161+
*
162+
* @param executor 任务
163+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
164+
*/
165+
@JvmOverloads
166+
fun Block.runTask(executor: Runnable, useScheduler: Boolean = true): PlatformExecutor.PlatformTask {
167+
return location.runTask(executor, useScheduler)
168+
}
169+
170+
/**
171+
* 在方块所在位置注册一个调度器(Folia 安全)
172+
*
173+
* @param now 是否立即执行
174+
* @param async 是否异步执行
175+
* @param delay 延迟执行时间(tick)
176+
* @param period 重复执行时间(tick)
177+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
178+
* @param executor 调度器具体行为
179+
*/
180+
@JvmOverloads
181+
fun Block.submit(
182+
now: Boolean = false,
183+
async: Boolean = false,
184+
delay: Long = 0,
185+
period: Long = 0,
186+
useScheduler: Boolean = true,
187+
executor: PlatformExecutor.PlatformTask.() -> Unit,
188+
): PlatformExecutor.PlatformTask {
189+
return location.submit(now, async, delay, period, useScheduler, executor)
190+
}
191+
192+
// ============================================
193+
// Chunk 扩展函数
194+
// ============================================
195+
196+
/**
197+
* 在区块中心位置执行一个任务(Folia 安全)
198+
*
199+
* @param executor 任务
200+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
201+
*/
202+
@JvmOverloads
203+
fun Chunk.runTask(executor: Runnable, useScheduler: Boolean = true): PlatformExecutor.PlatformTask {
204+
val location = Location(world, (x shl 4) + 8.0, 64.0, (z shl 4) + 8.0)
205+
return location.runTask(executor, useScheduler)
206+
}
207+
208+
/**
209+
* 在区块中心位置注册一个调度器(Folia 安全)
210+
*
211+
* @param now 是否立即执行
212+
* @param async 是否异步执行
213+
* @param delay 延迟执行时间(tick)
214+
* @param period 重复执行时间(tick)
215+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
216+
* @param executor 调度器具体行为
217+
*/
218+
@JvmOverloads
219+
fun Chunk.submit(
220+
now: Boolean = false,
221+
async: Boolean = false,
222+
delay: Long = 0,
223+
period: Long = 0,
224+
useScheduler: Boolean = true,
225+
executor: PlatformExecutor.PlatformTask.() -> Unit,
226+
): PlatformExecutor.PlatformTask {
227+
val location = Location(world, (x shl 4) + 8.0, 64.0, (z shl 4) + 8.0)
228+
return location.submit(now, async, delay, period, useScheduler, executor)
229+
}
230+
231+
// ============================================
232+
// World 扩展函数(坐标)
233+
// ============================================
234+
235+
/**
236+
* 在指定世界坐标执行一个任务(Folia 安全)
237+
*
238+
* @param x X 坐标
239+
* @param z Z 坐标
240+
* @param executor 任务
241+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
242+
*/
243+
@JvmOverloads
244+
fun World.runTask(x: Double, z: Double, executor: Runnable, useScheduler: Boolean = true): PlatformExecutor.PlatformTask {
245+
val location = Location(this, x, 64.0, z)
246+
return location.runTask(executor, useScheduler)
247+
}
248+
249+
/**
250+
* 在指定世界坐标注册一个调度器(Folia 安全)
251+
*
252+
* @param x X 坐标
253+
* @param z Z 坐标
254+
* @param now 是否立即执行
255+
* @param async 是否异步执行
256+
* @param delay 延迟执行时间(tick)
257+
* @param period 重复执行时间(tick)
258+
* @param useScheduler 在非 Folia 环境下是否使用调度器(默认 true)
259+
* @param executor 调度器具体行为
260+
*/
261+
@JvmOverloads
262+
fun World.submit(
263+
x: Double,
264+
z: Double,
265+
now: Boolean = false,
266+
async: Boolean = false,
267+
delay: Long = 0,
268+
period: Long = 0,
269+
useScheduler: Boolean = true,
270+
executor: PlatformExecutor.PlatformTask.() -> Unit,
271+
): PlatformExecutor.PlatformTask {
272+
val location = Location(this, x, 64.0, z)
273+
return location.submit(now, async, delay, period, useScheduler, executor)
274+
}

0 commit comments

Comments
 (0)