From ced809d2fb58d60c9ea35f90c132336a55557221 Mon Sep 17 00:00:00 2001 From: Lukas <113579672+byPixelTV@users.noreply.github.com> Date: Fri, 12 Apr 2024 21:31:33 +0200 Subject: [PATCH] Fixed some bugs with the Lifecycle conditions --- .../services/CondCloudnetServiceDeleted.kt | 58 ++++++++++++++++++ .../services/CondCloudnetServicePrepared.kt | 58 ++++++++++++++++++ .../services/CondCloudnetServiceRunning.kt | 11 ++-- .../services/CondCloudnetServiceStopping.kt | 58 ++++++++++++++++++ .../services/ExprAllCloudnetServices.kt | 2 +- .../ExprAllRunningCloudnetServices.kt | 60 +++++++++++++++++++ 6 files changed, 242 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceDeleted.kt create mode 100644 src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServicePrepared.kt create mode 100644 src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceStopping.kt create mode 100644 src/main/kotlin/de/bypixeltv/skcloudnet/elements/expressions/services/ExprAllRunningCloudnetServices.kt diff --git a/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceDeleted.kt b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceDeleted.kt new file mode 100644 index 0000000..66a2098 --- /dev/null +++ b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceDeleted.kt @@ -0,0 +1,58 @@ +package de.bypixeltv.skcloudnet.elements.conditions.services + +import ch.njol.skript.Skript +import ch.njol.skript.doc.Description +import ch.njol.skript.doc.Examples +import ch.njol.skript.doc.Name +import ch.njol.skript.doc.Since +import ch.njol.skript.lang.Condition +import ch.njol.skript.lang.Expression +import ch.njol.skript.lang.SkriptParser +import ch.njol.util.Kleenean +import eu.cloudnetservice.driver.inject.InjectionLayer +import eu.cloudnetservice.driver.provider.CloudServiceProvider +import org.bukkit.event.Event + + +@Name("CloudNet Service Deleted") +@Description("Returns if a CloudNet service is deleted or not") +@Examples("if cloudnet service \"Lobby-1\" is deleted: send \"Lobby-1 is deleted\"") +@Since("1.1") + +class CondCloudnetServiceDeleted : Condition() { + + val cnServiceProvider: CloudServiceProvider = InjectionLayer.ext().instance(CloudServiceProvider::class.java) + + companion object{ + init { + Skript.registerCondition(CondCloudnetServiceDeleted::class.java, "[lifecycle] [of] [cloudnet] service %string% (1¦is|2¦is(n't| not)) deleted") + } + } + + private var service: Expression? = null + + override fun init( + expressions: Array>, + matchedPattern: Int, + isDelayed: Kleenean?, + parser: SkriptParser.ParseResult + ): Boolean { + this.service = expressions[0] as Expression? + isNegated = parser.mark === 1 + return true + } + + override fun check(e: Event?): Boolean { + val service = service?.getSingle(e) ?: return isNegated + return if (cnServiceProvider.serviceByName(service)?.lifeCycle()?.name == "DELETED") { + !isNegated + } else { + isNegated + } + } + + override fun toString(e: Event?, debug: Boolean): String { + return "${service.toString()} is deleted" + } + +} \ No newline at end of file diff --git a/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServicePrepared.kt b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServicePrepared.kt new file mode 100644 index 0000000..c31442e --- /dev/null +++ b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServicePrepared.kt @@ -0,0 +1,58 @@ +package de.bypixeltv.skcloudnet.elements.conditions.services + +import ch.njol.skript.Skript +import ch.njol.skript.doc.Description +import ch.njol.skript.doc.Examples +import ch.njol.skript.doc.Name +import ch.njol.skript.doc.Since +import ch.njol.skript.lang.Condition +import ch.njol.skript.lang.Expression +import ch.njol.skript.lang.SkriptParser +import ch.njol.util.Kleenean +import eu.cloudnetservice.driver.inject.InjectionLayer +import eu.cloudnetservice.driver.provider.CloudServiceProvider +import org.bukkit.event.Event + + +@Name("CloudNet Service Prepared") +@Description("Returns if a CloudNet service is prepared or not") +@Examples("if cloudnet service \"Lobby-1\" is prepared: send \"Lobby-1 is prepared\"") +@Since("1.1") + +class CondCloudnetServicePrepared : Condition() { + + val cnServiceProvider: CloudServiceProvider = InjectionLayer.ext().instance(CloudServiceProvider::class.java) + + companion object{ + init { + Skript.registerCondition(CondCloudnetServicePrepared::class.java, "[lifecycle] [of] [cloudnet] service %string% (1¦is|2¦is(n't| not)) prepared") + } + } + + private var service: Expression? = null + + override fun init( + expressions: Array>, + matchedPattern: Int, + isDelayed: Kleenean?, + parser: SkriptParser.ParseResult + ): Boolean { + this.service = expressions[0] as Expression? + isNegated = parser.mark === 1 + return true + } + + override fun check(e: Event?): Boolean { + val service = service?.getSingle(e) ?: return isNegated + return if (cnServiceProvider.serviceByName(service)?.lifeCycle()?.name == "PREPARED") { + !isNegated + } else { + isNegated + } + } + + override fun toString(e: Event?, debug: Boolean): String { + return "${service.toString()} is prepared" + } + +} \ No newline at end of file diff --git a/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceRunning.kt b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceRunning.kt index ba8c800..aaac322 100644 --- a/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceRunning.kt +++ b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceRunning.kt @@ -25,7 +25,7 @@ class CondCloudnetServiceRunning : Condition() { companion object{ init { - Skript.registerCondition(CondCloudnetServiceRunning::class.java, "[cloudnet] service %string% (1¦is|2¦is(n't| not)) running") + Skript.registerCondition(CondCloudnetServiceRunning::class.java, "[lifecycle] [of] [cloudnet] service %string% (1¦is|2¦is(n't| not)) (running|started)") } } @@ -43,9 +43,12 @@ class CondCloudnetServiceRunning : Condition() { } override fun check(e: Event?): Boolean { - val service = service?.getSingle(e) - if (service == null) return isNegated - return if (cnServiceProvider.runningServices().any { it.name() == service }) isNegated else !isNegated + val service = service?.getSingle(e) ?: return isNegated + return if (cnServiceProvider.serviceByName(service)?.lifeCycle()?.name == "RUNNING") { + !isNegated + } else { + isNegated + } } override fun toString(e: Event?, debug: Boolean): String { diff --git a/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceStopping.kt b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceStopping.kt new file mode 100644 index 0000000..5f49253 --- /dev/null +++ b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/conditions/services/CondCloudnetServiceStopping.kt @@ -0,0 +1,58 @@ +package de.bypixeltv.skcloudnet.elements.conditions.services + +import ch.njol.skript.Skript +import ch.njol.skript.doc.Description +import ch.njol.skript.doc.Examples +import ch.njol.skript.doc.Name +import ch.njol.skript.doc.Since +import ch.njol.skript.lang.Condition +import ch.njol.skript.lang.Expression +import ch.njol.skript.lang.SkriptParser +import ch.njol.util.Kleenean +import eu.cloudnetservice.driver.inject.InjectionLayer +import eu.cloudnetservice.driver.provider.CloudServiceProvider +import org.bukkit.event.Event + + +@Name("CloudNet Service Stopping") +@Description("Returns if a CloudNet service is stopping or not") +@Examples("if cloudnet service \"Lobby-1\" is stopping: send \"Lobby-1 is stopping\"") +@Since("1.1") + +class CondCloudnetServiceStopping : Condition() { + + val cnServiceProvider: CloudServiceProvider = InjectionLayer.ext().instance(CloudServiceProvider::class.java) + + companion object{ + init { + Skript.registerCondition(CondCloudnetServiceStopping::class.java, "[lifecycle] [of] [cloudnet] service %string% (1¦is|2¦is(n't| not)) stopping") + } + } + + private var service: Expression? = null + + override fun init( + expressions: Array>, + matchedPattern: Int, + isDelayed: Kleenean?, + parser: SkriptParser.ParseResult + ): Boolean { + this.service = expressions[0] as Expression? + isNegated = parser.mark === 1 + return true + } + + override fun check(e: Event?): Boolean { + val service = service?.getSingle(e) ?: return isNegated + return if (cnServiceProvider.serviceByName(service)?.lifeCycle()?.name == "STOPPING") { + !isNegated + } else { + isNegated + } + } + + override fun toString(e: Event?, debug: Boolean): String { + return "${service.toString()} is stopping" + } + +} \ No newline at end of file diff --git a/src/main/kotlin/de/bypixeltv/skcloudnet/elements/expressions/services/ExprAllCloudnetServices.kt b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/expressions/services/ExprAllCloudnetServices.kt index af4c3a1..907793d 100644 --- a/src/main/kotlin/de/bypixeltv/skcloudnet/elements/expressions/services/ExprAllCloudnetServices.kt +++ b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/expressions/services/ExprAllCloudnetServices.kt @@ -28,7 +28,7 @@ class ExprAllCloudnetServices : SimpleExpression() { init { Skript.registerExpression( ExprAllCloudnetServices::class.java, String::class.java, - ExpressionType.SIMPLE, "[(all [[of] the]|the)] [running] cloudnet services") + ExpressionType.SIMPLE, "[(all [[of] the]|the)] [cloudnet] services") } } diff --git a/src/main/kotlin/de/bypixeltv/skcloudnet/elements/expressions/services/ExprAllRunningCloudnetServices.kt b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/expressions/services/ExprAllRunningCloudnetServices.kt new file mode 100644 index 0000000..4c4c63d --- /dev/null +++ b/src/main/kotlin/de/bypixeltv/skcloudnet/elements/expressions/services/ExprAllRunningCloudnetServices.kt @@ -0,0 +1,60 @@ +package de.bypixeltv.skcloudnet.elements.expressions.services + +import ch.njol.skript.Skript +import ch.njol.skript.doc.Description +import ch.njol.skript.doc.Examples +import ch.njol.skript.doc.Name +import ch.njol.skript.doc.Since +import ch.njol.skript.lang.Expression +import ch.njol.skript.lang.ExpressionType +import ch.njol.skript.lang.SkriptParser +import ch.njol.skript.lang.util.SimpleExpression +import ch.njol.util.Kleenean +import eu.cloudnetservice.driver.inject.InjectionLayer +import eu.cloudnetservice.driver.provider.CloudServiceProvider +import org.bukkit.event.Event + + +@Name("All Running Services") +@Description("Returns all running CloudNet services") +@Examples("loop all cloudnet services:\n" + "\tsend \"%loop-value%\"") +@Since("1.0") + +class ExprAllRunningCloudnetServices : SimpleExpression() { + + private val cnServiceProvider: CloudServiceProvider = InjectionLayer.ext().instance(CloudServiceProvider::class.java) + + companion object{ + init { + Skript.registerExpression( + ExprAllRunningCloudnetServices::class.java, String::class.java, + ExpressionType.SIMPLE, "[(all [[of] the]|the)] running [cloudnet] services") + } + } + + override fun isSingle(): Boolean { + return false + } + + override fun init( + exprs: Array?>?, + matchedPattern: Int, + isDelayed: Kleenean?, + parseResult: SkriptParser.ParseResult? + ): Boolean { + return true + } + + override fun get(e: Event?): Array { + return cnServiceProvider.runningServices().map { it.name() }.toTypedArray() + } + + override fun getReturnType(): Class { + return String::class.java + } + + override fun toString(e: Event?, debug: Boolean): String { + return "all running cloudnet services" + } + +} \ No newline at end of file