Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
完善脚本调用问题
Browse files Browse the repository at this point in the history
//TODO event 调用待优化
  • Loading branch information
EikoocS committed Jun 21, 2022
1 parent aa26462 commit d375a98
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ object ScriptManager {
throw IllegalArgumentException("Can't load ${script.path} Script with namespaced key $namespacedKey " +
"already loaded by ${getScript(namespacedKey)?.path}")
}

val event = ScriptLoadEvent(script)
if(event.call()){ return }
if(!event.call()){
ScriptUnloadEvent(script).call()
return
}
scripts.add(script)
}

Expand All @@ -41,14 +45,13 @@ object ScriptManager {

fun unloadAll(){
scripts.forEach {
namespacedKeys.remove(it.namespacedKey)
ScriptUnloadEvent(it).call()
unload(it)
}
namespacedKeys.clear()
scripts.clear()
}

@Awake(LifeCycle.ENABLE)
@Awake(LifeCycle.ACTIVE)
fun reloadAll(){
unloadAll()
loadByDir()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ object AsyncModifier : Modifier(){
@SubscribeEvent(ignoreCancelled = true)
fun onLoad(event: ScriptLoadEvent){
val script = event.script
if (script.async && script.sync) throw IllegalArgumentException("Script cannot be async and sync at the same time")
event.isCancelled = true
if (script.async && script.sync){
event.isCancelled = true
throw IllegalArgumentException("Script cannot be async and sync at the same time")
}
}
@SubscribeEvent(ignoreCancelled = true)
fun onExecute(event: ScriptExecuteEvent){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tech.cookiepower.jythonengine.script.trigger

import org.bukkit.event.EventPriority
import org.bukkit.event.HandlerList
import org.bukkit.event.Listener
import org.bukkit.plugin.RegisteredListener
import taboolib.common.platform.function.submit
import taboolib.platform.BukkitPlugin

class EventListener : Listener {
private val handlers = ArrayList<HandlerList>()
private val listener = RegisteredListener( this,
{ _, event -> EventTrigger.onEventCall(event) },
EventPriority.NORMAL,
BukkitPlugin.getInstance(),
false
)
private var lastSize = 0
val task = submit(period = 20, async = true) {
if (lastSize != HandlerList.getHandlerLists().size){
HandlerList.getHandlerLists().filter {
it !in handlers
}.forEach {
it.register(listener)
handlers.add(it)
}
lastSize = HandlerList.getHandlerLists().size
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package tech.cookiepower.jythonengine.script.trigger

import org.bukkit.Bukkit
import org.bukkit.event.Event
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.Platform
import taboolib.common.platform.PlatformSide
import taboolib.common.platform.event.SubscribeEvent
import taboolib.platform.BukkitPlugin
import tech.cookiepower.jythonengine.annotation.ReflexCall
import tech.cookiepower.jythonengine.event.ScriptExecuteEvent
import tech.cookiepower.jythonengine.event.ScriptLoadEvent
import tech.cookiepower.jythonengine.event.ScriptUnloadEvent
import tech.cookiepower.jythonengine.script.Script
Expand All @@ -22,35 +30,27 @@ object EventTrigger : Trigger<Map<String, MutableList<Script>>>() {
}
}

@SubscribeEvent(ignoreCancelled = true)
@SubscribeEvent
fun onScriptUnload(event: ScriptUnloadEvent){
if(event.script.isEventScript){
onUnsubscribe(event.script)
}
}

// @SubscribeEvent
// fun onEventCall(event: Event){
// event.javaClass.name.let {
// event2Scripts[it]?.forEach { script ->
// val exeEvent = ScriptExecuteEvent(script,defaultInterpreter)
// exeEvent.argument("__EVENT__" to event)
// defaultHandlerAndPost(exeEvent)
// }
// }
// }
@Awake(LifeCycle.ACTIVE)
@PlatformSide([Platform.BUKKIT])
fun registerListener(){
Bukkit.getPluginManager().registerEvents(EventListener(), BukkitPlugin.getInstance())
}

// @SubscribeEvent(bind = "org.bukkit.event.Event")
// fun onEventCall(ope: OptionalEvent){
// val event = ope.get<Event>()
// event.javaClass.name.let {
// event2Scripts[it]?.forEach { script ->
// val exeEvent = ScriptExecuteEvent(script,defaultInterpreter)
// exeEvent.argument("__EVENT__" to event)
// defaultHandlerAndPost(exeEvent)
// }
// }
// }
fun onEventCall(event: Event){
val name = event.javaClass.name
event2Scripts[name]?.forEach { script ->
val exeEvent = ScriptExecuteEvent(script,defaultInterpreter)
exeEvent.argument("__EVENT__" to event)
defaultHandlerAndPost(exeEvent)
}
}

override fun onSubscribe(script: Script) {
val events = script.events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tech.cookiepower.jythonengine.script.trigger

import org.python.util.PythonInterpreter
import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.function.info
import tech.cookiepower.jythonengine.event.ScriptLoadEvent
import tech.cookiepower.jythonengine.event.ScriptUnloadEvent
import tech.cookiepower.jythonengine.script.Script
Expand All @@ -14,19 +13,13 @@ object SchedulerTrigger : Trigger<List<SchedulerTriggerTask>>(){

@SubscribeEvent(ignoreCancelled = true)
fun onScriptLoad(event: ScriptLoadEvent){
info("####################################################################################")
info("event subscriber: ScriptLoadEvent")
info("####################################################################################")
if(event.script.isSchedulerScript){
onSubscribe(event.script)
}
}

@SubscribeEvent(ignoreCancelled = true)
@SubscribeEvent
fun onScriptUnload(event: ScriptUnloadEvent){
info("####################################################################################")
info("event subscriber: ScriptUnloadEvent")
info("####################################################################################")
if(event.script.isSchedulerScript){
onUnsubscribe(event.script)
}
Expand All @@ -39,10 +32,11 @@ object SchedulerTrigger : Trigger<List<SchedulerTriggerTask>>(){
}

override fun onUnsubscribe(script: Script): Boolean {
val task = tasks.find { it.script == script }
task?.stop()
val task = tasks.find { it.script.path == script.path } ?:
throw IllegalStateException("Scheduler Script is not subscribed")
task.stop()
tasks.remove(task)
return task!=null
return true
}

override fun getAll(): List<SchedulerTriggerTask> = tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object StatusTrigger : Trigger<Unit>() {
}
}

@SubscribeEvent(ignoreCancelled = true)
@SubscribeEvent
fun onScriptUnload(event: ScriptUnloadEvent){
if(event.script.release){
onUnsubscribe(event.script)
Expand Down

0 comments on commit d375a98

Please sign in to comment.