├── project-icon.png ├── gradle ├── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties └── libs.versions.toml ├── exposed ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── spring │ │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ │ └── kotlin │ │ └── kr │ │ └── junhyung │ │ └── mainframe │ │ └── exposed │ │ ├── table │ │ ├── Table.kt │ │ └── TableInitializer.kt │ │ ├── adventure │ │ └── ComponentColoumnType.kt │ │ ├── transaction │ │ ├── ExposedTransaction.kt │ │ └── ExposedTransactionManager.kt │ │ └── ExposedAutoConfiguration.kt └── build.gradle.kts ├── brigadier ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── spring │ │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ │ └── kotlin │ │ └── kr │ │ └── junhyung │ │ └── mainframe │ │ └── brigadier │ │ ├── CommandAnnotatedElement.kt │ │ ├── CommandRegistrar.kt │ │ ├── CommandAnnotationProcessor.kt │ │ ├── Command.kt │ │ └── CommandAutoConfiguration.kt └── build.gradle.kts ├── platforms ├── paper-brigadier │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── spring │ │ │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ │ │ └── kotlin │ │ │ └── kr │ │ │ └── junhyung │ │ │ └── mainframe │ │ │ └── paper │ │ │ └── brigadier │ │ │ ├── PaperCommandAutoConfiguration.kt │ │ │ └── PaperCommandRegistrar.kt │ └── build.gradle.kts ├── paper │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── spring │ │ │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ │ │ └── kotlin │ │ │ └── kr │ │ │ └── junhyung │ │ │ └── mainframe │ │ │ └── paper │ │ │ ├── condition │ │ │ ├── ConditionalOnPlugin.kt │ │ │ ├── ConditionalOnMinecraftVersion.kt │ │ │ ├── OnPluginCondition.kt │ │ │ └── OnMinecraftVersionCondition.kt │ │ │ ├── coroutines │ │ │ ├── BukkitDispatchers.kt │ │ │ ├── BukkitPluginCoroutineScope.kt │ │ │ ├── BukkitDispatchersImpl.kt │ │ │ └── BukkitCoroutinesAutoConfiguration.kt │ │ │ ├── PaperPluginAutoConfiguration.kt │ │ │ ├── service │ │ │ └── ServiceAnnotationProcessor.kt │ │ │ ├── PaperPluginApplicationContext.kt │ │ │ ├── event │ │ │ └── EventHandlerAnnotationProcessor.kt │ │ │ └── MainframePlugin.kt │ └── build.gradle.kts └── velocity │ ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── spring │ │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ │ └── kotlin │ │ └── kr │ │ └── junhyung │ │ └── mainframe │ │ └── velocity │ │ ├── VelocityPluginApplicationContext.kt │ │ ├── brigadier │ │ ├── VelocityCommandAutoConfiguration.kt │ │ ├── VelocityCommandRegistrar.kt │ │ └── MainframeCommand.kt │ │ ├── coroutines │ │ ├── VelocityPluginCoroutineScope.kt │ │ └── VelocityCoroutinesAutoConfiguration.kt │ │ ├── VelocityPluginAutoConfiguration.kt │ │ ├── MainframePlugin.kt │ │ └── event │ │ └── SubscribeAnnotationProcessor.kt │ └── build.gradle.kts ├── core ├── src │ └── main │ │ └── kotlin │ │ └── kr │ │ └── junhyung │ │ └── mainframe │ │ └── core │ │ ├── jackson │ │ ├── ObjectMapperAware.kt │ │ └── ObjectMapperAwareBeanPostProcessor.kt │ │ ├── coroutines │ │ ├── CoroutineScopeAware.kt │ │ ├── AbstractCoroutineExceptionHandler.kt │ │ ├── CoroutineScopeExtensions.kt │ │ ├── CoroutineScopeAwareBeanPostProcessor.kt │ │ └── GracefulShutdownCoroutineScope.kt │ │ ├── util │ │ ├── IdentifiedKeyProvider.kt │ │ ├── stdlib │ │ │ └── CastExtensions.kt │ │ ├── StringKeyProvider.kt │ │ ├── UUIDKeyProvider.kt │ │ └── ClassLoaderUtils.kt │ │ ├── event │ │ └── Listener.kt │ │ ├── transaction │ │ ├── TransactionManager.kt │ │ └── Transaction.kt │ │ ├── adventure │ │ └── AdventureComponentExtensions.kt │ │ ├── MainframeApplicationCustomizer.kt │ │ ├── concurrent │ │ └── DistributedLock.kt │ │ ├── io │ │ └── PrioritizedResourceLoader.kt │ │ ├── annotation │ │ ├── ClassAnnotationProcessor.kt │ │ └── MethodAnnotationProcessor.kt │ │ └── MainframeApplicationContext.kt └── build.gradle.kts ├── bom └── build.gradle.kts ├── gradle.properties ├── gradle-plugin ├── src │ └── main │ │ └── kotlin │ │ └── kr │ │ └── junhyung │ │ └── mainframe │ │ └── gradle │ │ ├── MainframeExtension.kt │ │ ├── shadow │ │ └── MainframeShadowPlugin.kt │ │ ├── bom │ │ ├── MainframeDependenciesExtension.kt │ │ └── MainframeBomPlugin.kt │ │ └── MainframePlugin.kt └── build.gradle.kts ├── .gitignore ├── settings.gradle.kts ├── gradlew.bat ├── gradlew └── LICENSE.txt /project-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vjh0107/mainframe/HEAD/project-icon.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vjh0107/mainframe/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /exposed/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | kr.junhyung.mainframe.exposed.ExposedAutoConfiguration -------------------------------------------------------------------------------- /brigadier/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | kr.junhyung.mainframe.brigadier.CommandAutoConfiguration -------------------------------------------------------------------------------- /platforms/paper-brigadier/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | kr.junhyung.mainframe.paper.brigadier.PaperCommandAutoConfiguration -------------------------------------------------------------------------------- /brigadier/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | alias(conventions.plugins.kotlin.jvm) 3 | alias(conventions.plugins.publishing) 4 | } 5 | 6 | dependencies { 7 | api(projects.core) 8 | 9 | api(libs.brigadier) 10 | } -------------------------------------------------------------------------------- /brigadier/src/main/kotlin/kr/junhyung/mainframe/brigadier/CommandAnnotatedElement.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.brigadier 2 | 3 | public interface CommandAnnotatedElement { 4 | 5 | public fun setCommand(command: Command) 6 | 7 | } -------------------------------------------------------------------------------- /brigadier/src/main/kotlin/kr/junhyung/mainframe/brigadier/CommandRegistrar.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.brigadier 2 | 3 | public interface CommandRegistrar { 4 | 5 | public fun registerCommand(command: Any, metadata: Command) 6 | 7 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | kr.junhyung.mainframe.paper.PaperPluginAutoConfiguration 2 | kr.junhyung.mainframe.paper.coroutines.BukkitCoroutinesAutoConfiguration -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists -------------------------------------------------------------------------------- /platforms/paper/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `java-library` 3 | alias(conventions.plugins.kotlin.jvm) 4 | alias(conventions.plugins.publishing) 5 | } 6 | 7 | dependencies { 8 | api(projects.core) 9 | 10 | compileOnlyApi(libs.paper.api) 11 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/jackson/ObjectMapperAware.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.jackson 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper 4 | 5 | public interface ObjectMapperAware { 6 | 7 | public fun setObjectMapper(objectMapperAware: ObjectMapper) 8 | 9 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/coroutines/CoroutineScopeAware.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.coroutines 2 | 3 | import kotlinx.coroutines.CoroutineScope 4 | 5 | public interface CoroutineScopeAware { 6 | 7 | public fun setCoroutineScope(coroutineScope: CoroutineScope) 8 | 9 | } -------------------------------------------------------------------------------- /platforms/velocity/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports: -------------------------------------------------------------------------------- 1 | kr.junhyung.mainframe.velocity.VelocityPluginAutoConfiguration 2 | kr.junhyung.mainframe.velocity.coroutines.VelocityCoroutinesAutoConfiguration 3 | kr.junhyung.mainframe.velocity.brigadier.VelocityCommandAutoConfiguration -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/util/IdentifiedKeyProvider.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.util 2 | 3 | public interface IdentifiedKeyProvider { 4 | 5 | public val prefix: String 6 | 7 | public fun getKey(identifier: ID): String 8 | 9 | public fun extractIdentifier(key: String): ID 10 | 11 | } -------------------------------------------------------------------------------- /platforms/velocity/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `java-library` 3 | alias(conventions.plugins.kotlin.jvm) 4 | alias(conventions.plugins.publishing) 5 | } 6 | 7 | dependencies { 8 | api(projects.core) 9 | api(projects.brigadier) 10 | 11 | compileOnlyApi(libs.velocity.api) 12 | implementation(kotlin("reflect")) 13 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/util/stdlib/CastExtensions.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.util.stdlib 2 | 3 | @Suppress("UNCHECKED_CAST") 4 | public fun Any.uncheckedCast(): T { 5 | return this as T 6 | } 7 | 8 | @Suppress("UNCHECKED_CAST") 9 | public fun Any.uncheckedCastOrNull(): T? { 10 | return this as? T 11 | } -------------------------------------------------------------------------------- /platforms/paper-brigadier/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `java-library` 3 | alias(conventions.plugins.kotlin.jvm) 4 | alias(conventions.plugins.publishing) 5 | } 6 | 7 | dependencies { 8 | api(projects.core) 9 | api(projects.paper) 10 | api(projects.brigadier) 11 | 12 | compileOnlyApi(libs.paper.api.v21) 13 | } 14 | 15 | kotlin { 16 | jvmToolchain(21) 17 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/condition/ConditionalOnPlugin.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.condition 2 | 3 | import org.springframework.context.annotation.Conditional 4 | 5 | @Conditional(OnPluginCondition::class) 6 | @Retention(AnnotationRetention.RUNTIME) 7 | @Target(AnnotationTarget.CLASS) 8 | public annotation class ConditionalOnPlugin(val plugin: String) -------------------------------------------------------------------------------- /exposed/src/main/kotlin/kr/junhyung/mainframe/exposed/table/Table.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.exposed.table 2 | 3 | import org.springframework.core.annotation.AliasFor 4 | import org.springframework.stereotype.Component 5 | 6 | @Target(AnnotationTarget.CLASS) 7 | @Retention(AnnotationRetention.RUNTIME) 8 | @Component 9 | public annotation class Table( 10 | @get:AliasFor(annotation = Component::class) 11 | val value: String = "" 12 | ) -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/event/Listener.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.event 2 | 3 | import org.springframework.core.annotation.AliasFor 4 | import org.springframework.stereotype.Component 5 | 6 | @Component 7 | @Retention(AnnotationRetention.RUNTIME) 8 | @Target(AnnotationTarget.CLASS) 9 | public annotation class Listener( 10 | 11 | @get:AliasFor(annotation = Component::class) 12 | val value: String = "" 13 | 14 | ) -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/util/StringKeyProvider.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.util 2 | 3 | public class StringKeyProvider(override val prefix: String) : IdentifiedKeyProvider { 4 | 5 | override fun getKey(identifier: String): String { 6 | return "$prefix$identifier" 7 | } 8 | 9 | override fun extractIdentifier(key: String): String { 10 | return key.removePrefix(prefix) 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /exposed/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | alias(conventions.plugins.kotlin.jvm) 3 | alias(conventions.plugins.publishing) 4 | } 5 | 6 | dependencies { 7 | api(projects.core) 8 | 9 | api(libs.exposed.core) 10 | api(libs.exposed.java.time) 11 | api(libs.exposed.json) 12 | api(libs.hikaricp) 13 | runtimeOnly(libs.exposed.jdbc) 14 | 15 | implementation(libs.adventure.text.serializer.gson) 16 | 17 | api(libs.spring.boot.starter.jdbc) 18 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/coroutines/AbstractCoroutineExceptionHandler.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.coroutines 2 | 3 | import kotlinx.coroutines.CoroutineExceptionHandler 4 | import kotlin.coroutines.AbstractCoroutineContextElement 5 | 6 | /** 7 | * Polymorphic helper class for [CoroutineExceptionHandler]. 8 | */ 9 | public abstract class AbstractCoroutineExceptionHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler.Key), CoroutineExceptionHandler -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/transaction/TransactionManager.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.transaction 2 | 3 | import kotlin.coroutines.CoroutineContext 4 | 5 | public interface TransactionManager { 6 | 7 | public suspend fun transactional( 8 | coroutineContext: CoroutineContext? = null, 9 | transactionIsolation: Int? = null, 10 | readOnly: Boolean? = null, 11 | block: suspend Transaction.() -> T 12 | ): T 13 | 14 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/condition/ConditionalOnMinecraftVersion.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.condition 2 | 3 | import org.springframework.context.annotation.Conditional 4 | 5 | /** 6 | * e.g. `@ConditionalOnMinecraftVersion("1.20.1")` 7 | */ 8 | @Conditional(OnMinecraftVersionCondition::class) 9 | @Retention(AnnotationRetention.RUNTIME) 10 | @Target(AnnotationTarget.CLASS) 11 | public annotation class ConditionalOnMinecraftVersion(val version: String) -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/adventure/AdventureComponentExtensions.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.adventure 2 | 3 | import net.kyori.adventure.text.Component 4 | import net.kyori.adventure.text.flattener.ComponentFlattener 5 | 6 | public fun Component.flatten(flattener: ComponentFlattener = ComponentFlattener.textOnly()): String { 7 | return buildString { 8 | flattener.flatten(this@flatten) { segment -> 9 | append(segment) 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/transaction/Transaction.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.transaction 2 | 3 | import kotlin.coroutines.CoroutineContext 4 | 5 | public interface Transaction { 6 | 7 | public fun isReadOnly(): Boolean 8 | 9 | public fun close() 10 | 11 | public fun commit() 12 | 13 | public fun rollback() 14 | 15 | public suspend fun withTransaction(coroutineContext: CoroutineContext? = null, statement: suspend Transaction.() -> T) 16 | 17 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/util/UUIDKeyProvider.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.util 2 | 3 | import java.util.UUID 4 | 5 | public open class UUIDKeyProvider(override val prefix: String) : IdentifiedKeyProvider { 6 | 7 | public override fun getKey(identifier: UUID): String { 8 | return "$prefix$identifier" 9 | } 10 | 11 | public override fun extractIdentifier(key: String): UUID { 12 | return UUID.fromString(key.removePrefix(prefix)) 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/coroutines/BukkitDispatchers.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.coroutines 2 | 3 | import kotlinx.coroutines.CoroutineDispatcher 4 | import org.jetbrains.annotations.ApiStatus.NonExtendable 5 | 6 | /** 7 | * Provides several [CoroutineDispatcher]s for the Bukkit platform. 8 | */ 9 | @NonExtendable 10 | public interface BukkitDispatchers { 11 | 12 | public fun getMainDispatcher(): CoroutineDispatcher 13 | 14 | public fun getAsyncDispatcher(): CoroutineDispatcher 15 | 16 | } -------------------------------------------------------------------------------- /core/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `java-library` 3 | alias(conventions.plugins.kotlin.jvm) 4 | alias(conventions.plugins.publishing) 5 | } 6 | 7 | dependencies { 8 | api(libs.spring.boot.starter) 9 | api(libs.spring.boot.starter.json) 10 | implementation("com.fasterxml.jackson.module:jackson-module-kotlin") 11 | 12 | api(libs.kotlinx.coroutines.core) 13 | api(libs.kotlinx.coroutines.reactive) 14 | api(libs.kotlinx.coroutines.reactor) 15 | 16 | compileOnlyApi(libs.adventure.api) 17 | 18 | implementation(kotlin("reflect")) 19 | } -------------------------------------------------------------------------------- /bom/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `java-platform` 3 | alias(conventions.plugins.publishing) 4 | } 5 | 6 | javaPlatform { 7 | allowDependencies() 8 | } 9 | 10 | dependencies { 11 | api(platform(libs.kotlin.bom)) 12 | api(platform(libs.spring.boot.dependencies)) 13 | api(platform(libs.exposed.bom)) 14 | 15 | constraints { 16 | api(libs.mysql.connector) 17 | 18 | api(projects.core) 19 | api(projects.exposed) 20 | api(projects.paper) 21 | api(projects.paperBrigadier) 22 | api(projects.velocity) 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /exposed/src/main/kotlin/kr/junhyung/mainframe/exposed/adventure/ComponentColoumnType.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.exposed.adventure 2 | 3 | import net.kyori.adventure.text.Component 4 | import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer 5 | import org.jetbrains.exposed.sql.Column 6 | import org.jetbrains.exposed.sql.Table 7 | import org.jetbrains.exposed.sql.json.json 8 | 9 | public fun Table.adventureComponent(name: String): Column { 10 | return json(name, GsonComponentSerializer.gson()::serialize, GsonComponentSerializer.gson()::deserialize) 11 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | 3 | project.group=kr.junhyung.mainframe 4 | project.version=0.0.1-SNAPSHOT 5 | project.name=spring-minecraft 6 | project.url=https://github.com/vjh0107/mainframe 7 | project.url.scm=https://github.com/vjh0107/mainframe.git 8 | 9 | project.description=A seamless Spring Framework integration for Minecraft. 10 | 11 | project.license=Apache License v2.0 12 | project.license.url=https://github.com/vjh0107/spring-minecraft/LICENSE.txt 13 | 14 | project.developer.id=vjh0107 15 | project.developer.name=Junhyung Park 16 | project.developer.email=park@junhyung.kr -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/util/ClassLoaderUtils.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.util 2 | 3 | public object ClassLoaderUtils { 4 | 5 | public fun withClassLoader( 6 | classLoader: ClassLoader, 7 | block: () -> Unit 8 | ) { 9 | val originalClassLoader = Thread.currentThread().contextClassLoader 10 | try { 11 | Thread.currentThread().contextClassLoader = classLoader 12 | block() 13 | } finally { 14 | Thread.currentThread().contextClassLoader = originalClassLoader 15 | } 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /brigadier/src/main/kotlin/kr/junhyung/mainframe/brigadier/CommandAnnotationProcessor.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.brigadier 2 | 3 | import kr.junhyung.mainframe.core.annotation.ClassAnnotationProcessor 4 | 5 | public class CommandAnnotationProcessor( 6 | private val registrar: CommandRegistrar, 7 | ) : ClassAnnotationProcessor() { 8 | 9 | override fun postProcessBeforeInitialization(bean: Any, beanName: String, annotation: Command) { 10 | registrar.registerCommand(bean, annotation) 11 | if (bean is CommandAnnotatedElement) { 12 | bean.setCommand(annotation) 13 | } 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/MainframeApplicationCustomizer.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core 2 | 3 | import org.springframework.boot.builder.SpringApplicationBuilder 4 | 5 | public interface MainframeApplicationCustomizer { 6 | 7 | public fun customizeBuilder(builder: SpringApplicationBuilder): SpringApplicationBuilder { 8 | return builder 9 | } 10 | 11 | public fun customizeContext(applicationContext: T) { 12 | } 13 | 14 | public fun onApplicationStart() { 15 | } 16 | 17 | public fun onApplicationStarted(applicationContext: T) { 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /gradle-plugin/src/main/kotlin/kr/junhyung/mainframe/gradle/MainframeExtension.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.gradle 2 | 3 | import org.gradle.api.model.ObjectFactory 4 | import org.gradle.api.provider.Property 5 | import javax.inject.Inject 6 | 7 | public abstract class MainframeExtension @Inject constructor( 8 | objects: ObjectFactory, 9 | ) { 10 | 11 | internal companion object { 12 | const val NAME: String = "mainframe" 13 | } 14 | 15 | public val enableShadowJar: Property = objects.property(Boolean::class.java).convention(false) 16 | 17 | public fun shadowJar() { 18 | enableShadowJar.set(true) 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | !**/src/main/**/build/ 5 | !**/src/test/**/build/ 6 | 7 | ### IntelliJ IDEA ### 8 | .idea/ 9 | *.iws 10 | *.iml 11 | *.ipr 12 | out/ 13 | !**/src/main/**/out/ 14 | !**/src/test/**/out/ 15 | 16 | ### Eclipse ### 17 | .apt_generated 18 | .classpath 19 | .factorypath 20 | .project 21 | .settings 22 | .springBeans 23 | .sts4-cache 24 | bin/ 25 | !**/src/main/**/bin/ 26 | !**/src/test/**/bin/ 27 | 28 | ### NetBeans ### 29 | /nbproject/private/ 30 | /nbbuild/ 31 | /dist/ 32 | /nbdist/ 33 | /.nb-gradle/ 34 | 35 | ### VS Code ### 36 | .vscode/ 37 | 38 | ### Mac OS ### 39 | .DS_Store 40 | 41 | /test-bukkit 42 | publication.gradle.kts -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/concurrent/DistributedLock.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.concurrent 2 | 3 | import kotlin.time.Duration 4 | 5 | /** 6 | * A standard interface for a distributed lock. 7 | */ 8 | public interface DistributedLock { 9 | 10 | public suspend fun withLock( 11 | key: String, 12 | waitTime: Duration, 13 | leaseTime: Duration, 14 | action: suspend () -> T 15 | ): T 16 | 17 | public suspend fun tryLock( 18 | key: String, 19 | waitTime: Duration, 20 | leaseTime: Duration 21 | ): Boolean 22 | 23 | public suspend fun unlock(key: String) 24 | 25 | public suspend fun isLocked(key: String): Boolean 26 | 27 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/coroutines/CoroutineScopeExtensions.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.coroutines 2 | 3 | import kotlinx.coroutines.CoroutineExceptionHandler 4 | import kotlinx.coroutines.CoroutineScope 5 | import kotlinx.coroutines.future.future 6 | 7 | public fun CoroutineScope.block(block: suspend CoroutineScope.() -> Unit) { 8 | this.future { 9 | try { 10 | block() 11 | } catch (throwable: Throwable) { 12 | val exceptionHandler = coroutineContext[CoroutineExceptionHandler] 13 | if (exceptionHandler != null) { 14 | exceptionHandler.handleException(coroutineContext, throwable) 15 | } else { 16 | throw throwable 17 | } 18 | } 19 | }.join() 20 | } -------------------------------------------------------------------------------- /brigadier/src/main/kotlin/kr/junhyung/mainframe/brigadier/Command.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.brigadier 2 | 3 | import org.springframework.core.annotation.AliasFor 4 | import org.springframework.stereotype.Component 5 | 6 | /** 7 | * Annotation to mark a class as a Brigadier command. 8 | * This annotation provides a way to register command in lifecycle events. 9 | */ 10 | @Component 11 | @Retention(AnnotationRetention.RUNTIME) 12 | @Target(AnnotationTarget.CLASS) 13 | public annotation class Command( 14 | 15 | val name: String, 16 | 17 | val description: String = "", 18 | 19 | val aliases: Array = [], 20 | 21 | val permission: String = "", 22 | 23 | val usageMessage: String = "", 24 | 25 | @get:AliasFor(annotation = Component::class) 26 | val value: String = "" 27 | 28 | ) -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/coroutines/BukkitPluginCoroutineScope.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.coroutines 2 | 3 | import kotlinx.coroutines.CoroutineExceptionHandler 4 | import kotlinx.coroutines.CoroutineName 5 | import kr.junhyung.mainframe.core.coroutines.GracefulShutdownCoroutineScope 6 | import org.bukkit.plugin.Plugin 7 | import org.slf4j.Logger 8 | import kotlin.coroutines.CoroutineContext 9 | 10 | public class BukkitPluginCoroutineScope( 11 | private val plugin: Plugin, 12 | private val exceptionHandler: CoroutineExceptionHandler 13 | ) : GracefulShutdownCoroutineScope() { 14 | 15 | override fun getLogger(): Logger { 16 | return plugin.slF4JLogger 17 | } 18 | 19 | override fun coroutineContext(): CoroutineContext { 20 | return CoroutineName(plugin.name) + exceptionHandler 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /brigadier/src/main/kotlin/kr/junhyung/mainframe/brigadier/CommandAutoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.brigadier 2 | 3 | import org.springframework.beans.factory.config.BeanDefinition 4 | import org.springframework.boot.autoconfigure.AutoConfiguration 5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean 6 | import org.springframework.context.annotation.Bean 7 | import org.springframework.context.annotation.Role 8 | 9 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 10 | @AutoConfiguration 11 | public class CommandAutoConfiguration { 12 | 13 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 14 | @ConditionalOnBean(CommandRegistrar::class) 15 | @Bean 16 | public fun commandAnnotationProcessor( 17 | registrar: CommandRegistrar 18 | ): CommandAnnotationProcessor { 19 | return CommandAnnotationProcessor(registrar) 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /gradle-plugin/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `kotlin-dsl` 3 | alias(libs.plugins.plugin.publish) 4 | alias(conventions.plugins.kotlin.jvm) 5 | alias(conventions.plugins.publishing) 6 | } 7 | 8 | dependencies { 9 | implementation(libs.shadow) 10 | implementation(libs.spring.dependency.management) 11 | implementation(kotlin("gradle-plugin")) 12 | implementation(kotlin("allopen")) 13 | } 14 | 15 | gradlePlugin { 16 | website = property("project.url").toString() 17 | vcsUrl = property("project.url.scm").toString() 18 | plugins { 19 | register("mainframe") { 20 | id = "kr.junhyung.mainframe" 21 | displayName = property("project.name").toString() 22 | description = property("project.description").toString() 23 | implementationClass = "kr.junhyung.mainframe.gradle.MainframePlugin" 24 | tags.set(listOf("mainframe")) 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/jackson/ObjectMapperAwareBeanPostProcessor.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.jackson 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper 4 | import org.springframework.beans.factory.config.BeanPostProcessor 5 | import org.springframework.context.ApplicationContext 6 | import org.springframework.util.function.SingletonSupplier 7 | 8 | internal class ObjectMapperAwareBeanPostProcessor(private val applicationContext: ApplicationContext) : BeanPostProcessor { 9 | 10 | override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any? { 11 | if (bean is ObjectMapperAware) { 12 | bean.setObjectMapper(getObjectMapper()) 13 | } 14 | return bean 15 | } 16 | 17 | private fun getObjectMapper(): ObjectMapper { 18 | return SingletonSupplier.of { 19 | applicationContext.getBean(ObjectMapper::class.java) 20 | }.obtain() 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/coroutines/CoroutineScopeAwareBeanPostProcessor.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.coroutines 2 | 3 | import kotlinx.coroutines.CoroutineScope 4 | import org.springframework.beans.factory.config.BeanPostProcessor 5 | import org.springframework.context.ApplicationContext 6 | import org.springframework.util.function.SingletonSupplier 7 | 8 | internal class CoroutineScopeAwareBeanPostProcessor(private val applicationContext: ApplicationContext) : BeanPostProcessor { 9 | 10 | override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any? { 11 | if (bean is CoroutineScopeAware) { 12 | bean.setCoroutineScope(getCoroutineScope()) 13 | } 14 | return bean 15 | } 16 | 17 | private fun getCoroutineScope(): CoroutineScope { 18 | return SingletonSupplier.of { 19 | applicationContext.getBean(CoroutineScope::class.java) 20 | }.obtain() 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /exposed/src/main/kotlin/kr/junhyung/mainframe/exposed/table/TableInitializer.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.exposed.table 2 | 3 | import org.jetbrains.exposed.sql.Database 4 | import org.jetbrains.exposed.sql.SchemaUtils 5 | import org.jetbrains.exposed.sql.Table 6 | import org.jetbrains.exposed.sql.transactions.transaction 7 | import org.springframework.beans.factory.ObjectProvider 8 | import org.springframework.boot.ApplicationArguments 9 | import org.springframework.boot.ApplicationRunner 10 | 11 | internal class TableInitializer( 12 | private val database: Database, 13 | private val tables: ObjectProvider 14 | ) : ApplicationRunner { 15 | 16 | override fun run(args: ApplicationArguments) { 17 | val nonProxiedTables = tables.map { table -> 18 | table::class.objectInstance ?: error("Table ${table::class.simpleName} is not a singleton") 19 | } 20 | transaction(database) { 21 | SchemaUtils.create(*nonProxiedTables.toTypedArray()) 22 | } 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /gradle-plugin/src/main/kotlin/kr/junhyung/mainframe/gradle/shadow/MainframeShadowPlugin.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.gradle.shadow 2 | 3 | import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin 4 | import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar 5 | import kr.junhyung.mainframe.gradle.MainframeExtension 6 | import org.gradle.api.Plugin 7 | import org.gradle.api.Project 8 | import org.gradle.kotlin.dsl.getByType 9 | 10 | public abstract class MainframeShadowPlugin : Plugin { 11 | 12 | override fun apply(target: Project) { 13 | target.afterEvaluate(::applyAfterEvaluate) 14 | } 15 | 16 | private fun applyAfterEvaluate(target: Project) { 17 | val extension = target.extensions.getByType() 18 | if (!extension.enableShadowJar.get()) { 19 | return 20 | } 21 | target.pluginManager.apply(ShadowPlugin::class.java) 22 | target.tasks.withType(ShadowJar::class.java).configureEach { 23 | mergeServiceFiles { 24 | setPath("META-INF/spring") 25 | } 26 | } 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /platforms/paper-brigadier/src/main/kotlin/kr/junhyung/mainframe/paper/brigadier/PaperCommandAutoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.brigadier 2 | 3 | import kr.junhyung.mainframe.brigadier.CommandAutoConfiguration 4 | import kr.junhyung.mainframe.brigadier.CommandRegistrar 5 | import kr.junhyung.mainframe.paper.condition.ConditionalOnMinecraftVersion 6 | import org.bukkit.plugin.Plugin 7 | import org.springframework.beans.factory.config.BeanDefinition 8 | import org.springframework.boot.autoconfigure.AutoConfiguration 9 | import org.springframework.boot.autoconfigure.AutoConfigureBefore 10 | import org.springframework.context.annotation.Bean 11 | import org.springframework.context.annotation.Role 12 | 13 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 14 | @AutoConfigureBefore(CommandAutoConfiguration::class) 15 | @ConditionalOnMinecraftVersion("1.21.1") 16 | @AutoConfiguration 17 | public class PaperCommandAutoConfiguration { 18 | 19 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 20 | @Bean 21 | public fun paperCommandRegistrar( 22 | plugin: Plugin 23 | ): CommandRegistrar { 24 | return PaperCommandRegistrar(plugin) 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /exposed/src/main/kotlin/kr/junhyung/mainframe/exposed/transaction/ExposedTransaction.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.exposed.transaction 2 | 3 | import kr.junhyung.mainframe.core.transaction.Transaction 4 | import org.jetbrains.exposed.sql.transactions.experimental.withSuspendTransaction 5 | import org.jetbrains.exposed.sql.Transaction as ExposedTx 6 | import kotlin.coroutines.CoroutineContext 7 | 8 | internal class ExposedTransaction(private val transaction: ExposedTx) : Transaction { 9 | 10 | override fun isReadOnly(): Boolean { 11 | return transaction.readOnly 12 | } 13 | 14 | override fun close() { 15 | transaction.close() 16 | } 17 | 18 | override fun commit() { 19 | transaction.commit() 20 | } 21 | 22 | override fun rollback() { 23 | transaction.rollback() 24 | } 25 | 26 | override suspend fun withTransaction(coroutineContext: CoroutineContext?, statement: suspend Transaction.() -> T) { 27 | val statement: suspend (ExposedTx) -> T = { transaction -> 28 | statement.invoke(ExposedTransaction(transaction)) 29 | } 30 | transaction.withSuspendTransaction(coroutineContext, statement) 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/coroutines/BukkitDispatchersImpl.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.coroutines 2 | 3 | import kotlinx.coroutines.CoroutineDispatcher 4 | import kotlinx.coroutines.asCoroutineDispatcher 5 | import org.bukkit.Bukkit 6 | import org.bukkit.plugin.Plugin 7 | import java.util.concurrent.Executor 8 | 9 | internal class BukkitDispatchersImpl( 10 | private val plugin: Plugin 11 | ) : BukkitDispatchers { 12 | 13 | @get:JvmName("mainDispatcher0") 14 | private val mainDispatcher by lazy { 15 | val executor = Bukkit.getScheduler().getMainThreadExecutor(plugin) 16 | executor.asCoroutineDispatcher() 17 | } 18 | 19 | @get:JvmName("asyncDispatcher0") 20 | private val asyncDispatcher by lazy { 21 | val executor = Executor { runnable -> 22 | Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable) 23 | } 24 | executor.asCoroutineDispatcher() 25 | } 26 | 27 | override fun getMainDispatcher(): CoroutineDispatcher { 28 | return mainDispatcher 29 | } 30 | 31 | override fun getAsyncDispatcher(): CoroutineDispatcher { 32 | return asyncDispatcher 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/VelocityPluginApplicationContext.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity 2 | 3 | import com.velocitypowered.api.plugin.PluginContainer 4 | import com.velocitypowered.api.proxy.ProxyServer 5 | import kr.junhyung.mainframe.core.MainframeApplicationContext 6 | import kr.junhyung.mainframe.velocity.event.SubscribeAnnotationProcessor 7 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory 8 | import java.io.File 9 | 10 | public class VelocityPluginApplicationContext( 11 | public val proxyServer: ProxyServer, 12 | public val pluginContainer: PluginContainer, 13 | dataDirectory: File 14 | ) : MainframeApplicationContext(dataDirectory) { 15 | 16 | override fun onPostProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory) { 17 | registerSubscribeAnnotationProcessor(beanFactory) 18 | } 19 | 20 | private fun registerSubscribeAnnotationProcessor(beanFactory: ConfigurableListableBeanFactory) { 21 | val subscribeAnnotationProcessor = SubscribeAnnotationProcessor(this, pluginContainer, proxyServer.eventManager) 22 | beanFactory.addBeanPostProcessor(subscribeAnnotationProcessor) 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/brigadier/VelocityCommandAutoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity.brigadier 2 | 3 | import com.velocitypowered.api.plugin.PluginContainer 4 | import com.velocitypowered.api.proxy.ProxyServer 5 | import kr.junhyung.mainframe.brigadier.CommandAutoConfiguration 6 | import kr.junhyung.mainframe.brigadier.CommandRegistrar 7 | import org.springframework.beans.factory.config.BeanDefinition 8 | import org.springframework.boot.autoconfigure.AutoConfiguration 9 | import org.springframework.boot.autoconfigure.AutoConfigureBefore 10 | import org.springframework.context.annotation.Bean 11 | import org.springframework.context.annotation.Role 12 | 13 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 14 | @AutoConfigureBefore(CommandAutoConfiguration::class) 15 | @AutoConfiguration 16 | public class VelocityCommandAutoConfiguration { 17 | 18 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 19 | @Bean 20 | public fun velocityCommandRegistrar( 21 | proxyServer: ProxyServer, 22 | pluginContainer: PluginContainer, 23 | ): CommandRegistrar { 24 | return VelocityCommandRegistrar(proxyServer.commandManager, pluginContainer.instance.get()) 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/coroutines/VelocityPluginCoroutineScope.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity.coroutines 2 | 3 | import com.velocitypowered.api.plugin.PluginContainer 4 | import kotlinx.coroutines.CoroutineExceptionHandler 5 | import kotlinx.coroutines.CoroutineName 6 | import kr.junhyung.mainframe.core.coroutines.GracefulShutdownCoroutineScope 7 | import kr.junhyung.mainframe.velocity.MainframePlugin 8 | import org.slf4j.Logger 9 | import kotlin.coroutines.CoroutineContext 10 | 11 | public class VelocityPluginCoroutineScope( 12 | private val pluginContainer: PluginContainer, 13 | private val exceptionHandler: CoroutineExceptionHandler 14 | ) : GracefulShutdownCoroutineScope() { 15 | 16 | private val pluginLogger by lazy { 17 | val instance = pluginContainer.instance.get() 18 | if (instance !is MainframePlugin<*>) { 19 | error("Plugin instance is not of type MainframePlugin") 20 | } 21 | instance.logger 22 | } 23 | 24 | override fun getLogger(): Logger { 25 | return pluginLogger 26 | } 27 | 28 | override fun coroutineContext(): CoroutineContext { 29 | return CoroutineName(pluginContainer.description.id) + exceptionHandler 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/PaperPluginAutoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper 2 | 3 | import org.bukkit.plugin.Plugin 4 | import org.slf4j.Logger 5 | import org.springframework.beans.factory.NoUniqueBeanDefinitionException 6 | import org.springframework.beans.factory.config.BeanDefinition 7 | import org.springframework.boot.autoconfigure.AutoConfiguration 8 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean 9 | import org.springframework.context.annotation.Bean 10 | import org.springframework.context.annotation.Role 11 | 12 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 13 | @AutoConfiguration 14 | public class PaperPluginAutoConfiguration { 15 | 16 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 17 | @ConditionalOnMissingBean(Plugin::class) 18 | @Bean(destroyMethod = "") 19 | public fun plugin(): Plugin { 20 | throw NoUniqueBeanDefinitionException( 21 | Plugin::class.java, 22 | "Plugin bean is not available. Please make sure you are using MainframePlugin." 23 | ) 24 | } 25 | 26 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 27 | @Bean 28 | public fun logger(plugin: Plugin): Logger { 29 | return plugin.slF4JLogger 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /exposed/src/main/kotlin/kr/junhyung/mainframe/exposed/transaction/ExposedTransactionManager.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.exposed.transaction 2 | 3 | import kr.junhyung.mainframe.core.transaction.Transaction 4 | import kr.junhyung.mainframe.core.transaction.TransactionManager 5 | import org.jetbrains.exposed.sql.Transaction as ExposedTx 6 | import org.jetbrains.exposed.sql.Database 7 | import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction 8 | import kotlin.coroutines.CoroutineContext 9 | 10 | internal class ExposedTransactionManager(private val database: Database) : TransactionManager { 11 | 12 | override suspend fun transactional( 13 | coroutineContext: CoroutineContext?, 14 | transactionIsolation: Int?, 15 | readOnly: Boolean?, 16 | block: suspend Transaction.() -> T 17 | ): T { 18 | val statement: suspend (ExposedTx) -> T = { transaction -> 19 | block.invoke(ExposedTransaction(transaction)) 20 | } 21 | return newSuspendedTransaction( 22 | context = coroutineContext, 23 | db = database, 24 | transactionIsolation = transactionIsolation, 25 | readOnly = readOnly, 26 | statement = statement 27 | ) 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/brigadier/VelocityCommandRegistrar.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity.brigadier 2 | 3 | import com.velocitypowered.api.command.CommandManager 4 | import com.velocitypowered.api.command.Command as VelocityCommand 5 | import kr.junhyung.mainframe.brigadier.Command 6 | import kr.junhyung.mainframe.brigadier.CommandRegistrar 7 | import org.slf4j.LoggerFactory 8 | import kotlin.jvm.java 9 | 10 | internal class VelocityCommandRegistrar( 11 | private val commandManager: CommandManager, 12 | private val plugin: Any 13 | ) : CommandRegistrar { 14 | 15 | private val logger = LoggerFactory.getLogger(VelocityCommandRegistrar::class.java) 16 | 17 | override fun registerCommand(command: Any, metadata: Command) { 18 | if (command !is VelocityCommand) { 19 | throw UnsupportedOperationException("Command annotation can only be used on Command, but found ${command::class.java.name}") 20 | } 21 | val commandMeta = commandManager.metaBuilder(metadata.name) 22 | .aliases(*metadata.aliases) 23 | .plugin(plugin) 24 | .build() 25 | commandManager.register(commandMeta, command) 26 | logger.debug("Registered command: ${metadata.name} with aliases: ${metadata.aliases.joinToString(", ")}") 27 | } 28 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/coroutines/BukkitCoroutinesAutoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.coroutines 2 | 3 | import kotlinx.coroutines.CoroutineExceptionHandler 4 | import org.bukkit.plugin.Plugin 5 | import org.springframework.boot.autoconfigure.AutoConfiguration 6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean 7 | import org.springframework.context.annotation.Bean 8 | 9 | @AutoConfiguration 10 | public class BukkitCoroutinesAutoConfiguration { 11 | 12 | @Bean 13 | public fun bukkitDispatchers(plugin: Plugin): BukkitDispatchers { 14 | return BukkitDispatchersImpl(plugin) 15 | } 16 | 17 | @Bean 18 | public fun pluginCoroutineScope( 19 | plugin: Plugin, 20 | exceptionHandler: CoroutineExceptionHandler 21 | ): BukkitPluginCoroutineScope { 22 | return BukkitPluginCoroutineScope(plugin, exceptionHandler) 23 | } 24 | 25 | @ConditionalOnMissingBean(CoroutineExceptionHandler::class) 26 | @Bean 27 | public fun coroutinesExceptionHandler( 28 | plugin: Plugin 29 | ): CoroutineExceptionHandler { 30 | return CoroutineExceptionHandler { context, exception -> 31 | plugin.slF4JLogger.error("An unhandled exception occurred in ${plugin.name}, coroutine: $context", exception) 32 | } 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/service/ServiceAnnotationProcessor.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.service 2 | 3 | import kr.junhyung.mainframe.core.annotation.ClassAnnotationProcessor 4 | import kr.junhyung.mainframe.core.util.stdlib.uncheckedCast 5 | import org.bukkit.Bukkit 6 | import org.bukkit.plugin.Plugin 7 | import org.bukkit.plugin.ServicePriority 8 | import org.bukkit.plugin.ServicesManager 9 | import org.springframework.aop.framework.AopProxyUtils 10 | import org.springframework.stereotype.Service 11 | 12 | internal class ServiceAnnotationProcessor( 13 | private val plugin: Plugin, 14 | private val servicesManager: ServicesManager = Bukkit.getServicesManager() 15 | ) : ClassAnnotationProcessor() { 16 | 17 | override fun postProcessBeforeInitialization(bean: Any, beanName: String, annotation: Service) { 18 | val ultimateTargetClass = AopProxyUtils.ultimateTargetClass(bean) 19 | val binds = mutableListOf>() 20 | binds.add(ultimateTargetClass) 21 | ultimateTargetClass.interfaces.forEach { bindClass -> 22 | binds.add(bindClass) 23 | } 24 | binds.forEach { bindClass -> 25 | servicesManager.register( 26 | bindClass.uncheckedCast(), 27 | bean, 28 | plugin, 29 | ServicePriority.Normal 30 | ) 31 | } 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "mainframe" 2 | 3 | includeBuild("build-logic") 4 | 5 | include(":gradle-plugin") 6 | include(":bom") 7 | 8 | include(":core") 9 | include(":exposed") 10 | include(":brigadier") 11 | 12 | include(":paper", file("platforms/paper")) 13 | include(":paper-brigadier", file("platforms/paper-brigadier")) 14 | include(":velocity", file("platforms/velocity")) 15 | 16 | fun include(name: String, path: File) { 17 | include(name) 18 | project(name).projectDir = path 19 | } 20 | 21 | pluginManagement { 22 | repositories { 23 | maven("https://junhyung.nexus/") 24 | } 25 | plugins { 26 | id("org.gradle.toolchains.foojay-resolver-convention") version "0.9.0" 27 | } 28 | } 29 | 30 | @Suppress("UnstableApiUsage") 31 | dependencyResolutionManagement { 32 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 33 | repositories { 34 | maven("https://junhyung.nexus/") 35 | } 36 | 37 | versionCatalogs { 38 | create("conventions") { 39 | file("build-logic/convention/src/main/kotlin/kr/junhyung/mainframe/convention").listFiles { 40 | file -> file.name.endsWith(".gradle.kts") 41 | }!!.forEach { file -> 42 | val pluginName = file.name.removeSuffix(".gradle.kts") 43 | plugin(pluginName.removeSuffix("-convention"), "kr.junhyung.mainframe.convention.$pluginName").version("") 44 | } 45 | } 46 | } 47 | } 48 | 49 | enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/condition/OnPluginCondition.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.condition 2 | 3 | import org.bukkit.Bukkit 4 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome 5 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition 6 | import org.springframework.context.annotation.ConditionContext 7 | import org.springframework.core.Ordered 8 | import org.springframework.core.annotation.Order 9 | import org.springframework.core.type.AnnotatedTypeMetadata 10 | 11 | @Order(Ordered.LOWEST_PRECEDENCE) 12 | internal class OnPluginCondition : SpringBootCondition() { 13 | 14 | override fun getMatchOutcome( 15 | context: ConditionContext, 16 | metadata: AnnotatedTypeMetadata 17 | ): ConditionOutcome { 18 | val attributes = metadata.getAnnotationAttributes(ConditionalOnPlugin::class.java.name) 19 | if (attributes == null) { 20 | return ConditionOutcome(false, "No attributes found") 21 | } 22 | val dependPlugin = attributes["plugin"] 23 | if (dependPlugin == null || dependPlugin as? String == null) { 24 | return ConditionOutcome(false, "Plugin name is null") 25 | } 26 | val pluginInstance = Bukkit.getPluginManager().getPlugin(dependPlugin) 27 | if (pluginInstance == null) { 28 | return ConditionOutcome(false, "Plugin $dependPlugin is not loaded") 29 | } 30 | return ConditionOutcome(true, "Plugin $dependPlugin is loaded") 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/coroutines/GracefulShutdownCoroutineScope.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.coroutines 2 | 3 | import kotlinx.coroutines.CompletableJob 4 | import kotlinx.coroutines.CoroutineScope 5 | import kotlinx.coroutines.SupervisorJob 6 | import kotlinx.coroutines.runBlocking 7 | import kotlinx.coroutines.withTimeoutOrNull 8 | import org.slf4j.Logger 9 | import org.slf4j.LoggerFactory 10 | import org.springframework.beans.factory.DisposableBean 11 | import kotlin.coroutines.CoroutineContext 12 | 13 | public abstract class GracefulShutdownCoroutineScope : CoroutineScope, DisposableBean { 14 | 15 | protected val supervisorJob: CompletableJob = SupervisorJob() 16 | 17 | public open fun getLogger(): Logger { 18 | return LoggerFactory.getLogger(GracefulShutdownCoroutineScope::class.java) 19 | } 20 | 21 | final override val coroutineContext: CoroutineContext 22 | get() = supervisorJob + coroutineContext() 23 | 24 | public abstract fun coroutineContext(): CoroutineContext 25 | 26 | final override fun destroy() { 27 | shutdownGracefully() 28 | } 29 | 30 | private fun shutdownGracefully(period: Long = 10_000) { 31 | supervisorJob.cancel() 32 | runBlocking { 33 | val result = withTimeoutOrNull(period) { 34 | supervisorJob.join() 35 | } 36 | if (result == null) { 37 | getLogger().warn("Plugin shutdown took too long, forcefully shutting down") 38 | } 39 | } 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/coroutines/VelocityCoroutinesAutoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity.coroutines 2 | 3 | import com.velocitypowered.api.plugin.PluginContainer 4 | import kotlinx.coroutines.CoroutineExceptionHandler 5 | import kr.junhyung.mainframe.velocity.MainframePlugin 6 | import org.springframework.boot.autoconfigure.AutoConfiguration 7 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean 8 | import org.springframework.context.annotation.Bean 9 | 10 | @AutoConfiguration 11 | public class VelocityCoroutinesAutoConfiguration { 12 | 13 | @Bean 14 | public fun pluginCoroutineScope( 15 | pluginContainer: PluginContainer, 16 | exceptionHandler: CoroutineExceptionHandler 17 | ): VelocityPluginCoroutineScope { 18 | return VelocityPluginCoroutineScope(pluginContainer, exceptionHandler) 19 | } 20 | 21 | @ConditionalOnMissingBean(CoroutineExceptionHandler::class) 22 | @Bean 23 | public fun coroutinesExceptionHandler( 24 | pluginContainer: PluginContainer 25 | ): CoroutineExceptionHandler { 26 | val instance = pluginContainer.instance.get() 27 | if (instance !is MainframePlugin<*>) { 28 | error("Plugin instance is not of type MainframePlugin") 29 | } 30 | return CoroutineExceptionHandler { context, exception -> 31 | instance.logger.error("An unhandled exception occurred in ${pluginContainer.description.id}, coroutine: $context", exception) 32 | } 33 | } 34 | 35 | 36 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/PaperPluginApplicationContext.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper 2 | 3 | import kr.junhyung.mainframe.core.MainframeApplicationContext 4 | import kr.junhyung.mainframe.paper.event.EventHandlerAnnotationProcessor 5 | import kr.junhyung.mainframe.paper.service.ServiceAnnotationProcessor 6 | import org.bukkit.plugin.Plugin 7 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory 8 | 9 | public class PaperPluginApplicationContext(private val plugin: Plugin) : MainframeApplicationContext(plugin.dataFolder) { 10 | 11 | override fun onPostProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory) { 12 | registerPluginBean(beanFactory) 13 | registerEventHandlerAnnotationProcessor(beanFactory) 14 | registerServiceAnnotationProcessor(beanFactory) 15 | } 16 | 17 | private fun registerEventHandlerAnnotationProcessor(beanFactory: ConfigurableListableBeanFactory) { 18 | val eventHandlerAnnotationProcessor = EventHandlerAnnotationProcessor(plugin) 19 | beanFactory.addBeanPostProcessor(eventHandlerAnnotationProcessor) 20 | } 21 | 22 | private fun registerServiceAnnotationProcessor(beanFactory: ConfigurableListableBeanFactory) { 23 | val serviceAnnotationProcessor = ServiceAnnotationProcessor(plugin) 24 | beanFactory.addBeanPostProcessor(serviceAnnotationProcessor) 25 | } 26 | 27 | private fun registerPluginBean(beanFactory: ConfigurableListableBeanFactory) { 28 | beanFactory.registerSingleton(plugin.name, plugin) 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /gradle-plugin/src/main/kotlin/kr/junhyung/mainframe/gradle/bom/MainframeDependenciesExtension.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.gradle.bom 2 | 3 | import org.gradle.api.artifacts.ExternalModuleDependency 4 | import org.gradle.api.artifacts.dsl.DependencyHandler 5 | import org.gradle.kotlin.dsl.create 6 | 7 | public abstract class MainframeDependenciesExtension( 8 | private val dependencyHandler: DependencyHandler, 9 | ) { 10 | 11 | private companion object { 12 | const val MAINFRAME_GROUP = "kr.junhyung.mainframe" 13 | 14 | const val MAINFRAME_CORE = "mainframe-core" 15 | const val MAINFRAME_EXPOSED = "mainframe-exposed" 16 | 17 | const val MAINFRAME_PAPER = "mainframe-paper" 18 | const val MAINFRAME_PAPER_BRIGADIER = "mainframe-paper-brigadier" 19 | const val MAINFRAME_VELOCITY = "mainframe-velocity" 20 | } 21 | 22 | public val core: ExternalModuleDependency get() { 23 | return dependencyHandler.create(MAINFRAME_GROUP, MAINFRAME_CORE) 24 | } 25 | 26 | public val exposed: ExternalModuleDependency get() { 27 | return dependencyHandler.create(MAINFRAME_GROUP, MAINFRAME_EXPOSED) 28 | } 29 | 30 | public val paper: ExternalModuleDependency get() { 31 | return dependencyHandler.create(MAINFRAME_GROUP, MAINFRAME_PAPER) 32 | } 33 | 34 | public val paperBrigadier: ExternalModuleDependency get() { 35 | return dependencyHandler.create(MAINFRAME_GROUP, MAINFRAME_PAPER_BRIGADIER) 36 | } 37 | 38 | public val velocity: ExternalModuleDependency get() { 39 | return dependencyHandler.create(MAINFRAME_GROUP, MAINFRAME_VELOCITY) 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/io/PrioritizedResourceLoader.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.io 2 | 3 | import org.springframework.core.io.DefaultResourceLoader 4 | import org.springframework.core.io.Resource 5 | import org.springframework.core.io.ResourceLoader 6 | 7 | public class PrioritizedResourceLoader(private val classLoader: ClassLoader) : ResourceLoader { 8 | 9 | private val primaryResourceLoader: ResourceLoader = DefaultResourceLoader(classLoader) 10 | private val secondaryResourceLoaders: MutableList = mutableListOf() 11 | 12 | public fun add(resourceLoader: ResourceLoader) { 13 | this.secondaryResourceLoaders.add(resourceLoader) 14 | } 15 | 16 | public fun add(classLoader: ClassLoader) { 17 | this.add(createResourceLoader(classLoader)) 18 | } 19 | 20 | private fun createResourceLoader(classLoader: ClassLoader): ResourceLoader { 21 | return DefaultResourceLoader(classLoader) 22 | } 23 | 24 | override fun getResource(location: String): Resource { 25 | val resource = primaryResourceLoader.getResource(location) 26 | 27 | if (resource.exists()) { 28 | return resource 29 | } 30 | 31 | for (resourceLoader in secondaryResourceLoaders) { 32 | val secondaryResource = resourceLoader.getResource(location) 33 | if (secondaryResource.exists()) { 34 | return secondaryResource 35 | } 36 | } 37 | return primaryResourceLoader.getResource(location) 38 | } 39 | 40 | override fun getClassLoader(): ClassLoader { 41 | return classLoader 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/condition/OnMinecraftVersionCondition.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.condition 2 | 3 | import org.apache.maven.artifact.versioning.ComparableVersion 4 | import org.bukkit.Bukkit 5 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome 6 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition 7 | import org.springframework.context.annotation.ConditionContext 8 | import org.springframework.core.Ordered 9 | import org.springframework.core.annotation.Order 10 | import org.springframework.core.type.AnnotatedTypeMetadata 11 | 12 | @Order(Ordered.LOWEST_PRECEDENCE) 13 | internal class OnMinecraftVersionCondition : SpringBootCondition() { 14 | 15 | override fun getMatchOutcome( 16 | context: ConditionContext, 17 | metadata: AnnotatedTypeMetadata 18 | ): ConditionOutcome { 19 | val attributes = metadata.getAnnotationAttributes(ConditionalOnMinecraftVersion::class.java.name) 20 | if (attributes == null) { 21 | return ConditionOutcome(false, "No attributes found") 22 | } 23 | val expectedVersion = attributes["version"] as? String ?: error("Expected version is null") 24 | val currentVersion = Bukkit.getMinecraftVersion() 25 | 26 | val expectedComparableVersion = ComparableVersion(expectedVersion) 27 | val currentComparableVersion = ComparableVersion(currentVersion) 28 | 29 | return ConditionOutcome( 30 | currentComparableVersion >= expectedComparableVersion, 31 | "Minecraft version is $currentVersion, expected $expectedVersion" 32 | ) 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /gradle-plugin/src/main/kotlin/kr/junhyung/mainframe/gradle/MainframePlugin.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.gradle 2 | 3 | import kr.junhyung.mainframe.gradle.bom.MainframeBomPlugin 4 | import kr.junhyung.mainframe.gradle.shadow.MainframeShadowPlugin 5 | import org.gradle.api.Plugin 6 | import org.gradle.api.Project 7 | import org.gradle.api.plugins.JavaPluginExtension 8 | import org.gradle.kotlin.dsl.apply 9 | import org.gradle.kotlin.dsl.configure 10 | import org.gradle.kotlin.dsl.create 11 | import org.gradle.kotlin.dsl.getByType 12 | import org.jetbrains.kotlin.allopen.gradle.SpringGradleSubplugin 13 | import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension 14 | import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper 15 | 16 | public abstract class MainframePlugin : Plugin { 17 | 18 | override fun apply(target: Project) { 19 | configureKotlin(target) 20 | 21 | createExtension(target) 22 | 23 | target.pluginManager.apply(MainframeBomPlugin::class) 24 | target.pluginManager.apply(MainframeShadowPlugin::class) 25 | } 26 | 27 | private fun configureKotlin(target: Project) { 28 | target.pluginManager.apply(KotlinPluginWrapper::class) 29 | target.pluginManager.apply(SpringGradleSubplugin::class) 30 | val javaVersion = target.extensions.getByType().toolchain.languageVersion 31 | if (javaVersion.isPresent) { 32 | target.configure { 33 | jvmToolchain { 34 | languageVersion.set(javaVersion.get()) 35 | } 36 | } 37 | } 38 | } 39 | 40 | private fun createExtension(target: Project) { 41 | target.extensions.create(MainframeExtension.NAME) 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /exposed/src/main/kotlin/kr/junhyung/mainframe/exposed/ExposedAutoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.exposed 2 | 3 | import kr.junhyung.mainframe.core.transaction.TransactionManager 4 | import kr.junhyung.mainframe.exposed.table.TableInitializer 5 | import kr.junhyung.mainframe.exposed.transaction.ExposedTransactionManager 6 | import org.jetbrains.exposed.sql.Database 7 | import org.jetbrains.exposed.sql.Table 8 | import org.springframework.beans.factory.ObjectProvider 9 | import org.springframework.boot.ApplicationRunner 10 | import org.springframework.boot.autoconfigure.AutoConfiguration 11 | import org.springframework.boot.autoconfigure.ImportAutoConfiguration 12 | import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration 13 | import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration 14 | import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizationAutoConfiguration 15 | import org.springframework.context.annotation.Bean 16 | import javax.sql.DataSource 17 | 18 | @ImportAutoConfiguration( 19 | exclude = [ 20 | TransactionManagerCustomizationAutoConfiguration::class, 21 | TransactionAutoConfiguration::class, 22 | DataSourceTransactionManagerAutoConfiguration::class, 23 | ] 24 | ) 25 | @AutoConfiguration 26 | public class ExposedAutoConfiguration { 27 | 28 | @Bean 29 | public fun database(dataSource: DataSource): Database { 30 | val database = Database.connect(dataSource) 31 | return database 32 | } 33 | 34 | @Bean 35 | public fun exposedTransactionManager(database: Database): TransactionManager { 36 | return ExposedTransactionManager(database) 37 | } 38 | 39 | @Bean 40 | public fun tableInitializer( 41 | database: Database, 42 | tables: ObjectProvider
43 | ): ApplicationRunner { 44 | return TableInitializer(database, tables) 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /gradle-plugin/src/main/kotlin/kr/junhyung/mainframe/gradle/bom/MainframeBomPlugin.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.gradle.bom 2 | 3 | import io.spring.gradle.dependencymanagement.DependencyManagementPlugin 4 | import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension 5 | import org.gradle.api.Plugin 6 | import org.gradle.api.Project 7 | import org.gradle.api.plugins.PluginManager 8 | import org.gradle.kotlin.dsl.apply 9 | import org.gradle.kotlin.dsl.create 10 | import org.gradle.kotlin.dsl.getByType 11 | 12 | public abstract class MainframeBomPlugin : Plugin { 13 | 14 | private companion object { 15 | const val MAINFRAME_BOM_GROUP: String = "kr.junhyung.mainframe" 16 | const val MAINFRAME_BOM_NAME: String = "mainframe-bom" 17 | 18 | const val MAINFRAME_DEPENDENCIES_EXTENSION_NAME: String = "mainframe" 19 | } 20 | 21 | override fun apply(target: Project) { 22 | applyPlugin(target.pluginManager) 23 | configureBom(target) 24 | configureDependenciesExtension(target) 25 | } 26 | 27 | private fun applyPlugin(pluginManager: PluginManager) { 28 | pluginManager.apply(DependencyManagementPlugin::class) 29 | } 30 | 31 | private fun configureBom(target: Project) { 32 | val bomCoordinates = "$MAINFRAME_BOM_GROUP:$MAINFRAME_BOM_NAME:${getBomVersion()}" 33 | target.extensions.getByType(DependencyManagementExtension::class) 34 | .imports { 35 | mavenBom(bomCoordinates) 36 | } 37 | } 38 | 39 | private fun configureDependenciesExtension(target: Project) { 40 | target.dependencies.extensions.create( 41 | MAINFRAME_DEPENDENCIES_EXTENSION_NAME, 42 | target.dependencies 43 | ) 44 | } 45 | 46 | private fun getBomVersion(): String { 47 | return this::class.java.`package`.implementationVersion ?: error("'implementation-version' not found") 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/VelocityPluginAutoConfiguration.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity 2 | 3 | import com.velocitypowered.api.plugin.PluginContainer 4 | import com.velocitypowered.api.proxy.ProxyServer 5 | import kotlinx.coroutines.CoroutineDispatcher 6 | import kotlinx.coroutines.asCoroutineDispatcher 7 | import org.slf4j.Logger 8 | import org.springframework.beans.factory.config.BeanDefinition 9 | import org.springframework.boot.autoconfigure.AutoConfiguration 10 | import org.springframework.context.annotation.Bean 11 | import org.springframework.context.annotation.Role 12 | 13 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 14 | @AutoConfiguration 15 | public class VelocityPluginAutoConfiguration { 16 | 17 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 18 | @Bean(destroyMethod = "") 19 | public fun proxyServer(velocityPluginApplicationContext: VelocityPluginApplicationContext): ProxyServer { 20 | return velocityPluginApplicationContext.proxyServer 21 | } 22 | 23 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 24 | @Bean(destroyMethod = "") 25 | public fun pluginContainer(velocityPluginApplicationContext: VelocityPluginApplicationContext): PluginContainer { 26 | return velocityPluginApplicationContext.pluginContainer 27 | } 28 | 29 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 30 | @Bean 31 | public fun logger(pluginContainer: PluginContainer): Logger { 32 | val instance = pluginContainer.instance.get() 33 | if (instance !is MainframePlugin<*>) { 34 | error("Plugin instance is not a MainframePlugin. Please ensure your plugin extends MainframePlugin.") 35 | } 36 | return instance.logger 37 | } 38 | 39 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 40 | @Bean 41 | public fun pluginCoroutineDispatcher(pluginContainer: PluginContainer): CoroutineDispatcher { 42 | return pluginContainer.executorService.asCoroutineDispatcher() 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /platforms/paper-brigadier/src/main/kotlin/kr/junhyung/mainframe/paper/brigadier/PaperCommandRegistrar.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.brigadier 2 | 3 | import io.papermc.paper.command.brigadier.BasicCommand 4 | import io.papermc.paper.command.brigadier.CommandSourceStack 5 | import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents 6 | import kr.junhyung.mainframe.brigadier.Command 7 | import kr.junhyung.mainframe.brigadier.CommandRegistrar 8 | import org.bukkit.command.CommandSender 9 | import org.bukkit.plugin.Plugin 10 | 11 | internal class PaperCommandRegistrar( 12 | private val plugin: Plugin 13 | ) : CommandRegistrar { 14 | 15 | @Suppress("UnstableApiUsage") 16 | override fun registerCommand(command: Any, metadata: Command) { 17 | if (command !is BasicCommand) { 18 | throw UnsupportedOperationException("Command annotation can only be used on BasicCommand, but found ${command::class.java.name}") 19 | } 20 | plugin.lifecycleManager.registerEventHandler(LifecycleEvents.COMMANDS) { event -> 21 | val delegatedCommand = DelegatedCommand(command, metadata.permission) 22 | event.registrar().register(metadata.name, metadata.description, metadata.aliases.toList(), delegatedCommand) 23 | } 24 | } 25 | 26 | @Suppress("UnstableApiUsage") 27 | private class DelegatedCommand( 28 | private val delegate: BasicCommand, 29 | private val permission: String 30 | ) : BasicCommand by delegate { 31 | 32 | override fun suggest( 33 | commandSourceStack: CommandSourceStack, 34 | args: Array 35 | ): Collection { 36 | return delegate.suggest(commandSourceStack, args) 37 | } 38 | 39 | override fun canUse(sender: CommandSender): Boolean { 40 | return delegate.canUse(sender) 41 | } 42 | 43 | override fun permission(): String? { 44 | return permission 45 | } 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/annotation/ClassAnnotationProcessor.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.annotation 2 | 3 | import kr.junhyung.mainframe.core.util.stdlib.uncheckedCast 4 | import org.springframework.aop.framework.AopInfrastructureBean 5 | import org.springframework.aop.framework.AopProxyUtils 6 | import org.springframework.beans.factory.config.BeanPostProcessor 7 | import org.springframework.core.ResolvableType 8 | import org.springframework.core.annotation.AnnotationUtils 9 | import kotlin.jvm.java 10 | 11 | public abstract class ClassAnnotationProcessor : BeanPostProcessor { 12 | 13 | private val annotationClass: Class by lazy { 14 | ResolvableType 15 | .forClass(ClassAnnotationProcessor::class.java, this::class.java) 16 | .getGeneric(0) 17 | .resolve(Annotation::class.java) 18 | .uncheckedCast() 19 | } 20 | 21 | protected open fun postProcessAfterInitialization(bean: Any, beanName: String, annotation: T) {} 22 | 23 | protected open fun postProcessBeforeInitialization(bean: Any, beanName: String, annotation: T) {} 24 | 25 | final override fun postProcessAfterInitialization(bean: Any, beanName: String): Any? { 26 | val annotation = resolveAnnotation(bean) ?: return bean 27 | postProcessAfterInitialization(bean, beanName, annotation) 28 | return bean 29 | } 30 | 31 | final override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any? { 32 | val annotation = resolveAnnotation(bean) ?: return bean 33 | postProcessBeforeInitialization(bean, beanName, annotation) 34 | return bean 35 | } 36 | 37 | private fun resolveAnnotation(bean: Any): T? { 38 | if (bean is AopInfrastructureBean) { 39 | return null 40 | } 41 | val ultimateTargetClass = AopProxyUtils.ultimateTargetClass(bean) 42 | if (!AnnotationUtils.isCandidateClass(ultimateTargetClass, annotationClass)) { 43 | return null 44 | } 45 | val annotation = AnnotationUtils.findAnnotation(ultimateTargetClass, annotationClass) 46 | if (annotation == null) { 47 | return null 48 | } 49 | return annotation 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/event/EventHandlerAnnotationProcessor.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper.event 2 | 3 | import kr.junhyung.mainframe.core.annotation.MethodAnnotationProcessor 4 | import kr.junhyung.mainframe.core.util.stdlib.uncheckedCast 5 | import org.bukkit.Bukkit 6 | import org.bukkit.event.Event 7 | import org.bukkit.event.EventHandler 8 | import org.bukkit.event.Listener 9 | import org.bukkit.plugin.EventExecutor 10 | import org.bukkit.plugin.Plugin 11 | import org.bukkit.plugin.PluginManager 12 | import org.springframework.core.MethodParameter 13 | import java.lang.reflect.InvocationTargetException 14 | import java.lang.reflect.Method 15 | 16 | internal class EventHandlerAnnotationProcessor( 17 | private val plugin: Plugin, 18 | private val pluginManager: PluginManager = Bukkit.getPluginManager() 19 | ) : MethodAnnotationProcessor() { 20 | 21 | override fun postProcessAfterInitialization(bean: Any, beanName: String, method: Method, annotation: EventHandler) { 22 | val methodParameter = MethodParameter(method, 0) 23 | .withContainingClass(bean::class.java) 24 | .parameterType 25 | check(Event::class.java.isAssignableFrom(methodParameter)) { 26 | "first parameter of event handler must be a subclass of Event." 27 | } 28 | val eventExecutor = create(bean, method, methodParameter) 29 | registerEvent(eventExecutor, methodParameter, annotation) 30 | } 31 | 32 | private fun create(listener: Any, method: Method, targetEventClass: Class<*>): EventExecutor { 33 | return EventExecutor { _, event -> 34 | if (!targetEventClass.isInstance(event)) { 35 | return@EventExecutor 36 | } 37 | try { 38 | method.invoke(listener, event) 39 | } catch (invocationTargetException: InvocationTargetException) { 40 | val cause = invocationTargetException.cause ?: throw invocationTargetException 41 | throw cause 42 | } 43 | } 44 | } 45 | 46 | private fun registerEvent( 47 | eventExecutor: EventExecutor, 48 | eventType: Class<*>, 49 | eventHandler: EventHandler 50 | ) { 51 | pluginManager.registerEvent( 52 | eventType.uncheckedCast(), 53 | object : Listener {}, 54 | eventHandler.priority, 55 | eventExecutor, 56 | plugin 57 | ) 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/MainframeApplicationContext.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core 2 | 3 | import kr.junhyung.mainframe.core.coroutines.CoroutineScopeAwareBeanPostProcessor 4 | import kr.junhyung.mainframe.core.jackson.ObjectMapperAwareBeanPostProcessor 5 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory 6 | import org.springframework.boot.env.YamlPropertySourceLoader 7 | import org.springframework.context.support.GenericApplicationContext 8 | import org.springframework.core.io.FileSystemResource 9 | import java.io.File 10 | 11 | public abstract class MainframeApplicationContext(private val dataDirectory: File) : GenericApplicationContext() { 12 | 13 | private val yamlPropertySourceLoader = YamlPropertySourceLoader() 14 | 15 | public abstract fun onPostProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory) 16 | 17 | public override fun postProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory) { 18 | registerJacksonBeans(beanFactory) 19 | registerCoroutineBeans(beanFactory) 20 | 21 | onPostProcessBeanFactory(beanFactory) 22 | } 23 | 24 | final override fun prepareRefresh() { 25 | if (getResource("classpath:application.yml").exists()) { 26 | installConfiguration("application.yml") 27 | } 28 | super.prepareRefresh() 29 | } 30 | 31 | private fun registerJacksonBeans(beanFactory: ConfigurableListableBeanFactory) { 32 | beanFactory.addBeanPostProcessor(ObjectMapperAwareBeanPostProcessor(this)) 33 | } 34 | 35 | private fun registerCoroutineBeans(beanFactory: ConfigurableListableBeanFactory) { 36 | beanFactory.addBeanPostProcessor(CoroutineScopeAwareBeanPostProcessor(this)) 37 | } 38 | 39 | public fun installConfiguration(configurationFileName: String) { 40 | if (!dataDirectory.exists()) { 41 | dataDirectory.mkdirs() 42 | } 43 | val preset = getResourceByPath(configurationFileName) 44 | if (!preset.exists()) { 45 | error("Preset file not found: ${preset.description}") 46 | } 47 | val destination = dataDirectory.resolve(configurationFileName) 48 | if (!destination.exists()) { 49 | preset.inputStream.use { input -> 50 | destination.outputStream().use { output -> 51 | input.copyTo(output) 52 | } 53 | } 54 | } 55 | loadYamlProperties(destination) 56 | } 57 | 58 | public fun loadYamlProperties(yamlFile: File) { 59 | val resource = FileSystemResource(yamlFile) 60 | yamlPropertySourceLoader.load(resource.filename, resource).forEach { propertySource -> 61 | environment.propertySources.addLast(propertySource) 62 | } 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/brigadier/MainframeCommand.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity.brigadier 2 | 3 | import com.velocitypowered.api.command.CommandSource 4 | import com.velocitypowered.api.command.SimpleCommand 5 | import kotlinx.coroutines.CoroutineScope 6 | import kotlinx.coroutines.future.future 7 | import kotlinx.coroutines.launch 8 | import kr.junhyung.mainframe.brigadier.Command 9 | import kr.junhyung.mainframe.brigadier.CommandAnnotatedElement 10 | import kr.junhyung.mainframe.core.coroutines.CoroutineScopeAware 11 | import java.util.concurrent.CompletableFuture 12 | 13 | public abstract class MainframeCommand : SimpleCommand, CommandAnnotatedElement, CoroutineScopeAware { 14 | 15 | public lateinit var name: String 16 | public lateinit var aliases: List 17 | public lateinit var permission: String 18 | public lateinit var description: String 19 | public lateinit var usageMessage: String 20 | private lateinit var coroutineScope: CoroutineScope 21 | 22 | final override fun setCommand(command: Command) { 23 | this.name = command.name 24 | this.aliases = command.aliases.toList() 25 | this.permission = command.permission 26 | this.description = command.description 27 | this.usageMessage = command.usageMessage 28 | } 29 | 30 | final override fun setCoroutineScope(coroutineScope: CoroutineScope) { 31 | this.coroutineScope = coroutineScope 32 | } 33 | 34 | public open suspend fun execute(source: CommandSource, label: String, arguments: List) { 35 | } 36 | 37 | final override fun execute(invocation: SimpleCommand.Invocation) { 38 | coroutineScope.launch { 39 | execute(invocation.source(), invocation.alias(), invocation.arguments().toList()) 40 | } 41 | } 42 | 43 | public open suspend fun completions(source: CommandSource, label: String, arguments: List): List { 44 | return emptyList() 45 | } 46 | 47 | final override fun suggestAsync(invocation: SimpleCommand.Invocation): CompletableFuture> { 48 | return coroutineScope.future { 49 | completions(invocation.source(), invocation.alias(), invocation.arguments().toList()) 50 | } 51 | } 52 | 53 | final override fun hasPermission(invocation: SimpleCommand.Invocation): Boolean { 54 | return hasPermission(invocation.source(), invocation.alias(), invocation.arguments().toList()) 55 | } 56 | 57 | public open fun hasPermission(source: CommandSource, label: String, arguments: List): Boolean { 58 | if (permission.isBlank()) { 59 | return true 60 | } 61 | return source.hasPermission(permission) 62 | } 63 | 64 | public fun sendUsageMessage(commandSource: CommandSource) { 65 | commandSource.sendRichMessage(usageMessage) 66 | } 67 | 68 | final override fun suggest(invocation: SimpleCommand.Invocation): List { 69 | error("This method should not be called. Use completions instead.") 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /core/src/main/kotlin/kr/junhyung/mainframe/core/annotation/MethodAnnotationProcessor.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.core.annotation 2 | 3 | import kr.junhyung.mainframe.core.util.stdlib.uncheckedCast 4 | import org.springframework.aop.framework.AopInfrastructureBean 5 | import org.springframework.beans.factory.config.BeanPostProcessor 6 | import org.springframework.core.MethodIntrospector 7 | import org.springframework.core.ResolvableType 8 | import org.springframework.core.annotation.AnnotatedElementUtils 9 | import org.springframework.core.annotation.AnnotationUtils 10 | import java.lang.reflect.Method 11 | 12 | public abstract class MethodAnnotationProcessor : BeanPostProcessor { 13 | 14 | private val annotationClass: Class by lazy { 15 | ResolvableType 16 | .forClass(MethodAnnotationProcessor::class.java, this::class.java) 17 | .getGeneric(0) 18 | .resolve(Annotation::class.java) 19 | .uncheckedCast() 20 | } 21 | 22 | protected open fun postProcessBeforeInitialization( 23 | bean: Any, 24 | beanName: String, 25 | method: Method, 26 | annotation: T 27 | ) {} 28 | 29 | protected open fun postProcessAfterInitialization( 30 | bean: Any, 31 | beanName: String, 32 | method: Method, 33 | annotation: T 34 | ) {} 35 | 36 | 37 | final override fun postProcessAfterInitialization(bean: Any, beanName: String): Any { 38 | val methods = selectMethods(bean) 39 | if (methods.isEmpty()) { 40 | return bean 41 | } 42 | methods.forEach { method -> 43 | val annotation = AnnotatedElementUtils.getMergedAnnotation(method, annotationClass) 44 | ?: error("Method $method is expected to be annotated with ${annotationClass.simpleName} but not found") 45 | postProcessAfterInitialization(bean, beanName, method, annotation) 46 | } 47 | return bean 48 | } 49 | 50 | final override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any { 51 | val methods = selectMethods(bean) 52 | if (methods.isEmpty()) { 53 | return bean 54 | } 55 | methods.forEach { method -> 56 | val annotation = AnnotatedElementUtils.getMergedAnnotation(method, annotationClass) 57 | ?: error("Method $method is expected to be annotated with ${annotationClass.simpleName} but not found") 58 | postProcessBeforeInitialization(bean, beanName, method, annotation) 59 | } 60 | return bean 61 | } 62 | 63 | private fun selectMethods(bean: Any): Set { 64 | if (bean is AopInfrastructureBean) { 65 | return emptySet() 66 | } 67 | val targetClass = bean::class.java 68 | if (!AnnotationUtils.isCandidateClass(targetClass, annotationClass)) { 69 | return emptySet() 70 | } 71 | return MethodIntrospector.selectMethods(targetClass) { method -> 72 | AnnotatedElementUtils.isAnnotated(method, annotationClass) 73 | } 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /platforms/paper/src/main/kotlin/kr/junhyung/mainframe/paper/MainframePlugin.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.paper 2 | 3 | import kr.junhyung.mainframe.core.MainframeApplicationCustomizer 4 | import kr.junhyung.mainframe.core.io.PrioritizedResourceLoader 5 | import kr.junhyung.mainframe.core.util.ClassLoaderUtils 6 | import kr.junhyung.mainframe.core.util.stdlib.uncheckedCast 7 | import org.bukkit.Bukkit 8 | import org.bukkit.plugin.java.JavaPlugin 9 | import org.springframework.boot.ApplicationContextFactory 10 | import org.springframework.boot.Banner 11 | import org.springframework.boot.WebApplicationType 12 | import org.springframework.boot.builder.SpringApplicationBuilder 13 | import org.springframework.context.ConfigurableApplicationContext 14 | import org.springframework.core.ResolvableType 15 | import org.springframework.core.io.ResourceLoader 16 | import kotlin.jvm.java 17 | 18 | @Suppress("unused") 19 | public abstract class MainframePlugin : JavaPlugin(), ApplicationContextFactory, 20 | MainframeApplicationCustomizer { 21 | 22 | private lateinit var applicationContext: ConfigurableApplicationContext 23 | 24 | final override fun create(webApplicationType: WebApplicationType?): ConfigurableApplicationContext { 25 | val context = PaperPluginApplicationContext(this) 26 | context.classLoader = this.classLoader 27 | val resourceLoader = createResourceLoader() 28 | context.setResourceLoader(resourceLoader) 29 | context.apply(this::customizeContext) 30 | return context 31 | } 32 | 33 | @Suppress("UnstableApiUsage") 34 | private fun createResourceLoader(): ResourceLoader { 35 | val resourceLoader = PrioritizedResourceLoader(this.classLoader) 36 | (pluginMeta.pluginDependencies + pluginMeta.pluginSoftDependencies).map(Bukkit.getPluginManager()::getPlugin) 37 | .forEach { plugin -> 38 | requireNotNull(plugin) { "Plugin ${pluginMeta.pluginDependencies} is not loaded" } 39 | resourceLoader.add(plugin.javaClass.classLoader) 40 | } 41 | return resourceLoader 42 | } 43 | 44 | final override fun onEnable() { 45 | ClassLoaderUtils.withClassLoader(this.classLoader, ::startApplication) 46 | } 47 | 48 | private fun startApplication() { 49 | onApplicationStart() 50 | val context = SpringApplicationBuilder() 51 | .sources(determineMainClass()) 52 | .contextFactory(this) 53 | .headless(true) 54 | .registerShutdownHook(false) 55 | .bannerMode(Banner.Mode.LOG) 56 | .let(this::customizeBuilder) 57 | .run() 58 | this.applicationContext = context 59 | onApplicationStarted(context.uncheckedCast()) 60 | } 61 | 62 | final override fun onDisable() { 63 | if (this::applicationContext.isInitialized) { 64 | this.applicationContext.close() 65 | } 66 | } 67 | 68 | private fun determineMainClass(): Class<*> { 69 | return ResolvableType 70 | .forClass(this::class.java) 71 | .`as`(MainframePlugin::class.java) 72 | .generics 73 | .single() 74 | .resolve() ?: error("Unable to resolve plugin class") 75 | } 76 | 77 | } -------------------------------------------------------------------------------- /gradle/libs.versions.toml: -------------------------------------------------------------------------------- 1 | [versions] 2 | kotlin = "2.0.21" 3 | spring-boot = "3.4.5" 4 | spring-dependency-management = "1.1.7" 5 | kotlinx-coroutines = "1.10.2" 6 | paper = "1.20.1-R0.1-SNAPSHOT" 7 | paper-v21 = "1.21.1-R0.1-SNAPSHOT" 8 | brigadier = "1.0.18" 9 | 10 | adventure = "4.14.0" 11 | velocity = "3.4.0-SNAPSHOT" 12 | plugin-publish = "1.3.1" 13 | exposed = "0.61.0" 14 | hikaricp = "6.3.0" 15 | mysql-connector = "9.3.0" 16 | 17 | shadow = "8.3.6" 18 | 19 | [libraries] 20 | kotlin-bom = { group = "org.jetbrains.kotlin", name = "kotlin-bom", version.ref = "kotlin" } 21 | 22 | paper-api = { group = "io.papermc.paper", name = "paper-api", version.ref = "paper" } 23 | paper-api-v21 = { group = "io.papermc.paper", name = "paper-api", version.ref = "paper-v21" } 24 | brigadier = { group = "com.mojang", name = "brigadier", version.ref = "brigadier" } 25 | 26 | velocity-api = { group = "com.velocitypowered", name = "velocity-api", version.ref = "velocity" } 27 | adventure-api = { group = "net.kyori", name = "adventure-api", version.ref = "adventure" } 28 | adventure-text-serializer-gson = { group = "net.kyori", name = "adventure-text-serializer-gson", version.ref = "adventure" } 29 | 30 | kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } 31 | kotlinx-coroutines-reactive = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-reactive", version.ref = "kotlinx-coroutines" } 32 | kotlinx-coroutines-reactor = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-reactor", version.ref = "kotlinx-coroutines" } 33 | 34 | spring-boot-starter = { group = "org.springframework.boot", name = "spring-boot-starter", version.ref = "spring-boot" } 35 | spring-boot-starter-json = { group = "org.springframework.boot", name = "spring-boot-starter-json", version.ref = "spring-boot" } 36 | spring-boot-starter-jdbc = { group = "org.springframework.boot", name = "spring-boot-starter-jdbc", version.ref = "spring-boot" } 37 | spring-boot-dependencies = { group = "org.springframework.boot", name = "spring-boot-dependencies", version.ref = "spring-boot" } 38 | 39 | exposed-bom = { group = "org.jetbrains.exposed", name = "exposed-bom", version.ref = "exposed" } 40 | exposed-core = { group = "org.jetbrains.exposed", name = "exposed-core", version.ref = "exposed" } 41 | exposed-jdbc = { group = "org.jetbrains.exposed", name = "exposed-jdbc", version.ref = "exposed" } 42 | exposed-java-time = { group = "org.jetbrains.exposed", name = "exposed-java-time", version.ref = "exposed" } 43 | exposed-json = { group = "org.jetbrains.exposed", name = "exposed-json", version.ref = "exposed" } 44 | hikaricp = { group = "com.zaxxer", name = "HikariCP", version.ref = "hikaricp" } 45 | mysql-connector = { group = "com.mysql", name = "mysql-connector-j", version.ref = "mysql-connector" } 46 | 47 | # plugin dependencies 48 | shadow = { group = "com.gradleup.shadow", name = "shadow-gradle-plugin", version.ref = "shadow" } 49 | spring-dependency-management = { group = "io.spring.gradle", name = "dependency-management-plugin", version.ref = "spring-dependency-management" } 50 | 51 | [plugins] 52 | kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } 53 | kotlin-plugin-spring = { id = "org.jetbrains.kotlin.plugin.spring", version.ref = "kotlin" } 54 | plugin-publish = { id = "com.gradle.plugin-publish", version.ref = "plugin-publish" } -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/MainframePlugin.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity 2 | 3 | import com.velocitypowered.api.event.Subscribe 4 | import com.velocitypowered.api.event.proxy.ProxyInitializeEvent 5 | import com.velocitypowered.api.event.proxy.ProxyShutdownEvent 6 | import com.velocitypowered.api.plugin.PluginContainer 7 | import com.velocitypowered.api.plugin.annotation.DataDirectory 8 | import com.velocitypowered.api.proxy.ProxyServer 9 | import kr.junhyung.mainframe.core.MainframeApplicationCustomizer 10 | import kr.junhyung.mainframe.core.io.PrioritizedResourceLoader 11 | import kr.junhyung.mainframe.core.util.ClassLoaderUtils 12 | import kr.junhyung.mainframe.core.util.stdlib.uncheckedCast 13 | import org.slf4j.Logger 14 | import org.springframework.boot.ApplicationContextFactory 15 | import org.springframework.boot.Banner 16 | import org.springframework.boot.WebApplicationType 17 | import org.springframework.boot.builder.SpringApplicationBuilder 18 | import org.springframework.context.ConfigurableApplicationContext 19 | import org.springframework.core.ResolvableType 20 | import java.nio.file.Path 21 | import javax.inject.Inject 22 | import kotlin.jvm.java 23 | 24 | public abstract class MainframePlugin : ApplicationContextFactory, MainframeApplicationCustomizer { 25 | 26 | @Inject 27 | protected lateinit var proxyServer: ProxyServer 28 | @Inject 29 | protected lateinit var pluginContainer: PluginContainer 30 | @DataDirectory 31 | @Inject 32 | protected lateinit var dataDirectory: Path 33 | @Inject 34 | public lateinit var logger: Logger 35 | 36 | private lateinit var applicationContext: ConfigurableApplicationContext 37 | 38 | override fun create(webApplicationType: WebApplicationType?): ConfigurableApplicationContext { 39 | val context = VelocityPluginApplicationContext(proxyServer, pluginContainer, dataDirectory.toFile()) 40 | context.classLoader = this.javaClass.classLoader 41 | context.apply(this::customizeContext) 42 | val resourceLoader = PrioritizedResourceLoader(this.javaClass.classLoader) 43 | pluginContainer.description.dependencies.forEach { dependency -> 44 | val dependencyPlugin = proxyServer.pluginManager.getPlugin(dependency.id) 45 | val classLoader = dependencyPlugin.get().instance.javaClass.classLoader 46 | resourceLoader.add(classLoader) 47 | } 48 | context.setResourceLoader(resourceLoader) 49 | return context 50 | } 51 | 52 | @Subscribe 53 | internal fun onProxyInitialize(event: ProxyInitializeEvent) { 54 | require(this::proxyServer.isInitialized) { "ProxyServer is not injected" } 55 | require(this::pluginContainer.isInitialized) { "PluginContainer is not injected" } 56 | require(this::dataDirectory.isInitialized) { "DataDirectory is not injected" } 57 | ClassLoaderUtils.withClassLoader(this.javaClass.classLoader, ::startApplication) 58 | } 59 | 60 | private fun startApplication() { 61 | onApplicationStart() 62 | val context = SpringApplicationBuilder() 63 | .sources(determineMainClass()) 64 | .contextFactory(this) 65 | .headless(true) 66 | .registerShutdownHook(false) 67 | .bannerMode(Banner.Mode.LOG) 68 | .let(this::customizeBuilder) 69 | .run() 70 | this.applicationContext = context 71 | onApplicationStarted(context.uncheckedCast()) 72 | } 73 | 74 | @Subscribe 75 | internal fun onProxyShutdown(event: ProxyShutdownEvent) { 76 | if (this::applicationContext.isInitialized) { 77 | this.applicationContext.close() 78 | } 79 | } 80 | 81 | private fun determineMainClass(): Class<*> { 82 | return ResolvableType 83 | .forClass(this::class.java) 84 | .`as`(MainframePlugin::class.java) 85 | .generics 86 | .single() 87 | .resolve() ?: error("Unable to resolve plugin class") 88 | } 89 | 90 | } -------------------------------------------------------------------------------- /platforms/velocity/src/main/kotlin/kr/junhyung/mainframe/velocity/event/SubscribeAnnotationProcessor.kt: -------------------------------------------------------------------------------- 1 | package kr.junhyung.mainframe.velocity.event 2 | 3 | import com.velocitypowered.api.event.AwaitingEventExecutor 4 | import com.velocitypowered.api.event.EventHandler 5 | import com.velocitypowered.api.event.EventManager 6 | import com.velocitypowered.api.event.EventTask 7 | import com.velocitypowered.api.event.Subscribe 8 | import com.velocitypowered.api.plugin.PluginContainer 9 | import kotlinx.coroutines.CoroutineExceptionHandler 10 | import kotlinx.coroutines.CoroutineScope 11 | import kotlinx.coroutines.launch 12 | import kr.junhyung.mainframe.core.annotation.MethodAnnotationProcessor 13 | import kr.junhyung.mainframe.core.util.stdlib.uncheckedCast 14 | import org.springframework.context.ApplicationContext 15 | import org.springframework.core.MethodParameter 16 | import java.lang.reflect.InvocationTargetException 17 | import java.lang.reflect.Method 18 | import kotlin.reflect.full.callSuspend 19 | import kotlin.reflect.jvm.kotlinFunction 20 | 21 | internal class SubscribeAnnotationProcessor( 22 | private val applicationContext: ApplicationContext, 23 | private val pluginContainer: PluginContainer, 24 | private val eventManager: EventManager 25 | ) : MethodAnnotationProcessor() { 26 | 27 | private val coroutineScope: CoroutineScope by lazy { 28 | applicationContext.getBean(CoroutineScope::class.java) 29 | ?: error("CoroutineScope bean is not available in the application context") 30 | } 31 | 32 | override fun postProcessAfterInitialization(bean: Any, beanName: String, method: Method, annotation: Subscribe) { 33 | val methodParameter = MethodParameter(method, 0) 34 | .withContainingClass(bean::class.java) 35 | .parameterType 36 | 37 | val eventExecutor = create(bean, method) 38 | registerEvent(methodParameter, annotation, eventExecutor) 39 | } 40 | 41 | private fun create(instance: Any, method: Method): EventHandler<*> { 42 | return AwaitingEventExecutor { event -> 43 | EventTask.withContinuation { continuation -> 44 | val kotlinFunction = method.kotlinFunction 45 | if (kotlinFunction?.isSuspend != true) { 46 | try { 47 | method.invoke(instance, event) 48 | continuation.resume() 49 | } catch (invocationTargetException: InvocationTargetException) { 50 | val cause = invocationTargetException.cause 51 | if (cause == null) { 52 | continuation.resumeWithException(invocationTargetException) 53 | return@withContinuation 54 | } 55 | continuation.resumeWithException(cause) 56 | } catch (throwable: Throwable) { 57 | continuation.resumeWithException(throwable) 58 | } 59 | return@withContinuation 60 | } 61 | 62 | coroutineScope.launch { 63 | try { 64 | kotlinFunction.callSuspend(instance, event) 65 | } catch (invocationTargetException: InvocationTargetException) { 66 | val cause = invocationTargetException.cause 67 | if (cause == null) { 68 | coroutineContext[CoroutineExceptionHandler]?.handleException(coroutineContext, invocationTargetException) 69 | return@launch 70 | } 71 | coroutineContext[CoroutineExceptionHandler]?.handleException(coroutineContext, cause) 72 | } catch (throwable: Throwable) { 73 | coroutineContext[CoroutineExceptionHandler]?.handleException(coroutineContext, throwable) 74 | } finally { 75 | continuation.resume() 76 | } 77 | } 78 | } 79 | } 80 | } 81 | 82 | private fun registerEvent( 83 | eventType: Class<*>, 84 | subscribeAnnotation: Subscribe, 85 | eventHandler: EventHandler<*> 86 | ) { 87 | eventManager.register(pluginContainer, eventType.uncheckedCast(), subscribeAnnotation.priority, eventHandler) 88 | } 89 | 90 | } -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [2018] [Ohad Shai] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------