KotlinScript

KotlinScript

This plugin allows you to write Kotlin scripts for your server leveraging SpongeAPI API8+
Write your own commands and make quick configurations for your server!

You can find our wiki here

What does a script look like?

A script simply needs the file extension of .plugin.kts

You can find many example scripts here;
however, a simple example:

import me.zodd.*
import net.kyori.adventure.text.Component

//Without context scripts will execute during construction
Logger.info("Hello Plugin Construction Event")

//Wrapper functions are provided to register events
onServerStarted {
    Logger.info("Hello Server Started Event")
}

//Sample first join message
onPlayerJoin {
    val player = player()
    if (!player.hasPlayedBefore()) {
        player.sendMessage(Component.text("Hello ${player.displayName()}"))
        /*
        Multi-line comments are supported
        
        Returns for events (if used) should use return flags as below
        */
        return@onPlayerJoin
    }
    Logger.info("${player.name()} - ${player.firstJoined()}")
}

Where do I install the scripts at?

Scripts are installed in ./config/scripting-host/scripts/

How could I possibly script without any auto-completion!?

No need fear! While you can script with a simple text editor
you may find that text completion and code suggestions are much easier!

All you need to do is copy the KotlinScriptingExampleRepo,
create files with the proper file extension, and start scripting.

This will require IntelliJ IDE to handle context and auto-completion

Limitations and Plans

  • SpongeForge does not work currently, only SpongeVanilla
  • Scripts cannot use other scripts as “libraries”
  • Scripts cannot define outside dependencies

I also would like to provide a DSL for other aspects of SpongeAPI where it could use it. This is yet to be determined, but it’s a wishlist item for me.

Support

You can find support in my discord guild

Credits

Kt-skript by randombyte was a huge inspiration for this project.

  • This plugin differs from Kt-Skript in many ways, most notably IDE support & the DSL syntax for commands.

Why here and not on Ore?

Unfortunately much like Kt-Skript, the size of the jar is too large to be hosted on Ore!
Where Ore’s filesize limit is about 20Mb the scripting-host Jar is just over 60Mb.
So If you would like this plugin you’ll need to receive the binaries through Github for the time being!

2 Likes