Skip to content

Commit

Permalink
Scripting!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Jan 25, 2024
1 parent 94804a2 commit 8848382
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 11 deletions.
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "1.9.21"
kotlin("jvm") version "1.9.22"
id("com.github.johnrengelman.shadow") version "8.1.1"
id("net.minecrell.plugin-yml.bukkit") version "0.5.3"
id("xyz.jpenilla.run-paper") version "2.2.0"
Expand All @@ -16,6 +16,10 @@ 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"))

compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("com.github.Slimefun:Slimefun4:RC-36")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class DayCycle {

companion object {

val ETERNAL_DAY = DayCycle(6000)
val ETERNAL_NIGHT = DayCycle(18000)
val ETERNAL_DAY = eternal(6000)
val ETERNAL_NIGHT = eternal(18000)
val EARTH_LIKE = DayCycle(1.days)

fun eternal(time: Long) = DayCycle(time)
fun eternal(time: Int) = DayCycle(time)
}

val description: String
Expand Down Expand Up @@ -48,10 +48,10 @@ class DayCycle {
/**
* Constructor for eternal day or night
*/
private constructor(time: Long) {
private constructor(time: Int) {
description = "Eternal " + (if (time < 12000) "day" else "night")
perFiveSeconds = 0
startTime = time
startTime = time.toLong()
}

fun applyEffects(world: World) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ value class Distance private constructor(val lightYears: Double) {
@get:JvmName("kilometers")
val Int.kilometers: Distance
get() = Distance(this / Constants.KM_PER_LY)

@get:JvmName("au")
val Double.au: Distance
get() = Distance(this * Constants.AU_PER_LY)

@get:JvmName("au")
val Int.au: Distance
get() = Distance(this * Constants.AU_PER_LY)

@get:JvmName("au")
val Long.au: Distance
get() = Distance(this * Constants.AU_PER_LY)
}

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ object BaseUniverse {
Orbit(27000.lightYears, 250000000.years)
)

val earth = Earth()
val moon = Moon()

fun init() {
Earth.register()
Moon.register()
earth.register()
moon.register()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.bukkit.World
import org.bukkit.WorldCreator
import org.bukkit.inventory.ItemStack

object Earth : PlanetaryWorld("Earth", ItemStack(Material.GRASS_BLOCK)) {
class Earth : PlanetaryWorld("Earth", ItemStack(Material.GRASS_BLOCK)) {

override val orbiting = BaseUniverse.solarSystem
override val orbit = Orbit(149600000.kilometers, 1.years)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import io.github.addoncommunity.galactifun.api.objects.properties.DayCycle
import io.github.addoncommunity.galactifun.api.objects.properties.Distance.Companion.kilometers
import io.github.addoncommunity.galactifun.api.objects.properties.Orbit
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Atmosphere
import io.github.addoncommunity.galactifun.base.BaseUniverse
import org.bukkit.Material
import org.bukkit.inventory.ItemStack
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours

object Moon : AlienWorld("Moon", ItemStack(Material.END_STONE)) {
class Moon : AlienWorld("Moon", ItemStack(Material.END_STONE)) {
override val atmosphere = Atmosphere.NONE
override val dayCycle = DayCycle(29.days + 12.hours)
override val orbiting = Earth
override val orbiting = BaseUniverse.earth
override val orbit = Orbit(382500.kilometers, 27.days)
override val generator: WorldGenerator = MoonGenerator()
}
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
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
}
}
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)
}
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()
}
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)
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package io.github.addoncommunity.galactifun.util
object Constants {

const val KM_PER_LY = 9.461e12
const val AU_PER_LY = 63241.1
}

0 comments on commit 8848382

Please sign in to comment.