-
-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db441bf
commit 8a9d1e9
Showing
6 changed files
with
176 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/kotlin/net/ccbluex/liquidbounce/script/bindings/api/ScriptAsyncUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce) | ||
* | ||
* Copyright (c) 2015 - 2025 CCBlueX | ||
* | ||
* LiquidBounce is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LiquidBounce is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package net.ccbluex.liquidbounce.script.bindings.api | ||
|
||
import net.ccbluex.liquidbounce.event.EventListener | ||
import net.ccbluex.liquidbounce.event.events.GameTickEvent | ||
import net.ccbluex.liquidbounce.event.handler | ||
import net.ccbluex.liquidbounce.script.ScriptApiRequired | ||
import net.ccbluex.liquidbounce.utils.client.mc | ||
import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention.FIRST_PRIORITY | ||
import org.graalvm.polyglot.Value | ||
import org.graalvm.polyglot.proxy.ProxyExecutable | ||
import java.util.function.BooleanSupplier | ||
|
||
/** | ||
* @author MukjepScarlet | ||
*/ | ||
class ScriptAsyncUtil( | ||
private val jsPromiseConstructor: Value | ||
) { | ||
|
||
companion object TickScheduler : EventListener { | ||
private val schedules = arrayListOf<BooleanSupplier>() | ||
|
||
@Suppress("unused") | ||
private val tickHandler = handler<GameTickEvent>(priority = FIRST_PRIORITY) { | ||
schedules.removeIf { it.asBoolean } | ||
} | ||
|
||
private fun schedule(breakLoop: BooleanSupplier) { | ||
mc.execute { schedules += breakLoop } | ||
} | ||
} | ||
|
||
private val defaultPromise: Value = jsPromiseConstructor.invokeMember("resolve", 0); | ||
|
||
/** | ||
* Example: `await ticks(10)` | ||
* | ||
* @return `Promise<number>` | ||
*/ | ||
@ScriptApiRequired | ||
fun ticks(n: Int): Value { | ||
if (n == 0) { | ||
return defaultPromise | ||
} | ||
|
||
var remains = n | ||
return until { --remains == 0 } | ||
} | ||
|
||
/** | ||
* Example: `await seconds(1)` | ||
* | ||
* @return `Promise<number>` | ||
*/ | ||
@ScriptApiRequired | ||
fun seconds(n: Int): Value = ticks(n * 20) | ||
|
||
/** | ||
* Example: `const duration = await until(() => mc.player.isOnGround())` | ||
* | ||
* @return `Promise<number>` | ||
*/ | ||
@ScriptApiRequired | ||
fun until(condition: BooleanSupplier): Value = jsPromiseConstructor.newInstance( | ||
ProxyExecutable { (onResolve, onReject) -> | ||
var waitingTick = 0 | ||
schedule { | ||
waitingTick++ | ||
try { | ||
if (condition.asBoolean) { | ||
onResolve.executeVoid(waitingTick) | ||
true | ||
} else { | ||
false | ||
} | ||
} catch (e: Throwable) { | ||
onReject.executeVoid(e) | ||
true | ||
} | ||
} | ||
} | ||
) | ||
|
||
/** | ||
* Example: `const result = await conditional(20, () => mc.player.isOnGround())` | ||
* | ||
* @return `Promise<number>` | ||
*/ | ||
@JvmOverloads | ||
@ScriptApiRequired | ||
fun conditional( | ||
ticks: Int, | ||
breakLoop: BooleanSupplier = BooleanSupplier { false } | ||
): Value { | ||
if (ticks == 0) { | ||
return defaultPromise | ||
} | ||
|
||
var remains = ticks | ||
return until { --remains == 0 || breakLoop.asBoolean } | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
src/main/kotlin/net/ccbluex/liquidbounce/script/bindings/async/JsSequenceHandler.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters