Skip to content

Commit

Permalink
Merge pull request #126 from Rubenicos/feature/function-action
Browse files Browse the repository at this point in the history
Add function action
  • Loading branch information
YsGqHY authored Jan 14, 2025
2 parents dd1add7 + f6206c7 commit aa2b9a7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import taboolib.common.platform.function.submit
import taboolib.library.reflex.Reflex.Companion.invokeConstructor
import trplugins.menu.api.action.base.ActionBase
import trplugins.menu.api.action.base.ActionEntry
import trplugins.menu.api.action.impl.logic.Break
import trplugins.menu.api.action.base.ActionEval
import trplugins.menu.api.action.impl.logic.Delay
import trplugins.menu.util.ClassUtils
import trplugins.menu.util.EvalResult
import java.util.concurrent.atomic.AtomicBoolean
import java.util.function.BiFunction

/**
Expand Down Expand Up @@ -71,6 +72,7 @@ class ActionHandle(
val run = mutableListOf<ActionEntry>()
var result = true
var delay = 0L
val allowed = AtomicBoolean(true)

run filter@{
while (actions.hasNext()) {
Expand All @@ -79,12 +81,22 @@ class ActionHandle(
continue
}
when {
action.base is Break && action.option.evalCondition(player) -> {
result = false
return@filter
action.base is ActionEval -> {
if (delay > 0) {
submit(delay = delay) {
allowed.set(action.eval(player))
}
} else if (!action.eval(player)) {
result = false
return@filter
}
}
action.base is Delay -> delay += action.base.getDelay(player, action.contents.stringContent())
delay > 0 -> submit(delay = delay) { action.execute(player) }
delay > 0 -> submit(delay = delay) {
if (allowed.get()) {
action.execute(player)
}
}
else -> run.add(action)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ data class ActionEntry(
else proceed.invoke()
}

fun eval(player: ProxyPlayer): Boolean {
var result = true
if (option.evalCondition(player)) {
option.evalPlayers(player) {
if (!(base as ActionEval).onEval(contents, it, player)) {
result = false
}
}
}
return result
}


companion object {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package trplugins.menu.api.action.base

import taboolib.common.platform.ProxyPlayer

/**
* @author Rubenicos
* @date 2024/11/21 14:42
*/
interface ActionEval {

fun onEval(contents: ActionContents, player: ProxyPlayer, placeholderPlayer: ProxyPlayer = player): Boolean
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package trplugins.menu.api.action.impl.logic

import taboolib.common.platform.ProxyPlayer
import trplugins.menu.api.action.ActionHandle
import trplugins.menu.api.action.base.ActionBase
import trplugins.menu.api.action.base.ActionContents
import trplugins.menu.api.action.base.ActionEval

/**
* TrMenu
Expand All @@ -10,6 +13,10 @@ import trplugins.menu.api.action.base.ActionBase
* @author Score2
* @since 2022/02/10 22:09
*/
class Break(handle: ActionHandle) : ActionBase(handle) {
class Break(handle: ActionHandle) : ActionBase(handle), ActionEval {
override val regex = "return|break".toRegex()

override fun onEval(contents: ActionContents, player: ProxyPlayer, placeholderPlayer: ProxyPlayer): Boolean {
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package trplugins.menu.api.action.impl.script

import taboolib.common.platform.ProxyPlayer
import taboolib.common.util.subList
import trplugins.menu.api.action.ActionHandle
import trplugins.menu.api.action.base.ActionBase
import trplugins.menu.api.action.base.ActionContents
import trplugins.menu.api.action.base.ActionEval
import trplugins.menu.module.display.session
import trplugins.menu.util.Regexs

/**
* @author Rubenicos
* @date 2024/11/21 14:42
*/
class Function(handle: ActionHandle) : ActionBase(handle), ActionEval {

override val regex = "(run-?)?functions?|run".toRegex()

override fun onExecute(contents: ActionContents, player: ProxyPlayer, placeholderPlayer: ProxyPlayer) {
onEval(contents, player, placeholderPlayer)
}

override fun onEval(contents: ActionContents, player: ProxyPlayer, placeholderPlayer: ProxyPlayer): Boolean {
val session = player.session()
val menu = session.menu ?: return true
val func = contents.stringContent().parseContent(placeholderPlayer).split(' ')

menu.settings.internalFunctions.forEach {
if (it.id == func[0]) {
val args = subList(func, 1, func.size)
return !it.compile(session, args).asString().lowercase().matches(Regexs.FALSE)
}
}
return true
}

}

0 comments on commit aa2b9a7

Please sign in to comment.