Skip to content

Commit

Permalink
Fix bug of null pointer exception when requesting event context (caus…
Browse files Browse the repository at this point in the history
…e by mistake method name 'arising', should be 'arise').

Fix bug of all events failures when no contexts registered.
Update gradle version to '8.11' .
Update dependencies versions.
Prepared to release new fix version.
  • Loading branch information
cao-awa committed Nov 16, 2024
1 parent f00d9c3 commit 02d7a7c
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 51 deletions.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}

Expand Down Expand Up @@ -97,7 +98,11 @@ java {

jar {
from("LICENSE") {
rename { "${it}" }
rename { "$it" }
}

from("README.md") {
rename { "$it" }
}

with copySpec({
Expand Down
58 changes: 58 additions & 0 deletions document/script/kotlin/README.md
Original file line number Diff line number Diff line change
@@ -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/<world>/datapacks/<pack-name>/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
}
```

91 changes: 91 additions & 0 deletions document/script/kotlin/event/README.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -34,15 +34,6 @@ request(
true
}

request(
PLACE_BLOCK,
SERVER_WORLD
) { _, world ->
println(world)

true
}

// Let blue bed explosion!
request(
USE_BLOCK,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/cao/awa/conium/Conium.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -44,7 +43,7 @@ import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromT
class ConiumScriptManager : SinglePreparationResourceReloader<MutableMap<Identifier, Resource>>() {
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"))
Expand Down
Loading

0 comments on commit 02d7a7c

Please sign in to comment.