Skip to content

Commit

Permalink
feat: bundle modules into simplecoreapi
Browse files Browse the repository at this point in the history
Due to the difficulty of loading modules dynamically into the classpath (at least in jaa 9 or superior) we'll be bundling the modules into the API.
  • Loading branch information
Im-Fran committed Nov 7, 2023
1 parent dc8c496 commit b468379
Show file tree
Hide file tree
Showing 37 changed files with 1,956 additions and 567 deletions.
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ dependencies {
compileOnly("net.md-5:bungeecord-api:1.20-R0.1")
compileOnly("com.velocitypowered:velocity-api:3.1.2-SNAPSHOT")

/* Logging Module */
implementation("org.apache.logging.log4j:log4j-api:2.21.0")
implementation("org.apache.logging.log4j:log4j-core:2.21.0")

/* Files Module */
implementation("me.carleslc.Simple-YAML:Simple-Yaml:1.8.4")

/* Global Depends */
implementation("org.jetbrains:annotations:24.0.1")
implementation("commons-io:commons-io:2.15.0")
implementation("com.google.code.gson:gson:2.10.1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package xyz.theprogramsrc.simplecoreapi.bungee.modules.tasksmodule

import net.md_5.bungee.api.scheduler.ScheduledTask
import net.md_5.bungee.api.scheduler.TaskScheduler
import xyz.theprogramsrc.simplecoreapi.bungee.BungeeLoader
import xyz.theprogramsrc.simplecoreapi.global.module.Module
import xyz.theprogramsrc.simplecoreapi.global.module.ModuleDescription
import xyz.theprogramsrc.simplecoreapi.global.modules.tasksmodule.models.RecurringTask
import java.util.concurrent.TimeUnit

class BungeeTasksModule: Module {


companion object {
/**
* Instance of the [BungeeTasksModule].
*/
lateinit var instance: BungeeTasksModule
private set
}

override val description: ModuleDescription = ModuleDescription(
name = "TasksModule",
version = "0.3.0",
authors = listOf("Im-Fran"),
)

/**
* BungeeCord Scheduler
*/
val scheduler: TaskScheduler = BungeeLoader.instance.proxy.scheduler

/**
* Instance of Bungee Loader
*/
private val plugin = BungeeLoader.instance

override fun onEnable() {
instance = this
}

override fun onDisable() {
}

/**
* Runs an async task after the specified delay in ticks
* @param delay The delay in ticks. Defaults to 1 (1 tick = 0.05 seconds)
* @param task The task to run
* @return the [ScheduledTask]
*/
fun runAsync(delay: Int = 1, task: () -> Unit): ScheduledTask = scheduler.schedule(plugin, task, delay.times(50).toLong(), TimeUnit.MILLISECONDS)

/**
* Runs a repeating task asynchronously every given ticks (1 tick = 0.05 seconds) after the given ticks (1 tick = 0.05 seconds)
* @param delay The delay in ticks. Defaults to 1
* @param period The period in ticks. Defaults to 1
* @param task The task to run
* @return the [RecurringTask]
*/
fun runAsyncRepeating(delay: Int = 1, period: Int = 1, task: () -> Unit): RecurringTask {
val bungeeTask = scheduler.schedule(plugin, task, delay.times(50).toLong(), period.times(50).toLong(), TimeUnit.MILLISECONDS)
return object: RecurringTask(){
var cancelled: Boolean = false

override fun start(): RecurringTask = this.apply {
if(cancelled) {
this@BungeeTasksModule.runAsyncRepeating(delay, period, task)
cancelled = false
}
}

override fun stop(): RecurringTask = this.apply {
bungeeTask.cancel()
cancelled = true
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@ package xyz.theprogramsrc.simplecoreapi.global
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.simple.SimpleLogger
import xyz.theprogramsrc.simplecoreapi.global.dependencydownloader.DependencyDownloader
import xyz.theprogramsrc.simplecoreapi.global.dependencydownloader.DependencyLoader
import xyz.theprogramsrc.simplecoreapi.global.dependencydownloader.interfaces.DependencyLoader
import xyz.theprogramsrc.simplecoreapi.global.modules.filesmodule.extensions.file
import xyz.theprogramsrc.simplecoreapi.global.modules.filesmodule.extensions.folder
import xyz.theprogramsrc.simplecoreapi.global.utils.SoftwareType
import xyz.theprogramsrc.simplecoreapi.global.utils.extensions.file
import xyz.theprogramsrc.simplecoreapi.global.utils.extensions.folder
import xyz.theprogramsrc.simplecoreapi.global.utils.update.GitHubUpdateChecker
import xyz.theprogramsrc.simplecoreapi.standalone.StandaloneLoader
import java.io.File

/**
* Class used to initialize SimpleCoreAPI (DO NOT CALL IT FROM EXTERNAL PLUGINS, IT MAY CRASH)
* @param dependencyClassLoader The [DependencyLoader] to use to load the dependencies
*/
class SimpleCoreAPI(
private val dependencyClassLoader: xyz.theprogramsrc.simplecoreapi.global.dependencydownloader.interfaces.DependencyLoader
){
class SimpleCoreAPI {

companion object {
/**
Expand Down Expand Up @@ -83,39 +77,6 @@ class SimpleCoreAPI(
} else {
logger.info("Running on unknown server software. Some features might not work as expected!")
}

// Now we'll download the dependencies
measureLoad("Downloaded dependencies in {time}") {
DependencyDownloader()
}

// Now we load the downloaded dependencies
measureLoad("Loaded dependencies in {time}") {
DependencyLoader(dependencyClassLoader = dependencyClassLoader)
}
}

/**
* Measures the amount of time in milliseconds it takes to execute the given block. Example:
* ```kt
* measureLoad("Waited for {time}") {
* // wait for 100 ms
* Thread.sleep(100)
* }
* ```
*
* Sample console output:
* ```log
* Waited for 100ms
* ```
* @param message The message to print. You can use '{time}' to replace with the amount of time in ms
* @param block The block to execute
*/
fun <T> measureLoad(message: String, block: () -> T): T {
val now = System.currentTimeMillis()
val response = block()
logger.info(message.replace("{time}", "${System.currentTimeMillis() - now}ms"))
return response
}

/**
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit b468379

Please sign in to comment.