Skip to content

Commit

Permalink
[6.0.11][publish] Experimental > fix kether
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Jun 26, 2023
1 parent 8d6e508 commit abb656a
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,55 @@ import taboolib.common5.Coerce
import java.util.concurrent.CompletableFuture

fun <T> CompletableFuture<Any?>.str(then: (String) -> T): CompletableFuture<T> {
return thenApply { then(it?.toString()?.trimIndent() ?: "") }
return thenApply { then(it?.toString()?.trimIndent() ?: "") }.except { then("") }
}

fun <T> CompletableFuture<Any?>.strOrNull(then: (String?) -> T): CompletableFuture<T> {
return thenApply { then(it?.toString()?.trimIndent()) }
return thenApply { then(it?.toString()?.trimIndent()) }.except { then(null) }
}

fun <T> CompletableFuture<Any?>.bool(then: (Boolean) -> T): CompletableFuture<T> {
return thenApply { then(Coerce.toBoolean(it)) }
return thenApply { then(Coerce.toBoolean(it)) }.except { then(false) }
}

fun <T> CompletableFuture<Any?>.int(then: (Int) -> T): CompletableFuture<T> {
return thenApply { then(Coerce.toInteger(it)) }
return thenApply { then(Coerce.toInteger(it)) }.except { then(0) }
}

fun <T> CompletableFuture<Any?>.long(then: (Long) -> T): CompletableFuture<T> {
return thenApply { then(Coerce.toLong(it)) }
return thenApply { then(Coerce.toLong(it)) }.except { then(0) }
}

fun <T> CompletableFuture<Any?>.double(then: (Double) -> T): CompletableFuture<T> {
return thenApply { then(Coerce.toDouble(it)) }
return thenApply { then(Coerce.toDouble(it)) }.except { then(0.0) }
}

fun <T> CompletableFuture<Any?>.float(then: (Float) -> T): CompletableFuture<T> {
return thenApply { then(Coerce.toFloat(it)) }
return thenApply { then(Coerce.toFloat(it)) }.except { then(0.0f) }
}

fun <T> CompletableFuture<T>.orNull(): T? {
return getNow(null)
}

fun <T> CompletableFuture<T>.except(): CompletableFuture<T> {
return exceptionally {
it.printStackTrace()
null
}
}

fun <T> CompletableFuture<T>.except(fn: (Throwable) -> T): CompletableFuture<T> {
return exceptionally {
it.printStackTrace()
fn(it)
}
}

fun <T> CompletableFuture<T>.exceptNull(fn: (Throwable) -> Unit): CompletableFuture<T> {
return exceptionally {
it.printStackTrace()
fn(it)
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import taboolib.library.kether.AbstractQuestContext
*/
open class ScriptContext(service: ScriptService, script: Script) : AbstractQuestContext<ScriptContext>(service, script, null) {

lateinit var id: String
var id = "null"

/** 脚本执行者 */
var sender: ProxyCommandSender?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package taboolib.module.kether.action

import taboolib.common.platform.function.warning
import taboolib.library.kether.ParsedAction
import taboolib.module.kether.Kether
import taboolib.module.kether.ScriptAction
Expand Down Expand Up @@ -47,7 +48,8 @@ object ActionProperty {
val future = CompletableFuture<Void>()
frame.newFrame(instance).run<Any>().thenApply { instance ->
if (instance == null) {
error("Property object must be not null.")
warning("Property object must be not null.")
future.complete(null)
}
frame.newFrame(value).run<Any?>().thenAccept close@{ value ->
val propertyList = getScriptProperty(instance)
Expand All @@ -58,7 +60,8 @@ object ActionProperty {
return@close
}
}
error("${instance.javaClass.simpleName}[$key] not supported yet.")
warning("${instance.javaClass.simpleName}[$key] not supported yet.")
future.complete(null)
}
}
return future
Expand All @@ -70,7 +73,8 @@ object ActionProperty {
override fun run(frame: ScriptFrame): CompletableFuture<Any?> {
return frame.newFrame(instance).run<Any>().thenApply {
if (it == null) {
error("Property object must be not null.")
warning("Property object must be not null.")
return@thenApply null
}
val propertyList = getScriptProperty(it)
for (property in propertyList) {
Expand All @@ -79,7 +83,8 @@ object ActionProperty {
return@thenApply result.value
}
}
error("${it.javaClass.simpleName}[$key] not supported yet.")
warning("${it.javaClass.simpleName}[$key] not supported yet.")
return@thenApply null
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package taboolib.module.kether.action
import taboolib.library.kether.ParsedAction
import taboolib.library.kether.QuestAction
import taboolib.library.kether.QuestContext
import taboolib.module.kether.KetherParser
import taboolib.module.kether.literalAction
import taboolib.module.kether.run
import taboolib.module.kether.scriptParser
import taboolib.module.kether.*
import java.util.concurrent.CompletableFuture

class ActionSet {
Expand All @@ -26,9 +23,7 @@ class ActionSet {
class ForAction(val key: String, val action: ParsedAction<*>) : QuestAction<Void>() {

override fun process(frame: QuestContext.Frame): CompletableFuture<Void> {
return frame.run(action).thenAccept {
frame.variables().set(key, it)
}
return frame.run(action).thenAccept { frame.variables().set(key, it) }.except()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ActionFor(val key: String, val values: ParsedAction<*>, val action: Parsed
} else {
process(cur + 1, i)
}
}
}.except { future.complete(null) }
} else {
frame.variables().also { v ->
v.remove(key)
Expand All @@ -49,7 +49,7 @@ class ActionFor(val key: String, val values: ParsedAction<*>, val action: Parsed
is Map<*, *> -> process(0, it.entries.toList())
else -> process(0, listOf(it))
}
}
}.except { future.complete(null) }
return future
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ class ActionJoin(val source: List<ParsedAction<*>>, val separator: ParsedAction<

override fun run(frame: ScriptFrame): CompletableFuture<String> {
val future = CompletableFuture<String>()
frame.newFrame(separator).run<Any>().thenAccept { separator ->
frame.run(separator).str { separator ->
val array = ArrayList<Any>()
fun process(cur: Int) {
if (cur < source.size) {
frame.newFrame(source[cur]).run<Any>().thenApply {
array.add(it)
if (frame.script().breakLoop) {
frame.script().breakLoop = false
future.complete(array.joinToString(separator.toString()))
future.complete(array.joinToString(separator))
} else {
process(cur + 1)
}
}
}.except { future.complete("") }
} else {
future.complete(array.joinToString(separator.toString()))
future.complete(array.joinToString(separator))
}
}
process(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ActionMap(val key: String, val values: ParsedAction<*>, val action: Parsed
} else {
process(cur + 1, i)
}
}
}.except { future.complete(result) }
} else {
frame.variables().also { v ->
v.remove(key)
Expand All @@ -53,7 +53,7 @@ class ActionMap(val key: String, val values: ParsedAction<*>, val action: Parsed
is Map<*, *> -> process(0, it.entries.toList())
else -> process(0, listOf(it))
}
}
}.except { future.complete(result) }
return future
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class ActionWhile(val condition: ParsedAction<*>, val action: ParsedAction<*>) :
} else {
process()
}
}
}.except { future.complete(null) }
} else {
future.complete(null)
}
}
}.except { future.complete(null) }
}
process()
return future
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package taboolib.module.kether.action.transform

import taboolib.common5.Coerce
import taboolib.common5.cint
import taboolib.library.kether.ArgTypes
import taboolib.library.kether.ParsedAction
import taboolib.module.kether.*
Expand All @@ -18,7 +19,7 @@ class ActionElement {
class SizeOf(val array: ParsedAction<*>) : ScriptAction<Int>() {

override fun run(frame: ScriptFrame): CompletableFuture<Int> {
return frame.newFrame(array).run<Any>().thenApply {
return frame.run(array).thenApply {
when (it) {
is Collection<*> -> it.size
is Array<*> -> it.size
Expand All @@ -32,12 +33,12 @@ class ActionElement {

override fun run(frame: ScriptFrame): CompletableFuture<Any?> {
val future = CompletableFuture<Any?>()
frame.newFrame(index).run<Any>().thenApply { index ->
frame.newFrame(array).run<Any>().thenApply {
frame.run(index).int { index ->
frame.run(array).thenApply {
future.complete(when (it) {
is Collection<*> -> it.toList().getOrNull(Coerce.toInteger(index))
is Array<*> -> it.toList().getOrNull(Coerce.toInteger(index))
else -> it.toString().getOrNull(Coerce.toInteger(index))
is Collection<*> -> it.toList().getOrNull(index)
is Array<*> -> it.toList().getOrNull(index)
else -> it.toString().getOrNull(index)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ActionMath(val type: Type, val array: List<ParsedAction<*>>) : ScriptActio
} else {
process(cur + 1)
}
}
}.except { future.complete(0) }
} else {
future.complete(type.exec(number))
}
Expand Down Expand Up @@ -128,17 +128,17 @@ class ActionMath(val type: Type, val array: List<ParsedAction<*>>) : ScriptActio
}
check()
if (stack.size > 1) {
actionFuture {
actionFuture { f ->
newFrame(stack[0].action).run<Any>().thenApply { base ->
var num = base.inferType() as Number
fun process(cur: Int) {
if (cur < stack.size) {
newFrame(stack[cur].action).run<Any>().thenApply { num2 ->
num = stack[cur].symbol!!.exec(listOf(num, num2))
process(cur + 1)
}
}.except { f.complete(0) }
} else {
it.complete(num)
f.complete(num)
}
}
process(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal object ActionWhen {
val caseBlock = blocks[cur]
// 默认块
if (caseBlock is CaseDefaultAction) {
run(caseBlock.action).thenAccept { r -> f.complete(r) }
run(caseBlock.action).thenAccept { r -> f.complete(r) }.exceptNull { f.complete(null) }
} else {
// 包含判断
if (caseBlock.checkType.multi) {
Expand All @@ -94,11 +94,11 @@ internal object ActionWhen {
}
CompletableFuture.allOf(*values).thenAccept {
if (caseBlock.checkType.check(input, if (values.size > 1) values.map { el -> el!!.get() } else values[0]!!.get())) {
run(caseBlock.action).thenAccept { r -> f.complete(r) }
run(caseBlock.action).thenAccept { r -> f.complete(r) }.exceptNull { f.complete(null) }
} else {
process(cur + 1)
}
}
}.exceptNull { f.complete(null) }
}
// 非包含则进行「或」判断
else {
Expand All @@ -108,19 +108,19 @@ internal object ActionWhen {
}
future.thenAccept { cond ->
if (cond) {
run(caseBlock.action).thenAccept { r -> f.complete(r) }
run(caseBlock.action).thenAccept { r -> f.complete(r) }.exceptNull { f.complete(null) }
} else {
process(cur + 1)
}
}
}.exceptNull { f.complete(null) }
}
}
} else {
f.complete(null)
}
}
process(0)
}
}.exceptNull { f.complete(null) }
}
}
}

0 comments on commit abb656a

Please sign in to comment.