-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
166 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/PlanetDsl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.github.addoncommunity.galactifun.scripting | ||
|
||
@DslMarker | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE) | ||
annotation class PlanetDsl |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/RequiredProperty.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.github.addoncommunity.galactifun.scripting | ||
|
||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KProperty | ||
|
||
class RequiredProperty<T>(private var value: T? = null) : ReadWriteProperty<Any, T> { | ||
|
||
override fun getValue(thisRef: Any, property: KProperty<*>): T { | ||
return value ?: error("${property.name} must be set") | ||
} | ||
|
||
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) { | ||
this.value = value | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/ScriptDef.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.github.addoncommunity.galactifun.scripting | ||
|
||
import io.github.addoncommunity.galactifun.api.objects.TheUniverse | ||
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 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.jvm | ||
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost | ||
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate | ||
|
||
@KotlinScript( | ||
fileExtension = "planet.kts", | ||
compilationConfiguration = PlanetScriptConfig::class | ||
) | ||
abstract class PlanetScript { | ||
val eternalDay = DayCycle.ETERNAL_DAY | ||
val eternalNight = DayCycle.ETERNAL_NIGHT | ||
} | ||
|
||
object PlanetScriptConfig : ScriptCompilationConfiguration({ | ||
defaultImports( | ||
"io.github.addoncommunity.galactifun.util.*", | ||
"io.github.addoncommunity.galactifun.scripting.dsl.*", | ||
"io.github.addoncommunity.galactifun.api.objects.properties.Distance.Companion.lightYears", | ||
"io.github.addoncommunity.galactifun.api.objects.properties.Distance.Companion.kilometers", | ||
"io.github.addoncommunity.galactifun.api.objects.properties.Distance.Companion.au", | ||
"kotlin.time.Duration.Companion.hours", | ||
"kotlin.time.Duration.Companion.days" | ||
) | ||
defaultImports(Material::class, BaseUniverse::class, TheUniverse::class, Earth::class, Moon::class) | ||
compilerOptions("-jvm-target", "17") | ||
jvm { | ||
dependenciesFromClassContext(PlanetScript::class, wholeClasspath = true) | ||
} | ||
}) | ||
|
||
fun evalScript(script: SourceCode): ResultWithDiagnostics<EvaluationResult> { | ||
val config = createJvmCompilationConfigurationFromTemplate<PlanetScript>() | ||
return BasicJvmScriptingHost().eval(script, config, null) | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/dsl/OrbitBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.addoncommunity.galactifun.scripting.dsl | ||
|
||
import io.github.addoncommunity.galactifun.api.objects.properties.Distance | ||
import io.github.addoncommunity.galactifun.api.objects.properties.Orbit | ||
import io.github.addoncommunity.galactifun.scripting.PlanetDsl | ||
import io.github.addoncommunity.galactifun.scripting.RequiredProperty | ||
import kotlin.time.Duration | ||
|
||
@PlanetDsl | ||
class OrbitBuilder { | ||
|
||
var distance: Distance by RequiredProperty() | ||
var yearLength: Duration by RequiredProperty() | ||
|
||
fun build(): Orbit { | ||
return Orbit(distance, yearLength) | ||
} | ||
} | ||
|
||
inline fun orbit(block: OrbitBuilder.() -> Unit): Orbit { | ||
return OrbitBuilder().apply(block).build() | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/dsl/PlanetBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.github.addoncommunity.galactifun.scripting.dsl | ||
|
||
import io.github.addoncommunity.galactifun.api.objects.UniversalObject | ||
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryObject | ||
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryWorld | ||
import io.github.addoncommunity.galactifun.api.objects.properties.DayCycle | ||
import io.github.addoncommunity.galactifun.api.objects.properties.Orbit | ||
import io.github.addoncommunity.galactifun.scripting.PlanetDsl | ||
import io.github.addoncommunity.galactifun.scripting.RequiredProperty | ||
import org.bukkit.Material | ||
import org.bukkit.inventory.ItemStack | ||
import kotlin.time.Duration | ||
|
||
@PlanetDsl | ||
class PlanetBuilder { | ||
|
||
var name: String by RequiredProperty() | ||
var item: Material by RequiredProperty() | ||
var orbiting: UniversalObject by RequiredProperty() | ||
var orbit: Orbit by RequiredProperty() | ||
var dayCycle: DayCycle by RequiredProperty() | ||
|
||
fun build(): PlanetaryObject { | ||
return object : PlanetaryObject(name, ItemStack(item)) { | ||
override val dayCycle = this@PlanetBuilder.dayCycle | ||
override val orbiting = this@PlanetBuilder.orbiting | ||
override val orbit = this@PlanetBuilder.orbit | ||
} | ||
} | ||
|
||
val Duration.long: DayCycle | ||
get() = DayCycle(this) | ||
} | ||
|
||
inline fun planet(block: PlanetBuilder.() -> Unit): PlanetaryObject { | ||
val planet = PlanetBuilder().apply(block).build() | ||
if (planet is PlanetaryWorld) { | ||
planet.register() | ||
} | ||
return planet | ||
} | ||
|
||
inline fun PlanetBuilder.orbit(block: OrbitBuilder.() -> Unit) { | ||
orbit = OrbitBuilder().apply(block).build() | ||
} | ||
|
||
fun PlanetBuilder.eternal(ticks: Int): DayCycle = DayCycle.eternal(ticks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.