Skip to content

Commit

Permalink
Update loom version to '1.9.2'.
Browse files Browse the repository at this point in the history
Add events support: 'ITEM_INVENTORY_TICK', 'ITEM_INVENTORY_TICKED', 'ITEM_STACK_CLICK', 'ITEM_STACK_CLICKED'.
Add context args: 'CURSOR_STACK', 'CLICK_TYPE', 'SLOT', 'SLOT_NUMBER', 'SELECT_STATUS'.
Update version id to '1.0.0-alpha7'.
Rename language translator to structuring translator.
Supporting to some javascript stdlib.
No longer using local variable to access bedrock system, importing now.
Upgrade gradle to '8.11.1'.
  • Loading branch information
cao-awa committed Dec 16, 2024
1 parent 079c6fe commit c7cb358
Show file tree
Hide file tree
Showing 24 changed files with 275 additions and 68 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Conium lets you complete your mods only using datapacks.
| Minecraft | 1.21.4!! | Need | Only 1.21.4 can be use |
| Fabric language kotlin | 1.13.0+kotlin.2.1.0!! | Need | Only kotlin 2.1.0 can be use |
| Fabric API | ? | Unnecessary | Any version is ok |
| Fluxia | 1.0.10!! | Unnecessary | Built-in contents translator |
| Fluxia | 1.1.0-fix2!! | Unnecessary | Built-in contents translator |

### Build requirements

Expand Down Expand Up @@ -41,7 +41,7 @@ Not completed bedrock script APIs supports now, only framework able to runs the

### Grammars

About typescript grammar, supported by ```language-translator``` or called ```fluxia```: [Typescript supports](https://github.com/cao-awa/language-translator/tree/main/doc/typescript)
About typescript grammar, supported by ```structring-translator``` or called ```fluxia```: [Typescript supports](https://github.com/cao-awa/structring-translator/tree/main/doc/typescript)

### APIs

Expand Down
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ repositories {
}
maven {
name = "GitHubPackages"
url = "https://github.com/cao-awa/language-translator"
url = "https://github.com/cao-awa/structuring-translator"
credentials {
username = "cao-awa"
password = "ghp_UgALm1Ta2ONYeNh" + "S2m0XoWSc27BcE22v4Umn"
Expand All @@ -52,7 +52,7 @@ configurations {
}

dependencies {
implementation 'org.jetbrains:annotations:23.0.0'
implementation 'org.jetbrains:annotations:26.0.1'

// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
Expand All @@ -72,7 +72,8 @@ dependencies {

shadow 'com.github.cao.awa:catheter:1.0.42'
shadow 'com.github.cao.awa:sinuatum:1.0.11'
implementation "com.github.cao.awa:language-translator:${project.language_translator_version}"

implementation "com.github.cao.awa:structuring-translator:${project.structuring_translator_version}"
}

processResources {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
kotlin_loader_version = 1.13.0+kotlin.2.1.0
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version = 0.111.0+1.21.4
fabric_version = 0.112.0+1.21.4
# Kotlin
kotlin_version = 2.1.0
# Language translator
language_translator_version = 1.0.10
structuring_translator_version = 1.1.0-fix
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.11-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/github/cao/awa/conium/Conium.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import com.github.cao.awa.conium.server.datapack.ConiumContentDatapack
import com.github.cao.awa.conium.server.datapack.ConiumServerLoadDatapacks
import com.github.cao.awa.conium.template.ConiumTemplate
import com.github.cao.awa.conium.template.ConiumTemplates
import com.github.cao.awa.language.translator.translate.LanguageTranslator
import com.github.cao.awa.language.translator.translate.lang.TranslateTarget
import com.github.cao.awa.language.translator.translate.lang.element.TranslateElementData
import com.github.cao.awa.sinuatum.resource.loader.ResourceLoader
import com.github.cao.awa.sinuatum.util.collection.CollectionFactor
import com.github.cao.awa.sinuatum.util.io.IOUtil
import com.github.cao.awa.translator.structuring.translate.StructuringTranslator
import com.github.cao.awa.translator.structuring.translate.element.TranslateElementData
import com.github.cao.awa.translator.structuring.translate.language.LanguageTranslateTarget
import net.fabricmc.api.ModInitializer
import net.minecraft.util.Identifier
import org.apache.logging.log4j.LogManager
Expand All @@ -36,7 +36,7 @@ class Conium : ModInitializer {
var VERSION = "1.0.0-alpha7"

@JvmField
var LANGUAGE_TRANSLATOR_VERSION: String = LanguageTranslator.getVersion()
var STRUCTURING_TRANSLATOR_VERSION: String = StructuringTranslator.getVersion()

@JvmField
var itemInjectManager: ItemPropertyInjectManager? = null
Expand All @@ -57,7 +57,7 @@ class Conium : ModInitializer {
val reloadCallbacks: MutableList<Runnable> = CollectionFactor.arrayList()

@JvmField
var enableDebugs: Boolean = false
var enableDebugs: Boolean = true

@JvmField
var allowBedrock: Boolean = true
Expand Down Expand Up @@ -158,9 +158,9 @@ class Conium : ModInitializer {
}
}

private fun collectTranslators(translators: Map<TranslateTarget, Map<TranslateElementData<*>, LanguageTranslator<*>>>): Map<TranslateTarget, Collection<Class<*>>> {
val result: MutableMap<TranslateTarget, Collection<Class<*>>> = CollectionFactor.hashMap()
translators.forEach { (target: TranslateTarget, targetTranslators: Map<TranslateElementData<*>, LanguageTranslator<*>>) ->
private fun collectTranslators(translators: Map<LanguageTranslateTarget, Map<TranslateElementData<*>, StructuringTranslator<*>>>): Map<LanguageTranslateTarget, Collection<Class<*>>> {
val result: MutableMap<LanguageTranslateTarget, Collection<Class<*>>> = CollectionFactor.hashMap()
translators.forEach { (target: LanguageTranslateTarget, targetTranslators: Map<TranslateElementData<*>, StructuringTranslator<*>>) ->
result[target] = targetTranslators.keys.map { it.clazz() }
}
return result
Expand Down Expand Up @@ -220,17 +220,17 @@ class Conium : ModInitializer {
}

// Initialize script translator for bedrock's typescript.
LOGGER.info("Loading conium '{}' language translator providers for [typescript]", VERSION)
LOGGER.info("Loading conium '{}' structuring translator providers for [typescript]", VERSION)
ConiumScriptTranslator.postRegister()

LanguageTranslator.getTranslators("conium").let { translators ->
StructuringTranslator.getTranslators("conium").let { translators ->
LOGGER.info(
"Loaded {} translators by conium language providers({})",
"Loaded {} translators by conium structuring translator providers({})",
translators.size,
VERSION
)
debug(
"Loaded {} translators by conium language providers({}): {}",
"Loaded {} translators by conium structuring translator providers({}): {}",
translators::size,
{ VERSION },
{ collectTranslators(translators) },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.cao.awa.conium.annotation.script.javascript

annotation class JavaScriptApi(vararg val refName: String)
30 changes: 30 additions & 0 deletions src/main/java/com/github/cao/awa/conium/bedrock/IndexD.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.cao.awa.conium.bedrock

import com.github.cao.awa.conium.bedrock.server.ServerIndexD
import com.github.cao.awa.sinuatum.manipulate.QuickManipulate
import com.github.cao.awa.sinuatum.util.collection.CollectionFactor
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import java.util.function.Consumer

abstract class IndexD {
companion object {
private val LOGGER: Logger = LogManager.getLogger("IndexD")
private val packages: Map<String, IndexD> = QuickManipulate.operation(CollectionFactor.hashMap()) { packages: MutableMap<String, IndexD> ->
packages["@minecraft/server"] = ServerIndexD()
}

@JvmStatic
fun tryImport(packageName: String, refs: Collection<String>, action: Consumer<String>) {
packages[packageName]?.also { indexD: IndexD ->
refs.distinct().forEach { ref: String ->
indexD.forName(ref, action)
}
} ?: {
LOGGER.warn("The package '{}' are not found in conium IndexD", packageName)
}
}
}

abstract fun forName(refName: String, action: Consumer<String>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import com.github.cao.awa.sinuatum.util.collection.CollectionFactor

abstract class BedrockEventContext(val scriptSource: Any) {
companion object {
var system: BedrockSystem = BedrockSystem()
val system: BedrockSystem get() = this._system
var _system: BedrockSystem = BedrockSystem()
var currentPosting: Any? = null
val contexts: MutableMap<Any, BedrockEventContext?> = CollectionFactor.hashMap()

fun newSystem() {
this.system = BedrockSystem()
this._system = BedrockSystem()
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.cao.awa.conium.bedrock.server

import com.github.cao.awa.conium.bedrock.IndexD
import com.github.cao.awa.conium.bedrock.event.context.BedrockEventContext
import com.github.cao.awa.sinuatum.manipulate.QuickManipulate
import com.github.cao.awa.sinuatum.util.collection.CollectionFactor
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import java.util.function.Consumer

class ServerIndexD : IndexD() {
companion object {
private val LOGGER: Logger = LogManager.getLogger("ServerIndexD")
private val mappings: Map<String, String> = QuickManipulate.operation(CollectionFactor.hashMap()) { mappings: MutableMap<String, String> ->
mappings["system"] = "${BedrockEventContext::class.qualifiedName}.Companion.system"
}
}

override fun forName(refName: String, action: Consumer<String>) {
mappings[refName]?.also { ref: String ->
action.accept(ref)
} ?: {
LOGGER.warn("The reference name '{}' are not found in conium ServerIndexD(@minecraft/server)", refName)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ class BedrockScreenDisplayTitle(private var title: String, private var subtitle:
}
}

fun updateSubtitle(title: String) = MinecraftClient.getInstance().inGameHud.setSubtitle(Text.of(title))
fun updateSubtitle(title: String): Unit = MinecraftClient.getInstance().inGameHud.setSubtitle(Text.of(title))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import com.github.cao.awa.conium.script.ScriptExport
import com.github.cao.awa.conium.script.eval.ScriptEval
import com.github.cao.awa.conium.script.interaction.NamedInteractionScript
import com.github.cao.awa.conium.script.kts.ConiumScript
import com.github.cao.awa.language.translator.builtin.typescript.antlr.TypescriptLexer
import com.github.cao.awa.language.translator.builtin.typescript.antlr.TypescriptParser
import com.github.cao.awa.language.translator.builtin.typescript.translate.element.TypescriptTranslateElement
import com.github.cao.awa.language.translator.builtin.typescript.tree.TypescriptFile
import com.github.cao.awa.language.translator.builtin.typescript.visitor.LanguageTypescriptVisitor
import com.github.cao.awa.language.translator.translate.LanguageTranslator
import com.github.cao.awa.language.translator.translate.lang.TranslateTarget
import com.github.cao.awa.sinuatum.resource.loader.ResourceLoader
import com.github.cao.awa.sinuatum.util.collection.CollectionFactor
import com.github.cao.awa.sinuatum.util.io.IOUtil
import com.github.cao.awa.translator.structuring.builtin.typescript.antlr.TypescriptLexer
import com.github.cao.awa.translator.structuring.builtin.typescript.antlr.TypescriptParser
import com.github.cao.awa.translator.structuring.builtin.typescript.translate.element.TypescriptTranslateElement
import com.github.cao.awa.translator.structuring.builtin.typescript.tree.TypescriptFile
import com.github.cao.awa.translator.structuring.builtin.typescript.visitor.LanguageTypescriptVisitor
import com.github.cao.awa.translator.structuring.translate.StructuringTranslator
import com.github.cao.awa.translator.structuring.translate.language.LanguageTranslateTarget
import net.minecraft.registry.RegistryKeys
import net.minecraft.resource.Resource
import net.minecraft.resource.ResourceFinder
Expand Down Expand Up @@ -197,14 +197,18 @@ class ConiumScriptManager : SinglePreparationResourceReloader<MutableMap<Identif
LOGGER.warn("Conium are disabled bedrock script, ignored '$identifier'")
} else if (path.endsWith(".ts")) {
// Load script data after translate typescript to kotlin.
scripts.add(
ScriptEval(
translateBedrockTypescript(content),
path,
"ConiumCommons",
"ConiumBedrockCommons"
runCatching {
scripts.add(
ScriptEval(
translateBedrockTypescript(content),
path,
"ConiumCommons",
"ConiumBedrockCommons"
)
)
)
}.exceptionOrNull()?.let {
LOGGER.warn("Failed to translate the script: {}", content, it)
}
} else if (path.endsWith(".js")) {
// Javascript supports are not done.
TODO("Javascript translator are not implemented yet.")
Expand Down Expand Up @@ -250,12 +254,12 @@ class ConiumScriptManager : SinglePreparationResourceReloader<MutableMap<Identif
typescriptFile.prepares()

// Translate typescript to conium script (kotlin script with conium API).
val translated: String = LanguageTranslator.translate(
val translated: String = StructuringTranslator.translate(
// Use conium provider to processes something additional features.
// See the package 'com.github.cao.awa.conium.script.translate'
"conium",
// Translate to kotlin script.
TranslateTarget.KOTLIN_SCRIPT,
LanguageTranslateTarget.KOTLIN_SCRIPT,
// Whole file to translates.
TypescriptTranslateElement.FILE,
typescriptFile
Expand All @@ -265,13 +269,7 @@ class ConiumScriptManager : SinglePreparationResourceReloader<MutableMap<Identif
// because the 'world' or 'system' or others variables may uniques in different context.
// The bedrock commons use 'get() = access(this)' to access current context,
// when context created, it will push to the 'contexts' in 'BedrockEventContext', so 'access' can got current context for this script.
return@let """
BedrockEventContext.post(this) {
$translated
}.also {
BedrockEventContext.completePost()
}
""".trimIndent()
return@let translated
}
}

Expand Down Expand Up @@ -334,6 +332,13 @@ class ConiumScriptManager : SinglePreparationResourceReloader<MutableMap<Identif
scriptEval.source
)

Conium.debug(
"Evaluating script '{}': {}",
scriptEval::source,
{ content },
LOGGER::info
)

// The 'eval' in host will compile and evaluate the script.
val result: ResultWithDiagnostics<EvaluationResult> = host.eval(
StringScriptSource(content),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.cao.awa.conium.script.javascript.std.collection.iterator

class JavascriptIterator<V>(private val delegate: MutableIterator<V>): MutableIterator<JavascriptIteratorResult<V>> {
val done: Boolean get() = hasNext()

override operator fun next(): JavascriptIteratorResult<V> {
if (hasNext()) {
return JavascriptIteratorResult(false, this.delegate.next())
} else {
return JavascriptIteratorResult(true, null)
}
}

override fun remove(): Unit = this.delegate.remove()

override operator fun hasNext(): Boolean = this.delegate.hasNext()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.cao.awa.conium.script.javascript.std.collection.iterator

class JavascriptIteratorResult<V>(val done: Boolean, val value: V?)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.cao.awa.conium.script.javascript.std.collection.set

import com.github.cao.awa.conium.annotation.script.javascript.JavaScriptApi
import com.github.cao.awa.conium.script.javascript.std.collection.iterator.JavascriptIterator
import com.github.cao.awa.sinuatum.util.collection.CollectionFactor

@JavaScriptApi("Set")
class Set<V> : MutableSet<V> {
private val delegate: MutableSet<V> = CollectionFactor.hashSet()

// Delegate properties.
// Also 'size' in Javascript.
@JavaScriptApi("Set", "#size")
override val size: Int get() = this.delegate.size

// Delegate operations.
override fun iterator(): MutableIterator<V> = this.delegate.iterator()

// Also 'add' in Javascript.
@JavaScriptApi("Set", "add")
override fun add(element: V): Boolean = this.delegate.add(element)
override fun remove(element: V): Boolean = this.delegate.remove(element)
override fun addAll(elements: Collection<V>): Boolean = this.delegate.addAll(elements)
override fun removeAll(elements: Collection<V>): Boolean = this.delegate.removeAll(elements.toSet())
override fun retainAll(elements: Collection<V>): Boolean = this.delegate.retainAll(elements.toSet())

// Also 'clear' in Javascript.
@JavaScriptApi("Set", "clear")
override fun clear(): Unit = this.delegate.clear()
override fun isEmpty(): Boolean = this.delegate.isEmpty()
override fun contains(element: @UnsafeVariance V): Boolean = this.delegate.contains(element)
override fun containsAll(elements: Collection<@UnsafeVariance V>): Boolean = this.delegate.containsAll(elements)

// Javascript operations.
@JavaScriptApi("Set", "delete")
fun delete(element: V): Boolean = remove(element)
@JavaScriptApi("Set", "forEach")
fun forEach(action: (@UnsafeVariance V, V, Int) -> Unit): Unit = this.delegate.forEachIndexed { index: Int, value: V -> action(value, value, index) }
@JavaScriptApi("Set", "has")
fun has(element: @UnsafeVariance V): Boolean = contains(element)
@JavaScriptApi("Set", "keys")
fun keys(): JavascriptIterator<V> = values()
@JavaScriptApi("Set", "values")
fun values(): JavascriptIterator<V> = JavascriptIterator(iterator())
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.cao.awa.conium.script.typescript
package com.github.cao.awa.conium.script.javascript.typescript

import com.github.cao.awa.conium.parameter.ParameterSelective2
import org.jetbrains.annotations.ApiStatus
Expand Down
Loading

0 comments on commit c7cb358

Please sign in to comment.