Skip to content

Commit

Permalink
Merge pull request #83 from Ayfri/feat/k2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayfri authored May 22, 2024
2 parents 24424f6 + 2c6af7b commit 461d7b7
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/utils.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import gradle.kotlin.dsl.accessors._adc73c88e0a124c97e28fdd97139246a.ext
import gradle.kotlin.dsl.accessors._59ba98f56c77a2c46552bf0ab3713b77.ext
import org.gradle.api.Project
import org.gradle.kotlin.dsl.extra

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
kotlin.version=1.9.22
kotlin.version=2.0.0
minecraft.version=1.20.5-pre1
# Gradle optimization flags
org.gradle.caching=true
Expand Down
9 changes: 4 additions & 5 deletions kore/src/main/kotlin/io/github/ayfri/kore/DataPack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,14 @@ class DataPack(val name: String) {
zipOutputStream.write(it.toFile().readBytes())
}

functions.forEach { function ->
zipOutputStream.putNextEntry(ZipEntry("data/${function.namespace}/functions/${function.directory}/${function.name}.mcfunction"))
functions.distinctBy(Function::getFinalPath).forEach { function ->
zipOutputStream.putNextEntry(ZipEntry(function.getFinalPath()))
zipOutputStream.write(function.lines.joinToString("\n").toByteArray())
zipOutputStream.closeEntry()
}

generatedFunctions.forEach { function ->
function.directory = function.directory.removePrefix(configuration.generatedFunctionsFolder)
zipOutputStream.putNextEntry(ZipEntry("data/${function.namespace}/functions/${configuration.generatedFunctionsFolder}/${function.directory}/${function.name}.mcfunction"))
generatedFunctions.distinctBy(Function::getFinalPath).forEach { function ->
zipOutputStream.putNextEntry(ZipEntry(function.getFinalPath()))
zipOutputStream.write(function.lines.joinToString("\n").toByteArray())
zipOutputStream.closeEntry()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import io.github.ayfri.kore.Generator
import io.github.ayfri.kore.arguments.types.resources.ItemModifierArgument
import io.github.ayfri.kore.features.itemmodifiers.functions.ItemFunction
import io.github.ayfri.kore.serializers.InlinableList
import kotlinx.serialization.*
import io.github.ayfri.kore.serializers.inlinableListSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.serializer

typealias ItemModifierAsList = @Serializable(with = ItemModifier.Companion.ItemModifierAsListSerializer::class) ItemModifier

Expand All @@ -18,7 +22,8 @@ data class ItemModifier(
override var fileName: String = "item_modifier",
var modifiers: InlinableList<ItemFunction> = emptyList(),
) : Generator("item_modifier") {
override fun generateJson(dataPack: DataPack) = dataPack.jsonEncoder.encodeToString(modifiers)
override fun generateJson(dataPack: DataPack) =
dataPack.jsonEncoder.encodeToString(inlinableListSerializer(ItemFunction.serializer()), modifiers)

companion object {
object ItemModifierAsListSerializer : KSerializer<ItemModifier> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package io.github.ayfri.kore.features.itemmodifiers.functions

import io.github.ayfri.kore.features.itemmodifiers.ItemModifier
import io.github.ayfri.kore.features.predicates.PredicateAsList
import io.github.ayfri.kore.features.predicates.providers.IntOrIntNumberProvidersRange
import io.github.ayfri.kore.features.predicates.providers.IntOrNumberProvidersRange
import io.github.ayfri.kore.features.predicates.providers.int
import io.github.ayfri.kore.features.predicates.providers.intRange
import kotlinx.serialization.Serializable

@Serializable
data class LimitCount(
override var conditions: PredicateAsList? = null,
val limit: IntOrIntNumberProvidersRange,
val limit: IntOrNumberProvidersRange,
) : ItemFunction()

fun ItemModifier.limitCount(limit: IntOrIntNumberProvidersRange, block: LimitCount.() -> Unit = {}) {
fun ItemModifier.limitCount(limit: IntOrNumberProvidersRange, block: LimitCount.() -> Unit = {}) {
modifiers += LimitCount(limit = limit).apply(block)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import io.github.ayfri.kore.Generator
import io.github.ayfri.kore.arguments.types.resources.PredicateArgument
import io.github.ayfri.kore.features.predicates.conditions.PredicateCondition
import io.github.ayfri.kore.serializers.InlinableList
import io.github.ayfri.kore.serializers.inlinableListSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encodeToString
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.serializer

Expand All @@ -20,7 +20,8 @@ data class Predicate(
override var fileName: String = "predicate",
var predicateConditions: InlinableList<PredicateCondition> = emptyList(),
) : Generator("predicates") {
override fun generateJson(dataPack: DataPack) = dataPack.jsonEncoder.encodeToString(predicateConditions)
override fun generateJson(dataPack: DataPack) =
dataPack.jsonEncoder.encodeToString(inlinableListSerializer(PredicateCondition.serializer()), predicateConditions)

companion object {
object PredicateAsListSerializer : KSerializer<Predicate> by serializer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class IntOrIntNumberProvidersRange internal constructor(
}
}

fun int(value: Int) = IntOrIntNumberProvidersRange(value)
fun providersRange(min: NumberProvider, max: NumberProvider) = IntOrIntNumberProvidersRange(range = min to max)
fun providersRange(min: Float, max: NumberProvider) = IntOrIntNumberProvidersRange(range = constant(min) to max)
fun providersRange(min: NumberProvider, max: Float) = IntOrIntNumberProvidersRange(range = min to constant(max))
fun intRange(min: Float, max: Float) = IntOrIntNumberProvidersRange(range = constant(min) to constant(max))
fun int(value: Int) = IntOrNumberProvidersRange(value)
fun providersRange(min: NumberProvider, max: NumberProvider) = IntOrNumberProvidersRange(range = min to max)
fun providersRange(min: Float, max: NumberProvider) = IntOrNumberProvidersRange(range = constant(min) to max)
fun providersRange(min: NumberProvider, max: Float) = IntOrNumberProvidersRange(range = min to constant(max))
fun intRange(min: Float, max: Float) = IntOrNumberProvidersRange(range = constant(min) to constant(max))
fun intRange(range: ClosedFloatingPointRange<Float>) =
IntOrIntNumberProvidersRange(range = constant(range.start) to constant(range.endInclusive))
IntOrNumberProvidersRange(range = constant(range.start) to constant(range.endInclusive))
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ open class Function(
lines.add("# $comment")
}

fun getFinalPath() = "data/$namespace/functions/${directory.ifNotEmpty { "$it/" }}$name.mcfunction"

fun generate(directory: File) {
val file = File(directory, "${this.directory}/$name.mcfunction")
file.parentFile.mkdirs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement

typealias InlinableList<T> = @Serializable(with = InlinableListSerializer::class) List<T>

fun <T> inlinableListSerializer(kSerializer: KSerializer<T>): KSerializer<InlinableList<T>> = InlinableListSerializer(kSerializer)

@OptIn(ExperimentalSerializationApi::class)
open class InlinableListSerializer<T>(private val kSerializer: KSerializer<T>) : KSerializer<List<T>> {
override val descriptor = ListSerializer(JsonElement.serializer()).descriptor
override val descriptor = ListSerializer(kSerializer).descriptor

override fun deserialize(decoder: Decoder) = error("List of ${kSerializer.descriptor.serialName} cannot be deserialized")

Expand Down
2 changes: 1 addition & 1 deletion kore/src/test/kotlin/io/github/ayfri/kore/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.github.cdimascio.dotenv.dotenv
import kotlin.io.path.Path

val configuration = dotenv()
val minecraftSaveTestPath = Path(configuration["TEST_FOLDER"])
val minecraftSaveTestPath = Path(configuration["TEST_FOLDER", "out"])

fun DataPack.setTestPath() {
path = minecraftSaveTestPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import org.intellij.lang.annotations.Language
import java.util.zip.ZipInputStream
import kotlin.io.path.absolute
import kotlin.io.path.absolutePathString
import kotlin.io.path.exists

Expand Down Expand Up @@ -43,7 +44,7 @@ infix fun Generator.assertsIs(@Language("json") expected: String) {
}

fun TestDataPack.assertFileGenerated(path: String) {
val file = dp.path.resolve(path)
val file = dp.path.resolve(path).normalize().absolute()
callAfterGeneration {
if (!file.exists()) {
error("File for datapack '${dp.name}' at '${file.absolutePathString()}' was not found.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,19 @@ fun zipTests() = testDataPack("zip_tests") {
say("Hello, tick!")
}
}.apply {
assertFileGenerated("test.zip")
assertFileGenerated("zip_tests.zip")

val myFunction = dp.functions[0]
assertFileGeneratedInZip("data/${myFunction.namespace}/functions/${myFunction.directory}/${myFunction.name}.mcfunction")
assertFileGeneratedInZip("data/${myFunction.namespace}/functions/${myFunction.name}.mcfunction")

val myFunction2 = dp.functions[1]
assertFileGeneratedInZip("data/${myFunction2.namespace}/functions/${myFunction2.directory}/${myFunction2.name}.mcfunction")

val loadFunction = dp.generatedFunctions[0]
val directory = loadFunction.directory.removePrefix(dp.configuration.generatedFunctionsFolder)
assertFileGeneratedInZip("data/${loadFunction.namespace}/functions/${dp.configuration.generatedFunctionsFolder}/$directory/${loadFunction.name}.mcfunction")
assertFileGeneratedInZip("data/${loadFunction.namespace}/functions/${dp.configuration.generatedFunctionsFolder}/${loadFunction.name}.mcfunction")

val tickFunction = dp.generatedFunctions[1]
val tickDirectory = tickFunction.directory.removePrefix(dp.configuration.generatedFunctionsFolder)
assertFileGeneratedInZip("data/${tickFunction.namespace}/functions/${dp.configuration.generatedFunctionsFolder}/$tickDirectory/${tickFunction.name}.mcfunction")
val tickDirectory = tickFunction.directory.removePrefix(dp.configuration.generatedFunctionsFolder + "/")
assertFileGeneratedInZip("data/${tickFunction.namespace}/functions/${dp.configuration.generatedFunctionsFolder}/${tickDirectory}/${tickFunction.name}.mcfunction")
generateZip()
}
8 changes: 4 additions & 4 deletions libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[versions]
jetbrains-compose = "1.6.0"
jetbrains-compose = "1.6.10"
joml = "1.10.5"
knbt = "0.11.5"
kobweb = "0.17.0"
kobweb = "0.18.0"
kotlin-dotenv = "6.4.1"
kotlinpoet = "1.16.0"
kotlinx-serialization = "1.6.3"
ktor = "2.3.5"
kotlinx-serialization = "1.7.0-RC"
ktor = "2.3.11"
nexus-publish = "1.3.0"

[libraries]
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pluginManagement {
kotlin("jvm") version kotlinVersion
kotlin("multiplatform") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
kotlin("plugin.compose") version kotlinVersion
}
}

Expand Down
13 changes: 7 additions & 6 deletions website/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import com.varabyte.kobweb.gradle.application.util.configAsKobwebApplication
import com.varabyte.kobwebx.gradle.markdown.children
import com.varabyte.kobwebx.gradle.markdown.yamlStringToKotlinString
import kotlinx.html.link
import kotlinx.html.script
import kotlinx.html.unsafe
import org.commonmark.ext.front.matter.YamlFrontMatterBlock
import org.commonmark.ext.front.matter.YamlFrontMatterVisitor
import org.commonmark.node.AbstractVisitor
import org.commonmark.node.CustomBlock
import org.commonmark.node.Text
import kotlinx.html.link
import kotlinx.html.script
import kotlinx.html.unsafe

plugins {
kotlin("multiplatform")
kotlin("plugin.compose")
alias(libs.plugins.jetbrains.compose)
alias(libs.plugins.kobweb.application)
alias(libs.plugins.kobwebx.markdown)
Expand Down Expand Up @@ -217,7 +218,7 @@ val generateDocSourceTask = task("generateDocSource") {
kotlin {
configAsKobwebApplication("website")

js(IR) {
js {
browser {
commonWebpackConfig {
devServer?.open = false
Expand All @@ -228,13 +229,13 @@ kotlin {
}

sourceSets {
jsMain {
commonMain {
kotlin.srcDir(generateDocSourceTask)

dependencies {
implementation(compose.html.core)
implementation(libs.kobweb.core)
implementation(compose.runtime)
implementation(libs.kobweb.core)
implementation(libs.kobwebx.markdown)
implementation(libs.kobwebx.silk.icons.mdi)
}
Expand Down

0 comments on commit 461d7b7

Please sign in to comment.