Skip to content

Commit 2195553

Browse files
committed
Version Catalog & Cleanup
1 parent 0ba774e commit 2195553

File tree

15 files changed

+146
-190
lines changed

15 files changed

+146
-190
lines changed

buildSrc/build.gradle.kts

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ repositories {
77
}
88

99
dependencies {
10-
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20")
11-
implementation("com.github.johnrengelman:shadow:8.1.1")
10+
implementation(tooling.kotlin)
11+
implementation(tooling.shadow)
12+
implementation(files(tooling.javaClass.superclass.protectionDomain.codeSource.location))
13+
1214
}
1315

1416
java {

buildSrc/settings.gradle.kts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dependencyResolutionManagement {
2+
versionCatalogs {
3+
create("libs") {
4+
from(files("../gradle/libraries.versions.toml"))
5+
}
6+
create("tooling") {
7+
from(files("../gradle/toolchain.versions.toml"))
8+
}
9+
}
10+
}

buildSrc/src/main/kotlin/api-plugin.gradle.kts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ plugins {
44
id("common")
55
}
66

7-
87
tasks.jar {
98
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
109
}

buildSrc/src/main/kotlin/common.gradle.kts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ plugins {
33
}
44

55
group = "me.zodd"
6-
version = "0.1.7"
6+
version = "0.1.91"
77

88
repositories {
99
mavenCentral()
1010
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
11+
}
12+
13+
java {
14+
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
1115
}

buildSrc/src/main/kotlin/host-plugin.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dependencies {
77
implementation("org.spongepowered:configurate-extra-kotlin:4.1.2") {
88
isTransitive = false
99
}
10-
shadow(kotlin("reflect"))
10+
compileOnly(kotlin("reflect"))
1111
shadow(kotlin("scripting-jvm-host"))
1212
shadow(project(":KotlinScript-script-definition"))
1313
shadow(project(":KotlinScript-kotlin-script-api"))

gradle/toolchain.versions.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
[versions]
2-
3-
[plugins]
2+
project_version = "0.1.8"
3+
shadow = "8.1.1"
4+
kotlin = "1.9.20"
5+
configurate = "4.1.2"
46

57
[libraries]
8+
configurate = { group = "org.spongepowered", name = "configurate-extra-koltin", version.ref = "configurate" }
9+
shadow = { group = "com.github.johnrengelman", name = "shadow", version.ref = "shadow" }
10+
kotlin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }

kotlin-script-api/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ dependencies {
1010
compileOnly(kotlin("scripting-jvm-host"))
1111
compileOnly(project(":KotlinScript-script-definition"))
1212
compileOnly(libs.paper)
13-
compileOnly("org.spongepowered:spongeapi:8.1.0")
13+
compileOnly(libs.sponge8)
1414
}

kotlin-script-api/src/main/kotlin/me/zodd/ScriptCoreLogger.kt

+30-69
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,22 @@ import java.io.File
44
import kotlin.reflect.KClass
55
import kotlin.script.experimental.api.EvaluationResult
66
import kotlin.script.experimental.api.ResultWithDiagnostics
7+
import kotlin.script.experimental.api.ScriptAcceptedLocation
78
import kotlin.script.experimental.api.ScriptCompilationConfiguration
89
import kotlin.script.experimental.api.ScriptDiagnostic
910
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
1011
import kotlin.script.experimental.api.SourceCode
12+
import kotlin.script.experimental.api.acceptedLocations
1113
import kotlin.script.experimental.api.asSuccess
12-
import kotlin.script.experimental.api.compilationConfiguration
1314
import kotlin.script.experimental.api.compilerOptions
1415
import kotlin.script.experimental.api.defaultImports
15-
import kotlin.script.experimental.api.hostConfiguration
16+
import kotlin.script.experimental.api.ide
1617
import kotlin.script.experimental.api.onFailure
1718
import kotlin.script.experimental.api.onSuccess
18-
import kotlin.script.experimental.api.providedProperties
19-
import kotlin.script.experimental.api.refineConfigurationBeforeEvaluate
20-
import kotlin.script.experimental.api.scriptExecutionWrapper
2119
import kotlin.script.experimental.host.toScriptSource
2220
import kotlin.script.experimental.jvm.baseClassLoader
23-
import kotlin.script.experimental.jvm.dependenciesFromClassContext
24-
import kotlin.script.experimental.jvm.dependenciesFromClassloader
2521
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
2622
import kotlin.script.experimental.jvm.jvm
27-
import kotlin.script.experimental.jvm.loadDependencies
28-
import kotlin.script.experimental.jvm.util.classpathFromClass
29-
import kotlin.script.experimental.jvm.util.classpathFromClassloader
3023
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
3124
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
3225
import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTemplate
@@ -53,50 +46,7 @@ interface KtScriptPluginContainer<C, L> {
5346
*/
5447
class Script(
5548
private val script: String,
56-
private val defaultPlatformImports: List<String>,
57-
private val scriptContainer: KtScriptPluginContainer<*, *>
5849
) {
59-
private val defaultImports: List<String> = listOf(
60-
//Kotlin Packages
61-
"kotlin.reflect.*",
62-
"kotlin.reflect.jvm.*",
63-
//Kyori
64-
"net.kyori.adventure.text.*",
65-
//Plugin
66-
"me.zodd.*",
67-
)
68-
69-
private fun mergeImports(): List<String> {
70-
val imports = mutableListOf<String>()
71-
imports.addAll(defaultImports)
72-
imports.addAll(defaultPlatformImports)
73-
return imports
74-
}
75-
76-
private val configuration = createJvmCompilationConfigurationFromTemplate<PluginScript> {
77-
compilerOptions("-jvm-target=21")
78-
defaultImports(*mergeImports().toTypedArray())
79-
jvm {
80-
81-
dependenciesFromCurrentContext(wholeClasspath = true)
82-
83-
// https://youtrack.jetbrains.com/issue/KT-57907
84-
compilerOptions.append("-Xadd-modules=ALL-MODULE-PATH")
85-
}
86-
}
87-
88-
private val evalConfig = createJvmEvaluationConfigurationFromTemplate<PluginScript> {
89-
90-
// scriptExecutionWrapper {
91-
// val api = scriptContainer
92-
// }
93-
94-
}
95-
96-
fun eval(): ResultWithDiagnostics<EvaluationResult> {
97-
return BasicJvmScriptingHost().eval(compile(), configuration, evalConfig)
98-
}
99-
10050
fun eval(
10151
configuration: ScriptCompilationConfiguration,
10252
evalConfig: ScriptEvaluationConfiguration
@@ -117,36 +67,47 @@ object ScriptLoader {
11767
private val scriptFileDir = File(SCRIPT_DIR)
11868

11969
/**
120-
* defaultPlatformImports should be a list of common imports for scripts to use
121-
* This will allow scripts to omit these imports
70+
* @param defaultImports A list of default imports to provide to scripts
71+
* @param classloaderClass The class instance to share the classloader to scripts
12272
*/
123-
fun loadScripts(container: KtScriptPluginContainer<*, *>, defaultPlatformImports: List<String>) {
124-
scriptFileDir.mkdirs()
125-
scriptFileDir.listFiles()?.forEach { file ->
126-
ScriptCoreLogger.logger.info("Loading script : ${file.name}...")
127-
Script(file.readText(), defaultPlatformImports, container).eval().logResult(file.name)
128-
}
129-
}
130-
13173
fun loadScripts(
132-
container: KtScriptPluginContainer<*, *>,
133-
defaultPlatformImports: List<String>,
134-
configuration: ScriptCompilationConfiguration,
135-
evalConfig: ScriptEvaluationConfiguration,
74+
defaultImports: List<String>,
75+
classloaderClass: KClass<*>,
13676
) {
13777
scriptFileDir.mkdirs()
13878
scriptFileDir.listFiles()?.forEach { file ->
13979
ScriptCoreLogger.logger.info("Loading script : ${file.name}...")
140-
Script(file.readText(), defaultPlatformImports, container).eval(configuration, evalConfig)
80+
Script(file.readText()).eval(
81+
configurationConfig(defaultImports),
82+
evaluationConfig(classloaderClass)
83+
)
14184
.logResult(file.name)
14285
}
14386
}
14487

88+
private fun evaluationConfig(classloaderClass: KClass<*>) =
89+
createJvmEvaluationConfigurationFromTemplate<PluginScript> {
90+
jvm {
91+
baseClassLoader.put(classloaderClass.java.classLoader)
92+
}
93+
}
94+
95+
private fun configurationConfig(imports: List<String>) =
96+
createJvmCompilationConfigurationFromTemplate<PluginScript> {
97+
ide.acceptedLocations(ScriptAcceptedLocation.Everywhere)
98+
compilerOptions("-jvm-target=21")
99+
defaultImports(imports)
100+
jvm {
101+
dependenciesFromCurrentContext(wholeClasspath = true)
102+
// https://youtrack.jetbrains.com/issue/KT-57907
103+
compilerOptions.append("-Xadd-modules=ALL-MODULE-PATH")
104+
}
105+
}
106+
145107
private fun ResultWithDiagnostics<EvaluationResult>.logResult(name: String) {
146108
onFailure {
147109
LogInfo(name, it.reports).printLog()
148110
}.onSuccess {
149-
ScriptCoreLogger.logger.info("Script: $name successfully loaded! - {${it}}")
150111
asSuccess()
151112
}
152113
}

platforms/paper/api/src/main/kotlin/me/zodd/API.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package me.zodd
33
import org.bukkit.plugin.Plugin
44
import org.slf4j.Logger
55

6-
data class API(
6+
class API(
77
override val container: Plugin,
88
override val logger: Logger,
99
) : KtScriptPluginContainer<Plugin, Logger> {
@@ -15,10 +15,7 @@ data class API(
1515
}
1616

1717
init {
18-
println("Being Created")
1918
Companion.container = container
2019
Companion.logger = logger
2120
}
22-
23-
2421
}
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,78 @@
11
package me.zodd
22

3+
import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent
4+
import io.papermc.paper.event.entity.EntityMoveEvent
5+
import org.bukkit.entity.EntityType
6+
import org.bukkit.event.block.BlockBreakEvent
7+
import org.bukkit.event.block.BlockMultiPlaceEvent
8+
import org.bukkit.event.block.BlockPlaceEvent
9+
import org.bukkit.event.entity.EntityDamageByEntityEvent
310
import org.bukkit.event.entity.EntityDamageEvent
11+
import org.bukkit.event.entity.EntityDeathEvent
12+
import org.bukkit.event.entity.EntitySpawnEvent
13+
import org.bukkit.event.entity.PlayerDeathEvent
14+
import org.bukkit.event.player.PlayerInteractEntityEvent
15+
import org.bukkit.event.player.PlayerInteractEvent
416
import org.bukkit.event.player.PlayerJoinEvent
517
import org.bukkit.event.player.PlayerLoginEvent
18+
import org.bukkit.event.player.PlayerMoveEvent
619
import org.bukkit.event.player.PlayerQuitEvent
720
import org.bukkit.event.server.ServerLoadEvent
821

22+
// INTERACTION
23+
924
// === EVENTS ===
1025

11-
// INTERACTION
12-
fun onServerLoad(executor: (ServerLoadEvent) -> Unit) = RegistrationHelper.registerListener(executor)
13-
14-
15-
//fun onBlockLeftClick(executor: (Listener,PlayerInteractEvent) -> Unit) = RegistrationHelper.registerListener(executor)
16-
//
17-
//fun onBlockRightClick(executor: InteractBlockEvent.Secondary.() -> Unit) = RegistrationHelper.registerListener(executor)
18-
//
19-
//fun onEntityLeftClick(executor: InteractEntityEvent.Primary.() -> Unit) = RegistrationHelper.registerListener(executor)
20-
//
21-
//fun onEntityRightClick(executor: InteractEntityEvent.Secondary.() -> Unit) =
22-
// RegistrationHelper.registerListener(executor)
23-
//
26+
fun onPlayerInteract(executor: PlayerInteractEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
27+
28+
fun onBlockInteract(fn: PlayerInteractEvent.() -> Unit) = onPlayerInteract {
29+
if (hasBlock()) {
30+
fn()
31+
}
32+
}
33+
34+
fun onBlockLeftClick(fn: () -> Unit) = onBlockInteract {
35+
if (action.isLeftClick) {
36+
fn()
37+
}
38+
}
39+
40+
fun onBlockRightClick(fn: () -> Unit) = onBlockInteract {
41+
if (action.isRightClick) {
42+
fn()
43+
}
44+
}
45+
46+
fun onEntityInteract(fn: PlayerInteractEntityEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
47+
fun onEntityRightClick(fn: PlayerInteractEntityEvent.() -> Unit) = onEntityInteract(fn)
48+
49+
fun onEntityDamageByEntity(fn: EntityDamageByEntityEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
50+
fun onEntityLeftClick(fn: EntityDamageByEntityEvent.() -> Unit) = onEntityDamageByEntity(fn)
51+
2452
//// MOVE
25-
//
26-
//fun onEntityMove(executor: MoveEntityEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
27-
//
28-
//fun onPlayerMove(executor: MoveEntityEvent.() -> Unit) =
29-
// onEntityMove { if (entity().type() == EntityTypes.PLAYER) executor() }
30-
//
53+
54+
fun onEntityMove(fn: EntityMoveEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
55+
56+
fun onPlayerMove(fn: PlayerMoveEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
57+
3158
//// BLOCKS
32-
//
33-
//fun onBlockChange(executor: ChangeBlockEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
34-
//
59+
60+
fun onBlockBreak(fn: BlockBreakEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
61+
62+
fun onBlockPlace(fn: BlockPlaceEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
63+
64+
fun onMultiBlockPlace(fn: BlockMultiPlaceEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
65+
3566
//// ENTITIES
36-
//
37-
//fun onEntitySpawn(executor: SpawnEntityEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
38-
//
67+
68+
fun onEntitySpawn(fn: EntitySpawnEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
69+
3970
fun onEntityDamage(executor: EntityDamageEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
40-
//
41-
//fun onEntityRemove(executor: DestructEntityEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
42-
//
43-
//fun onEntityDeath(executor: DestructEntityEvent.Death.() -> Unit) = RegistrationHelper.registerListener(executor)
44-
//
71+
72+
fun onEntityDeath(fn: EntityDeathEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
73+
74+
fun onEntityRemove(fn: EntityRemoveFromWorldEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
75+
4576
//// PLAYERS
4677

4778
fun onPlayerLogin(executor: PlayerLoginEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
@@ -50,28 +81,18 @@ fun onPlayerJoin(executor: PlayerJoinEvent.() -> Unit) = RegistrationHelper.regi
5081

5182
fun onPlayerLeave(executor: PlayerQuitEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
5283

84+
fun onPlayerDamage(fn: EntityDamageEvent.() -> Unit) = onEntityDamage {
85+
if (entityType == EntityType.PLAYER) {
86+
fn()
87+
}
88+
}
89+
90+
fun onPlayerDeath(fn: PlayerDeathEvent.() -> Unit) = RegistrationHelper.registerListener(fn)
5391

54-
//fun onPlayerDamage(executor: DamageEntityEvent.() -> Unit) = onEntityDamage { if (entity() is Player) executor() }
55-
//
56-
//fun onPlayerDeath(executor: DestructEntityEvent.Death.() -> Unit) =
57-
// onEntityDeath { if (entity() is Player) executor() }
58-
//
5992
//// SERVER
60-
//
61-
//fun onServerStarting(executor: (ServerLoadEvent) -> Unit) = RegistrationHelper.registerListener(executor)
62-
//
63-
//fun onServerStarted(executor: StartedEngineEvent<Engine>.() -> Unit) = RegistrationHelper.registerListener(executor)
64-
//
65-
//fun onServerStopping(executor: StoppingEngineEvent<Engine>.() -> Unit) = RegistrationHelper.registerListener(executor)
66-
//
67-
//fun onServerStopped(executor: StoppedGameEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
68-
//
69-
//// COMMANDS
70-
//
71-
//fun onRegisterCommandsParameterized(executor: RegisterCommandEvent<Parameterized>.() -> Unit) =
72-
// RegistrationHelper.registerListener(executor)
73-
//
93+
94+
fun onServerLoad(executor: ServerLoadEvent.() -> Unit) = RegistrationHelper.registerListener(executor)
95+
7496
//// OTHERS
75-
//
76-
////fun onScriptsUnload(executor: UnloadScriptsEvent.() -> Unit) = registerListener(executor)
77-
//
97+
98+
//// fun onScriptsUnload(executor: UnloadScriptsEvent.() -> Unit) = registerListener(executor)

0 commit comments

Comments
 (0)