diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 586a2a4..50d1a64 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -7,8 +7,10 @@ repositories { } dependencies { - implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20") - implementation("com.github.johnrengelman:shadow:8.1.1") + implementation(tooling.kotlin) + implementation(tooling.shadow) + implementation(files(tooling.javaClass.superclass.protectionDomain.codeSource.location)) + } java { diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts new file mode 100644 index 0000000..16fa73f --- /dev/null +++ b/buildSrc/settings.gradle.kts @@ -0,0 +1,10 @@ +dependencyResolutionManagement { + versionCatalogs { + create("libs") { + from(files("../gradle/libraries.versions.toml")) + } + create("tooling") { + from(files("../gradle/toolchain.versions.toml")) + } + } +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/api-plugin.gradle.kts b/buildSrc/src/main/kotlin/api-plugin.gradle.kts index bfcbd26..e84eccf 100644 --- a/buildSrc/src/main/kotlin/api-plugin.gradle.kts +++ b/buildSrc/src/main/kotlin/api-plugin.gradle.kts @@ -4,7 +4,6 @@ plugins { id("common") } - tasks.jar { duplicatesStrategy = DuplicatesStrategy.EXCLUDE } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/common.gradle.kts b/buildSrc/src/main/kotlin/common.gradle.kts index 32af72f..7bc38a7 100644 --- a/buildSrc/src/main/kotlin/common.gradle.kts +++ b/buildSrc/src/main/kotlin/common.gradle.kts @@ -3,9 +3,13 @@ plugins { } group = "me.zodd" -version = "0.1.7" +version = "0.1.91" repositories { mavenCentral() maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") +} + +java { + toolchain.languageVersion.set(JavaLanguageVersion.of(21)) } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/host-plugin.gradle.kts b/buildSrc/src/main/kotlin/host-plugin.gradle.kts index 8b5d45d..b625831 100644 --- a/buildSrc/src/main/kotlin/host-plugin.gradle.kts +++ b/buildSrc/src/main/kotlin/host-plugin.gradle.kts @@ -7,7 +7,7 @@ dependencies { implementation("org.spongepowered:configurate-extra-kotlin:4.1.2") { isTransitive = false } - shadow(kotlin("reflect")) + compileOnly(kotlin("reflect")) shadow(kotlin("scripting-jvm-host")) shadow(project(":KotlinScript-script-definition")) shadow(project(":KotlinScript-kotlin-script-api")) diff --git a/gradle/toolchain.versions.toml b/gradle/toolchain.versions.toml index 341a470..c37e189 100644 --- a/gradle/toolchain.versions.toml +++ b/gradle/toolchain.versions.toml @@ -1,5 +1,10 @@ [versions] - -[plugins] +project_version = "0.1.8" +shadow = "8.1.1" +kotlin = "1.9.20" +configurate = "4.1.2" [libraries] +configurate = { group = "org.spongepowered", name = "configurate-extra-koltin", version.ref = "configurate" } +shadow = { group = "com.github.johnrengelman", name = "shadow", version.ref = "shadow" } +kotlin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" } \ No newline at end of file diff --git a/kotlin-script-api/build.gradle.kts b/kotlin-script-api/build.gradle.kts index 6c51488..7509623 100644 --- a/kotlin-script-api/build.gradle.kts +++ b/kotlin-script-api/build.gradle.kts @@ -10,5 +10,5 @@ dependencies { compileOnly(kotlin("scripting-jvm-host")) compileOnly(project(":KotlinScript-script-definition")) compileOnly(libs.paper) - compileOnly("org.spongepowered:spongeapi:8.1.0") + compileOnly(libs.sponge8) } diff --git a/kotlin-script-api/src/main/kotlin/me/zodd/ScriptCoreLogger.kt b/kotlin-script-api/src/main/kotlin/me/zodd/ScriptCoreLogger.kt index aab3361..abf3373 100644 --- a/kotlin-script-api/src/main/kotlin/me/zodd/ScriptCoreLogger.kt +++ b/kotlin-script-api/src/main/kotlin/me/zodd/ScriptCoreLogger.kt @@ -4,29 +4,22 @@ import java.io.File import kotlin.reflect.KClass import kotlin.script.experimental.api.EvaluationResult import kotlin.script.experimental.api.ResultWithDiagnostics +import kotlin.script.experimental.api.ScriptAcceptedLocation import kotlin.script.experimental.api.ScriptCompilationConfiguration import kotlin.script.experimental.api.ScriptDiagnostic import kotlin.script.experimental.api.ScriptEvaluationConfiguration import kotlin.script.experimental.api.SourceCode +import kotlin.script.experimental.api.acceptedLocations import kotlin.script.experimental.api.asSuccess -import kotlin.script.experimental.api.compilationConfiguration import kotlin.script.experimental.api.compilerOptions import kotlin.script.experimental.api.defaultImports -import kotlin.script.experimental.api.hostConfiguration +import kotlin.script.experimental.api.ide import kotlin.script.experimental.api.onFailure import kotlin.script.experimental.api.onSuccess -import kotlin.script.experimental.api.providedProperties -import kotlin.script.experimental.api.refineConfigurationBeforeEvaluate -import kotlin.script.experimental.api.scriptExecutionWrapper import kotlin.script.experimental.host.toScriptSource import kotlin.script.experimental.jvm.baseClassLoader -import kotlin.script.experimental.jvm.dependenciesFromClassContext -import kotlin.script.experimental.jvm.dependenciesFromClassloader import kotlin.script.experimental.jvm.dependenciesFromCurrentContext import kotlin.script.experimental.jvm.jvm -import kotlin.script.experimental.jvm.loadDependencies -import kotlin.script.experimental.jvm.util.classpathFromClass -import kotlin.script.experimental.jvm.util.classpathFromClassloader import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTemplate @@ -53,50 +46,7 @@ interface KtScriptPluginContainer { */ class Script( private val script: String, - private val defaultPlatformImports: List, - private val scriptContainer: KtScriptPluginContainer<*, *> ) { - private val defaultImports: List = listOf( - //Kotlin Packages - "kotlin.reflect.*", - "kotlin.reflect.jvm.*", - //Kyori - "net.kyori.adventure.text.*", - //Plugin - "me.zodd.*", - ) - - private fun mergeImports(): List { - val imports = mutableListOf() - imports.addAll(defaultImports) - imports.addAll(defaultPlatformImports) - return imports - } - - private val configuration = createJvmCompilationConfigurationFromTemplate { - compilerOptions("-jvm-target=21") - defaultImports(*mergeImports().toTypedArray()) - jvm { - - dependenciesFromCurrentContext(wholeClasspath = true) - - // https://youtrack.jetbrains.com/issue/KT-57907 - compilerOptions.append("-Xadd-modules=ALL-MODULE-PATH") - } - } - - private val evalConfig = createJvmEvaluationConfigurationFromTemplate { - -// scriptExecutionWrapper { -// val api = scriptContainer -// } - - } - - fun eval(): ResultWithDiagnostics { - return BasicJvmScriptingHost().eval(compile(), configuration, evalConfig) - } - fun eval( configuration: ScriptCompilationConfiguration, evalConfig: ScriptEvaluationConfiguration @@ -117,36 +67,47 @@ object ScriptLoader { private val scriptFileDir = File(SCRIPT_DIR) /** - * defaultPlatformImports should be a list of common imports for scripts to use - * This will allow scripts to omit these imports + * @param defaultImports A list of default imports to provide to scripts + * @param classloaderClass The class instance to share the classloader to scripts */ - fun loadScripts(container: KtScriptPluginContainer<*, *>, defaultPlatformImports: List) { - scriptFileDir.mkdirs() - scriptFileDir.listFiles()?.forEach { file -> - ScriptCoreLogger.logger.info("Loading script : ${file.name}...") - Script(file.readText(), defaultPlatformImports, container).eval().logResult(file.name) - } - } - fun loadScripts( - container: KtScriptPluginContainer<*, *>, - defaultPlatformImports: List, - configuration: ScriptCompilationConfiguration, - evalConfig: ScriptEvaluationConfiguration, + defaultImports: List, + classloaderClass: KClass<*>, ) { scriptFileDir.mkdirs() scriptFileDir.listFiles()?.forEach { file -> ScriptCoreLogger.logger.info("Loading script : ${file.name}...") - Script(file.readText(), defaultPlatformImports, container).eval(configuration, evalConfig) + Script(file.readText()).eval( + configurationConfig(defaultImports), + evaluationConfig(classloaderClass) + ) .logResult(file.name) } } + private fun evaluationConfig(classloaderClass: KClass<*>) = + createJvmEvaluationConfigurationFromTemplate { + jvm { + baseClassLoader.put(classloaderClass.java.classLoader) + } + } + + private fun configurationConfig(imports: List) = + createJvmCompilationConfigurationFromTemplate { + ide.acceptedLocations(ScriptAcceptedLocation.Everywhere) + compilerOptions("-jvm-target=21") + defaultImports(imports) + jvm { + dependenciesFromCurrentContext(wholeClasspath = true) + // https://youtrack.jetbrains.com/issue/KT-57907 + compilerOptions.append("-Xadd-modules=ALL-MODULE-PATH") + } + } + private fun ResultWithDiagnostics.logResult(name: String) { onFailure { LogInfo(name, it.reports).printLog() }.onSuccess { - ScriptCoreLogger.logger.info("Script: $name successfully loaded! - {${it}}") asSuccess() } } diff --git a/platforms/paper/api/src/main/kotlin/me/zodd/API.kt b/platforms/paper/api/src/main/kotlin/me/zodd/API.kt index 68d3bb0..a15421b 100644 --- a/platforms/paper/api/src/main/kotlin/me/zodd/API.kt +++ b/platforms/paper/api/src/main/kotlin/me/zodd/API.kt @@ -3,7 +3,7 @@ package me.zodd import org.bukkit.plugin.Plugin import org.slf4j.Logger -data class API( +class API( override val container: Plugin, override val logger: Logger, ) : KtScriptPluginContainer { @@ -15,10 +15,7 @@ data class API( } init { - println("Being Created") Companion.container = container Companion.logger = logger } - - } \ No newline at end of file diff --git a/platforms/paper/api/src/main/kotlin/me/zodd/Events.kt b/platforms/paper/api/src/main/kotlin/me/zodd/Events.kt index da0de7a..cc794f3 100644 --- a/platforms/paper/api/src/main/kotlin/me/zodd/Events.kt +++ b/platforms/paper/api/src/main/kotlin/me/zodd/Events.kt @@ -1,47 +1,78 @@ package me.zodd +import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent +import io.papermc.paper.event.entity.EntityMoveEvent +import org.bukkit.entity.EntityType +import org.bukkit.event.block.BlockBreakEvent +import org.bukkit.event.block.BlockMultiPlaceEvent +import org.bukkit.event.block.BlockPlaceEvent +import org.bukkit.event.entity.EntityDamageByEntityEvent import org.bukkit.event.entity.EntityDamageEvent +import org.bukkit.event.entity.EntityDeathEvent +import org.bukkit.event.entity.EntitySpawnEvent +import org.bukkit.event.entity.PlayerDeathEvent +import org.bukkit.event.player.PlayerInteractEntityEvent +import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.event.player.PlayerLoginEvent +import org.bukkit.event.player.PlayerMoveEvent import org.bukkit.event.player.PlayerQuitEvent import org.bukkit.event.server.ServerLoadEvent +// INTERACTION + // === EVENTS === -// INTERACTION -fun onServerLoad(executor: (ServerLoadEvent) -> Unit) = RegistrationHelper.registerListener(executor) - - -//fun onBlockLeftClick(executor: (Listener,PlayerInteractEvent) -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onBlockRightClick(executor: InteractBlockEvent.Secondary.() -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onEntityLeftClick(executor: InteractEntityEvent.Primary.() -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onEntityRightClick(executor: InteractEntityEvent.Secondary.() -> Unit) = -// RegistrationHelper.registerListener(executor) -// +fun onPlayerInteract(executor: PlayerInteractEvent.() -> Unit) = RegistrationHelper.registerListener(executor) + +fun onBlockInteract(fn: PlayerInteractEvent.() -> Unit) = onPlayerInteract { + if (hasBlock()) { + fn() + } +} + +fun onBlockLeftClick(fn: () -> Unit) = onBlockInteract { + if (action.isLeftClick) { + fn() + } +} + +fun onBlockRightClick(fn: () -> Unit) = onBlockInteract { + if (action.isRightClick) { + fn() + } +} + +fun onEntityInteract(fn: PlayerInteractEntityEvent.() -> Unit) = RegistrationHelper.registerListener(fn) +fun onEntityRightClick(fn: PlayerInteractEntityEvent.() -> Unit) = onEntityInteract(fn) + +fun onEntityDamageByEntity(fn: EntityDamageByEntityEvent.() -> Unit) = RegistrationHelper.registerListener(fn) +fun onEntityLeftClick(fn: EntityDamageByEntityEvent.() -> Unit) = onEntityDamageByEntity(fn) + //// MOVE -// -//fun onEntityMove(executor: MoveEntityEvent.() -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onPlayerMove(executor: MoveEntityEvent.() -> Unit) = -// onEntityMove { if (entity().type() == EntityTypes.PLAYER) executor() } -// + +fun onEntityMove(fn: EntityMoveEvent.() -> Unit) = RegistrationHelper.registerListener(fn) + +fun onPlayerMove(fn: PlayerMoveEvent.() -> Unit) = RegistrationHelper.registerListener(fn) + //// BLOCKS -// -//fun onBlockChange(executor: ChangeBlockEvent.() -> Unit) = RegistrationHelper.registerListener(executor) -// + +fun onBlockBreak(fn: BlockBreakEvent.() -> Unit) = RegistrationHelper.registerListener(fn) + +fun onBlockPlace(fn: BlockPlaceEvent.() -> Unit) = RegistrationHelper.registerListener(fn) + +fun onMultiBlockPlace(fn: BlockMultiPlaceEvent.() -> Unit) = RegistrationHelper.registerListener(fn) + //// ENTITIES -// -//fun onEntitySpawn(executor: SpawnEntityEvent.() -> Unit) = RegistrationHelper.registerListener(executor) -// + +fun onEntitySpawn(fn: EntitySpawnEvent.() -> Unit) = RegistrationHelper.registerListener(fn) + fun onEntityDamage(executor: EntityDamageEvent.() -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onEntityRemove(executor: DestructEntityEvent.() -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onEntityDeath(executor: DestructEntityEvent.Death.() -> Unit) = RegistrationHelper.registerListener(executor) -// + +fun onEntityDeath(fn: EntityDeathEvent.() -> Unit) = RegistrationHelper.registerListener(fn) + +fun onEntityRemove(fn: EntityRemoveFromWorldEvent.() -> Unit) = RegistrationHelper.registerListener(fn) + //// PLAYERS fun onPlayerLogin(executor: PlayerLoginEvent.() -> Unit) = RegistrationHelper.registerListener(executor) @@ -50,28 +81,18 @@ fun onPlayerJoin(executor: PlayerJoinEvent.() -> Unit) = RegistrationHelper.regi fun onPlayerLeave(executor: PlayerQuitEvent.() -> Unit) = RegistrationHelper.registerListener(executor) +fun onPlayerDamage(fn: EntityDamageEvent.() -> Unit) = onEntityDamage { + if (entityType == EntityType.PLAYER) { + fn() + } +} + +fun onPlayerDeath(fn: PlayerDeathEvent.() -> Unit) = RegistrationHelper.registerListener(fn) -//fun onPlayerDamage(executor: DamageEntityEvent.() -> Unit) = onEntityDamage { if (entity() is Player) executor() } -// -//fun onPlayerDeath(executor: DestructEntityEvent.Death.() -> Unit) = -// onEntityDeath { if (entity() is Player) executor() } -// //// SERVER -// -//fun onServerStarting(executor: (ServerLoadEvent) -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onServerStarted(executor: StartedEngineEvent.() -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onServerStopping(executor: StoppingEngineEvent.() -> Unit) = RegistrationHelper.registerListener(executor) -// -//fun onServerStopped(executor: StoppedGameEvent.() -> Unit) = RegistrationHelper.registerListener(executor) -// -//// COMMANDS -// -//fun onRegisterCommandsParameterized(executor: RegisterCommandEvent.() -> Unit) = -// RegistrationHelper.registerListener(executor) -// + +fun onServerLoad(executor: ServerLoadEvent.() -> Unit) = RegistrationHelper.registerListener(executor) + //// OTHERS -// -////fun onScriptsUnload(executor: UnloadScriptsEvent.() -> Unit) = registerListener(executor) -// + +//// fun onScriptsUnload(executor: UnloadScriptsEvent.() -> Unit) = registerListener(executor) diff --git a/platforms/paper/api/src/main/kotlin/me/zodd/RegistrationHelper.kt b/platforms/paper/api/src/main/kotlin/me/zodd/RegistrationHelper.kt index d285b5a..def10ca 100644 --- a/platforms/paper/api/src/main/kotlin/me/zodd/RegistrationHelper.kt +++ b/platforms/paper/api/src/main/kotlin/me/zodd/RegistrationHelper.kt @@ -7,8 +7,17 @@ import org.bukkit.event.Listener import org.bukkit.plugin.Plugin object RegistrationHelper { + /** + * A simple event listener register wrapper + * @param E Where E is the Event to register + * @param executor Executor runs when the event fires + * @param eventClass the event class to register + * @param listener A listener Dummy object + * @param priority The priority for the event to fire + * @param plugin The Plugin instance to register the listener to + */ inline fun registerListener( - noinline executor: (event: E) -> Unit, + noinline executor: E.() -> Unit, eventClass: Class = E::class.java, listener: Listener = object : Listener {}, priority: EventPriority = EventPriority.NORMAL, @@ -23,6 +32,5 @@ object RegistrationHelper { plugin ) } - } diff --git a/platforms/paper/host/build.gradle.kts b/platforms/paper/host/build.gradle.kts index 0ac288b..0d41172 100644 --- a/platforms/paper/host/build.gradle.kts +++ b/platforms/paper/host/build.gradle.kts @@ -6,8 +6,6 @@ plugins { dependencies { shadow(project(":paper-api")) - - paperweight.paperDevBundle(libs.versions.paper.api) } @@ -15,12 +13,7 @@ tasks { assemble { dependsOn(reobfJar) } - runServer { minecraftVersion(libs.versions.minecraft.get()) } } - -java { - toolchain.languageVersion.set(JavaLanguageVersion.of(21)) -} \ No newline at end of file diff --git a/platforms/paper/host/src/main/kotlin/me/zodd/Host.kt b/platforms/paper/host/src/main/kotlin/me/zodd/Host.kt index 0545f91..1153a86 100644 --- a/platforms/paper/host/src/main/kotlin/me/zodd/Host.kt +++ b/platforms/paper/host/src/main/kotlin/me/zodd/Host.kt @@ -1,19 +1,6 @@ package me.zodd import org.bukkit.plugin.java.JavaPlugin -import kotlin.script.experimental.api.ScriptAcceptedLocation -import kotlin.script.experimental.api.acceptedLocations -import kotlin.script.experimental.api.compilerOptions -import kotlin.script.experimental.api.defaultImports -import kotlin.script.experimental.api.ide -import kotlin.script.experimental.jvm.baseClassLoader -import kotlin.script.experimental.jvm.dependenciesFromClassContext -import kotlin.script.experimental.jvm.dependenciesFromClassloader -import kotlin.script.experimental.jvm.dependenciesFromCurrentContext -import kotlin.script.experimental.jvm.jvm -import kotlin.script.experimental.jvm.loadDependencies -import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate -import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTemplate class Host : JavaPlugin() { @@ -29,30 +16,14 @@ class Host : JavaPlugin() { "org.slf4j.Logger" ) - val compConfig = createJvmCompilationConfigurationFromTemplate { - ide.acceptedLocations(ScriptAcceptedLocation.Everywhere) - compilerOptions("-jvm-target=21") - defaultImports(imports) - jvm { - dependenciesFromCurrentContext(wholeClasspath = true) - compilerOptions.append("-Xadd-modules=ALL-MODULE-PATH") - } - } - - val evalConfig = createJvmEvaluationConfigurationFromTemplate { - jvm { - baseClassLoader.put(this@Host.classLoader) - } - } + // Initialize API for scripts + API(this, slF4JLogger) ScriptLoader.loadScripts( - API(this, slF4JLogger), imports, - compConfig, - evalConfig + this@Host::class ) slF4JLogger.info("Finished loading scripts...") } } - diff --git a/platforms/paper/host/src/main/resources/paper-plugin.yml b/platforms/paper/host/src/main/resources/paper-plugin.yml index 96b6fc9..f795024 100644 --- a/platforms/paper/host/src/main/resources/paper-plugin.yml +++ b/platforms/paper/host/src/main/resources/paper-plugin.yml @@ -4,9 +4,3 @@ main: me.zodd.Host description: Kotlin Scripting for Paper author: Zodd api-version: '1.20' -#bootstrapper: io.papermc.testplugin.TestPluginBootstrap -#loader: io.papermc.testplugin.TestPluginLoader -# dependencies: -# server: -# MCKotlin-Paper: -# required: true diff --git a/script-definition/src/main/kotlin/me/zodd/PluginScript.kt b/script-definition/src/main/kotlin/me/zodd/PluginScript.kt index 141508d..49f5459 100644 --- a/script-definition/src/main/kotlin/me/zodd/PluginScript.kt +++ b/script-definition/src/main/kotlin/me/zodd/PluginScript.kt @@ -1,17 +1,8 @@ package me.zodd import kotlin.script.experimental.annotations.KotlinScript -import kotlin.script.experimental.api.ScriptAcceptedLocation -import kotlin.script.experimental.api.ScriptCompilationConfiguration -import kotlin.script.experimental.api.acceptedLocations -import kotlin.script.experimental.api.compilerOptions -import kotlin.script.experimental.api.defaultImports -import kotlin.script.experimental.api.ide -import kotlin.script.experimental.jvm.dependenciesFromClassloader -import kotlin.script.experimental.jvm.dependenciesFromCurrentContext -import kotlin.script.experimental.jvm.jvm @KotlinScript( fileExtension = "plugin.kts", ) -abstract class PluginScript +abstract class PluginScript \ No newline at end of file