Skip to content

Commit

Permalink
IT WORKED!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Jan 25, 2024
1 parent 8848382 commit d25093a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ dependencies {
library(kotlin("stdlib"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2")

library(kotlin("scripting-common"))
library(kotlin("scripting-jvm"))
library(kotlin("scripting-jvm-host"))
implementation(kotlin("scripting-common"))
implementation(kotlin("scripting-jvm"))
implementation(kotlin("scripting-jvm-host"))

compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("com.github.Slimefun:Slimefun4:RC-36")
Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/io/github/addoncommunity/galactifun/Galactifun2.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.addoncommunity.galactifun

import io.github.addoncommunity.galactifun.base.BaseUniverse
import io.github.addoncommunity.galactifun.scripting.evalScript
import io.github.seggan.kfun.AbstractAddon
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun
Expand All @@ -9,6 +10,10 @@ import org.bstats.bukkit.Metrics
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import java.util.logging.Level
import kotlin.script.experimental.api.ResultValue
import kotlin.script.experimental.api.ScriptDiagnostic
import kotlin.script.experimental.api.valueOrThrow
import kotlin.script.experimental.host.toScriptSource

class Galactifun2 : AbstractAddon() {

Expand Down Expand Up @@ -45,6 +50,32 @@ class Galactifun2 : AbstractAddon() {

BaseUniverse.init()

val scriptsFolder = dataFolder.resolve("planets")
if (!scriptsFolder.exists()) {
scriptsFolder.mkdirs()
}
for (script in scriptsFolder.listFiles()!!) {
if (script.isFile && script.name.endsWith(".planet.kts")) {
log("Loading planet script: ${script.name}")
val result = evalScript(script.toScriptSource())
for (diagnostic in result.reports) {
log(
when (diagnostic.severity) {
ScriptDiagnostic.Severity.ERROR, ScriptDiagnostic.Severity.FATAL -> Level.SEVERE
ScriptDiagnostic.Severity.WARNING -> Level.WARNING
ScriptDiagnostic.Severity.INFO -> Level.INFO
ScriptDiagnostic.Severity.DEBUG -> Level.FINE
},
diagnostic.message
)
}
val returnValue = result.valueOrThrow().returnValue
if (returnValue is ResultValue.Error) {
throw returnValue.error
}
}
}

runOnNextTick {
log(
Level.INFO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import io.github.addoncommunity.galactifun.api.objects.properties.DayCycle
import io.github.addoncommunity.galactifun.base.BaseUniverse
import io.github.addoncommunity.galactifun.base.objects.earth.Earth
import io.github.addoncommunity.galactifun.base.objects.earth.Moon
import io.github.addoncommunity.galactifun.pluginInstance
import org.bukkit.Material
import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.*
import kotlin.script.experimental.jvm.dependenciesFromClassContext
import kotlin.script.experimental.jvm.baseClassLoader
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvm.jvmTarget
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTemplate

@KotlinScript(
fileExtension = "planet.kts",
compilationConfiguration = PlanetScriptConfig::class
compilationConfiguration = PlanetScriptConfig::class,
evaluationConfiguration = PlanetScriptEval::class
)
abstract class PlanetScript {
val eternalDay = DayCycle.ETERNAL_DAY
Expand All @@ -33,13 +38,21 @@ object PlanetScriptConfig : ScriptCompilationConfiguration({
"kotlin.time.Duration.Companion.days"
)
defaultImports(Material::class, BaseUniverse::class, TheUniverse::class, Earth::class, Moon::class)
compilerOptions("-jvm-target", "17")
compilerOptions.append("-Xadd-modules=ALL-MODULE-PATH")
jvm {
dependenciesFromClassContext(PlanetScript::class, wholeClasspath = true)
dependenciesFromCurrentContext(wholeClasspath = true)
jvmTarget("17")
}
})

object PlanetScriptEval : ScriptEvaluationConfiguration({
jvm {
baseClassLoader(pluginInstance::class.java.classLoader)
}
})

fun evalScript(script: SourceCode): ResultWithDiagnostics<EvaluationResult> {
val config = createJvmCompilationConfigurationFromTemplate<PlanetScript>()
return BasicJvmScriptingHost().eval(script, config, null)
val evalConfig = createJvmEvaluationConfigurationFromTemplate<PlanetScript>()
return BasicJvmScriptingHost().eval(script, config, evalConfig)
}

0 comments on commit d25093a

Please sign in to comment.