diff --git a/build.gradle b/build.gradle index 09beb0c..bfb5972 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ plugins { id "fabric-loom" version "1.8-SNAPSHOT" id "maven-publish" + id 'com.github.ben-manes.versions' version '+' id "org.jetbrains.kotlin.jvm" version "2.0.21" } @@ -97,7 +98,11 @@ java { jar { from("LICENSE") { - rename { "${it}" } + rename { "$it" } + } + + from("README.md") { + rename { "$it" } } with copySpec({ diff --git a/document/script/kotlin/README.md b/document/script/kotlin/README.md new file mode 100644 index 0000000..fc0484a --- /dev/null +++ b/document/script/kotlin/README.md @@ -0,0 +1,58 @@ +# Kotlin script +Conium supported to run kotlin script in game running, called to 'Conium script'. + +Directly write and put the '.kts' file to ```/saves//datapacks//data/script```. + +## Events +Conium script can handles game event used to complete what you need. + +Use ```request``` to subscribe an event, first parameter is the type of event, for all events, see [Event types](/document/script/kotlin/event/README.md#event-types). + +Then other parameters is the context args, for all args type, see [Context arg types](/document/script/kotlin/event/README.md#context-args). + +### Arising +``` kts +// Shorter schema. +request( + SERVER_TICK, + SERVER +) { _, server -> + // Do something here. + + // Here must return a boolean. + // True means this event is succeed, false then means failures. + true +} + +// Full schema. +// The 'arise' call be shorter as missing, but 'presage' cannot. +request( + SERVER_TICK, + SERVER +).arise { _, server -> + // Do something here. + + // Here must return a boolean. + // True means this event is succeed, false then means failures. + true +} +``` + +### Presaging + +``` kts +request( + SERVER_TICK, + SERVER +) { _, server -> + // Do something here. + true +}.presage { + // Here must return a boolean. + // The presaging is called before the event really happening, + // the whole event will be canceled when the result is false. + // (whole mean the event after events, like 'PLACED_BLOCK' is the after event of 'PLACE_BLOCK') + true +} +``` + diff --git a/document/script/kotlin/event/README.md b/document/script/kotlin/event/README.md new file mode 100644 index 0000000..f3d4469 --- /dev/null +++ b/document/script/kotlin/event/README.md @@ -0,0 +1,91 @@ +# Events in kotlin script + +## Event types + +| Key | Notes | +|------------------:|:-----------------------------------:| +| SERVER_TICK | Trigger in every server tick | +| ITEM_USE_ON_BLOCK | Trigger when an item use on a block | +| BREAK_BLOCK | Trigger when breaking block | +| PLACE_BLOCK | Trigger when placing block | +| PLACED_BLOCK | Trigger when block placed | +| USE_BLOCK | Trigger when using block | +| USED_BLOCK | Trigger when block used | + +## Context args + +Context args is the parameters starting from the second parameter in ```request```. + +For example: + +``` kts +request( + // This is the event type. + PLACE_BLOCK, + // This is the conext args. + SERVER_WORLD, + // This also. + BLOCK_POS, + // This also. + ITEM_STACK +) { _, world, pos, stack -> + // When you defines how many context args in 'request', + // then you must defines they in arising context as consistent order and quantity. + true +} + +request( + // This is the event type. + PLACE_BLOCK, + // This is the conext args. + SERVER_WORLD, + // This also. + ITEM_STACK +) { _, world, stack -> + // Here missing 'BLOCK_POS', so arising context also must missing it. + // Arising context should match to 'request' required args. + true +} +``` + +## Dynamic args + +If you are reading the source code, maybe you take seen some args doesn't pushed to ```ConiumEventContext```,\ +but the required args still able to get in scripts. + +This is the ```DynamicArgs``` transform(or adapter) mechanism, dynamic args use other presenting args try to found required arg. + +For details, see [ConiumEventArgTypes](/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt) and [DynamicArgsBuilder#transform](/src/main/java/com/github/cao/awa/conium/parameter/DynamicArgsBuilder.kt). + +If required arg is still unable to found when the dynamic args for-each to all other args and runs all transform presets,\ +then this ```request``` of this event will not be arising, because the parameters of arising and presaging don't receive null value. + +Avoid the trouble of guessing yourself, all args possible to uses for every event is here, \ +if you are finding not rarely used parameters, then you need read the ```ConiumEventArgTypes```. + +### SERVER_TICK + +SERVER + +### ITEM_USE_ON_BLOCK + +### BREAK_BLOCK + +### PLACE_BLOCK + +```ITEM_PLACEMENT_CONTEXT``` () + +```WORLD``` (transform from ```ITEM_PLACEMENT_CONTEXT```) \ +```SERVER_WORLD``` (transform from ```ITEM_PLACEMENT_CONTEXT```) \ +```CLIENT_WORLD``` (transform from ```ITEM_PLACEMENT_CONTEXT```) \ +```ITEM_STACK``` (transform from ```ITEM_PLACEMENT_CONTEXT```) \ +```PLAYER``` (transform from ```ITEM_PLACEMENT_CONTEXT```) \ +```SERVER_PLAYER``` (transform from ```ITEM_PLACEMENT_CONTEXT```) \ +```CLIENT_PLAYER``` (transform from ```ITEM_PLACEMENT_CONTEXT```) \ +```BLOCK_POS``` (transform from ```ITEM_PLACEMENT_CONTEXT```) + +### PLACED_BLOCK + +### USE_BLOCK + +### USED_BLOCK diff --git a/gradle.properties b/gradle.properties index 170ef0b..381af73 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ # Done to increase the memory available to gradle. org.gradle.jvmargs = -Xmx4G # Mod Properties - mod_version = 1.0.0-alpha5 + mod_version = 1.0.0-alpha5-fix1 maven_group = com.github.cao.awa archives_base_name = conium # Fabric Properties @@ -12,7 +12,7 @@ kotlin_loader_version = 1.12.3+kotlin.2.0.21 # Dependencies # check this on https://modmuss50.me/fabric.html - fabric_version = 0.107.0+1.21.3 + fabric_version = 0.108.0+1.21.3 # Kotlin kotlin_version = 2.0.21 # Language translator diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index df97d72..94113f2 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/sample/datapacks/tests/data/awa/scripts/conium_fools_day.kts b/sample/datapacks/tests/data/awa/script/conium_fools_day.kts similarity index 92% rename from sample/datapacks/tests/data/awa/scripts/conium_fools_day.kts rename to sample/datapacks/tests/data/awa/script/conium_fools_day.kts index f4d11e0..73346a1 100644 --- a/sample/datapacks/tests/data/awa/scripts/conium_fools_day.kts +++ b/sample/datapacks/tests/data/awa/script/conium_fools_day.kts @@ -3,12 +3,12 @@ import net.minecraft.server.network.ServerPlayerEntity import java.util.* import net.minecraft.util.math.Vec3d -val random = Random() - request( SERVER_TICK, SERVER ) { _, server -> + val random = Random() + server.worlds.forEach { world -> world.iterateEntities().forEach { entity -> if (entity != null && entity !is ServerPlayerEntity) { @@ -34,15 +34,6 @@ request( true } -request( - PLACE_BLOCK, - SERVER_WORLD -) { _, world -> - println(world) - - true -} - // Let blue bed explosion! request( USE_BLOCK, diff --git a/sample/datapacks/tests/data/awa/scripts/test.ts b/sample/datapacks/tests/data/awa/script/test.ts similarity index 100% rename from sample/datapacks/tests/data/awa/scripts/test.ts rename to sample/datapacks/tests/data/awa/script/test.ts diff --git a/src/main/java/com/github/cao/awa/conium/Conium.kt b/src/main/java/com/github/cao/awa/conium/Conium.kt index 4f94441..5c6ad41 100644 --- a/src/main/java/com/github/cao/awa/conium/Conium.kt +++ b/src/main/java/com/github/cao/awa/conium/Conium.kt @@ -26,7 +26,7 @@ class Conium : ModInitializer { val isClient: Boolean get() = ConiumClient.initialized @JvmField - var VERSION = "1.0.0-alpha5" + var VERSION = "1.0.0-alpha5-fix1" @JvmField var LANGUAGE_TRANSLATOR_VERSION = LanguageTranslator.getVersion() diff --git a/src/main/java/com/github/cao/awa/conium/datapack/script/ConiumScriptManager.kt b/src/main/java/com/github/cao/awa/conium/datapack/script/ConiumScriptManager.kt index 08015fb..58a30d0 100644 --- a/src/main/java/com/github/cao/awa/conium/datapack/script/ConiumScriptManager.kt +++ b/src/main/java/com/github/cao/awa/conium/datapack/script/ConiumScriptManager.kt @@ -1,7 +1,6 @@ package com.github.cao.awa.conium.datapack.script import com.github.cao.awa.conium.Conium -import com.github.cao.awa.conium.bedrock.event.context.BedrockEventContext import com.github.cao.awa.conium.event.ConiumEvent import com.github.cao.awa.conium.registry.ConiumRegistryKeys import com.github.cao.awa.conium.script.ScriptExport @@ -44,7 +43,7 @@ import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromT class ConiumScriptManager : SinglePreparationResourceReloader>() { companion object { private val LOGGER: Logger = LogManager.getLogger("ConiumScriptManager") - private val DATA_TYPE = RegistryKeys.getPath(ConiumRegistryKeys.SCRIPTS) + private val DATA_TYPE = RegistryKeys.getPath(ConiumRegistryKeys.SCRIPT) // Commons script here, all script uses theses script. private val defaultCommons = IOUtil.read(ResourceLoader.get("assets/conium/scripts/conium.commons.kts")) diff --git a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt index e0b668e..a96e176 100644 --- a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt +++ b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt @@ -92,15 +92,15 @@ object ConiumEventContextBuilder { @SuppressWarnings("UNCHECKED_CAST") fun request( eventType: ConiumEventType, - arising: ParameterSelective1 = ParameterSelective1 { _ -> false } + arising: ParameterSelective1 = ParameterSelective1 { _ -> true } ): ConiumEventContext> { val context = ConiumEventContext( - DynamicArgsBuilder.requires(false).lifecycle(DynamicArgsLifecycle.FOREVER) + DynamicArgsBuilder.requires(true).lifecycle(DynamicArgsLifecycle.FOREVER) ) ConiumEvent.forever(eventType, context) - context.arising(arising) + context.arise(arising) return context } @@ -110,18 +110,18 @@ object ConiumEventContextBuilder { fun request( eventType: ConiumEventType, arg1: DynamicArgType, - arising: ParameterSelective2 = ParameterSelective2 { _, _ -> false } + arising: ParameterSelective2 = ParameterSelective2 { _, _ -> true } ): ConiumEventContext> { val context = ConiumEventContext( DynamicArgsBuilder.requires( arg1, - false + true ).lifecycle(DynamicArgsLifecycle.FOREVER) ) ConiumEvent.forever(eventType, context) - context.arising(arising) + context.arise(arising) return context } @@ -132,19 +132,19 @@ object ConiumEventContextBuilder { eventType: ConiumEventType, arg1: DynamicArgType, arg2: DynamicArgType, - arising: ParameterSelective3 = ParameterSelective3 { _, _, _ -> false } + arising: ParameterSelective3 = ParameterSelective3 { _, _, _ -> true } ): ConiumEventContext> { val context = ConiumEventContext( DynamicArgsBuilder.requires( arg1, arg2, - false + true ).lifecycle(DynamicArgsLifecycle.FOREVER) ) ConiumEvent.forever(eventType, context) - context.arising(arising) + context.arise(arising) return context } @@ -156,20 +156,20 @@ object ConiumEventContextBuilder { arg1: DynamicArgType, arg2: DynamicArgType, arg3: DynamicArgType, - arising: ParameterSelective4 = ParameterSelective4 { _, _, _, _ -> false } + arising: ParameterSelective4 = ParameterSelective4 { _, _, _, _ -> true } ): ConiumEventContext> { val context = ConiumEventContext( DynamicArgsBuilder.requires( arg1, arg2, arg3, - false + true ).lifecycle(DynamicArgsLifecycle.FOREVER) ) ConiumEvent.forever(eventType, context) - context.arising(arising) + context.arise(arising) return context } @@ -182,7 +182,7 @@ object ConiumEventContextBuilder { arg2: DynamicArgType, arg3: DynamicArgType, arg4: DynamicArgType, - arising: ParameterSelective5 = ParameterSelective5 { _, _, _, _, _ -> false } + arising: ParameterSelective5 = ParameterSelective5 { _, _, _, _, _ -> true } ): ConiumEventContext> { val context = ConiumEventContext( DynamicArgsBuilder.requires( @@ -190,13 +190,13 @@ object ConiumEventContextBuilder { arg2, arg3, arg4, - false + true ).lifecycle(DynamicArgsLifecycle.FOREVER) ) ConiumEvent.forever(eventType, context) - context.arising(arising) + context.arise(arising) return context } @@ -210,7 +210,7 @@ object ConiumEventContextBuilder { arg3: DynamicArgType, arg4: DynamicArgType, arg5: DynamicArgType, - arising: ParameterSelective6 = ParameterSelective6 { _, _, _, _, _, _ -> false } + arising: ParameterSelective6 = ParameterSelective6 { _, _, _, _, _, _ -> true } ): ConiumEventContext> { val context = ConiumEventContext( DynamicArgsBuilder.requires( @@ -219,13 +219,13 @@ object ConiumEventContextBuilder { arg3, arg4, arg5, - false + true ).lifecycle(DynamicArgsLifecycle.FOREVER) ) ConiumEvent.forever(eventType, context) - context.arising(arising) + context.arise(arising) return context } @@ -240,7 +240,7 @@ object ConiumEventContextBuilder { arg4: DynamicArgType, arg5: DynamicArgType, arg6: DynamicArgType, - arising: ParameterSelective7 = ParameterSelective7 { _, _, _, _, _, _, _ -> false } + arising: ParameterSelective7 = ParameterSelective7 { _, _, _, _, _, _, _ -> true } ): ConiumEventContext> { val context = ConiumEventContext( DynamicArgsBuilder.requires( @@ -250,13 +250,13 @@ object ConiumEventContextBuilder { arg4, arg5, arg6, - false + true ).lifecycle(DynamicArgsLifecycle.FOREVER) ) ConiumEvent.forever(eventType, context) - context.arising(arising) + context.arise(arising) return context } @@ -272,7 +272,7 @@ object ConiumEventContextBuilder { arg5: DynamicArgType, arg6: DynamicArgType, arg7: DynamicArgType, - arising: ParameterSelective8 = ParameterSelective8 { _, _, _, _, _, _, _, _ -> false } + arising: ParameterSelective8 = ParameterSelective8 { _, _, _, _, _, _, _, _ -> true } ): ConiumEventContext> { val context = ConiumEventContext( DynamicArgsBuilder.requires( @@ -283,13 +283,13 @@ object ConiumEventContextBuilder { arg5, arg6, arg7, - false + true ).lifecycle(DynamicArgsLifecycle.FOREVER) ) ConiumEvent.forever(eventType, context) - context.arising(arising) + context.arise(arising) return context } diff --git a/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt b/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt index bdc75bf..38f9bdf 100644 --- a/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt +++ b/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt @@ -62,13 +62,18 @@ object ConiumEventArgTypes { ) @JvmField - val BLOCK_STATE: DynamicArgType = arg("block_state") + val BLOCK_STATE: DynamicArgType = arg( + "block_state" + ) @JvmField val BLOCK_HIT_RESULT: DynamicArgType = arg("block_hit_result") @JvmField - val PLAYER: DynamicArgType = arg("player") + val PLAYER: DynamicArgType = arg( + "player", + DynamicArgsBuilder.transform(ITEM_PLACEMENT_CONTEXT) { placement -> placement.player } + ) @JvmField val LIVING_ENTITY: DynamicArgType = arg("living_entity") diff --git a/src/main/java/com/github/cao/awa/conium/mixin/block/BlockItemMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/block/BlockItemMixin.java index f46f8af..ca092bf 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/block/BlockItemMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/block/BlockItemMixin.java @@ -24,7 +24,7 @@ public class BlockItemMixin { @Inject( method = "place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;", - at = @At(value = "HEAD"), + at = @At("HEAD"), cancellable = true ) public void placeBlock(ItemPlacementContext context, CallbackInfoReturnable cir) { @@ -37,7 +37,7 @@ public void placeBlock(ItemPlacementContext context, CallbackInfoReturnable( fun arising(identity: Any, args: MutableMap, Any?>, p: P?): R { if (p == null) { - return this.trigger.apply(identity, args, p) + return this.trigger.apply(identity, args, null) } for (queryArg in this.queryArgs) { diff --git a/src/main/java/com/github/cao/awa/conium/registry/ConiumRegistryKeys.kt b/src/main/java/com/github/cao/awa/conium/registry/ConiumRegistryKeys.kt index 5a9f03c..abeca10 100644 --- a/src/main/java/com/github/cao/awa/conium/registry/ConiumRegistryKeys.kt +++ b/src/main/java/com/github/cao/awa/conium/registry/ConiumRegistryKeys.kt @@ -4,6 +4,7 @@ import com.github.cao.awa.conium.block.builder.ConiumBlockBuilder import com.github.cao.awa.conium.datapack.inject.item.ItemPropertyInject import com.github.cao.awa.conium.entity.builder.ConiumEntityBuilder import com.github.cao.awa.conium.item.builder.ConiumItemBuilder +import com.github.cao.awa.conium.script.eval.ScriptEval import net.minecraft.registry.Registry import net.minecraft.registry.RegistryKey import net.minecraft.util.Identifier @@ -12,7 +13,7 @@ object ConiumRegistryKeys { @JvmStatic val ITEM_PROPERTY_INJECT: RegistryKey>> = of("property/item") @JvmStatic - val SCRIPTS: RegistryKey>> = of("scripts") + val SCRIPT: RegistryKey> = of("script") @JvmStatic val ITEM: RegistryKey> = of("item") @JvmStatic diff --git a/src/main/resources/META-INF/services/javax.script.ScriptEngineFactory b/src/main/resources/META-INF/services/javax.script.ScriptEngineFactory deleted file mode 100644 index 5c81500..0000000 --- a/src/main/resources/META-INF/services/javax.script.ScriptEngineFactory +++ /dev/null @@ -1,2 +0,0 @@ -org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory -org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index b4f5546..b15c202 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -35,11 +35,19 @@ "depends": { "fluxia": "1.0.10", "fabricloader": "*", - "fabric": "*", "fabric-language-kotlin": "1.12.3+kotlin.2.0.21", "minecraft": "1.21.3", "java": "21" }, + "recommends": { + "fabric": "*", + "lithium": "*", + "sepals": "*", + "sodium": "*" + }, + "breaks": { + "optifabric": "*" + }, "jars": [ { "file": "META-INF/jars/fluxia-1.0.10.jar"